Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finished the last example for chapter game

  • Loading branch information...
commit 02538484f91aa6a12497b29c22b61c8a2f03aace 1 parent be33c6f
@kthakore kthakore authored
Showing with 120 additions and 4 deletions.
  1. +60 −0 code_listings/game_sdlx.pl
  2. +60 −4 src/04-game.pod
View
60 code_listings/game_sdlx.pl
@@ -0,0 +1,60 @@
+use strict;
+use warnings;
+use SDL;
+use SDL::Event;
+use SDLx::App;
+
+my $app = SDLx::App->new(
+ width => 200,
+ height => 200,
+ title => 'Pew Pew'
+);
+
+my $laser = 0;
+my $velocity = 10;
+
+#We can add an event handler
+$app->add_event_handler( \&quit_event );
+
+#We tell app to handle the appropriate times to
+#call both rendering and physics calculation
+
+$app->add_move_handler( \&calculate_laser );
+$app->add_show_handler( \&render_laser );
+
+$app->run();
+
+sub quit_event {
+
+ #The callback is provided a SDL::Event to use
+ my $event = shift;
+
+ #Each event handler also returns you back the Controller call it
+ my $controller = shift;
+
+ #Stoping the controller for us will exit $app->run() for us
+ $controller->stop if $event->type == SDL_QUIT;
+}
+
+sub calculate_laser {
+
+ # The step is the difference in Time calculated for the
+ # next jump
+ my ( $step, $app, $t ) = @_;
+ $laser += $velocity * $step;
+ $laser = 0 if $laser > $app->w;
+}
+
+sub render_laser {
+ my ( $delta, $app ) = @_;
+
+ # The delta can be used to render blurred frames
+
+ #Draw the background first
+ $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0 );
+
+ #Draw the laser
+ $app->draw_rect( [ $laser, $app->h / 2, 10, 2 ], [ 255, 0, 0, 255 ] );
+ $app->update();
+
+}
View
64 src/04-game.pod
@@ -294,10 +294,66 @@ and will not be explored in this book. However the implementation is made availa
=head2 Laser in Real Time
- use strict;
- use warnings;
- use SDL;
- use SDLx::App;
+ use strict;
+ use warnings;
+ use SDL;
+ use SDL::Event;
+ use SDLx::App;
+
+ my $app = SDLx::App->new(
+ width => 200,
+ height => 200,
+ title => 'Pew Pew'
+ );
+
+ my $laser = 0;
+ my $velocity = 10;
+
+ #We can add an event handler
+ $app->add_event_handler( \&quit_event );
+
+ #We tell app to handle the appropriate times to
+ #call both rendering and physics calculation
+
+ $app->add_move_handler( \&calculate_laser );
+ $app->add_show_handler( \&render_laser );
+
+ $app->run();
+
+ sub quit_event {
+
+ #The callback is provided a SDL::Event to use
+ my $event = shift;
+
+ #Each event handler also returns you back the Controller call it
+ my $controller = shift;
+
+ #Stoping the controller for us will exit $app->run() for us
+ $controller->stop if $event->type == SDL_QUIT;
+ }
+
+ sub calculate_laser {
+
+ # The step is the difference in Time calculated for the
+ # next jump
+ my ( $step, $app, $t ) = @_;
+ $laser += $velocity * $step;
+ $laser = 0 if $laser > $app->w;
+ }
+
+ sub render_laser {
+ my ( $delta, $app ) = @_;
+
+ # The delta can be used to render blurred frames
+
+ #Draw the background first
+ $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0 );
+
+ #Draw the laser
+ $app->draw_rect( [ $laser, $app->h / 2, 10, 2 ], [ 255, 0, 0, 255 ] );
+ $app->update();
+
+ }
=head1 Learn More
Please sign in to comment.
Something went wrong with that request. Please try again.