Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Tips for writing the main game loop?

I am attempting to make a game using OpenGL and C++ but i haven't been able to find any good tutorials..or anything that could assist me in setting up my main game loop with some kind of timer/tick system...i would prefer to use as little external libraries as possible...i am using Win32 any help would be appreciated

share|improve this question

marked as duplicate by Tetrad May 8 '11 at 4:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Specifically, this tutorial (linked in the dupe question) will get you started: – Tetrad May 8 '11 at 4:58
is there a way u can reopen this question...that link doesn't provide the correct information i wonted...and i believe this isn't a dup because the other person did no specify a language... – I Phantasm I May 17 '11 at 6:09
up vote 1 down vote accepted

This is called timer based movement. The premise is that game should run at the same speed regardless of your hardware or the current frame rate. Your program needs to ask the system its current time so that the objects will appear to move at the same speed on all computers. Started unzipping a huge file? Your character will still walk the same speed, only instead of taking 500 frames to do it he'll do it in 50, moving 10 times the distance each frame.

Be careful of pausing! You'll have to distinguish between system time and game time. I do this by subtracting out the accumulated pause time, otherwise things will appear to "jump" ahead. For example, if you had a sun/moon cycle based on the system time, where an in game day was equal to 15 real minutes, pause the game while it's day. Come back in 15 minutes, unpause it a WOW! it's magically nighttime.

In my games, I have all speeds expressed in terms of meters per second, or, if it is a 2D game, pixels per second. This is much easier to work with than using "units per frame." At the beginning of my game loop, I call the tick() function which calculates the time difference in seconds between this call to tick() and the last call to tick(). This difference is stored in a variable called df which I multiply all speeds by before adding them to object positions. If you do this then BAM! your game has timer based movement.

Windows has functions called QueryPerformanceCounter and QueryPerformanceFrequency that you can use to pull this off. Last time I checked, there may be some problems with df being negative if you get shuffled to another processor. Frank Luna recommends clamping df to the nonnegatives in this situation. Don't worry about external libraries, in my game engine the timer functionality is all contained in one small class.

share|improve this answer
hmm thanks for all the info :D would u be able to give me an example of your tick() function and would this be able to delay something from happening for a certain amount of ticks? – I Phantasm I May 8 '11 at 4:47
If you are talking about scheduling a time for an in-game event, I wouldn't use tick() for that. Better to set an actual time in seconds for when you want the event to occur by scheduling it to current_game_time + number_of_seconds_in_the_future then checking if we've got there yet. As for an example, you can check Mr. Luna's website: Download the source code and look for a file called Common/GameTimer.cpp – jeffythedragonslayer May 8 '11 at 5:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.