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