diff --git a/lib/SDLx/Controller.pm b/lib/SDLx/Controller.pm index a8024949..34b479d3 100644 --- a/lib/SDLx/Controller.pm +++ b/lib/SDLx/Controller.pm @@ -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; @@ -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(); @@ -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] ); } diff --git a/t/sdlx_controller_interface.t b/t/sdlx_controller_interface.t index a07b2587..6058b1dd 100644 --- a/t/sdlx_controller_interface.t +++ b/t/sdlx_controller_interface.t @@ -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 @@ -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;