Allegro.cc - Online Community

Allegro.cc 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
Locutus266
Member #7,638
August 2006

Hello,

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()
2{
3 //Timer for game logic
4 pending_cycles++;
5}
6END_OF_FUNCTION(pnd_LogicTimer)
7 
8void FramesTimer()
9{
10 pending_frames++;
11}
12END_OF_FUNCTION(pnd_FramesTimer)
13 
14int main()
15{
16while (!game_over)
17{
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);
22 
23 if (screen_buffer == page1)
24 screen_buffer = page2;
25 else
26 screen_buffer = page1;
27
28 clear_to_color(screen_buffer, black); //Reset memory bitmap to blackness.
29 blit_buffer = 0;
30 }
31 
32 PollEvents(); //User input polling
33 
34 //Logic
35 while (pending_cycles > 0)
36 {
37 compute_logic();
38 pending_cycles--;
39 }
40 
41 //Rendering
42 if (pending_frames > 0)
43 {
44 render_stuff_to_screen_buffer();
45 pending_frames--;
46 blit_buffer = 1;
47 }
48//rest(1);
49}
50}

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,
Christoph

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.

jamal
Member #3,326
March 2003
avatar

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.

Locutus266
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,
Christoph

Paul whoknows
Member #5,081
September 2004
avatar

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
avatar

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: