Note that there are some explanatory texts on larger screens.

plurals
  1. POLimiting Update Rate in C++. Why does this code update once a second not 60 times a second?
    primarykey
    data
    text
    <p>I am making a small game with C++ OpenGL. <code>update()</code> is normally called once every time the program runs through the code. I am trying to limit this to 60 times per second (I want the game to update at the same speed on different speed computers). </p> <p>The code included below runs a timer and should call <code>update()</code> once the timer is >= than 0.0166666666666667 (60 times per second). However the statement <code>if((seconds - lastTime) &gt;= 0.0166666666666667)</code> seems only to be tripped once per second. Does anyone know why?</p> <p>Thanks in advance for your help. </p> <pre><code>//Global Timer variables double secondsS; double lastTime; time_t timer; struct tm y2k; double seconds; void init() { glClearColor(0,0,0,0.0); // Sets the clear colour to white. // glClear(GL_COLOR_BUFFER_BIT) in the display function //Init viewport viewportX = 0; viewportY = 0; initShips(); //Time lastTime = 0; time_t timerS; struct tm y2k; y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0; y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1; time(&amp;timerS); /* get current time; same as: timer = time(NULL) */ secondsS = difftime(timerS,mktime(&amp;y2k)); printf ("%.f seconds since January 1, 2000 in the current timezone \n", secondsS); loadTextures(); ShowCursor(true); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } void timeKeeper() { y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0; y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1; time(&amp;timer); /* get current time; same as: timer = time(NULL) */ seconds = difftime(timer,mktime(&amp;y2k)); seconds -= secondsS; //Run 60 times a second. This limits updates to a constant standard. if((seconds - lastTime) &gt;= 0.0166666666666667) { lastTime = seconds; update(); //printf ("%.f seconds since beginning program \n", seconds); } } </code></pre> <p><code>timeKeeper()</code> is called in int <code>WINAPI WinMain</code>, while the program is <code>!done</code></p> <p>EDIT:</p> <p>Thanks to those who helped, you pointed me on the right track. As mentioned in the answer below <code>&lt;ctime&gt;</code> does not have ms accuracy. I have therefore implemented the following code that has the correct accuracy:</p> <pre><code>double GetSystemTimeSample() { FILETIME ft1, ft2; // assume little endian and that ULONGLONG has same alignment as FILETIME ULONGLONG &amp;t1 = *reinterpret_cast&lt;ULONGLONG*&gt;(&amp;ft1), &amp;t2 = *reinterpret_cast&lt;ULONGLONG*&gt;(&amp;ft2); GetSystemTimeAsFileTime(&amp;ft1); do { GetSystemTimeAsFileTime(&amp;ft2); } while (t1 == t2); return (t2 - t1) / 10000.0; }//GetSystemTimeSample void timeKeeper() { thisTime += GetSystemTimeSample(); cout &lt;&lt; thisTime &lt;&lt; endl; //Run 60 times a second. This limits updates to a constant standard. if(thisTime &gt;= 16.666666666666699825) //Compare to a value in milliseconds { thisTime = seconds; update(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload