Skip to main content
GameDev.net gamedev.net
🔒 Locked

Speeds

Started by Ys Nov 30, 2001 at 8:55 AM 4 replies 1.9k views
Original Post
Ys
Ys
Hey, ive been working on a tetris style game in opengl ive pretty done the game but one problem i still have is how can i make the blocks drop the same speed on all computers ive been trying to use gettickcount, please help thanks
Im not fat just big boned :)
Fruny
Fruny
Do a search on "framerate independence".
Use your time function to evaluate the time between two frame and use it as a coefficient in your update function.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
jacksonh
jacksonh
Here''s some pseudo code for what you need to do:

const FRAME_TIME = 30
static int previous_time;

while( FRAME_TIME < (GetCurrentTime() - previous_time) )
// You could do something in here, but you don''t need to

previous_time = GetCurrentTime();

Maximus
Maximus
Dont do what the previous poster said, frame rate limiting isnt a good answer to anything, ever. Instead, listen to Fruny

What youll want to do is more like this:

Create two global variables like so:

float framedelta=0.01, gametime=0;

At the start of your game loop, declare 3 variables like so:

LARGE_INTEGER start_time, tmp_time, freq;

Now we grab the frequency of our systems high performance timer:

QueryPerformanceFrequency(&freq);

Now in your game loop, at the very start of it call:

QueryPerformanceCounter(&start_time);

At the end of your game loop (right before it loops around to run again), add:

QueryPerformanceCounter(&tmp_time);
framedelta = (float)(tmp_time.QuadPart - start_time.QuadPart) / freq.QuadPart;
gametime+=framedelta;

Now when you are moving your blocks (or whatever), do it like so:

block.y += distance_to_move_in_one_second * framedelta;

So that in 1 second of game, the blocks y value will have moved to the value of distance_to_move_in_one_second. In half a second it will have moved half the distance.

Very simple but very useful.

Edit: Forgot to mention why I included the 'gametime' variable... it's useful for such things as displaying the amount of time the games been running and other such things.

Edited by - Maximus on December 1, 2001 1:13:46 AM
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
Maximus
Maximus
If that slows the program then you must be running something like a 386 SX, and for it to run on that youd be damn lucky to have a high performance timer. And what do you mean updating the same thing to the screen? The whole point of it is to update things based on the time that has passed between frames. The more updates you get, the more accurate things will be.

Think of a circle here, if its made out of 120 points it''s a acccurate circle than one made of 30 points. The logic is run from the game loop, they arent run seperate to each other. The game loop consists of checking input, running ai, doing collision detection and physics, reading and writing network packets if required, updating the screen, etc etc.

And having the games internals run like so isnt complicated at all, it''s very simple and very logical.
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.