4

I have already made some video games in C (small personal project). And the problem I encountered every time is the same, how to manage the cycles in a game.

For example, I coded a snake in SFML. I handled the cycles with the frame rates: 5 frame rates while normal, and with a power up, I changed it to 10. That works. But it's hideous. And it won't work correctly with a bad computer. In the same idea, I also made a game where I decided that a cycle was equal to a turn of a loop (with an infinite loop). Same problem, a high performance computer will go faster than a low one.

So, can someone advice me on how to manage correctly and properly cycles in a video game.

Thanks in advance !

Elfayer
  • 3,851
  • 8
  • 41
  • 71
  • 1
    That's a really broad topic and isn't specific to C/C++. Here is a good overview: http://gafferongames.com/game-physics/fix-your-timestep/ (This also probably belongs on http://gamedev.stackexchange.com/) – JoeG May 09 '13 at 14:16
  • I know =/ Didn't know what to tag, sry. – Elfayer May 09 '13 at 14:17
  • 4
    This question is more suited to (and probably already answered on) http://gamedev.stackexchange.com/ – Justin May 09 '13 at 14:17
  • Thanks didn't know that web site =) – Elfayer May 09 '13 at 14:19
  • 1
    What you're looking for is how to manage frame rate. 30, 50, 60 frames per second. – Captain Obvlious May 09 '13 at 14:20
  • Are you calling window.SetFrameRateLimit(5)? If yeah... You don't want to do that. My suggestion is that you measure the time that has passed since the last frame, and if this time is greater than the delay you want (1/5 or 1/10 seconds for you, I think) then you'll allow movement. Otherwise, you skip that section and continue. Don't apply this restriction to the rendering. Don't base it on the number of frames which has passed either, or as you said, high performance computers will just play faster. – SlxS May 09 '13 at 14:24
  • use timing between each frame. If you wanted to move a character along the screen at 5 m/s. Find the time difference between the current and last frame and move its position so it keeps going 5m/s. this eliminates any issue with the difference in performance. – andre May 09 '13 at 14:24
  • @Justin if it's not constructive here it won't really be constructive there. – djechlin May 09 '13 at 14:54
  • @djechlin - theoretically true, but in practice people are often more tolerant of topics they find interesting – Chris Stratton May 09 '13 at 16:22
  • @SlxS It's exactly what I've done. I have been advised to use frame skipping instead of counting time (it's quite the same, but the unit you'll use is not the time but the frame). The program is set to 60 FPS, and if a loop gives more FPS (ex: 70 FPS), you'll wait till the loop takes the time of a 60 FPS. If the FPS is below 60 (ex : 40 FPS), you'll wait till the 2nd (or more) frames pass (in fact you'll wait till the next frame you are in ends) and you'll then display not the next frame, but the 2nd (or more) frame. The problem is that I'm afraid this technique will make jump of images. – Elfayer May 10 '13 at 10:09

2 Answers2

6

Frame rate should have no effect on the game play what so ever. If its gettig 60FPS or 1000s of FPSs then that shouldn't effect anything.

What you want to do is look at the delta time or the time elapsed if that is enough time then do the simulation or power up.

So when you move the snake. It wants to move by a distance per frame. If the frame rate is low it will bigger distance than if the frame rate is high. The distance is the speed_it_moves_at * delta_time.

Andrew
  • 1,704
  • 3
  • 23
  • 37
  • The power up was an example. The point is that if I go left, with the arrow for example, with a loop, I'll go very fast unless I put a usleep in the main loop. But once more it's ugly. =/ I want to managed the speed of the game it's not only frame rates. It's mainly how I handle my main loop of the game. – Elfayer May 09 '13 at 14:33
  • edited answer to help – Andrew May 09 '13 at 14:36
  • What do you think of the frame skipping? – Elfayer May 10 '13 at 10:10
5

In very broad terms, you need to model your game state such that it can "advance" by a given increment of time, and then, during each cycle of your main loop, determine how much time has elapsed (typically a small fraction of a second) and advance the game state by only that much. This is how games appear to work smoothly regardless of frame rate.

The interval basically becomes a scaling factor on all movement. For example, if your snake is moving forward at 5 units of distance per second, and during your main loop you find that 0.01 seconds have elapsed since the last time the snake moved, you would advance your snake by (0.01 * 5) unit of distance during that loop.

meager
  • 209,754
  • 38
  • 307
  • 315
  • How can I know the time passed between a point A and B in my program ? – Elfayer May 09 '13 at 14:35
  • 1
    By examining a [high-precision source of time](http://stackoverflow.com/questions/538609/high-resolution-timer-with-c-and-linux). This is somewhat platform-dependent. This is *fundamental* to video games, so most game libraries provide a higher-level wrapper. – meager May 09 '13 at 14:39
  • What do you think of the frame skipping? – Elfayer May 10 '13 at 10:10