Sign in to follow this  

SDL,Sprite animation freeze on frame

This topic is 3493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've a problem with sprite animation. I use the idea of the cone3d tutorial. but the frame runs without any smoothness (what is the contrary?sorry my english:). I clean the code to test the draw fuction, look like this..
class Ssprite {
    SDL_Rect pos;
    vector<SDL_Surface*> tiles;

    SDL_Surface* screen;

    int currFrame;
    int lastTicks; //for animation

    public:
        Ssprite(const char*,SDL_Surface*);
        void Draw();
        
};



void Ssprite::Draw(){
    if(lastTicks + PAUSE < SDL_GetTicks()){
        currFrame++;
        if(currFrame > 4)
            currFrame=0;

        SDL_BlitSurface(tiles[currFrame],0,screen,&pos);
    }
    lastTicks = SDL_GetTicks();

} 
With PAUSE = 1 or PAUSE = 2 run very fast at start but with 3 or up it runs without any smoothness. with PAUSE = 5 freeze on some frame for about 1 sec before change frame!! This is the main:


int main ( int argc, char** argv )
{
    // initialize SDL video
    if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }


    atexit(SDL_Quit);

    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
    
    Ssprite player("",screen);

    bool done = false;
    while (!done)

          player.Draw();

        
        SDL_Flip(screen);
    } 
   
    printf("Exited cleanly\n");
    return 0;
}
I've clened the code a bit for better reading I can't figure out what i'm doing wrong. I'm devving on debian linux with Code::Blocks. Thanks Garulf- PS: what's the tag to show the code in a scrolling frame?

Share this post


Link to post
Share on other sites
I modify your draw function an bit. Try this out instead.
void Ssprite::Draw()
{
if(lastTicks + PAUSE < SDL_GetTicks())
{
currFrame++;
if(currFrame > 4)
currFrame=0;

lastTicks = SDL_GetTicks();
}
SDL_BlitSurface(tiles[currFrame],0,screen,&pos);
}

//I notice that you did not initialize lastTicks to anything. But your best bet could be that you could go and initialize it to SDL_GetTick() in the constructor of your Ssprite class. Also what is the value of PAUSE. If you want the animation to last one second, you should set Pause to 1000 since SDL_GetTick() is also in millisecond.

Share this post


Link to post
Share on other sites
Yes!..it works!Thanks.
Yes, i already initialize lastTicks in the class constructor, when i also fill the vector with the surfaces. :)
Thanks Again
Garulf-

Share this post


Link to post
Share on other sites
Sign in to follow this