Jump to content
• Advertisement

Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Game loop ... or whatever it's called ...

This topic is 5646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Yes, I''m yet another person interested in game development. I have a few questions about the thing that Im going to call the main game loop. Before I get into it, I should probably mention that I have finished my CS degree, so c++ or programming concepts probably wont be an issue here. I''m wondering how a person is supposed to throttle a game. My first forray into the game development world was something simple ... another tetris clone. I soon realized that I can''t let the main game loop run full bore, or the block would hit the bottom of the screen before the user knew what was happening. At first I tried the distance = (speed * time) approach ... this didnt work out since the time between each loop (or frame) was so small that the blocks never moved. Instead I decided to figure out how much time it should take for the block to fall one space, and only refresh the screen after this time is up. A simple enough solution, but I doubt this strategy would work in a real game. I''m trying to figure out how the Quakes, Unreals and Diablo 2s of the world solve this problem. In games such as these the user ideally wants frame rates that are as high as possible ... refreshing the screen after a certain amout of time isn''t the way to achieve this, and for some reason (in my head) it doesn''t make sence to let the loop run free without any throttle. is there enough happening in these game loops so that they essentially throttle themselves? thanks in advance

#### Share this post

##### Share on other sites
Advertisement
God no, don''t throttle the game loop. Let it run as fast as possible.

Your thought to use distance = speed * time was the correct one. Each time through the loop, find the number of time units that have elapsed since the last screen refresh and use that time delta to update the position of your game entities. If you are running at 200 fps, that time delta would be around 1/200 of a second. If your blocks move at 2 unites per second, then they should move 1/100 of a unit every frame at 200 fps. If your blocks weren''t moving at all the first time you tried this, you must have been doing something wrong.

Even if the framerate becomes slower (or faster), using this method will provide a physically realistic motion in your game, while allowing smooth screen updates.

#### Share this post

##### Share on other sites
quote:
Original post by fizban75
God no, don''t throttle the game loop. Let it run as fast as possible.

If your blocks weren''t moving at all the first time you tried this, you must have been doing something wrong.

I think my problem was most likely that I was using INTs as the type for my x,y coordinates of the blocks. If I had used DOUBLEs I wouldnt have rounded down to a distance of zero pixles for each move.

#### Share this post

##### Share on other sites
quote:
Original post by SpaceBoy2001

I think my problem was most likely that I was using INTs as the type for my x,y coordinates of the blocks. If I had used DOUBLEs I wouldnt have rounded down to a distance of zero pixles for each move.

That''s definitely the problem. All position information should be handled as floats or doubles.

#### Share this post

##### Share on other sites
Salutations.

I wrote a game with time-based motion. I used floats to store position on the map (in units of "tiles"), and timeGetTime() (it returns the current system time) to calculate the "time delta" per game loop, in milliseconds. All velocities were then worked into the game in units of tiles/milliseconds. You can find a copy of my game and source here if your interested in how I did it. Workspace & Project files for MSVC++ 6.0 pro included.

---email--- Tok ----surf----
~The Feature Creep of the Family~

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5
• Advertisement

• 36
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631703
• Total Posts
3001814
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!