PageRenderTime 40ms CodeModel.GetById 26ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/t_backcompat/Timer.pm

http://github.com/PerlGameDev/SDL
Perl | 81 lines | 44 code | 8 blank | 29 comment | 11 complexity | 3f6a0ecb21aa8c410ef6b42e3d5a9cf1 MD5 | raw file
 1#!/usr/bin/env perl
 2#
 3# Timer.pm
 4#
 5# Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
 6#
 7# ------------------------------------------------------------------------------
 8#
 9# This library is free software; you can redistribute it and/or
10# modify it under the terms of the GNU Lesser General Public
11# License as published by the Free Software Foundation; either
12# version 2.1 of the License, or (at your option) any later version.
13#
14# This library is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17# Lesser General Public License for more details.
18#
19# You should have received a copy of the GNU Lesser General Public
20# License along with this library; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22#
23# ------------------------------------------------------------------------------
24#
25# Please feel free to send questions, suggestions or improvements to:
26#
27#	David J. Goehrig
28#	dgoehrig@cpan.org
29#
30
31package SDL::Timer;
32
33use strict;
34use warnings;
35use Carp;
36use SDL;
37
38sub new {
39	my $proto     = shift;
40	my $class     = ref($proto) || $proto;
41	my $self      = {};
42	my $func      = shift;
43	my (%options) = @_;
44
45	Carp::confess "SDL::Timer::new no delay specified\n"
46		unless ( $options{-delay} );
47	$$self{-delay} = $options{-delay} || $options{-d} || 0;
48	$$self{-times} = $options{-times} || $options{-t} || 0;
49	if ( $$self{-times} ) {
50		$$self{-routine} = sub { &$func($self); $$self{-delay} if ( --$$self{-times} ) };
51	} else {
52		$$self{-routine} = sub { &$func; $$self{-delay} };
53	}
54	$$self{-timer} = SDL::NewTimer( $$self{-delay}, $$self{-routine} );
55	Carp::confess "Could not create timer, ", SDL::get_error(), "\n"
56		unless ( $self->{-timer} );
57	bless $self, $class;
58	return $self;
59}
60
61sub DESTROY {
62	my $self = shift;
63	SDL::RemoveTimer( $$self{-timer} ) if ( $$self{-timer} );
64	$$self{-timer} = 0;
65}
66
67sub run {
68	my ( $self, $delay, $times ) = @_;
69	$$self{-delay} = $delay;
70	$$self{-times} = $times;
71	SDL::RemoveTimer( $$self{-timer} ) if ( $$self{-timer} );
72	$$self{-timer} = SDL::AddTimer( $$self{-delay}, SDL::PerlTimerCallback, $$self{-routine} );
73}
74
75sub stop {
76	my ($self) = @_;
77	SDL::RemoveTimer( $$self{-timer} ) if ( $$self{-timer} );
78	$$self{-timer} = 0;
79}
80
811;