Brought all packages under eye of strict, warnings and love of Carp, For
[sdlgit/SDL_perl.git] / lib / SDL / Timer.pm
CommitLineData
8fde61e3 1# Timer.pm
2#
3# A package for manipulating SDL_Timer *
4#
5# Copyright (C) 2002 David J. Goehrig
6
7package SDL::Timer;
8use strict;
084b921f 9use warnings;
10use Carp;
8fde61e3 11use SDL;
12
13sub 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
38sub DESTROY {
39 my $self = shift;
40 SDL::RemoveTimer($$self{-timer}) if ($$self{-timer});
41 $$self{-timer} = 0;
42}
43
44sub 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
52sub stop {
53 my ($self) = @_;
54 SDL::RemoveTimer($$self{-timer}) if ($$self{-timer});
55 $$self{-timer} = 0;
56}
57
581;
59
60__END__;
61
62=pod
63
64
65=head1 NAME
66
67SDL::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
75C<SDL::Timer> provides an abstract interface to the SDL::Timer
76callback code. SDL::Timer::new requires a subroutine and a delay
77at which is the initial interval between the creation of the
78timer until the first call to the subroutine. The subroutine
79must return a value which is the delay until the it is called again.
80
81=head1 AUTHOR
82
83David J. Goehrig
84
85=head1 SEE ALSO
86
87L<perl> L<SDL>
88
89=pod