• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
GrimV5

Timing game loop

10 posts in this topic

Is there a standard value for the amount of time I should wait between each update of the main thread? I'm using SFML on c++, right now I just have a basic loop:

 

while(game.isRunning()){

   game.HandleEvent();

   game.Update();

   game.Paint();

   //time sleep method

}

 

I'm making a 2d game, so how long should the loop wait before starting over?

0

Share this post


Link to post
Share on other sites

You usually don't sleep at all in a game. You could yield (SwitchToThread on windows) if you really want, but it's not like we're still using win9x kernels or something.

 

Also keep in mind you'll already be sleeping until vsync if you're using vsync in your game.

1

Share this post


Link to post
Share on other sites

If you're not using a fixed time step or waiting for vsync, you may want to keep track of the time between frames. Which in your case it's pretty simple, a frame is each run of your game loop.

 

You should start a clock right before your game loop starts. At the beginning of each loop save the clock time and restart the clock. This would typically be called the game time as it'll be the time between the previous from to the beginning of the next frame. You would then pass this game time around to the various parts of your game to make sure everything is executed at the correct speed. I.E  movement of objects, physics, etc.

0

Share this post


Link to post
Share on other sites

You usually don't sleep at all in a game. You could yield (SwitchToThread on windows) if you really want, but it's not like we're still using win9x kernels or something.

 

Also keep in mind you'll already be sleeping until vsync if you're using vsync in your game.

I may have asked this the wrong way.  What I meant was what kind of time step should I use so that it doesn't look like the sprites are flying across the screen at mach 3?

0

Share this post


Link to post
Share on other sites

 

You usually don't sleep at all in a game. You could yield (SwitchToThread on windows) if you really want, but it's not like we're still using win9x kernels or something.

 

Also keep in mind you'll already be sleeping until vsync if you're using vsync in your game.

I may have asked this the wrong way.  What I meant was what kind of time step should I use so that it doesn't look like the sprites are flying across the screen at mach 3?

 

 

Try doing what I suggested above, and when you're manipulating the speed of your sprites, make sure to multiply the speed by the game time.

 

I.E

 

objectPosition.x = objectPosition.x + (speed * gameTime).

 

This would make sure the speed value for your object is only as fast as the time step. The longer it takes for the next frame (or loop) to begin, the greater that speed value will end up being, and vice versa.

 

Say it takes .161 seconds to go from frame 1 to frame 2, and you have a speed of 50.  50 * .161. Doing the math would give you a 'speed for this frame' of 8.05, and thus would only move the sprite by that much, instead of the full 50.

Edited by SolarChronus
0

Share this post


Link to post
Share on other sites

You usually don't sleep at all in a game. You could yield (SwitchToThread on windows) if you really want, but it's not like we're still using win9x kernels or something.
 
Also keep in mind you'll already be sleeping until vsync if you're using vsync in your game.

I may have asked this the wrong way.  What I meant was what kind of time step should I use so that it doesn't look like the sprites are flying across the screen at mach 3?

You want to update sprites at 30-75 Hz. 75Hz is at the high end of what a LCD will display. A frame rate higher than the monitor refresh rate cannot be seen. 60Hz is also a common limit for monitors. You may be able to poll the OS to find out the current display refresh rate. 30 Hz is the low end of what will look like smooth motion. You can run slower, but then tics start being visible.
0

Share this post


Link to post
Share on other sites
If anything, only throttle the drawing of objects. If you throttle input/physics it will make your game seem laggy. So basically just keep a timer and do something like
input();
update_states();
physics();
if(cur_time > max_frame_time) draw(), cur_time = 0;
cur_time += frame_time;
0

Share this post


Link to post
Share on other sites


If you have any form of physics, a fixed time step will also be necessary.
Fixed-Time-Step Implementation

 

What a great explanation.

1

Share this post


Link to post
Share on other sites

To play nice you can put in a Sleep(0).

This will yield your process to anything else that needs processing time after you finished your game loop.

If you do not do this then your process will starve anything waiting and eventually Windows will slice you out at some random place in the game-loop.

 

The nice part of Sleep(0) is if nothing is waiting then it returns to your app right away (unlike Sleep(1) which will probably take 10ms).

0

Share this post


Link to post
Share on other sites

Here's the fundamental thing to bear in mind - you can never, ever predict the frame rate on any other machine than the one you are on.

 

If you use vsync to control the frame rate, you're code will be run on a machine with a different refresh rate, or someone will force vsync off on their graphics card settings (causing the hardware to falsly report vertical blank period as soon as it is asked rather than when it is actually happening).

 

The Fix Your Timestep article has already been posted. This is the defacto standard article to read on this subject. Fix your timestep and allow your graphics to render as fast as possible. Make vsync optional and never assume it is on even if you have requested it in your setup.

 

You pass a delta value into your update() methods and move based on this. So if your object should move 10 units per second, you add 10 * delta to the position where delta is a float representing the number of seconds (normally a small value like 0.0016) that have passed since the last update.

 

The gaffer article, in my humble option, makes it seem a bit more complicated than it actually is. I've found all you need to store is the previous position and the current position, then interpolate between them based on the error. My game loop looks something like:

        float t = timer.elapsed();

        accumulator += t;

        while(accumulator >= delta)
        {
            app.update(delta);
            accumulator -= delta;
        }

        app.render(accumulator / delta);

I use a fixed delta (1.0f / 60.0f for example) so every update is deterministic and repeatable, and characters don't, for example, jump to different heights depending on the frame rate. This is a problem even with simple physics if you have variable timestep, not just highly sophisticated physics sims, although in the latter the problem can lead to worse consequences than characters jumping to different heights. Still, in a Mario style 2D platformer, you want complete control of the maximum jump height for obvious reasons. This approach is the way to ensure that on different hardware.

 

Each entity stores previous and current position, then I do a linear interpolation based on the blend factor passed to app.render when working out where to draw the entity. This means the visuals always lag very slightly behind the real game state, but that's fine.

 

You also need to store previous values for things like fade values, animation track positions and so on and interpolate these but I've never found it necessary to store and interpolate things like acceleration. Maybe its just too small a difference to be visually noticeable, or maybe I misunderstand Gaffer's article.

 

This approach is the most stable for physics, the most consistent across different hardware and very hard to find a reason not to do since it isn't really much work.

Edited by Aardvajk
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0