<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: deWiTTERS Game Loop</title>
	<atom:link href="http://www.koonsolo.com/news/dewitters-gameloop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.koonsolo.com/news/dewitters-gameloop/</link>
	<description>Koonsolo helps you create your own games!</description>
	<lastBuildDate>Wed, 04 Jan 2012 15:02:15 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Lindsay</title>
		<link>http://www.koonsolo.com/news/dewitters-gameloop/comment-page-1/#comment-769</link>
		<dc:creator>Lindsay</dc:creator>
		<pubDate>Mon, 18 Jul 2011 06:34:26 +0000</pubDate>
		<guid isPermaLink="false">http://dev.koonsolo.com/?p=7#comment-769</guid>
		<description>I actually don&#039;t have that full understanding regarding this but I really find this very useful..Maybe because I still have to read a lot more to know more as well..</description>
		<content:encoded><![CDATA[<p>I actually don&#8217;t have that full understanding regarding this but I really find this very useful..Maybe because I still have to read a lot more to know more as well..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AlbeyAmakiir</title>
		<link>http://www.koonsolo.com/news/dewitters-gameloop/comment-page-1/#comment-758</link>
		<dc:creator>AlbeyAmakiir</dc:creator>
		<pubDate>Tue, 24 May 2011 03:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://dev.koonsolo.com/?p=7#comment-758</guid>
		<description>I think I&#039;ve found an instance in which &quot;Game Speed dependent on Variable FPS&quot; is not so bad. If the game stores all movement in terms of vectors (for example: object a starts here at this time and will end here at this time using this path), then you just check the time every game update and move the object to where is should be, *regardless of where it is now*. This does not work in any game that has collisions at all, and games like that are few and far between, but I&#039;m making such a game, and the negatives you describe do not apply for it.
That said, I do wonder if there are any other hidden negatives that anyone can think of.</description>
		<content:encoded><![CDATA[<p>I think I&#8217;ve found an instance in which &#8220;Game Speed dependent on Variable FPS&#8221; is not so bad. If the game stores all movement in terms of vectors (for example: object a starts here at this time and will end here at this time using this path), then you just check the time every game update and move the object to where is should be, *regardless of where it is now*. This does not work in any game that has collisions at all, and games like that are few and far between, but I&#8217;m making such a game, and the negatives you describe do not apply for it.<br />
That said, I do wonder if there are any other hidden negatives that anyone can think of.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TheEnlightenedOne</title>
		<link>http://www.koonsolo.com/news/dewitters-gameloop/comment-page-1/#comment-756</link>
		<dc:creator>TheEnlightenedOne</dc:creator>
		<pubDate>Tue, 17 May 2011 14:36:45 +0000</pubDate>
		<guid isPermaLink="false">http://dev.koonsolo.com/?p=7#comment-756</guid>
		<description>Can someone explain the final example to me? LoL. The one where he&#039;s using implementation. I don&#039;t 100% understand what&#039;s going on with the ticks.</description>
		<content:encoded><![CDATA[<p>Can someone explain the final example to me? LoL. The one where he&#8217;s using implementation. I don&#8217;t 100% understand what&#8217;s going on with the ticks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil</title>
		<link>http://www.koonsolo.com/news/dewitters-gameloop/comment-page-1/#comment-743</link>
		<dc:creator>Phil</dc:creator>
		<pubDate>Wed, 30 Mar 2011 21:17:26 +0000</pubDate>
		<guid isPermaLink="false">http://dev.koonsolo.com/?p=7#comment-743</guid>
		<description>const int TICKS_PER_SECOND = 25;
    const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
    const int MAX_FRAMESKIP = 5;

    DWORD next_game_tick = GetTickCount();
    int loops;
    float interpolation;

    bool game_is_running = true;
    while( game_is_running ) {

        loops = 0;
        while( GetTickCount() &gt; next_game_tick &amp;&amp; loops &lt; MAX_FRAMESKIP) {
            update_game();

            next_game_tick += SKIP_TICKS;
            loops++;
        }

        interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick )
                        / float( SKIP_TICKS );
        display_game( interpolation );
    }


Nice!

Its going to take me all week to get my head around this properly!</description>
		<content:encoded><![CDATA[<p>const int TICKS_PER_SECOND = 25;<br />
    const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;<br />
    const int MAX_FRAMESKIP = 5;</p>
<p>    DWORD next_game_tick = GetTickCount();<br />
    int loops;<br />
    float interpolation;</p>
<p>    bool game_is_running = true;<br />
    while( game_is_running ) {</p>
<p>        loops = 0;<br />
        while( GetTickCount() &gt; next_game_tick &amp;&amp; loops &lt; MAX_FRAMESKIP) {<br />
            update_game();</p>
<p>            next_game_tick += SKIP_TICKS;<br />
            loops++;<br />
        }</p>
<p>        interpolation = float( GetTickCount() + SKIP_TICKS &#8211; next_game_tick )<br />
                        / float( SKIP_TICKS );<br />
        display_game( interpolation );<br />
    }</p>
<p>Nice!</p>
<p>Its going to take me all week to get my head around this properly!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jawad Amjad</title>
		<link>http://www.koonsolo.com/news/dewitters-gameloop/comment-page-1/#comment-742</link>
		<dc:creator>Jawad Amjad</dc:creator>
		<pubDate>Wed, 30 Mar 2011 05:03:21 +0000</pubDate>
		<guid isPermaLink="false">http://dev.koonsolo.com/?p=7#comment-742</guid>
		<description>Well i implemented dynamic fps this way.

 while (_run)
        {
        	c = null;
        	next_tick=System.currentTimeMillis(); //fpss
        	
            try {
                c = _surfaceHolder.lockCanvas(null);
                
                synchronized (_surfaceHolder)
                {
                    _panel.updatePhysics(sleep_time,fps);
                    _panel.onDraw(c);
                    	
                        
                    	if(fps_overall_flg)
                    	{
	                        skip_ticks = 1000/fps;             //fpss
	                    	next_tick += skip_ticks;
						   	sleep_time = (int) (next_tick - System.currentTimeMillis());
	             		
						   	if(sleep_time&gt;0 &#124;&#124; sleep_time  0 ) 
						   	{
	
						   		sleep( sleep_time );
						   		if(sleep_time&gt;0 &amp;&amp; fps_chg == true)
						   		fps+=1;
						    }
	
						    else if(sleep_time  0 &amp;&amp; sleep_time &lt;10)    
						    { fps_chg = false; fps_overall_flg = false;}
						
                    	Log.d(&quot;HEEELEOELEOE&quot;,&quot; &quot;+fps_overall_flg);
                     }	
                    	
                    else 
                    {sleep(sleep_time);Log.d(&quot;is sleepin for&quot;,&quot; &quot;+sleep_time);} 
					   	
                }


and i think its working fine....if any one find any problem in this code do tell me. Ill be thankful and will appreciate your corrections.</description>
		<content:encoded><![CDATA[<p>Well i implemented dynamic fps this way.</p>
<p> while (_run)<br />
        {<br />
        	c = null;<br />
        	next_tick=System.currentTimeMillis(); //fpss</p>
<p>            try {<br />
                c = _surfaceHolder.lockCanvas(null);</p>
<p>                synchronized (_surfaceHolder)<br />
                {<br />
                    _panel.updatePhysics(sleep_time,fps);<br />
                    _panel.onDraw(c);</p>
<p>                    	if(fps_overall_flg)<br />
                    	{<br />
	                        skip_ticks = 1000/fps;             //fpss<br />
	                    	next_tick += skip_ticks;<br />
						   	sleep_time = (int) (next_tick &#8211; System.currentTimeMillis());</p>
<p>						   	if(sleep_time&gt;0 || sleep_time  0 )<br />
						   	{</p>
<p>						   		sleep( sleep_time );<br />
						   		if(sleep_time&gt;0 &amp;&amp; fps_chg == true)<br />
						   		fps+=1;<br />
						    }</p>
<p>						    else if(sleep_time  0 &amp;&amp; sleep_time &lt;10)<br />
						    { fps_chg = false; fps_overall_flg = false;}</p>
<p>                    	Log.d(&quot;HEEELEOELEOE&quot;,&quot; &quot;+fps_overall_flg);<br />
                     }	</p>
<p>                    else<br />
                    {sleep(sleep_time);Log.d(&quot;is sleepin for&quot;,&quot; &quot;+sleep_time);} </p>
<p>                }</p>
<p>and i think its working fine&#8230;.if any one find any problem in this code do tell me. Ill be thankful and will appreciate your corrections.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ChrisH</title>
		<link>http://www.koonsolo.com/news/dewitters-gameloop/comment-page-1/#comment-741</link>
		<dc:creator>ChrisH</dc:creator>
		<pubDate>Wed, 16 Mar 2011 09:52:21 +0000</pubDate>
		<guid isPermaLink="false">http://dev.koonsolo.com/?p=7#comment-741</guid>
		<description>&lt;a href=&quot;#comment-738&quot; rel=&quot;nofollow&quot;&gt;@Joe &lt;/a&gt; 

You&#039;re right in that when GetTickCount() rolls over to 0, the while loop will evaluate to false and that iteration will be skipped. This is correct behavior, though, because the difference in time between GetTickCount() and nextGameTick at that particular moment will always be less than SKIP_TICKS and so the logic loop shouldn&#039;t run. In other words, at that particular moment, not enough time has passed to need a logic update.

Where you&#039;re a little off is thinking it&#039;ll still take another ~49 days to run. By casting the result of (GetTickCount() - nextGameTick) to an unsigned int, you&#039;re taking advantage of the wrap around behavior caused by integer overflow, ensuring that the result will always be a positive number. Initially (when GetTickCount rolls over to 0 and nextGameTick is stuck at, say, 4,294,967,295) the result of (unsigned int)(GetTickCount() - nextGameTick) will be 0. But as GetTickCount() is incremented, this result grows from 0 to 1, 2, 3 and so on until it will eventually (in a matter of milliseconds) surpass SKIP_TICKS, at which point the logic loop will run. 

It&#039;s tricky but the easiest way of understanding it is probably to see it in action. The simplest way is to code a little example for yourself. In C++ (or whatever language you&#039;re comfortable with that has unsigned ints), set a uint variable to 4,294,967,295. Then increment this variable by some random amount (say, 10) and print the result. And for a more relevant example, set two uints (one called CurrentTickCount and one called NextGameTick) to 5 and 4,294,967,295. Then print the result of (unsigned int)(CurrentTickCount - NextGameTick). 

You might be surprised. :]</description>
		<content:encoded><![CDATA[<p><a href="#comment-738" rel="nofollow">@Joe </a> </p>
<p>You&#8217;re right in that when GetTickCount() rolls over to 0, the while loop will evaluate to false and that iteration will be skipped. This is correct behavior, though, because the difference in time between GetTickCount() and nextGameTick at that particular moment will always be less than SKIP_TICKS and so the logic loop shouldn&#8217;t run. In other words, at that particular moment, not enough time has passed to need a logic update.</p>
<p>Where you&#8217;re a little off is thinking it&#8217;ll still take another ~49 days to run. By casting the result of (GetTickCount() &#8211; nextGameTick) to an unsigned int, you&#8217;re taking advantage of the wrap around behavior caused by integer overflow, ensuring that the result will always be a positive number. Initially (when GetTickCount rolls over to 0 and nextGameTick is stuck at, say, 4,294,967,295) the result of (unsigned int)(GetTickCount() &#8211; nextGameTick) will be 0. But as GetTickCount() is incremented, this result grows from 0 to 1, 2, 3 and so on until it will eventually (in a matter of milliseconds) surpass SKIP_TICKS, at which point the logic loop will run. </p>
<p>It&#8217;s tricky but the easiest way of understanding it is probably to see it in action. The simplest way is to code a little example for yourself. In C++ (or whatever language you&#8217;re comfortable with that has unsigned ints), set a uint variable to 4,294,967,295. Then increment this variable by some random amount (say, 10) and print the result. And for a more relevant example, set two uints (one called CurrentTickCount and one called NextGameTick) to 5 and 4,294,967,295. Then print the result of (unsigned int)(CurrentTickCount &#8211; NextGameTick). </p>
<p>You might be surprised. :]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gorlan</title>
		<link>http://www.koonsolo.com/news/dewitters-gameloop/comment-page-1/#comment-740</link>
		<dc:creator>Gorlan</dc:creator>
		<pubDate>Sat, 05 Mar 2011 19:09:59 +0000</pubDate>
		<guid isPermaLink="false">http://dev.koonsolo.com/?p=7#comment-740</guid>
		<description>Phew at last! I found your article really helpful, this is the kind of thing I just couldn&#039;t figure out myself (or didn&#039;t want anyway, it&#039;s almost always better to rely on  more experienced than oneself, isn&#039;t it? :D). I implemented the last method but with the little correction in the while.

while ( (unsigned long) (GetTickCount() - nextGameTick) &gt; SKIP_TICKS &amp;&amp; numloops &lt; MAX_FRAME_SKIP )

but I first put 0 instead of SKIP_TICKS which gave me some headaches and some white hairs... so stupid am I...

I just wanted to add a little thing for some beginners too:

Suppose you wanted to rotate something at 90°/sec. You know that your game is refreshing the position of an object at X Hz or X times per second (25 here) so you need to recalculate the game logic speed. Here it is:

float real_speed = 90.0; //
float speed = real_speed / TICKS_PER_SECOND;

in you game logic:
angle = angle * speed;

in the drawing:
viewed_angle = angle + speed * interpolation;

It may seem trivial but this is just what I said earlier about letting others doing things for you! :D</description>
		<content:encoded><![CDATA[<p>Phew at last! I found your article really helpful, this is the kind of thing I just couldn&#8217;t figure out myself (or didn&#8217;t want anyway, it&#8217;s almost always better to rely on  more experienced than oneself, isn&#8217;t it? <img src='http://www.koonsolo.com/news/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ). I implemented the last method but with the little correction in the while.</p>
<p>while ( (unsigned long) (GetTickCount() &#8211; nextGameTick) &gt; SKIP_TICKS &amp;&amp; numloops &lt; MAX_FRAME_SKIP )</p>
<p>but I first put 0 instead of SKIP_TICKS which gave me some headaches and some white hairs&#8230; so stupid am I&#8230;</p>
<p>I just wanted to add a little thing for some beginners too:</p>
<p>Suppose you wanted to rotate something at 90°/sec. You know that your game is refreshing the position of an object at X Hz or X times per second (25 here) so you need to recalculate the game logic speed. Here it is:</p>
<p>float real_speed = 90.0; //<br />
float speed = real_speed / TICKS_PER_SECOND;</p>
<p>in you game logic:<br />
angle = angle * speed;</p>
<p>in the drawing:<br />
viewed_angle = angle + speed * interpolation;</p>
<p>It may seem trivial but this is just what I said earlier about letting others doing things for you! <img src='http://www.koonsolo.com/news/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marku S</title>
		<link>http://www.koonsolo.com/news/dewitters-gameloop/comment-page-1/#comment-739</link>
		<dc:creator>Marku S</dc:creator>
		<pubDate>Fri, 25 Feb 2011 12:07:04 +0000</pubDate>
		<guid isPermaLink="false">http://dev.koonsolo.com/?p=7#comment-739</guid>
		<description>I just implemented the &quot;Constant Game Speed independent of Variable FPS&quot; in Java and it works great! But when I run my game, I get a CPU-load of nearly 100% and frame rates of up to 3500 fps. How can I use your method but limit the frame rate to e.g. the mean fps of 10 seconds?
Apart from that: great article! Thanks :-)</description>
		<content:encoded><![CDATA[<p>I just implemented the &#8220;Constant Game Speed independent of Variable FPS&#8221; in Java and it works great! But when I run my game, I get a CPU-load of nearly 100% and frame rates of up to 3500 fps. How can I use your method but limit the frame rate to e.g. the mean fps of 10 seconds?<br />
Apart from that: great article! Thanks <img src='http://www.koonsolo.com/news/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe</title>
		<link>http://www.koonsolo.com/news/dewitters-gameloop/comment-page-1/#comment-738</link>
		<dc:creator>Joe</dc:creator>
		<pubDate>Tue, 15 Feb 2011 22:53:39 +0000</pubDate>
		<guid isPermaLink="false">http://dev.koonsolo.com/?p=7#comment-738</guid>
		<description>&lt;a href=&quot;#comment-606&quot; rel=&quot;nofollow&quot;&gt;@ChrisH &lt;/a&gt; 
In your fix, when GetTickCount() rolls over,

while(unsigned int(0 – 4294967295) &gt; 40 …)

will be while( 0 &gt; 40 ),
and it also will never go into the loop to increase next_game_tick.
We still have to wait for 49 days. 


The “0 – 4294967295″ would return -4294967295 except it’s then casted to an unsigned int (which can’t represent negative numbers) so instead it returns: 0.</description>
		<content:encoded><![CDATA[<p><a href="#comment-606" rel="nofollow">@ChrisH </a><br />
In your fix, when GetTickCount() rolls over,</p>
<p>while(unsigned int(0 – 4294967295) &gt; 40 …)</p>
<p>will be while( 0 &gt; 40 ),<br />
and it also will never go into the loop to increase next_game_tick.<br />
We still have to wait for 49 days. </p>
<p>The “0 – 4294967295″ would return -4294967295 except it’s then casted to an unsigned int (which can’t represent negative numbers) so instead it returns: 0.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marc Tache</title>
		<link>http://www.koonsolo.com/news/dewitters-gameloop/comment-page-1/#comment-731</link>
		<dc:creator>Marc Tache</dc:creator>
		<pubDate>Tue, 18 Jan 2011 12:20:29 +0000</pubDate>
		<guid isPermaLink="false">http://dev.koonsolo.com/?p=7#comment-731</guid>
		<description>So I assume that display_game() is an asynchronous function running in a separate thread? Otherwise the number of iterations per second in the loop can never be greater than the FPS (in the last example).

is update_game() also an ansynchronous function?</description>
		<content:encoded><![CDATA[<p>So I assume that display_game() is an asynchronous function running in a separate thread? Otherwise the number of iterations per second in the loop can never be greater than the FPS (in the last example).</p>
<p>is update_game() also an ansynchronous function?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

