1
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ Specifically, this tutorial (linked in the dupe question) will get you started: koonsolo.com/news/dewitters-gameloop \$\endgroup\$
    – Tetrad
    Commented May 8, 2011 at 4:58
  • \$\begingroup\$ 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... \$\endgroup\$ Commented May 17, 2011 at 6:09

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$ Commented May 8, 2011 at 4:47
  • \$\begingroup\$ 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: d3dcoder.net/d3d10.htm Download the source code and look for a file called Common/GameTimer.cpp \$\endgroup\$
    – nahano
    Commented May 8, 2011 at 5:06

Not the answer you're looking for? Browse other questions tagged .