Skip to content
Browse files

putting the insides of SDLx::Controller back in

  • Loading branch information...
1 parent ebfaed5 commit e350231ec24801f7a58048ba4a635956b4ac8a8c @garu garu committed Dec 30, 2010
Showing with 85 additions and 104 deletions.
  1. +85 −104 lib/SDLx/Controller.pm
View
189 lib/SDLx/Controller.pm
@@ -9,192 +9,173 @@ 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;
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};
+ my ($class, %args) = @_;
+
+ my $self = bless {
+ dt => $args{dt} || 0.1,
+ min_t => $args{min_t} || 1 / 60,
+ stop => $args{stop},
+ event => $args{event} || SDL::Event->new(),
+ event_handlers => $args{event_handlers},
+ move_handlers => $args{move_handlers},
+ show_handlers => $args{show_handlers},
+ }, ref($class) || $class;
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};
-}
sub run {
- my ($self) = @_;
- my $ref = refaddr $self;
- my $dt = $_dt{ $ref };
- my $min_t = $_min_t{ $ref };
- my $t = 0.0;
+ my ($self) = @_;
+ my $dt = $self->{dt};
+ my $min_t = $self->{min_t};
+ my $t = 0.0;
#Allows us to do stop and run
- $_stop{ $ref } = 0;
+ $self->{stop} = 0;
- $_current_time{ $ref } = Time::HiRes::time;
- while ( !$_stop{ $ref } ) {
- $self->_event($ref);
+ $self->{current_time} = Time::HiRes::time;
+ while ( !$self->{stop} ) {
+ $self->_event();
my $new_time = Time::HiRes::time;
- my $delta_time = $new_time - $_current_time{ $ref };
+ my $delta_time = $new_time - $self->{current_time};
next if $delta_time < $min_t;
- $_current_time{ $ref} = $new_time;
+
+ $self->{current_time} = $new_time;
my $delta_copy = $delta_time;
while ( $delta_copy > $dt ) {
- $self->_move( $ref, 1, $t ); #a full move
+ $self->_move( 1, $t ); # a full move
$delta_copy -= $dt;
$t += $dt;
}
my $step = $delta_copy / $dt;
- $self->_move( $ref, $step, $t ); #a partial move
+ $self->_move( $step, $t ); # a partial move
$t += $dt * $step;
- $self->_show( $ref, $delta_time );
+ $self->_show( $delta_time );
- $dt = $_dt{ $ref}; #these can change
- $min_t = $_min_t{ $ref}; #during the cycle
+ $dt = $self->{dt}; # these can change
+ $min_t = $self->{min_t}; # during the cycle
}
}
sub pause {
my ($self, $callback) = @_;
- my $ref = refaddr $self;
- $callback ||= sub {1};
+ $callback ||= sub { 1 };
my $event = SDL::Event->new();
+
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;
+ SDL::Events::wait_event( $event )
+ or Carp::confess "pause failed waiting for an event";
+
+ # so run doesn't catch up with the time paused
+ if( $callback->($event, $self) ) {
+ $self->{current_time} = Time::HiRes::time;
+ last;
}
}
}
sub _event {
- my ($self, $ref) = @_;
- while ( SDL::Events::poll_event( $_event{ $ref} ) ) {
+ my ($self) = @_;
+
+ while ( SDL::Events::poll_event( $self->{event} ) ) {
SDL::Events::pump_events();
- foreach my $event_handler ( @{ $_event_handlers{ $ref} } ) {
+ foreach my $event_handler ( @{ $self->{event_handlers} } ) {
next unless $event_handler;
- $event_handler->( $_event{ $ref}, $self );
+ $event_handler->( $self->{event}, $self );
}
}
}
sub _move {
- my ($self, $ref, $move_portion, $t) = @_;
- foreach my $move_handler ( @{ $_move_handlers{ $ref} } ) {
+ my ($self, $move_portion, $t) = @_;
+
+ foreach my $move_handler ( @{ $self->{move_handlers} } ) {
next unless $move_handler;
$move_handler->( $move_portion, $self, $t );
}
}
sub _show {
- my ($self, $ref, $delta_ticks) = @_;
- foreach my $show_handler ( @{ $_show_handlers{ $ref} } ) {
+ my ($self, $delta_ticks) = @_;
+
+ foreach my $show_handler ( @{ $self->{show_handlers} } ) {
next unless $show_handler;
$show_handler->( $delta_ticks, $self );
}
}
-sub stop { $_stop{ refaddr $_[0] } = 1 }
+sub stop { $_[0]->{stop} = 1 }
sub _add_handler {
my ( $arr_ref, $handler ) = @_;
+
push @{$arr_ref}, $handler;
return $#{$arr_ref};
}
sub add_move_handler {
- my $ref = refaddr $_[0];
- $_[0]->remove_all_move_handlers if !$_move_handlers{ $ref };
- return _add_handler( $_move_handlers{ $ref}, $_[1] );
+ my ($self, $handler) = @_;
+ $self->remove_all_move_handlers unless $self->{move_handlers};
+
+ return _add_handler( $self->{move_handlers}, $handler );
}
sub add_event_handler {
- my $ref = refaddr $_[0];
+ my ($self, $handler) = @_;
+
Carp::confess 'SDLx::App or a Display (SDL::Video::get_video_mode) must be made'
unless SDL::Video::get_video_surface();
- $_[0]->remove_all_event_handlers if !$_event_handlers{ $ref };
- return _add_handler( $_event_handlers{ $ref}, $_[1] );
+
+ $self->remove_all_event_handlers unless $self->{event_handlers};
+
+ return _add_handler( $self->{event_handlers}, $handler );
}
sub add_show_handler {
- my $ref = refaddr $_[0];
- $_[0]->remove_all_show_handlers if !$_show_handlers{ $ref };
- return _add_handler( $_show_handlers{ $ref}, $_[1] );
+ my ($self, $handler) = @_;
+
+ $self->remove_all_show_handlers unless $self->{show_handlers};
+
+ return _add_handler( $self->{show_handlers}, $handler );
}
sub _remove_handler {
my ( $arr_ref, $id ) = @_;
+
if ( ref $id ) {
($id) = grep {
$id eq $arr_ref->[$_]
} 0..$#{$arr_ref};
- if ( !defined $id ) {
+ if ( not defined $id ) {
Carp::cluck("$id is not currently a handler of this type");
return;
}
}
- elsif(!defined $arr_ref->[$id]) {
+ elsif( not 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] );
+ return _remove_handler( $_[0]->{move_handlers}, $_[1] );
}
sub remove_event_handler {
- return _remove_handler( $_event_handlers{ refaddr $_[0] }, $_[1] );
+ return _remove_handler( $_[0]->{event_handlers}, $_[1] );
}
sub remove_show_handler {
- return _remove_handler( $_show_handlers{ refaddr $_[0] }, $_[1] );
+ return _remove_handler( $_[0]->{show_handlers}, $_[1] );
}
sub remove_all_handlers {
@@ -204,43 +185,43 @@ sub remove_all_handlers {
}
sub remove_all_move_handlers {
- $_move_handlers{ refaddr $_[0] } = [];
+ $_[0]->{move_handlers} = [];
}
sub remove_all_event_handlers {
- $_event_handlers{ refaddr $_[0] } = [];
+ $_[0]->{event_handlers} = [];
}
sub remove_all_show_handlers {
- $_show_handlers{ refaddr $_[0] } = [];
+ $_[0]->{show_handlers} = [];
}
-sub move_handlers { $_move_handlers{ refaddr $_[0] } }
-sub event_handlers { $_event_handlers{ refaddr $_[0] } }
-sub show_handlers { $_show_handlers{ refaddr $_[0] } }
+sub move_handlers { $_[0]->{move_handlers} }
+sub event_handlers { $_[0]->{event_handlers} }
+sub show_handlers { $_[0]->{show_handlers} }
sub dt {
my ($self, $arg) = @_;
- my $ref = refaddr $self;
- $_dt{ $ref} = $arg if defined $arg;
+
+ $self->{dt} = $arg if defined $arg;
- $_dt{ $ref};
+ return $self->{dt};
}
sub min_t {
my ($self, $arg) = @_;
- my $ref = refaddr $self;
- $_min_t{ $ref} = $arg if defined $arg;
+
+ $self->{min_t} = $arg if defined $arg;
- $_min_t{ $ref};
+ return $self->{min_t};
}
sub current_time {
my ($self, $arg) = @_;
- my $ref = refaddr $self;
- $_current_time{ $ref} = $arg if defined $arg;
+
+ $self->{current_time} = $arg if defined $arg;
- $_current_time{ $ref};
+ return $self->{current_time};
}
1; #not 42 man!

0 comments on commit e350231

Please sign in to comment.
Something went wrong with that request. Please try again.