<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GuShH&#039;s DevBlog &#187; timing</title>
	<atom:link href="http://gushh.net/blog/?tag=timing&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://gushh.net/blog</link>
	<description>This blog is about software, electronics engineering and game development.</description>
	<lastBuildDate>Tue, 24 Jan 2012 00:31:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>TimeLapse lib</title>
		<link>http://gushh.net/blog/timelapse-lib/</link>
		<comments>http://gushh.net/blog/timelapse-lib/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 18:48:02 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[callback]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[gettickcount]]></category>
		<category><![CDATA[lib]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[purebasic]]></category>
		<category><![CDATA[time lapse]]></category>
		<category><![CDATA[timelapse]]></category>
		<category><![CDATA[timer]]></category>
		<category><![CDATA[timing]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=171</guid>
		<description><![CDATA[This little lib is used to keep track of time using the low resolution timer provided by the OS (in Windows &#8220;GetTickCount&#8221;). The goal of this type of library is to encapsulate timing actions. It was originally designed for game development in mind but needless to say you can use it and adapt it to [...]]]></description>
			<content:encoded><![CDATA[<p>This little lib is used to keep track of time using the low resolution timer provided by the OS (in Windows &#8220;GetTickCount&#8221;). The goal of this type of library is to encapsulate timing actions. It was originally designed for game development in mind but needless to say you can use it and adapt it to your own needs.</p>
<p>Basically you define a time interval as you create the &#8220;timelapse&#8221; instance. Every time you call the update function a simple check determines whether the time was reached or not. This is trivial, but encapsulating it makes sense. Specially if you plan to create an animation system in the future!</p>
<p><span id="more-171"></span><br />
The code:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">EnableExplicit</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Prototype</span>.i TimeLapse_Callback<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.i <span style="color: #000066;">&#41;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Structure</span> TIMELAPSE
	old_time.i			<span style="color: #ff0000; font-style: italic;">; Stores the &quot;old&quot; time value.</span>
	new_time.i			<span style="color: #ff0000; font-style: italic;">; Stores the &quot;new&quot; time value.</span>
	time_out.i			<span style="color: #ff0000; font-style: italic;">; Time period for the TimeLapse , in ms.</span>
	<span style="color: #000066;">*</span>callback.TimeLapse_Callback	<span style="color: #ff0000; font-style: italic;">; User callback.</span>
	userdata.i			<span style="color: #ff0000; font-style: italic;">; User data.</span>
<span style="color: #000066; font-weight: bold;">EndStructure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">CompilerSelect</span> #PB_Compiler_OS
	<span style="color: #000066; font-weight: bold;">CompilerCase</span> #PB_OS_Windows
		<span style="color: #000066; font-weight: bold;">Macro</span> _time<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> : timeGetTime_<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>		: <span style="color: #000066; font-weight: bold;">EndMacro</span>
	<span style="color: #000066; font-weight: bold;">CompilerDefault</span>
		<span style="color: #000066; font-weight: bold;">Macro</span> _time<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> : <span style="color: #0000ff;">ElapsedMilliseconds</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>	: <span style="color: #000066; font-weight: bold;">EndMacro</span>
<span style="color: #000066; font-weight: bold;">CompilerEndSelect</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i TimeLapseCreate<span style="color: #000066;">&#40;</span> Timeout.i, <span style="color: #000066;">*</span>Callback.TimeLapse_Callback <span style="color: #000066;">=</span> #Null, UserData.i <span style="color: #000066;">=</span> #Null <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Define</span>.TimeLapse  <span style="color: #000066;">*</span>this <span style="color: #000066;">=</span> <span style="color: #0000ff;">AllocateMemory</span><span style="color: #000066;">&#40;</span> <span style="color: #0000ff;">SizeOf</span><span style="color: #000066;">&#40;</span>TimeLapse <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">With</span> <span style="color: #000066;">*</span>this
			<span style="color: #000066;">\</span>new_time 	<span style="color: #000066;">=</span> _time<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
			<span style="color: #000066;">\</span>old_time 	<span style="color: #000066;">=</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>new_time
			<span style="color: #000066;">\</span>time_out	<span style="color: #000066;">=</span> Timeout
			<span style="color: #000066;">\</span>callback	<span style="color: #000066;">=</span> <span style="color: #000066;">*</span>Callback
			<span style="color: #000066;">\</span>userdata	<span style="color: #000066;">=</span> UserData
		<span style="color: #000066; font-weight: bold;">EndWith</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i TimeLapseDestroy<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.TIMELAPSE  <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #0000ff;">FreeMemory</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>this<span style="color: #000066;">&#41;</span>
		<span style="color: #000066;">*</span>this <span style="color: #000066;">=</span> #Null
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i TimeLapseUpdate<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.TIMELAPSE  <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>new_time <span style="color: #000066;">=</span> _time<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">&#40;</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>new_time <span style="color: #000066;">-</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>old_time<span style="color: #000066;">&#41;</span> <span style="color: #000066;">=&gt;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>time_out<span style="color: #000066;">&#41;</span>
&nbsp;
			<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>old_time <span style="color: #000066;">=</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>new_time
&nbsp;
			<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>callback
				<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>callback<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this <span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> #True
&nbsp;
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
&nbsp;
<span style="color: #ff0000; font-style: italic;">; Helper routines</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i TimeLapseSet<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.TIMELAPSE , Timeout.i <span style="color: #000066;">&#41;</span>
&nbsp;
 	<span style="color: #000066; font-weight: bold;">If</span> Timeout <span style="color: #000066;">&gt;</span> <span style="color: #CC0000;">0</span>
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>time_out <span style="color: #000066;">=</span> Timeout
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i TimeLapseGet<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.TIMELAPSE  <span style="color: #000066;">&#41;</span>
&nbsp;
 	<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>time_out
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>Example:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">OpenConsole</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Define</span>.TIMELAPSE  <span style="color: #000066;">*</span>test_timer  <span style="color: #000066;">=</span> TimeLapseCreate<span style="color: #000066;">&#40;</span> <span style="color: #CC0000;">500</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Repeat</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> TimeLapseUpdate<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>test_timer  <span style="color: #000066;">&#41;</span>
			<span style="color: #0000ff;">PrintN</span><span style="color: #000066;">&#40;</span><span style="color: #009900;">&quot; Timer says hi!&quot;</span> <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #0000ff;">Delay</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Until</span> <span style="color: #0000ff;">Inkey</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">=</span> <span style="color: #0000ff;">Chr</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">27</span><span style="color: #000066;">&#41;</span>
	<span style="color: #0000ff;">CloseConsole</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">EndIf</span>
<span style="color: #000066; font-weight: bold;">End</span></pre></div></div>

<p>The nice thing about this library is the ability to use callbacks. Whenever a callback is defined, once the timer reaches the maximum allowed time-lapse, it&#8217;ll return by executing the callback.</p>
<p>In the callback you can do anything you want; From simple event triggering to manipulating another timer depending on XYZ conditions: it&#8217;s up to you!.</p>
<p><strong>Just keep in mind:</strong> Always return &#8220;true&#8221; unless you want to invalidate the update call.</p>
<p>Here is a more advanced example with a callback, user data and multiple timers:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Procedure</span>.i MyCallback<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.TIMELAPSE  <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #ff0000; font-style: italic;">; It's imperative that you return #true (1) in your callbacks if you want the &quot;Update&quot; calls to pass once the timeout is reached.</span>
	<span style="color: #ff0000; font-style: italic;">; This is a design choice for flexibility and nothing else.</span>
&nbsp;
	<span style="color: #0000ff;">ConsoleColor</span><span style="color: #000066;">&#40;</span><span style="color: #0000ff;">Random</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">15</span><span style="color: #000066;">&#41;</span>, <span style="color: #0000ff;">Random</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">15</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span>
	<span style="color: #0000ff;">PrintN</span><span style="color: #000066;">&#40;</span><span style="color: #009900;">&quot; Hi from the callback! &quot;</span><span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #0000ff;">PrintN</span><span style="color: #000066;">&#40;</span> <span style="color: #009900;">&quot; The other timer is set to: &quot;</span> <span style="color: #000066;">+</span> <span style="color: #0000ff;">Str</span><span style="color: #000066;">&#40;</span> TimeLapseGet<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>userdata <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span> <span style="color: #000066;">+</span> <span style="color: #009900;">&quot;ms&quot;</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #0000ff;">PrintN</span><span style="color: #000066;">&#40;</span> <span style="color: #009900;">&quot; ...Reducing the time interval down by 50 ms... &quot;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	TimeLapseSet<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>userdata, TimeLapseGet<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>userdata <span style="color: #000066;">&#41;</span> <span style="color: #000066;">-</span> <span style="color: #CC0000;">50</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #0000ff;">ConsoleColor</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">15</span>, <span style="color: #CC0000;">0</span><span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> #True
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Define</span>.TIMELAPSE  <span style="color: #000066;">*</span>timer_one 	<span style="color: #000066;">=</span> TimeLapseCreate<span style="color: #000066;">&#40;</span> <span style="color: #CC0000;">500</span> <span style="color: #000066;">&#41;</span>				<span style="color: #ff0000; font-style: italic;">; The first TimeLapse , nothing special.</span>
<span style="color: #000066; font-weight: bold;">Define</span>.TIMELAPSE  <span style="color: #000066;">*</span>timer_two 	<span style="color: #000066;">=</span> TimeLapseCreate<span style="color: #000066;">&#40;</span> <span style="color: #CC0000;">2000</span>, @MyCallback<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>, <span style="color: #000066;">*</span>timer_one <span style="color: #000066;">&#41;</span>	<span style="color: #ff0000; font-style: italic;">; The second TimeLapse , it manipulates the first one through a callback.</span>
<span style="color: #000066; font-weight: bold;">Define</span>.TIMELAPSE  <span style="color: #000066;">*</span>timer_three	<span style="color: #000066;">=</span> TimeLapseCreate<span style="color: #000066;">&#40;</span> <span style="color: #CC0000;">20</span><span style="color: #000066;">*</span><span style="color: #CC0000;">1000</span> <span style="color: #000066;">&#41;</span>				<span style="color: #ff0000; font-style: italic;">; This TimeLapse  will simply let us exit the example after the set time.</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">OpenConsole</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #0000ff;">EnableGraphicalConsole</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #000066;">&#41;</span>
	<span style="color: #0000ff;">ConsoleColor</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">15</span>, <span style="color: #CC0000;">0</span><span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Define</span>.i quit <span style="color: #000066;">=</span> #False
&nbsp;
	<span style="color: #000066; font-weight: bold;">Repeat</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> TimeLapseUpdate<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>timer_three <span style="color: #000066;">&#41;</span>
			quit <span style="color: #000066;">=</span> #True
		<span style="color: #000066; font-weight: bold;">Else</span>
			<span style="color: #000066; font-weight: bold;">If</span> TimeLapseUpdate<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>timer_one <span style="color: #000066;">&#41;</span>
				<span style="color: #0000ff;">PrintN</span><span style="color: #000066;">&#40;</span><span style="color: #009900;">&quot;timer_one: &quot;</span> <span style="color: #000066;">+</span> <span style="color: #0000ff;">Str</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>timer_one<span style="color: #000066;">\</span>time_out<span style="color: #000066;">&#41;</span> <span style="color: #000066;">+</span> <span style="color: #009900;">&quot;ms reached.&quot;</span><span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">EndIf</span>
			<span style="color: #000066; font-weight: bold;">If</span> TimeLapseUpdate<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>timer_two <span style="color: #000066;">&#41;</span>
				<span style="color: #0000ff;">PrintN</span><span style="color: #000066;">&#40;</span><span style="color: #009900;">&quot;timer_two: &quot;</span> <span style="color: #000066;">+</span> <span style="color: #0000ff;">Str</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>timer_two<span style="color: #000066;">\</span>time_out<span style="color: #000066;">&#41;</span> <span style="color: #000066;">+</span> <span style="color: #009900;">&quot;ms reached.&quot;</span><span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">EndIf</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #0000ff;">Delay</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Until</span> <span style="color: #0000ff;">Inkey</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">=</span> <span style="color: #0000ff;">Chr</span><span style="color: #000066;">&#40;</span><span style="color: #CC0000;">27</span><span style="color: #000066;">&#41;</span> <span style="color: #000066; font-weight: bold;">Or</span> quit 
&nbsp;
	<span style="color: #0000ff;">PrintN</span><span style="color: #000066;">&#40;</span> <span style="color: #009900;">&quot;timer_three: The example finished.&quot;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	TimeLapseDestroy<span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>timer_one<span style="color: #000066;">&#41;</span>
	TimeLapseDestroy<span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>timer_two<span style="color: #000066;">&#41;</span>
	TimeLapseDestroy<span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>timer_three<span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #0000ff;">Input</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
	<span style="color: #0000ff;">CloseConsole</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndIf</span>
<span style="color: #000066; font-weight: bold;">End</span></pre></div></div>

<p>The examples are crude, but they show every function in the library.<br />
Should you have any questions, feel free to ask.</p>
<p>I&#8217;ll soon post the &#8220;Animation&#8221; lib that depends on &#8220;TimeLapse&#8221;.</p>
<p>Cheers.</p>
<g:plusone href='http://gushh.net/blog/timelapse-lib/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/timelapse-lib/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: gushh.net @ 2012-02-09 07:01:15 by W3 Total Cache -->
