File Coverage

blib/lib/SDLx/Widget/Controller.pm
Criterion Covered Total %
statement 21 78 26.9
branch 0 12 0.0
condition 0 3 0.0
subroutine 7 22 31.8
pod 4 10 40.0
total 32 125 25.6


line stmt bran cond sub pod time code
1             package SDLx::Widget::Controller;
2 1     1   5467 use strict;
  1         3  
  1         38  
3 1     1   6 use warnings;
  1         2  
  1         29  
4 1     1   934 use SDL;
  1         91291  
  1         108  
5 1     1   1003 use SDL::Event;
  1         3234  
  1         953  
6 1     1   937 use SDL::Events;
  1         4076  
  1         4030  
7 1     1   3494 use SDLx::Widget::Controller::Timer;
  1         3  
  1         34  
8 1     1   1245 use Data::Dumper;
  1         8081  
  1         843  
9              
10             sub new {
11 0     0 0   my $class = shift;
12 0           my $self = bless {@_}, $class;
13 0           $self->{delta} = SDLx::Widget::Controller::Timer->new();
14 0           $self->{delta}->start(); # should do this after on_load
15 0 0         $self->{dt} = 0.1 unless $self->{dt};
16              
17 0           return $self;
18             }
19              
20             sub run {
21 0     0 1   my $self = shift;
22 0           $self->{quit} = 0;
23 0           my $accumulator = 0;
24 0           while ( !$self->{quit} ) {
25 0           $self->_event;
26 0           my $delta_time = $self->{delta}->get_ticks();
27 0           $accumulator += $delta_time;
28              
29 0   0       while ( $accumulator >= $self->{dt} && !$self->{quit} ) {
30 0           $self->_move( $self->{dt} );
31 0           $accumulator -= $self->{dt};
32              
33             #update how much real time we have animated
34              
35             }
36 0           $self->{delta}->start();
37              
38 0           $self->_show($delta_time);
39              
40             }
41              
42             }
43              
44             sub _event {
45 0     0     my $self = shift;
46              
47 0 0         $self->{event} = SDL::Event->new() unless $self->{event};
48 0           while ( SDL::Events::poll_event( $self->{event} ) ) {
49 0           SDL::Events::pump_events();
50 0           foreach my $event_handler ( @{ $self->{event_handlers} } ) {
  0            
51 0 0         $self->quit unless $event_handler->( $self->{event} );
52             }
53             }
54             }
55              
56             sub _move {
57 0     0     my $self = shift;
58 0           my $delta_ticks = shift;
59 0           foreach my $move_handler ( @{ $self->{move_handlers} } ) {
  0            
60              
61 0           $move_handler->($delta_ticks);
62              
63             }
64              
65             }
66              
67             sub _show {
68 0     0     my $self = shift;
69 0           my $delta_ticks = shift;
70 0           foreach my $event_handler ( @{ $self->{show_handlers} } ) {
  0            
71 0           $event_handler->($delta_ticks);
72             }
73              
74             }
75              
76             sub quit {
77 0     0 0   my $self = shift;
78              
79 0           $self->{quit} = 1;
80              
81             }
82              
83             sub _add_handler {
84 0     0     my ( $arr_ref, $handler ) = @_;
85 0           push @{$arr_ref}, $handler;
  0            
86 0           return $#{$arr_ref}
  0            
87              
88             }
89              
90             sub add_move_handler {
91 0 0   0 1   $_[0]->{move_handlers} = [] if !$_[0]->{move_handlers};
92 0           return _add_handler( $_[0]->{move_handlers}, $_[1] );
93              
94             }
95              
96             sub add_event_handler {
97 0 0   0 1   $_[0]->{event_handlers} = [] if !$_[0]->{event_handlers};
98 0           return _add_handler( $_[0]->{event_handlers}, $_[1] );
99             }
100              
101             sub add_show_handler {
102 0 0   0 1   $_[0]->{show_handlers} = [] if !$_[0]->{show_handlers};
103 0           return _add_handler( $_[0]->{show_handlers}, $_[1] );
104             }
105              
106             sub _remove_handler {
107 0     0     my ( $arr_ref, $id ) = @_;
108              
109 0           return splice( @{$arr_ref}, $id, 1 );
  0            
110              
111             }
112              
113             sub remove_move_handler {
114 0     0 0   return _remove_handler( $_[0]->{move_handlers}, $_[1] );
115             }
116              
117             sub remove_event_handler {
118 0     0 0   return _remove_handler( $_[0]->{event_handlers}, $_[1] );
119             }
120              
121             sub remove_show_handler {
122 0     0 0   return _remove_handler( $_[0]->{show_handlers}, $_[1] );
123             }
124              
125             sub remove_all_handlers {
126 0     0 0   $_[0]->{move_handlers} = [];
127 0           $_[0]->{event_handlers} = [];
128 0           $_[0]->{show_handlers} = [];
129             }
130              
131              
132             1; #not 42 man!
133              
134             =pod
135              
136             =head1 NAME
137              
138             SDLx::Controller::Widget - Handles the loops for event, movement and rendering
139              
140             =head2 Description
141              
142             Using http://www.lazyfoo.net/SDL_tutorials/lesson32/index.php as our base
143              
144             and
145              
146             http://gafferongames.com/game-physics/fix-your-timestep/
147              
148             for timing
149              
150              
151             =head1 METHODS
152              
153              
154             =head2 run
155              
156             =head2 add_move_handler
157              
158             Register a callback to update objects
159              
160             =head2 add_show_handler
161              
162             Register a callback to render objects
163              
164              
165             =head2 add_event_handler
166              
167             Register a callback to handle events SDL or game like
168              
169              
170             =head2 current_fps
171              
172              
173             =head1 AUTHOR
174              
175             kthakore
176             =cut