PageRenderTime 15ms CodeModel.GetById 6ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/SDLx/Controller/Timer.pm

http://github.com/PerlGameDev/SDL
Perl | 79 lines | 61 code | 16 blank | 2 comment | 6 complexity | 1ace04604cbdcee54e1e33692e68c818 MD5 | raw file
 1package SDLx::Controller::Timer;
 2
 3# Implementation of lesson 13 and 14 from http://lazyfoo.net/SDL_tutorials/index.php
 4#
 5use strict;
 6use warnings;
 7use SDL;
 8
 9our $VERSION = 2.548;
10
11sub new {
12	my $class = shift;
13	my $self = bless {@_}, $class;
14
15	$self->{started_ticks} = 0;
16	$self->{paused_ticks}  = 0;
17	$self->{started}       = 0;
18	$self->{paused}        = 0;
19
20	return $self;
21}
22
23sub start {
24	my $self = shift;
25	$self->{started}       = 1;
26	$self->{started_ticks} = SDL::get_ticks();
27}
28
29sub stop {
30	my $self = shift;
31
32	$self->{started} = 0;
33	$self->{paused}  = 0;
34}
35
36sub pause {
37	my $self = shift;
38	if ( $self->{started} && !$self->{paused} ) {
39		$self->{paused}       = 1;
40		$self->{paused_ticks} = SDL::get_ticks() - $self->{started_ticks};
41	}
42}
43
44sub unpause {
45	my $self = shift;
46	if ( $self->{paused} ) {
47		$self->{paused} = 0;
48
49		$self->{started_ticks} = SDL::get_ticks() - $self->{started_ticks};
50
51		$self->{paused_ticks} = 0;
52	}
53}
54
55sub get_ticks {
56	my $self = shift;
57	if ( $self->{started} ) {
58		if ( $self->{paused} ) {
59			return $self->{paused_ticks};
60		} else {
61			my $update = SDL::get_ticks();
62			my $diff   = $update - $self->{started_ticks};
63			return $diff;
64		}
65	}
66	return 0;
67}
68
69sub is_started {
70	my $self = shift;
71	return $self->{started};
72}
73
74sub is_paused {
75	my $self = shift;
76	return $self->{paused};
77}
78
791;