<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Getting current graphics mode&#39;s refresh rate</title>
		<link>http://www.allegro.cc/forums/view/613110</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 20 Aug 2013 04:26:00 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>TL;DR: Is there any way to get the current graphics mode&#39;s refresh rate?</p><p>I&#39;m using Allegro 5.1 pulled on August 13, 2013, on a Windows 7 x86_64 laptop.</p><p>I&#39;m currently looking to make a game loop that ideally runs each loop iteration on each monitor frame iteration (probably not correct wording there).</p><p>So in initialization I create the window specifying ALLEGRO_VSYNC then enter the loop, draw graphics then al_flip_display. The problem with this is that it is dependent on the refresh rate of the graphics mode. So if an object moves 1 pixel every frame on a display with a refresh rate of 60Hz, it will move twice as fast on a display with 120Hz. So, I need to know the refresh rate beforehand to accomodate for this in the game logic (distance = velocity / refresh rate).</p><p>al_get_new_display_refresh_rate apparently only returns what was previously set with al_set_new_display_refresh_rate (it should probably be said more explicitly in the docs).<br />al_get_num_display_modes/al_get_display_mode can enumerate all display modes, but does not tell you which is the current.</p><p>So the only option I have left is for the user to manually check the refresh rate of his graphics mode then enter it in the options, which we all know is backwards at its core.</p><p>(As an aside, I realize that graphics configurations can override the application&#39;s request for VSYNC, but I can&#39;t find any justification ever to disable VSYNC. Why the hell would anyone want tearing? It seems natural to align the CPU framerate with the GPU framerate. Nobody would even think of overwriting an audio buffer that&#39;s currently playing in the APU.)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (cinolt)</author>
		<pubDate>Tue, 13 Aug 2013 10:45:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve had a bit of code for many years that simply counts how many screen refreshes occur for, say, 3 seconds.  You could be displaying a splash screen during this wait.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Tue, 13 Aug 2013 10:50:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Arthur:</p><p>TBH I&#39;ve always hated code of that nature. It&#39;s so work-aroundy. Sure it might work well-enough every single time it&#39;s run, but conceptually it&#39;s garbage. I&#39;d prefer the manual configuration over that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (cinolt)</author>
		<pubDate>Tue, 13 Aug 2013 10:55:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Just use <span class="source-code"><a href="http://www.allegro.cc/manual/al_get_time"><span class="a">al_get_time</span></a></span> inside your game loop. So something like:
</p><div class="source-code snippet"><div class="inner"><pre>t <span class="k3">=</span> t2 <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_time"><span class="a">al_get_time</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">while</span> <span class="k2">(</span><span class="n">1</span><span class="k2">)</span> <span class="k2">{</span>
    dt <span class="k3">=</span> t2 <span class="k3">-</span> t<span class="k2">;</span>
    do_logic<span class="k2">(</span>dt<span class="k2">)</span><span class="k2">;</span>
    do_rendering<span class="k2">(</span>dt<span class="k2">)</span><span class="k2">;</span>
    t <span class="k3">=</span> t2<span class="k2">;</span>
    t2 <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_time"><span class="a">al_get_time</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/al_flip_display"><span class="a">al_flip_display</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Now your logic and your rendering will get passed 0.16666 with 60Hz and 0.00833 with 120 Hz and you can use that to adjust for it in whatever way you think is best.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Tue, 13 Aug 2013 14:56:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Elias:</p><p>That&#39;s what I&#39;ve done before, but getting the refresh rate seems much more simpler.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (cinolt)</author>
		<pubDate>Tue, 13 Aug 2013 23:01:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613110/988683#target">cinolt</a> said:</div><div class="quote"><p> I&#39;m currently looking to make a game loop that ideally runs each loop iteration on each monitor frame iteration (probably not correct wording there).</p></div></div><p>This is a bad idea. It over-complicates network programming, precludes replaying demos, invites the possibility of differing gameplay depending on which monitor you use, and doesn&#39;t play well with disabled vsync. Decoupling the game logic from the frame rate while maintaining smooth, tear-free rendering isn&#39;t particularly hard, and should be the preferred method.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> It seems natural to align the CPU framerate with the GPU framerate.</p></div></div><p>No, it is very unnatural. It&#39;s just happens particularly simple to code so people erroneously reach out for it first.</p><p>Anyway, I think you enumerate the display modes (`al_get_display_mode`) and then force one of them via <span class="source-code"><a href="http://www.allegro.cc/manual/al_set_new_display_refresh_rate"><span class="a">al_set_new_display_refresh_rate</span></a></span>. I&#39;m not sure how this works for non-true-fullscreen displays.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Tue, 13 Aug 2013 23:37:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Famous example of why this is a bad idea: In Quake III certain frame rates would make you jump a little longer, because the floating point rounding errors would turn out differently depending on the frame rate.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Wed, 14 Aug 2013 00:00:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613110/988705#target">SiegeLord</a> said:</div><div class="quote"><p>It over-complicates network programming, precludes replaying demos</p></div></div><p>

I have no idea how you can conclude this from just the game loop model.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>invites the possibility of differing gameplay depending on which monitor you use</p></div></div><p>

Actually know about what the thread is about please, I&#39;m asking how to get the current refresh rate <b>specifically to prevent this</b>.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>and doesn&#39;t play well with disabled vsync</p></div></div><p>

I can&#39;t deny this, but it&#39;s going to be a requirement for this model. Not that it&#39;s an unreasonable one.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Anyway, I think you enumerate the display modes (`al_get_display_mode`) and then force one of them via al_set_new_display_refresh_rate. I&#39;m not sure how this works for non-true-fullscreen displays.</p></div></div><p>

So tell me how this works for getting the refresh rate of the current graphics mode configuration?</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613110/988706#target">torhu</a> said:</div><div class="quote"><p>Famous example of why this is a bad idea: In Quake III certain frame rates would make you jump a little longer, because the floating point rounding errors would turn out differently depending on the frame rate.</p></div></div><p>

Floating point errors happen not only because of frame rate -- the problem lies within floating point itself. That&#39;s why I&#39;m using rational numbers instead.</p><p>I&#39;ve hacked Allegro to support what I want, I&#39;ll probably post a feature request on the mailing list for a more complete implementation.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (cinolt)</author>
		<pubDate>Wed, 14 Aug 2013 01:21:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You know it all, but you still ask. Interesting <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Wed, 14 Aug 2013 01:32:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613110/988709#target">cinolt</a> said:</div><div class="quote"><p> I have no idea how you can conclude this from just the game loop model.
</p></div></div><p>Person A and person B have different monitors. Person A records a demo and sends it to person B. A simple implementation of demo replay functionality will produce erroneous replay on B&#39;s computer because the simulation runs at a different rate. Similar issue with the networking.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> Actually know about what the thread is about please, I&#39;m asking how to get the current refresh rate specifically to prevent this.</p></div></div><p>It does very little to prevent it. Consider the following game world: player falling in a gravitational field (acceleration = 100) with initial velocity (1, 0). The initial position is (0, 1). When the player hits the ground (Y = 0), he stops falling... however if he goes past (0.15, 0) he will die, as there is lava there. Examine how the coordinates evolve for two different values of FPS:</p><p>FPS = 50
</p><pre class="terminal scroll">t       vy      x       y
0        0      0       1
0.02    -2      0.02    1
0.04    -4      0.04    0.96
0.06    -6      0.06    0.88
0.08    -8      0.08    0.76
0.1     -10     0.1     0.6
0.12    -12     0.12    0.4
0.14    -14     0.14    0.16
0.16     0      0.16    0
</pre><p>
Result: Death</p><p>FPS = 100
</p><pre class="terminal scroll">t        vy     x       y
0        0      0       1
0.01    -1      0.01    1
0.02    -2      0.02    0.99
0.03    -3      0.03    0.97
0.04    -4      0.04    0.94
0.05    -5      0.05    0.9
0.06    -6      0.06    0.85
0.07    -7      0.07    0.79
0.08    -8      0.08    0.72
0.09    -9      0.09    0.64
0.10    -10     0.1     0.55
0.11    -11     0.11    0.45
0.12    -12     0.12    0.34
0.13    -13     0.13    0.22
0.14    -14     0.14    0.09
0.15    -15     0.15    0
</pre><p>
Result: Barely made it</p><p>That sounds like a gameplay difference to me.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> So tell me how this works for getting the refresh rate of the current graphics mode configuration?</p></div></div><p>For each refresh rate you obtain by enumerating the graphics modes, you try to create a display. Assuming I understand how its supposed to work, it&#39;ll fail if you chose a refresh rate incompatible with what is currently set.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Wed, 14 Aug 2013 04:09:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613110/988724#target">SiegeLord</a> said:</div><div class="quote"><p>Person A and person B have different monitors. Person A records a demo and sends it to person B. A simple implementation of demo replay functionality will produce erroneous replay on B&#39;s computer because the simulation runs at a different rate. Similar issue with the networking.</p></div></div><p>

You&#39;re assuming a certain model that wouldn&#39;t work with different refresh rates. However there are models that can support replays/networking with different refresh rates.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>It does very little to prevent it. Consider the following game world: player falling in a gravitational field (acceleration = 100) with initial velocity (1, 0). The initial position is (0, 1). When the player hits the ground (Y = 0), he stops falling... however if he goes past (0.15, 0) he will die, as there is lava there. Examine how the coordinates evolve for two different values of FPS:</p></div></div><p>

Again, you&#39;re assuming a certain model of collision detection. My collision detection will support different refresh rates. By the way it looks like you got your two examples mixed up there, how does 0.16 -&gt; 0 mean death and 0.09 -&gt; 0 mean not death? 0.09 is past 0.15 and not 0.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (cinolt)</author>
		<pubDate>Wed, 14 Aug 2013 06:12:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613110/988728#target">cinolt</a> said:</div><div class="quote"><p> You&#39;re assuming a certain model that wouldn&#39;t work with different refresh rates. However there are models that can support replays/networking with different refresh rates.</p></div></div><p>Yes, they exist, but they are more complicated than they would have had been they not been dependent on the refresh rate (which is exactly what I said originally).</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> Again, you&#39;re assuming a certain model of collision detection.</p></div></div><p>This has relatively little to do with collision detection, it has to do with how the position is calculated over time. It is impossible in the general case to make this independent of the integration timestep (for some restricted systems it is possible, but again... the restriction is unnecessary if the timestep was independent of the refresh rate).</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> By the way it looks like you got your two examples mixed up there, how does 0.16 -&gt; 0 mean death and 0.09 -&gt; 0 mean not death? 0.09 is past 0.15 and not 0.</p></div></div><p>Those are Y coordinates. Them changing to 0 means that the player has landed on the ground. In the first case, the X axis at the time of landing is 0.16, which means the player is in the lava. In the second case the X axis at the time of landing is 0.15 which means that the player is not in the lava just yet.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Wed, 14 Aug 2013 07:40:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>My bad, I overlooked the example.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613110/988730#target">SiegeLord</a> said:</div><div class="quote"><p>Yes, they exist, but they are more complicated than they would have had been they not been dependent on the refresh rate (which is exactly what I said originally).</p></div></div><p>

pre·clude  (pr-kld)<br />tr.v. pre·clud·ed, pre·clud·ing, pre·cludes<br />1. To make impossible, as by action taken in advance; prevent.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>This has relatively little to do with collision detection, it has to do with how the position is calculated over time. It is impossible in the general case to make this independent of the integration timestep (for some restricted systems it is possible, but again... the restriction is unnecessary if the timestep was independent of the refresh rate).</p></div></div><p>

The reason that your example demonstrates a discrepancy is because position over time is calculated with constant velocity over each interval, but with the velocities being changed at different times (higher FPS means more frequent updates to the velocity).</p><p>Position over time in my engine calculated through &quot;events&quot;, I don&#39;t want to get into it but I don&#39;t think it&#39;s as restricted as you&#39;re making it to be.</p><p>Put it this way, it seems to me that you&#39;re advocating that the frames always involve the same &quot;delta t&quot;, aka framerate. How can you be sure that you&#39;re al_flip_display&#39;ing at anything near the desired framerate? Sure you can al_sleep, but you still have no idea how long it takes for game logic computation etc. Think about CPU&#39;s with differing clock rates, your program will be al_flip_display&#39;ing at different frequencies over different CPU clock rates.</p><p>Tear-free rendering must involve waiting for v-sync anyway.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (cinolt)</author>
		<pubDate>Wed, 14 Aug 2013 08:09:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This has nothing to do with tearing, you only get tearing when disabling vsync (which nobody would ever recommend I hope). What SiegeLord is saying is that there&#39;s no point synchronizing the amount of game state updates with the amount of screen refreshes - because it will cause the gameplay to depend on the used monitor (unless you do something very complex).</p><p>Instead, make it so you always have the exact same gameplay and try to adjust the rendering to account for the different refresh rates. (There&#39;s many different ways to do the latter.)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Wed, 14 Aug 2013 14:30:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613110/988744#target">Elias</a> said:</div><div class="quote"><p>This has nothing to do with tearing, you only get tearing when disabling vsync (which nobody would ever recommend I hope).</p></div></div><p>

When you enable v-sync, at least in Allegro, al_flip_display waits for the next refresh cycle. So the game loop would have to run with a rate that is at most the refresh rate.</p><p><a href="http://alleg.sourceforge.net/a5docs/refman/display.html#al_flip_display">http://alleg.sourceforge.net/a5docs/refman/display.html#al_flip_display</a></p><p>&quot;If ALLEGRO_VSYNC is 1, this function will force waiting for vsync.&quot;</p><p>SiegeLord obviously advocates handling the possibility of disabled v-sync:</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613110/988705#target">SiegeLord</a> said:</div><div class="quote"><p>... and doesn&#39;t play well with disabled vsync.</p></div></div><p>

It was my presupposition that enabling v-sync means that the game loop would be synchronized with the refresh rate. Am I wrong?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (cinolt)</author>
		<pubDate>Wed, 14 Aug 2013 21:09:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613110/988767#target">cinolt</a> said:</div><div class="quote"><p>
It was my presupposition that enabling v-sync means that the game loop would be synchronized with the refresh rate. Am I wrong?
</p></div></div><p>

Depends on how you coded your &quot;game loop.&quot; If you only update your game state between drawing frames, then yes. If your updates are independent of your drawing loop (which is the preferred approach these days) then no.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Sirocco)</author>
		<pubDate>Tue, 20 Aug 2013 04:20:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613110/989010#target">Sirocco</a> said:</div><div class="quote"><p> If you only update your game state between drawing frames, then yes. If your updates are independent of your drawing loop (which is the preferred approach these days) then no.</p></div></div><p>Let&#39;s say you were coding the next Mortal Kombat.  If you update the state between frames, it&#39;d be like taking pictures for a karate tutorial, with a picture taken at each step of a karate move, and the players halt obligingly until the cameraman says it&#39;s OK to proceed to the next step.  If the updates are independent of a loop, it&#39;d be more like taking pictures of a for-real tournament match, and the fighters have no knowledge of any pictures they&#39;re captured in.</p><p>In the second instance in the game, the action would still have to wait, but it&#39;d wait on the timer instead of a video frame, just like the real tournament fighters would have to &quot;wait&quot; on the limits of inertia, momentum and muscle strength which prevents them from being infinitely fast.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Tue, 20 Aug 2013 04:26:00 +0000</pubDate>
	</item>
</rss>
