First commit of SDL_Perl-2.1.3
[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;
9use SDL;
10
11sub 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
36sub DESTROY {
37 my $self = shift;
38 SDL::RemoveTimer($$self{-timer}) if ($$self{-timer});
39 $$self{-timer} = 0;
40}
41
42sub 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
50sub stop {
51 my ($self) = @_;
52 SDL::RemoveTimer($$self{-timer}) if ($$self{-timer});
53 $$self{-timer} = 0;
54}
55
561;
57
58__END__;
59
60=pod
61
62
63=head1 NAME
64
65SDL::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
73C<SDL::Timer> provides an abstract interface to the SDL::Timer
74callback code. SDL::Timer::new requires a subroutine and a delay
75at which is the initial interval between the creation of the
76timer until the first call to the subroutine. The subroutine
77must return a value which is the delay until the it is called again.
78
79=head1 AUTHOR
80
81David J. Goehrig
82
83=head1 SEE ALSO
84
85L<perl> L<SDL>
86
87=pod