blib/lib/SDL2/timer.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 46 CC 1 SCAR 6.9
CC
1
Cov
0%
END
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package SDL2::timer 0.01 {
2 use SDL2::Utils;
3 use experimental 'signatures';
4 #
5 use SDL2::stdinc;
6 use SDL2::error;
7 #
8 ffi->type( '(uint32,opaque)->uint32' => 'SDL_TimerCallback' );
9 ffi->type( 'int' => 'SDL_TimerID' );
10 my %_timers;
11 END { %_timers = () }
12 attach timer => {
13 SDL_GetTicks => [ [], 'uint32' ],
14 SDL_GetPerformanceCounter => [ [], 'uint64' ],
15 SDL_GetPerformanceFrequency => [ [], 'uint64' ],
16 SDL_Delay => [ ['uint32'] ],
17 Bundle_SDL_AddTimer => [
18 [ 'uint32', 'SDL_TimerCallback', 'opaque' ],
19 'SDL_TimerID',
20 sub ( $inner, $delay, $code, $params = () ) {
21 my $cb = ffi->closure(
22 sub {
23 my ( $delay, $etc ) = @_;
24 my $retval = $code->( $delay, $params );
25 $retval;
26 }
27 );
28 my $id = $inner->( $delay, $cb, undef );
29 $_timers{$id} = $cb; # Store reference
30 return $id;
31 }
32 ],
33 Bundle_SDL_RemoveTimer => [
34 ['SDL_TimerID'] => 'SDL_bool' => sub ( $inner, $id ) {
35 my $retval = $inner->($id);
36 delete $_timers{$id};
37 return $retval;
38 }
39 ]
40 };
41 define timer => [ [ SDL_TICKS_PASSED => sub ( $A, $B ) { ( $B - $A ) <= 0 } ] ];
42  
43 =encoding utf-8
44  
45 =head1 NAME
46  
47 SDL2::timer - SDL Time Management Routines
48  
49 =head1 SYNOPSIS
50  
51 use SDL2 qw[:timer];
52  
53 =head1 DESCRIPTION
54  
55 SDL2::timer contains functions for dealing with time.
56  
57 =head1 Functions
58  
59 These may be imported by name or with the C<:timer> tag.
60  
61 =head2 C<SDL_GetTicks( )>
62  
63 Get the number of milliseconds since SDL library initialization.
64  
65 my $time = SDL_GetTicks( );
66  
67 This value wraps if the program runs for more than C<~49> days.
68  
69 Returns an unsigned 32-bit value representing the number of milliseconds since
70 the SDL library initialized.
71  
72 =head2 C<SDL_TICKS_PASSED( ... )>
73  
74 Compare SDL ticks values, and return true if C<lhs> has passed C<rhs>.
75  
76 For example, if you want to wait 100 ms, you could do this:
77  
78 my $timeout = SDL_GetTicks() + 100;
79 while ( !SDL_TICKS_PASSED( SDL_GetTicks(), $timeout ) ) {
80  
81 # ... do work until timeout has elapsed
82 }
83  
84 Expected parameters include:
85  
86 =over
87  
88 =item C<lhs> - tick value to compare
89  
90 =item C<rhs> - tick value to compare
91  
92 =back
93  
94 =head2 C<SDL_GetPerformanceCounter( )>
95  
96 Get the current value of the high resolution counter.
97  
98 my $high_timer = SDL_GetPerformanceCounter( );
99  
100 This function is typically used for profiling.
101  
102 The counter values are only meaningful relative to each other. Differences
103 between values can be converted to times by using L<<
104 C<SDL_GetPerformanceFrequency( )>|/C<SDL_GetPerformanceFrequency( )> >>.
105  
106 Returns the current counter value.
107  
108 =head2 C<SDL_GetPerformanceFrequency( )>
109  
110 Get the count per second of the high resolution counter.
111  
112 my $hz = SDL_GetPerformanceFrequency( );
113  
114 Returns a platform-specific count per second.
115  
116 =head2 C<SDL_Delay( ... )>
117  
118 Wait a specified number of milliseconds before returning.
119  
120 SDL_Delay( 1000 );
121  
122 This function waits a specified number of milliseconds before returning. It
123 waits at least the specified time, but possibly longer due to OS scheduling.
124  
125 Expected parameters include:
126  
127 =over
128  
129 =item C<ms> - the number of milliseconds to delay
130  
131 =back
132  
133 =head2 C<SDL_AddTimer( ... )>
134  
135 Call a callback function at a future time.
136  
137 my $id = SDL_AddTimer( 1000, sub ( $interval, $data ) { warn 'ping!'; $interval; } );
138  
139 If you use this function, you must pass C<SDL_INIT_TIMER> to C<SDL_Init( ...
140 )>.
141  
142 The callback function is passed the current timer interval and returns the next
143 timer interval. If the returned value is the same as the one passed in, the
144 periodic alarm continues, otherwise a new alarm is scheduled. If the callback
145 returns C<0>, the periodic alarm is cancelled.
146  
147 The callback is run on a separate thread.
148  
149 Timers take into account the amount of time it took to execute the callback.
150 For example, if the callback took 250 ms to execute and returned 1000 (ms), the
151 timer would only wait another 750 ms before its next iteration.
152  
153 Timing may be inexact due to OS scheduling. Be sure to note the current time
154 with L<< C<SDL_GetTicks( )>|/C<SDL_GetTicks( )> >> or L<<
155 C<SDL_GetPerformanceCounter( )>|/C<SDL_GetPerformanceCounter( )> >> in case
156 your callback needs to adjust for variances.
157  
158 Expected parameters include:
159  
160 =over
161  
162 =item C<interval> - the timer delay, in milliseconds, passed to C<callback>
163  
164 =item C<callback> - the C<CODE> reference to call when the specified C<interval> elapses
165  
166 =item C<param> - a pointer that is passed to C<callback>
167  
168 =back
169  
170 Returns a timer ID or C<0> if an error occurs; call C<SDL_GetError( )> for more
171 information.
172  
173 =head2 C<SDL_RemoveTimer( ... )>
174  
175 Remove a timer created with L<< C<SDL_AddTimer( ... )>|/C<SDL_AddTimer( ... )>
176 >>.
177  
178 SDL_RemoveTimer( $id );
179  
180 Expected parameters include:
181  
182 =over
183  
184 =item C<id> - the ID of the timer to remove
185  
186 =back
187  
188 Returns C<SDL_TRUE> if the timer is removed or C<SDL_FALSE> if the timer wasn't
189 found.
190  
191 =head1 Defined Types
192  
193 Time keeps on slipping...
194  
195 =head2 C<SDL_TimerCallback>
196  
197 Function prototype for the timer callback function.
198  
199 The callback function is passed the current timer interval and returns the next
200 timer interval.
201  
202 Parameters to expect include:
203  
204 =over
205  
206 =item C<interval>
207  
208 =item C<param>
209  
210 =back
211  
212 If the returned value is the same as the one passed in, the periodic alarm
213 continues, otherwise a new alarm is scheduled. If the callback returns C<0>,
214 the periodic alarm is cancelled.
215  
216 =head2 C<SDL_TimerID>
217  
218 Timer ID type.
219  
220 =head1 LICENSE
221  
222 Copyright (C) Sanko Robinson.
223  
224 This library is free software; you can redistribute it and/or modify it under
225 the terms found in the Artistic License 2. Other copyrights, terms, and
226 conditions may apply to data transmitted through this module.
227  
228 =head1 AUTHOR
229  
230 Sanko Robinson E<lt>sanko@cpan.orgE<gt>
231  
232 =begin stopwords
233  
234 ms
235  
236 =end stopwords
237  
238 =cut
239  
240 };
241 1;