Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » FPS-Locking with timer

This thread is locked; no one can reply to it. rss feed Print
FPS-Locking with timer
poschma
Member #8,507
April 2007

Hi all!

I've a problem with a simple Pong program:

In my main loop I work with like it is told at http://www.allegro.cc/manual/miscellaneous/frequently-asked-questions-(faq)/general-problems/bd35ed0706dad8b5c3aa3b3a91cdba2c

1 while (!key[KEY_ESC])
2 {
3 iSkip=0;
4 while(speed_counter>0)
5 {
6 if (getKeyState(KEY_ESC)==1)
7 {
8 bEnd=true;
9 }
10 if (key[KEY_LEFT])
11 {
12 psBar->vMove(psBar->iGetSpeed()*-1,psDisplay);
13 }
14 else if (key[KEY_RIGHT])
15 {
16 psBar->vMove(psBar->iGetSpeed(),psDisplay);
17 }
18 psBall->vMove(psDisplay, psBar);
19 
20 speed_counter--;
21 }
22 acquire_bitmap(psDisplay->m_pBSArena);
23 psBalr->vDelete(psDisplay->m_pBSArena,psDisplay->m_pBSArena_bkgnd);
24 psBalr->vDraw(psDisplay->m_pBSArena);
25 psBall->vDelete(psDisplay->m_pBSArena,psDisplay->m_pBSArena_bkgnd);
26 psBall->vDraw(psDisplay->m_pBSArena);
27 release_bitmap(psDisplay->m_pBSArena);
28 blit(psDisplay->m_pBSArena,screen,0,0,0,0,psDisplay->m_pBSArena->w,psDisplay->m_pBSArena->h);
29 if (bEnd==true)
30 break;
31 }

My problem is, that the ball is jumping around the screen because there are many, many passes of the loop before the speed_counter is 0 and so the ball moves from (x,y) in one frame to (x+20,y+20) in the next frame. (Doesn't look very fine).
How have you solved such problems?

One idea is that I make a new loop. This loop match one second (With a framerate of 80 fps this loop will be executed 80 times a second) . So I need a timer that can tell me the durration of the loop (from the beginning to the end of drawing). After drawing I can then make a rest() of (1000/rramerate - duration of the loop).

Is this a good idea? - How do I make such a timer?

THX poschi

Kibiz0r
Member #6,203
September 2005
avatar

This is the most confusing thread I've read in a while, but that might just be me.

Quote:

there are many, many passes of the loop before the speed_counter is 0

You've got something making expensive rendering calls, then. So when control goes back to the logic bit, it has to make up all those lost logic frames in one visual frame.

Quote:

This loop match one second

Wait, what? Does that mean you are moving it in one go of the while loop the amount that you want it to move in one second? Because if so, you need to divide it by your FPS.

Quote:

So I need a timer that can tell me the durration of the loop (from the beginning to the end of drawing).

You can either hack an Allegro timer to do it, probably resulting in inaccurate data, or you can use Clock() or similar. Just take the difference between the return value before and after.

Quote:

After drawing I can then make a rest() of (1000/rramerate - duration of the loop).

You can, but I don't know why you would. That'll just put you further behind.

poschma
Member #8,507
April 2007

Sorry confusing you!
Ok, I understand why you are so confused. I was thinking in the wrong direction. My problem has nothing todo with the timer. They work exactly like they should. I insertet a debug routine and the number of loop passes was exactly my frame rate of 80 FPS. Still visually it looks like the ball is sometimes faster a little bit than other times.

gillius
Member #119
April 2000

If you are rendering at 80 FPS, you can try enabling vsync. Shearing due to not using vsync may make the animation appear less smooth, although enabling vsync may make it look like it "stutters" if it causes the framerate to shift rapidly. If you have an LCD monitor, you could be having problems with response time. I know breakout style games like Sunny Ball can be almost unplayable at the faster/harder skill levels because the ball smears on an LCD screen. I thought I bought a fast monitor at 15ms (previously all "normal" LCDs were 20-25ms), but playing Sunny Ball on a CRT was a TON better as the frame rate could be more than 60, but mostly because of no smearing. If this is what's causing the ball animation to not look good for you, there's nothing you can do.

Gillius
Gillius's Programming -- https://gillius.org/

Go to: