🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

SDL: delay between drawing

Started by
5 comments, last by graveyard filla 19 years, 10 months ago
Hi I i want to draw three images with an delay of 1 sec between each other. For the drawing i use my own drawing function, which is calling the normal SDL_BlitSurface() to draw the image surface on the screen surface. For the delay i use the SDL_Delay() function. But instead of seeing one picture beeing drawn after the other, about 3 sec happens nothing, and than all three pictures apear at once. Why? and how can i fix this?
Advertisement
Quote: Why? and how can i fix this?


post your code.

-me
Are you updating the screen after each blit, by calling SDL_Flip or SDL_UpdateRect for example?
You are going to have to set it in a loop and call SDL_GetTicks to get the elapsed miliseconds, after about 3000, (3 secs) draw the new image and flip the screen.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
I have tried SDL_UpdateRect for the whole screen after bliting of each image, but its still the same. (am usine a single buffer screen, so no need for flipping)
my function

SDL_Surface *surface_to_blit[2];float time = SDL_GetTicks();int DrawSprite(float time, int xpos, int ypos){    const float time_per_frame = 2000/10; // Time to change setting    const char total_frames = 2;    static char frame = 0;    static float time_old = 0;    static float frametime = 0;    time += frametime;    if( time >= time_per_frame )    {        frame++;        frame %= total_frames;        time_old += time;        time = 0;    }    DrawIMG(surface_to_blit[frame], xpos, ypos, 128, 128, 0, 0);    frametime = SDL_GetTicks() - time_old;    return 0;}
im kind of in a hurry, so i didnt really have time to try to fully understand what you were trying to do. but it looks like your over-complicated things. try this:

int DrawSprite(float time, int xpos, int ypos){    static Uint32 time = SDL_GetTicks();     //if 2 seconds have passed    if( SDL_GetTicks() - time > 2000 )    {          //incremenet to the next frame        frame++;                //if we get past the max frames, go back to 0        if(frame >= MAX_FRAMES)          frame = 0;        //re-set the timer again        time = SDL_GetTicks();    }    DrawIMG(surface_to_blit[frame], xpos, ypos, 128, 128, 0, 0);    return 0;}


basically i get the time, then you check if 2 seconds have passed... if it has, you increment to the next frame. so basically, every 2 seconds, you draw the next frame. when you get to MAX_FRAMES (whatever size that array of yours is), then you go back to 0. pretty simple and it looks like you were doing too much work.
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement