Advertisement

Syncronization....

Started by December 03, 2001 02:49 PM
3 comments, last by nowaydown1 23 years ago
Hey everyone, was wondering if maybe could explain to me how to use syncronization. Like I am programming a 2D space shooter in c++, and ive read some tutorials on sync, but they all say to alter the characters movement based on the number of frames per second, thats all well and good, but it only moves one pixel a second already, and that takes like no time at all to zoom back and forth across the screen and my engine runs at 300+ fps. What can I do? Maybe someone can hook up me with a good ''newbie friendly'' tutorial. Thanks... -Noway
Are you trying to keep things from moveing to fast? trying to maintain a near consistant frame rate?

There are a number of ways to do this...one is to pause the game and wait for the system clock to update...but the clock updates at about 18.7 times a second (or there abouts...I don''t recall the exact number)...there are ways to make it update faster, but this can cause problems if you don''t know exactly what you are doing...

Prolly the *safest* way to maintain frame rate is through the monitor V-Sync update...basicly the moniter sets a flag on one of the ports everytime it does a vertical refresh...in your game...just before you draw the screen...set up a loop to check for this flag...when found continue on and blit your sprites, etc...most PC moniters update at around 70mhz (70 times a second or so) altho some are faster/slower
Advertisement
If using Windows, you can use the function GetTickCount(). It returns the number of milliseconds since Windows was started.
At the beginning of the game loop, create a variable to hold the time.

DWORD start_time = GetTickCount(); // this holds the time that the loop started

A good frame rate to set would be 30fps, which is approx. 33 milliseconds.
So at the end of the loop, you can create a while statement to wait until the 33 milliseconds have passed.

while ( ( GetTickCount() - start_time ) < 33 );

Now your program will run at a consistent rate unless your program drops below 30fps, but the max is set.

All my info was from Andre LaMothe''s Tricks of the Windows Game Programming Gurus.

I think the problem is that he''s using integers to store the coordinates of the ship, and so each update, the smallest amount the ship can move is 1 pixel.

The solution is to use floats everywhere, right up to the point where you actually draw it on the screen, and only then covert to integers. That way, you can specify the ship moves 0.01 pixels per frame (which is 1 pixel every 100 frames).

codeka.com - Just click it.
Of course, I mean convert to integers... What you do in your spare time is your own business

codeka.com - Just click it.

This topic is closed to new replies.

Advertisement