e_lektro
e_lektro

Reputation: 31

Pretty way to code a bunch of similar if statements in c++?

i want to animate a sprite. To do that i have a ticker that gives me a time in milliseconds. Depending on the time i want the sprite to show a different image. Unfortunately i can't think of a simple and pretty way to do this except for a bunch of if statements.

    if (ticker_.getElapsedTime().asMilliseconds() >= ANIMATION_STEP * 0 &&
           ticker_.getElapsedTime().asMilliseconds() < ANIMATION_STEP * 1)
    {
        sprite_.setTexture(*game_->getResourceManager().getTexture("animation/explosion_00.png"));
    }

    if (ticker_.getElapsedTime().asMilliseconds() >= ANIMATION_STEP * 1 &&
           ticker_.getElapsedTime().asMilliseconds() < ANIMATION_STEP * 2)
    {
        sprite_.setTexture(*game_->getResourceManager().getTexture("animation/explosion_01.png"));
    }

    // 10 more of these ...

    if (ticker_.getElapsedTime().asMilliseconds() >= ANIMATION_STEP * 13 &&
           ticker_.getElapsedTime().asMilliseconds() < ANIMATION_STEP * 14)
    {
        sprite_.setTexture(*game_->getResourceManager().getTexture("animation/explosion_13.png"));
    }

Upvotes: 0

Views: 121

Answers (5)

yuri kilochek
yuri kilochek

Reputation: 13486

unsigned frame = ticker_.getElapsedTime().asMilliseconds() / ANIMATION_STEP;
auto digit0 = std::to_string(frame % 10);
auto digit1 = std::to_string(frame / 10);
sprite_.setTexture(*game_->getResourceManager().getTexture("animation/explosion_"+digit1+digit0+".png"));

Upvotes: 1

Hatatister
Hatatister

Reputation: 1052

Here is some oop like approach, which makes it easy to extend to a arbitrary number of animation_files.

It's just a quick'n'dirty prototype, but the scheme should be clear.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

class AnimationManager
{
public:
    AnimationManager(float pStepsize) : mStepsize(pStepsize) {}
    void registerSprite(std::string pFilename) //assuming that animation files are registered in the right order, alternatively use std::map as backend or something else
    {
        mFilenames.push_back(pFilename);
    }
    std::string getFilenameForAnimationStep(float ellapsed_time)
    {
        if (ellapsed_time < 0) //we dont want go pack in the past
            ellapsed_time = 0;
        unsigned int step = static_cast<unsigned int>(ellapsed_time/mStepsize); //trunc to int
        step = std::min(step, mFilenames.size() - 1); //make shure we use the last registered animation, if we are to fahre in the future
        return mFilenames[step];
    }

private:
    std::vector<std::string> mFilenames;
    float mStepsize;
};




int main()
{
    AnimationManager my_manager(0.2);
    my_manager.registerSprite("step_0.jpq");
    my_manager.registerSprite("step_1.jpq");
    my_manager.registerSprite("step_2.jpq");
    my_manager.registerSprite("step_3.jpq");
    //....
    //testing
    std::cout << my_manager.getFilenameForAnimationStep(-0.5) << std::endl;
    std::cout << my_manager.getFilenameForAnimationStep(0.1) << std::endl;
    std::cout << my_manager.getFilenameForAnimationStep(0.2) <<std::endl;
    std::cout << my_manager.getFilenameForAnimationStep(0.5) << std::endl;
    std::cout << my_manager.getFilenameForAnimationStep(0.7) << std::endl;
    std::cout << my_manager.getFilenameForAnimationStep(1.7) << std::endl;

    system("Pause");
}

Upvotes: 0

user1084944
user1084944

Reputation:

Given that the steps are even, you could just compute which one to use:

int step = ticker_.getElapsedTime().asMilliseconds() / ANIMATION_STEP;

You could do an integer-to-string conversion to use that to create the file-name, but that's both somewhat awkward to use and overly rigid. Instead, you could populate an array of filenames:

// populate array during program initialization 
vector<string> explosion_filenames;

// In your function,
sprite_.setTexture(*game_->getResourceManager().getTexture(explosion_filenames[step]));

or even better, an array of textures:

// populate array during program initialization 
vector<whatever_the_texture_type_is> explosion_textures;

// In your function,
sprite_.setTexture(*explosion_textures[step]));

If the steps aren't even, you could instead use an ordered map

// I use `int`, but use whatever type is most appropriate
// populate map during program initialization 
map<int, texture_type> explosion_textures;

// In your function,
int step = ticker_.getElapsedTime().asMilliseconds();
sprite_.setTexture(*explosion_textures.lower_bound(step)->second);

Upvotes: 2

Alex Zywicki
Alex Zywicki

Reputation: 2313

How about:

bool check(std::size_t step)
{
    auto tm = ticker_.getElapsedTime().asMilliseconds();
    return (tm >= ANIMATION_STEP * step) && (tm < ANIMATION_STEP * (step + 1));
}

for(int i=0; i <n;++i)
{
    if (check(i))
    {
        std::stringstream fname;
        fname<<"animation/explosion_"<<
        fname<<std::setfill(0)<<std::setw(2)<<i<<".png";
        sprite_.setTexture(*game_->getResourceManager().getTexture(fname.str()));
    }
}

Upvotes: 1

Greg Kikola
Greg Kikola

Reputation: 573

How about something like

int frame = ticker_.getElapsedTime().asMilliseconds() / ANIMATION_STEP;
if (frame < MAX_FRAMES) {
    std::string filename = "animation/explosion_"
                         + std::to_string(frame) + ".png";
}

Upvotes: 5

Related Questions