blib/lib/SDL2/timer.pm untested

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