<p>How can you make the display frames per second be independent from the game logic? That is so the game logic runs the same speed no matter how fast the video card can render. </p>

[{'answer_id': 17415, 'author': 'Jesse Beder', 'author_id': 112, 'author_profile': 'https://Stackoverflow.com/users/112', 'pm_score': 2, 'selected': False, 'text': "<p>You could make your game loop look like:</p>\n\n<pre><code>int lastTime = GetCurrentTime();\nwhile(1) {\n    // how long is it since we last updated?\n    int currentTime = GetCurrentTime();\n    int dt = currentTime - lastTime;\n    lastTime = currentTime;\n\n    // now do the game logic\n    Update(dt);\n\n    // and you can render\n    Draw();\n}\n</code></pre>\n\n<p>Then you just have to write your <code>Update()</code> function to take into account the time differential; e.g., if you've got an object moving at some speed <code>v</code>, then update its position by <code>v * dt</code> every frame.</p>\n"}, {'answer_id': 17417, 'author': 'Adam', 'author_id': 1366, 'author_profile': 'https://Stackoverflow.com/users/1366', 'pm_score': 5, 'selected': False, 'text': '<p>I think the question reveals a bit of misunderstanding of how game engines should be designed. Which is perfectly ok, because they are damn complex things that are difficult to get right ;)</p>\n\n<p>You are under the correct impression that you want what is called Frame Rate Independence. But this does not only refer to Rendering Frames.</p>\n\n<p>A Frame in single threaded game engines is commonly referred to as a Tick. Every Tick you process input, process game logic, and render a frame based off of the results of the processing.</p>\n\n<p>What you want to do is be able to process your game logic at any FPS (Frames Per Second) and have a deterministic result.</p>\n\n<p>This becomes a problem in the following case:</p>\n\n<p>Check input:\n - Input is key: \'W\' which means we move the player character forward 10 units:</p>\n\n<blockquote>\n  <p>playerPosition += 10;</p>\n</blockquote>\n\n<p>Now since you are doing this every frame, if you are running at 30 FPS you will move 300 units per second.</p>\n\n<p>But if you are instead running at 10 FPS, you will only move 100 units per second. And thus your game logic is <em>not</em> Frame Rate Independent.</p>\n\n<p>Happily, to solve this problem and make your game play logic Frame Rate Independent is a rather simple task.</p>\n\n<p>First, you need a timer which will count the time each frame takes to render. This number in terms of seconds (so 0.001 seconds to complete a Tick) is then multiplied by what ever it is that you want to be Frame Rate Independent. So in this case:</p>\n\n<p>When holding \'W\'</p>\n\n<blockquote>\n  <p>playerPosition += 10 * frameTimeDelta;</p>\n</blockquote>\n\n<p><em>(Delta is a fancy word for "Change In Something")</em></p>\n\n<p>So your player will move some fraction of 10 in a single Tick, and after a full second of Ticks, you will have moved the full 10 units.</p>\n\n<p>However, this will fall down when it comes to properties where the rate of change also changes over time, for example an accelerating vehicle. This can be resolved by using a more advanced integrator, such as "Verlet".</p>\n\n<h2><strong>Multithreaded Approach</strong></h2>\n\n<p>If you are still interested in an answer to your question (since I didn\'t answer it but presented an alternative), here it is. Separating Game Logic and Rendering into different threads. It has it\'s draw backs though. Enough so that the vast majority of Game Engines remain single threaded.</p>\n\n<p>That\'s not to say there is only ever one thread running in so called single threaded engines. But all significant tasks are usually in one central thread. Some things like Collision Detection may be multithreaded, but generally the Collision phase of a Tick blocks until all the threads have returned, and the engine is back to a single thread of execution.</p>\n\n<p>Multithreading presents a whole, very large class of issues, even some performance ones since everything, even containers, must be thread safe. And Game Engines are very complex programs to begin with, so it is rarely worth the added complication of multithreading them.</p>\n\n<h2><strong>Fixed Time Step Approach</strong></h2>\n\n<p>Lastly, as another commenter noted, having a Fixed size time step, and controlling how often you "step" the game logic can also be a very effective way of handling this with many benefits.</p>\n\n<p>Linked here for completeness, but the other commenter also links to it:\n<a href="http://gafferongames.com/game-physics/fix-your-timestep/" rel="noreferrer">Fix Your Time Step</a></p>\n'}, {'answer_id': 17420, 'author': 'Jarod Elliott', 'author_id': 1061, 'author_profile': 'https://Stackoverflow.com/users/1061', 'pm_score': -1, 'selected': False, 'text': '<p>From my experience (not much) Jesse and Adam\'s answers should put you on the right track.</p>\n\n<p>If you are after further information and insight into how this works, i found that the sample applications for <a href="http://www.truevision3d.com/" rel="nofollow noreferrer">TrueVision 3D</a> were very useful.</p>\n'}, {'answer_id': 17424, 'author': 'Ryan Fox', 'author_id': 55, 'author_profile': 'https://Stackoverflow.com/users/55', 'pm_score': 3, 'selected': False, 'text': '<p>Koen Witters has a <a href="https://dewitters.com/dewitters-gameloop/" rel="nofollow noreferrer">very detailed article</a> about different game loop setups.</p>\n\n<p>He covers:</p>\n\n<ul>\n<li>FPS dependent on Constant Game Speed</li>\n<li>Game Speed dependent on Variable FPS</li>\n<li>Constant Game Speed with Maximum FPS</li>\n<li>Constant Game Speed independent of Variable FPS</li>\n</ul>\n\n<p>(These are the headings pulled from the article, in order of desirability.)</p>\n'}, {'answer_id': 17427, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 2, 'selected': False, 'text': '<p>There was an excellent article on flipcode about this back in the day. I would like to dig it up and present it for you.</p>\n\n<p><a href="http://www.flipcode.com/archives/Main_Loop_with_Fixed_Time_Steps.shtml" rel="nofollow noreferrer">http://www.flipcode.com/archives/Main_Loop_with_Fixed_Time_Steps.shtml</a></p>\n\n<p>It\'s a nicely thought out loop for running a game:</p>\n\n<ol>\n<li>Single threaded</li>\n<li>At a fixed game clock</li>\n<li>With graphics as fast as possible using an interpolated clock</li>\n</ol>\n\n<p>Well, at least that\'s what I think it is. :-) Too bad the discussion that pursued after this posting is harder to find. Perhaps the wayback machine can help there.</p>\n\n<pre><code>time0 = getTickCount();\ndo\n{\n  time1 = getTickCount();\n  frameTime = 0;\n  int numLoops = 0;\n\n  while ((time1 - time0)  TICK_TIME &amp;&amp; numLoops &lt; MAX_LOOPS)\n  {\n    GameTickRun();\n    time0 += TICK_TIME;\n    frameTime += TICK_TIME;\n    numLoops++;\n// Could this be a good idea? We\'re not doing it, anyway.\n//    time1 = getTickCount();\n  }\n  IndependentTickRun(frameTime);\n\n  // If playing solo and game logic takes way too long, discard pending\ntime.\n  if (!bNetworkGame &amp;&amp; (time1 - time0)  TICK_TIME)\n    time0 = time1 - TICK_TIME;\n\n  if (canRender)\n  {\n    // Account for numLoops overflow causing percent  1.\n    float percentWithinTick = Min(1.f, float(time1 - time0)/TICK_TIME);\n    GameDrawWithInterpolation(percentWithinTick);\n  }\n}\nwhile (!bGameDone);\n</code></pre>\n'}, {'answer_id': 19880, 'author': 'GhassanPL', 'author_id': 1536, 'author_profile': 'https://Stackoverflow.com/users/1536', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://www.gamedev.net/reference/programming/features/enginuity1/" rel="nofollow noreferrer">Enginuity</a> has a slightly different, but interesting approach: the Task Pool.</p>\n'}, {'answer_id': 63396, 'author': 'realsugar', 'author_id': 6885, 'author_profile': 'https://Stackoverflow.com/users/6885', 'pm_score': 0, 'selected': False, 'text': '<p>Single-threaded solutions with time delays before displaying graphics are fine, but I think the progressive way is to run game logic in one thread, and displaying in other thread. </p>\n\n<p>But you should synchronize threads right way ;) It\'ll take a long time to implement, so if your game is not too big, single-threaded solution will be fine. </p>\n\n<p>Also, extracting GUI into separate thread seems to be great approach. Have you ever seen "Mission complete" pop-up message while units are moving around in RTS games? That\'s what I\'m talking about :)</p>\n'}, {'answer_id': 2163841, 'author': 'Camilo', 'author_id': 262009, 'author_profile': 'https://Stackoverflow.com/users/262009', 'pm_score': 0, 'selected': False, 'text': '<p>This doesn\' cover the higher program abstraction stuff, i.e. state machines etc.</p>\n\n<p>It\'s fine to control movement and acceleration by adjusting those with your frame time lapse.\nBut how about stuff like triggering a sound 2.55 seconds after this or that, or changing\ngame level 18.25 secs later, etc.</p>\n\n<p>That can be tied up to an elapsed frame time accumulator (counter), BUT these timings can \nget screwed up if your frame rate falls below your state script resolution\ni.e if your higher logic needs 0.05 sec granularity and you fall below 20fps. </p>\n\n<p>Determinism can be kept if the game logic is run on a separate "thread" \n(at the software level, which I would prefer for this, or OS level) with a fixed time-slice, independent of fps. </p>\n\n<p>The penalty might be that you might waste cpu time in-between frames if not much is happening,\nbut I think it\'s probably worth it.   </p>\n'}]
['https://Stackoverflow.com/questions/17411', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']