Permalink
Browse files

Updated to use SDL 2.518

  • Loading branch information...
1 parent fd407cf commit 49f25f3adc3eae594847462e41fee44ee7fdcbf8 @kthakore kthakore committed Oct 4, 2010
Showing with 20 additions and 16 deletions.
  1. +6 −7 code_listings/mouse_paint.pl
  2. +14 −9 src/03-events.pod
@@ -9,8 +9,9 @@
sub quit_event {
my $event = shift;
- return 0 if $event->type == SDL_QUIT;
- return 1;
+ my $controller = shift;
+ $controller->stop if $event->type == SDL_QUIT;
+
}
@@ -39,9 +40,6 @@ sub mouse_event {
$app->update();
}
$drawing = 0 if($event->type == SDL_MOUSEBUTTONUP );
-
-
- return 1;
}
@@ -62,7 +60,7 @@ sub save_image {
sub keyboard_event {
my $event = shift;
-
+ my $controller = shift;
if ( $event->type == SDL_KEYDOWN )
{
my $key_name = SDL::Events::get_key_name( $event->key_sym );
@@ -72,7 +70,8 @@ sub keyboard_event {
my $mod_state = SDL::Events::get_mod_state();
save_image if $key_name =~ /^s$/ && ($mod_state & KMOD_CTRL);
- $app->draw_rect( [0,0,$app->w, $app->h], 0 ) if $key_name =~ /^c$/
+ $app->draw_rect( [0,0,$app->w, $app->h], 0 ) if $key_name =~ /^c$/;
+ $controller->stop() if $key_name =~ /^q$/
}
$app->update();
return 1;
View
@@ -54,16 +54,16 @@ callbacks.
#The callback is provided a SDLx::Event to use
my $event = shift;
- #If we return 0 the $app will exit for us
- return 0 if $event->type == SDL_QUIT;
+ #Each event handler also returns you back the Controller call it
+ my $controller = shift;
- #Otherwise we continue run()ing
- return 1;
+ #Stoping the controller for us will exit $app->run() for us
+ $controller->stop if $event->type == SDL_QUIT;
}
=end programlisting
-C<SDLx::App> calls the event_handlers and expect either a 1 or 0 returned. When 0 is returned C<SDLx::App> will exit gracefully.
+C<SDLx::App> calls the event_handlers, from an internal C<SDLx::Controller>, until a C<SDLx::Controller->stop()> is called. C<SDLx::App> will exit gracefully once it is stoped.
=for sidebar
@@ -103,8 +103,8 @@ To handle the keyboard specifications we will create another event callback.
sub quit_event {
my $event = shift;
- return 0 if $event->type == SDL_QUIT;
- return 1;
+ my $controller = shift;
+ $controller->stop() if $event->type == SDL_QUIT;
}
@@ -153,6 +153,9 @@ To handle the keyboard specifications we will create another event callback.
#Clear the screen if we pressed C or c
$app->draw_rect( [0,0,$app->w, $app->h], 0 ) if $key_name =~ /^c$/
+
+ #Exit if we press a Q or q
+ $app->stop() if $key_name =~ /^q$/
}
$app->update();
}
@@ -205,7 +208,7 @@ Now we will go about capturing our Mouse events, by inserting the following code
# Turn drawing off if we lift the mouse button
$drawing = 0 if($event->type == SDL_MOUSEBUTTONUP );
- return 1;
+
}
@@ -214,6 +217,8 @@ Now we will go about capturing our Mouse events, by inserting the following code
=end programlisting
Currently we don't make a distinction between what mouse click is done. This can be accomplished by taking a look at the
-C<button_button()> method in C<SDL::Event>. At this point we have a simple paint application done.
+C<button_button()> method in C<SDL::Event>. At this point we have a simple paint application done.
+
+Another point to note is that each event_handler is called in the order that it was attached.
=for vim: spell

0 comments on commit 49f25f3

Please sign in to comment.