Merge branch 'master' of git.shadowcat.co.uk:SDL-Site
[sdlgit/SDL-Site.git] / pages / SDL-Time.html-inc
CommitLineData
cf23a5da 1<div class="pod">
2<!-- INDEX START -->
3<h3 id="TOP">Index</h3>
4
5<ul><li><a href="#NAME">NAME</a></li>
980e45b1 6<li><a href="#CATEGORY">CATEGORY</a></li>
cf23a5da 7<li><a href="#SYNOPSIS">SYNOPSIS</a></li>
8<li><a href="#METHODS">METHODS</a>
801213bd 9<ul><li><a href="#add_timer">add_timer</a></li>
c7e8d3c6 10<li><a href="#remove_timer">remove_timer</a></li>
cf23a5da 11</ul>
12</li>
c7e8d3c6 13<li><a href="#AUTHORS">AUTHORS</a>
14</li>
cf23a5da 15</ul><hr />
16<!-- INDEX END -->
17
18<h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
19<div id="NAME_CONTENT">
05c66c33 20<p>SDL::Time - An SDL Perl extension for managing timers</p>
cf23a5da 21
980e45b1 22</div>
23<h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
24<div id="CATEGORY_CONTENT">
25<p>Core</p>
cf23a5da 26
27</div>
28<h1 id="SYNOPSIS">SYNOPSIS</h1><p><a href="#TOP" class="toplink">Top</a></p>
29<div id="SYNOPSIS_CONTENT">
801213bd 30<pre> use warnings;
31 use strict;
32
33 use threads;
05c66c33 34 use threads::shared;
cf23a5da 35
05c66c33 36 use SDL::Time;
cf23a5da 37
05c66c33 38 package foo;
cf23a5da 39
801213bd 40 use SDL ':all';
41
05c66c33 42 SDL::init(SDL_INIT_TIMER);
cf23a5da 43
05c66c33 44 my $tick :shared = 0;
45 sub ticker { $tick++; warn $tick; return 100; }
cf23a5da 46
05c66c33 47 package main;
cf23a5da 48
05c66c33 49 my $id = SDL::Time::add_timer(100, 'foo::ticker');
cf23a5da 50
05c66c33 51 sleep(2);
cf23a5da 52
05c66c33 53 SDL::Time::remove_timer($id);
cf23a5da 54
55</pre>
56
57</div>
58<h1 id="METHODS">METHODS</h1><p><a href="#TOP" class="toplink">Top</a></p>
59<div id="METHODS_CONTENT">
60
cf23a5da 61</div>
801213bd 62<h2 id="add_timer">add_timer</h2>
63<div id="add_timer_CONTENT">
64<pre> my $id = SDL::Timer::add_timer( $ms_interval, $callback );
65
66</pre>
67<p>This runs in a separate thread and a cloned Perl thread.
68<code>threads</code> and <code>threads::shared</code> must be used to share any variables the timer uses.</p>
69<p>The <code>$callback</code> function, specified with a string of the function's name, will be called after the milliseconds of <code>$interval</code> have elapsed.
05c66c33 70The actual delay may be longer than specified depending on the underlying OS.
801213bd 71The callback function is passed the current timer interval as well as the <code>$interval</code> parameter and should return the next timer interval.
05c66c33 72If the return value from the callback is 0, the timer is cancelled; otherwise, the timer will continue to run.</p>
73<p>The timer callback function may run in a different thread to your main program, so it shouldn't call any functions from within itself.
74You may call SDL::push_event, however.</p>
75<p><code>SDL::Time::add_timer</code> returns the identifier value of the generated timer or undef on error.</p>
76<p><strong>Note:</strong> You must initialize (<code>SDL::init</code>) the timer subsystem to use this function.</p>
cf23a5da 77
78</div>
801213bd 79<h2 id="remove_timer">remove_timer</h2>
80<div id="remove_timer_CONTENT">
81<pre> SDL::Timer::remove_timer( $id );
82
83</pre>
84<p>The other way to cancel a timer is to use <code>SDL::Time::remove_timer</code> on the <code>$id</code> of a timer.
85This ID is the return value of the <code>SDL::Time::add_timer</code> function.</p>
05c66c33 86<p><code>SDL::Time::remove_timer</code> returns <code>0</code> on success or <code>-1</code> on error.</p>
cf23a5da 87
88</div>
c7e8d3c6 89<h1 id="AUTHORS">AUTHORS</h1><p><a href="#TOP" class="toplink">Top</a></p>
90<div id="AUTHORS_CONTENT">
1dbe1697 91<p>See <a href="/SDL.html#AUTHORS">/SDL.html#AUTHORS</a>.</p>
c7e8d3c6 92
93</div>
cf23a5da 94</div>