0

First off, the function in question.

bool Animation::loadFrames()
{
        sf::Texture frame[frameCount];
        std::string pathToFrame;
        for(int i = 1; i < frameCount; i++)
        {
            pathToFrame = pathToAnimation + i + ".jpg";
            if(!frame[i].loadFromFile(pathToFrame)) return false;
        }
        return true;
}

The line that's giving me trouble is

pathToFrame = pathToAnimation + i + ".jpg";

pathToAnimation is another std::string, frameCount is an int. I want to somehow make one string that looks like: "./Assets/Sprites/Player/Stand/1.jpg"

I know why my code doesn't work, I just don't know what the correct code would be.

7
  • I'm not saying there is only one way to do it, but often times sprite animations are loaded from a single sprite sheet. ken-soft.com/2009/01/02/sprite-class-in-c-using-sdl Commented Jun 8, 2012 at 7:44
  • So for example, spriteWidth = imageWidth / frames;, then you just blit the current frame (the animation). Commented Jun 8, 2012 at 7:46
  • The reason I'm not using a sprite sheet is because these are isometric sprites and so have 8 directions for every frame of every animation for the sprite. The resulting sheets would be huge, and so it's easier to organize them into files and folders and then load them separately. Commented Jun 8, 2012 at 7:50
  • Given a player that can move in 8 directions, and hence the animations are likely to change often you are still going to have to load all the animations into memory either way. So I don't fully see the advantage of breaking it up file by file. It's faster/easier to load one sprite and read from it than to load many sprites and have to manage them. Commented Jun 8, 2012 at 8:07
  • Though I guess it doesn't really matter that much If you have tools to easily manage all the animations. But most tile/map/animation editors I have worked with for gaming deal with sprite sheets and not individual tiles, hence my feedback. (I say this because my very first tile base games I developed, I did the same thing as you but had many headaches dealing with all the individual tiles.) Commented Jun 8, 2012 at 8:09

4 Answers 4

4

You are probably looking for std::ostringstream:

std::ostringstream ostr;
ostr << pathToAnimation << 123 << ".jpg"

std::cout << ostr.str();
Sign up to request clarification or add additional context in comments.

Comments

3

Besides using std::ostringstream, C++11 provides std::to_string with overloads for integral anf floating point types. With std::to_string:

pathToFrame = pathToAnimation + std::to_string(i) + ".jpg";

with std::ostringstream:

std::ostringstream os;
os << pathToAnomation << i << ".jpg";
pathToFrame = os.str();

Comments

1

Using boost::format you could write:

pathToFrame = (boost::format("%1%%2%.jpg") % pathToAnimation % i).str();

With boost::lexical_cast you can also write

pathToFrame = pathToAnimation + boost::lexical_cast<std::string>(i) + ".jpg";

Comments

0

In addition to Joachim's answer, you may also use C++11's new to_string functions to convert an int into a std::string

pathToFrame = pathToAnimation + std::to_string(i) + ".jpg";

1 Comment

Best solution, and allows me to remove the pathToFrame variable altogether and just do if(!frame[i].loadFromFile(pathToAnimation + std::to_string(i) + ".jpg")) return false;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.