Skip to content

Commit

Permalink
Better error reporting for event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kthakore committed Sep 10, 2010
1 parent 209b4b0 commit 7c98ef1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/SDLx/Controller.pm
Expand Up @@ -6,6 +6,7 @@ use Time::HiRes qw( time sleep );
use SDL;
use SDL::Event;
use SDL::Events;
use SDL::Video;
use SDLx::Controller::Timer;
use Scalar::Util 'refaddr';
use SDLx::Controller::Interface;
Expand Down Expand Up @@ -80,7 +81,6 @@ sub run {

sub _event {
my $self = shift;

$_event{ refaddr $self} = SDL::Event->new() unless $_event{ refaddr $self};
while ( SDL::Events::poll_event( $_event{ refaddr $self} ) ) {
SDL::Events::pump_events();
Expand Down Expand Up @@ -123,6 +123,7 @@ sub add_move_handler {
}

sub add_event_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{ refaddr $_[0] };
return _add_handler( $_event_handlers{ refaddr $_[0] }, $_[1] );
}
Expand Down
47 changes: 47 additions & 0 deletions t/sdlx_controller_interface.t
Expand Up @@ -2,12 +2,18 @@ use strict;
use warnings;
use Test::More;
use SDL;
use SDLx::App;
use SDLx::Controller;
use SDLx::Controller::State;
use SDLx::Controller::Interface;
use lib 't/lib';
use SDL::TestTool;
use Data::Dumper;

my $videodriver = $ENV{SDL_VIDEODRIVER};
$ENV{SDL_VIDEODRIVER} = 'dummy' unless $ENV{SDL_RELEASE_TESTING};


can_ok(
'SDLx::Controller::Interface',
qw( new ) #meh, put the rest in later
Expand Down Expand Up @@ -65,5 +71,46 @@ my $a = $obj->current;
my $a_x = $a->x();
is( $a_x, 2, '[obj/state] acceleration callback copies staet back to current' );

SKIP:
{
skip 'No Video', 1 unless SDL::TestTool->init(SDL_INIT_VIDEO);

my $dummy = SDLx::App->new();

my $controller = SDLx::Controller->new( dt => 0.2 );

my $interface = SDLx::Controller::Interface->new( );
my $event_called = 0;

require SDL::Event;
require SDL::Events;
my $eve = SDL::Event->new();

SDL::Events::push_event($eve);
my $counts = [0,0,0];
$controller->add_event_handler( sub { $counts->[0]++; return 0 if $interface->current->x; return 0 } );

$interface->set_acceleration( sub { die if $counts->[1] > 100; $counts->[1]++; isa_ok( $_[1], 'SDLx::Controller::State', '[Controller] called acceleration and gave us a state'), return( 10, 10, 10)});

$interface->attach( $controller, sub{ $counts->[2]++;
isa_ok( $_[0], 'SDLx::Controller::State', '[Controller] called render and gave us a state')
}
);


$controller->run();

is_deeply( $counts, [1,4,1]);

}


if ($videodriver) {
$ENV{SDL_VIDEODRIVER} = $videodriver;
} else {
delete $ENV{SDL_VIDEODRIVER};
}


done_testing;

0 comments on commit 7c98ef1

Please sign in to comment.