/usr/lib/perl5/SDLx/Controller.pm is in libsdl-perl 2.540-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | package SDLx::Controller;
use strict;
use warnings;
use Carp;
use Time::HiRes;
use SDL;
use SDL::Event;
use SDL::Events;
use SDL::Video;
use SDLx::Controller::Interface;
use SDLx::Controller::State;
use Scalar::Util 'refaddr';
# inside out, so this can work as the superclass of another
# SDL::Surface subclass
my %_dt;
my %_min_t;
my %_current_time;
my %_stop;
my %_event;
my %_event_handlers;
my %_move_handlers;
my %_show_handlers;
my %_sleep_cycle;
my %_eoq;
my %_paused;
sub new {
my ($self, %args) = @_;
if(ref $self) {
bless $self, ref $self;
}
else {
my $a;
$self = bless \$a, $self;
}
my $ref = refaddr $self;
$_dt{ $ref } = defined $args{dt} ? $args{dt} : 0.1;
$_min_t{ $ref } = defined $args{min_t} ? $args{min_t} : 1 / 60;
# $_current_time{ $ref } = $args{current_time} || 0; #no point
$_stop{ $ref } = $args{stop};
$_event{ $ref } = $args{event} || SDL::Event->new();
$_event_handlers{ $ref } = $args{event_handlers} || [];
$_move_handlers{ $ref } = $args{move_handlers} || [];
$_show_handlers{ $ref } = $args{show_handlers} || [];
$_sleep_cycle{ $ref } = $args{delay};
$_eoq{$ref} = $args{exit_on_quit} || $args{eoq} || 0;
# $_paused{ $ref } = $args{paused}; #read only
return $self;
}
sub delay {
my $self = shift;
my $delay = shift;
my $ref = refaddr $self;
$_sleep_cycle{ $ref } = $delay if $delay;
return $self;
}
sub DESTROY {
my $self = shift;
my $ref = refaddr $self;
delete $_dt{ $ref};
delete $_min_t{ $ref};
delete $_current_time{ $ref};
delete $_stop{ $ref};
delete $_event{ $ref};
delete $_event_handlers{ $ref};
delete $_move_handlers{ $ref};
delete $_show_handlers{ $ref};
delete $_sleep_cycle { $ref };
delete $_eoq{$ref};
delete $_paused{$ref};
}
sub run {
my ($self) = @_;
my $ref = refaddr $self;
my $dt = $_dt{ $ref };
my $min_t = $_min_t{ $ref };
my $t = 0.0;
#Allows us to do stop and run
$_stop{ $ref } = 0;
$_current_time{ $ref } = Time::HiRes::time;
while ( !$_stop{ $ref } ) {
$self->_event($ref);
my $new_time = Time::HiRes::time;
my $delta_time = $new_time - $_current_time{ $ref };
next if $delta_time < $min_t;
$_current_time{ $ref} = $new_time;
my $delta_copy = $delta_time;
while ( $delta_copy > $dt ) {
$self->_move( $ref, 1, $t ); #a full move
$delta_copy -= $dt;
$t += $dt;
}
my $step = $delta_copy / $dt;
$self->_move( $ref, $step, $t ); #a partial move
$t += $dt * $step;
$self->_show( $ref, $delta_time );
$dt = $_dt{ $ref}; #these can change
$min_t = $_min_t{ $ref}; #during the cycle
SDL::delay( $_sleep_cycle{ $ref } ) if $_sleep_cycle{ $ref };
}
}
sub exit_on_quit {
my ($self, $value) = @_;
my $ref = refaddr $self;
if (defined $value) {
$_eoq{$ref} = $value;
}
return $_eoq{$ref};
}
*eoq = \&exit_on_quit; # alias
sub pause {
my ($self, $callback) = @_;
my $ref = refaddr $self;
$callback ||= sub {1};
my $event = SDL::Event->new();
$_paused{ $ref} = 1;
while(1) {
SDL::Events::wait_event($event) or Carp::confess("pause failed waiting for an event");
if($callback->($event, $self)) {
$_current_time{ $ref} = Time::HiRes::time; #so run doesn't catch up with the time paused
last;
}
}
delete $_paused{ $ref};
}
sub _event {
my ($self, $ref) = @_;
SDL::Events::pump_events();
while ( SDL::Events::poll_event( $_event{ $ref} ) ) {
$self->_exit_on_quit( $_event{ $ref} ) if $_eoq{$ref};
foreach my $event_handler ( @{ $_event_handlers{ $ref} } ) {
next unless $event_handler;
$event_handler->( $_event{ $ref}, $self );
}
}
}
sub _move {
my ($self, $ref, $move_portion, $t) = @_;
foreach my $move_handler ( @{ $_move_handlers{ $ref} } ) {
next unless $move_handler;
$move_handler->( $move_portion, $self, $t );
}
}
sub _show {
my ($self, $ref, $delta_ticks) = @_;
foreach my $show_handler ( @{ $_show_handlers{ $ref} } ) {
next unless $show_handler;
$show_handler->( $delta_ticks, $self );
}
}
sub stop { $_stop{ refaddr $_[0] } = 1 }
sub _add_handler {
my ( $arr_ref, $handler ) = @_;
push @{$arr_ref}, $handler;
return $#{$arr_ref};
}
sub add_move_handler {
my $ref = refaddr $_[0];
return _add_handler( $_move_handlers{ $ref}, $_[1] );
}
sub add_event_handler {
my $ref = refaddr $_[0];
Carp::confess 'SDLx::App or a Display (SDL::Video::get_video_mode) must be made'
unless SDL::Video::get_video_surface();
return _add_handler( $_event_handlers{ $ref}, $_[1] );
}
sub add_show_handler {
my $ref = refaddr $_[0];
return _add_handler( $_show_handlers{ $ref}, $_[1] );
}
sub _remove_handler {
my ( $arr_ref, $id ) = @_;
if ( ref $id ) {
($id) = grep {
$id eq $arr_ref->[$_]
} 0..$#{$arr_ref};
if ( !defined $id ) {
Carp::cluck("$id is not currently a handler of this type");
return;
}
}
elsif(!defined $arr_ref->[$id]) {
Carp::cluck("$id is not currently a handler of this type");
return;
}
return delete( $arr_ref->[$id] );
}
sub remove_move_handler {
return _remove_handler( $_move_handlers{ refaddr $_[0] }, $_[1] );
}
sub remove_event_handler {
return _remove_handler( $_event_handlers{ refaddr $_[0] }, $_[1] );
}
sub remove_show_handler {
return _remove_handler( $_show_handlers{ refaddr $_[0] }, $_[1] );
}
sub remove_all_handlers {
$_[0]->remove_all_move_handlers;
$_[0]->remove_all_event_handlers;
$_[0]->remove_all_show_handlers;
}
sub remove_all_move_handlers {
$_move_handlers{ refaddr $_[0] } = [];
}
sub remove_all_event_handlers {
$_event_handlers{ refaddr $_[0] } = [];
}
sub remove_all_show_handlers {
$_show_handlers{ refaddr $_[0] } = [];
}
sub move_handlers { $_move_handlers{ refaddr $_[0] } }
sub event_handlers { $_event_handlers{ refaddr $_[0] } }
sub show_handlers { $_show_handlers{ refaddr $_[0] } }
sub dt {
my ($self, $arg) = @_;
my $ref = refaddr $self;
$_dt{ $ref} = $arg if defined $arg;
$_dt{ $ref};
}
sub min_t {
my ($self, $arg) = @_;
my $ref = refaddr $self;
$_min_t{ $ref} = $arg if defined $arg;
$_min_t{ $ref};
}
sub current_time {
my ($self, $arg) = @_;
my $ref = refaddr $self;
$_current_time{ $ref} = $arg if defined $arg;
$_current_time{ $ref};
}
sub paused {
$_paused{ refaddr $_[0]};
}
sub _exit_on_quit {
my ($self, $event) = @_;
$self->stop() if $event->type == SDL_QUIT;
}
1;
__END__
|