Commit | Line | Data |
8fde61e3 |
1 | # Timer.pm |
2 | # |
3 | # A package for manipulating SDL_Timer * |
4 | # |
5 | # Copyright (C) 2002 David J. Goehrig |
6 | |
7 | package SDL::Timer; |
8 | use strict; |
084b921f |
9 | use warnings; |
10 | use Carp; |
8fde61e3 |
11 | use SDL; |
12 | |
13 | sub new { |
14 | my $proto = shift; |
15 | my $class = ref($proto) || $proto; |
16 | my $self = {}; |
17 | my $func = shift; |
18 | my (%options) = @_; |
19 | |
20 | verify(%options,qw/ -delay -times -d -t /); |
21 | |
084b921f |
22 | croak "SDL::Timer::new no delay specified\n" |
8fde61e3 |
23 | unless ($options{-delay}); |
24 | $$self{-delay} = $options{-delay} || $options{-d} || 0; |
25 | $$self{-times} = $options{-times} || $options{-t} || 0; |
26 | if ($$self{-times}) { |
27 | $$self{-routine} = sub { &$func($self); $$self{-delay} if(--$$self{-times}) }; |
28 | } else { |
29 | $$self{-routine} = sub { &$func; $$self{-delay}}; |
30 | } |
31 | $$self{-timer} = SDL::NewTimer($$self{-delay},$$self{-routine}); |
084b921f |
32 | croak "Could not create timer, ", SDL::GetError(), "\n" |
8fde61e3 |
33 | unless ($self->{-timer}); |
34 | bless $self,$class; |
35 | return $self; |
36 | } |
37 | |
38 | sub DESTROY { |
39 | my $self = shift; |
40 | SDL::RemoveTimer($$self{-timer}) if ($$self{-timer}); |
41 | $$self{-timer} = 0; |
42 | } |
43 | |
44 | sub run { |
45 | my ($self,$delay,$times) = @_; |
46 | $$self{-delay} = $delay; |
47 | $$self{-times} = $times; |
48 | SDL::RemoveTimer($$self{-timer}) if ($$self{-timer}); |
49 | $$self{-timer} = SDL::AddTimer($$self{-delay},SDL::PerlTimerCallback,$$self{-routine}); |
50 | } |
51 | |
52 | sub stop { |
53 | my ($self) = @_; |
54 | SDL::RemoveTimer($$self{-timer}) if ($$self{-timer}); |
55 | $$self{-timer} = 0; |
56 | } |
57 | |
58 | 1; |
59 | |
60 | __END__; |
61 | |
62 | =pod |
63 | |
64 | |
65 | =head1 NAME |
66 | |
67 | SDL::Timer - a SDL perl extension to handle timers |
68 | |
69 | =head1 SYNOPSIS |
70 | |
71 | $timer = new SDL::Timer { print "tick"; 4000; } -delay => 4000; |
72 | |
73 | =head1 DESCRIPTION |
74 | |
75 | C<SDL::Timer> provides an abstract interface to the SDL::Timer |
76 | callback code. SDL::Timer::new requires a subroutine and a delay |
77 | at which is the initial interval between the creation of the |
78 | timer until the first call to the subroutine. The subroutine |
79 | must return a value which is the delay until the it is called again. |
80 | |
81 | =head1 AUTHOR |
82 | |
83 | David J. Goehrig |
84 | |
85 | =head1 SEE ALSO |
86 | |
87 | L<perl> L<SDL> |
88 | |
89 | =pod |