- Online Community Forums » Programming Questions » Jaggy/irregular Animation and main loop

This thread is locked; no one can reply to it. rss feed Print
Jaggy/irregular Animation and main loop
Member #7,638
August 2006


I need some advice on smooth animation. I tried two rendering techniques (double buffering and page flipping) and neither of them turned out to be smooth enough, even if I use vsync, the animation seems to be kind of "jaggy".

I am using the following approach to a main game loop:

1void LogicTimer()
3 //Timer for game logic
4 pending_cycles++;
8void FramesTimer()
10 pending_frames++;
14int main()
16while (!game_over)
18 if (blit_buffer) //Has the screen buffer changed, does it need to be blit?
19 {
20 //Page flipping
21 show_video_bitmap(screen_buffer);
23 if (screen_buffer == page1)
24 screen_buffer = page2;
25 else
26 screen_buffer = page1;
28 clear_to_color(screen_buffer, black); //Reset memory bitmap to blackness.
29 blit_buffer = 0;
30 }
32 PollEvents(); //User input polling
34 //Logic
35 while (pending_cycles > 0)
36 {
37 compute_logic();
38 pending_cycles--;
39 }
41 //Rendering
42 if (pending_frames > 0)
43 {
44 render_stuff_to_screen_buffer();
45 pending_frames--;
46 blit_buffer = 1;
47 }

As you can see, I am using two different timers to do logic calculations and rendering stuff. That's because I want to be able to modify game speed and (desired) framerate seperately from each other.

I have put blitting functions on top of the loop, because I might want to be able to rest() at the end of the loop, handing over some CPU time to other applications. This rest() call being a waste of time for my game, I think it's most efficient to calculate all the game logic between blit() and rest().

When I use a much simpler approach without timers, like in the Allegro example, animation is smooth.

I am working under Windows where Allegro timers internally make use of threads, I guess. I am now suspecting that my two timers are interfering with each other in some way or that the timing code in Allegro is not very accurate.

Thanks for any suggestions.

Best regards,

Mr. Big
Member #6,196
September 2005

Your objects probably have velocities that make them move, right?
It might be the integration technique that you are using that makes the animation jaggy.
I'm working under Windows too and don't recall having any trouble with the timers.

Member #3,326
March 2003

what speed are you setting your timers ?
Maybe if logic timer is faster than draw timer then animation can be irregular,
but not sure because I just use one timer in my main loop.

Member #7,638
August 2006

The logic timer is triggered about 250 times per sec, while the frame timer is normally at 60 times per sec. I tried to remove the frame timer for testing purposes and it didn't really change anything. I suppose the positional calculations themselves are already "jaggy".

I am using floating point values as speed settings (and for screen coordinates). My objects normally have speeds between 0.0f and 1.0f, so that they never move more than one pixel at a time. They are not allowed to skip any pixels, because collision detection depends on it. Else, I'd have to work with vectors and that's complicated I guess. :)

Best regards and thanks so far,

Paul whoknows
Member #5,081
September 2004

Allegro timers sucks on windows, use a more accurate timer instead.
Look at Phipps's articles, he uses a better timer with double buffering and page flipping.


"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Trent Gamblin
Member #261
April 2000

In addition to what's been mentioned so far, under Windows I get frequent "blips" in animation unless I set the process to run under a higher priority. BTW, does anyone know if there is a way for the program itself to set it's priority under Windows?

Go to: