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