Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added comments, fixed draw*, changed lazer to laser

  • Loading branch information...
commit 1f76232670fee7b47e4ec0b0fd4c20f14c75eb49 1 parent e1a6e81
@kthakore kthakore authored
View
160 code_listings/game_fixed.pl
@@ -1,98 +1,132 @@
-use strict;
-use warnings;
-use SDL;
-use SDL::Event;
-use SDL::Events;
-use SDLx::App;
+ use strict;
+ use warnings;
+ use SDL;
+ use SDL::Event;
+ use SDL::Events;
+ use SDLx::App;
-my $app = SDLx::App->new(
- width => 200,
- height => 200,
- title => 'Pew Pew'
-);
+ my $app = SDLx::App->new(
+ width => 200,
+ height => 200,
+ title => 'Pew Pew'
+ );
-my ( $start, $end, $delta_time, $FPS, $frames ) = ( 0, 0, 0, 0, 0 );
+ # Variables
+ # to save our start/end and delta times for each frame
+ # to save our frames and FPS
+ my ( $start, $end, $delta_time, $FPS, $frames ) = ( 0, 0, 0, 0, 0 );
-#Don't need to quit yet
-my $quit = 0;
+ # We will aim for a rate of 60 frames per second
+ my $fixed_rate = 60;
-#Start lazer on the left
-my $lazer = 0;
+ # Our times are in micro second, so we will compenstate for it
+ my $fps_check = ( 1000 / $fixed_rate );
-sub get_events {
+ #Don't need to quit yet
+ my $quit = 0;
- my $event = SDL::Event->new();
+ #Start laser on the left
+ my $laser = 0;
- #Pump the event queue
- SDL::Events::pump_events;
+ sub get_events {
- while ( SDL::Events::poll_event($event) ) {
- $quit = 1 if $event->type == SDL_QUIT;
+ my $event = SDL::Event->new();
+
+ #Pump the event queue
+ SDL::Events::pump_events;
+
+ while ( SDL::Events::poll_event($event) ) {
+ $quit = 1 if $event->type == SDL_QUIT;
+ }
+ }
+
+ sub calculate_next_positions {
+ $laser++;
+
+ $laser = 0 if $laser > $app->w;
}
-}
-sub calculate_next_positions {
- $lazer++;
+ sub render {
- $lazer = 0 if $lazer > $app->w;
-}
+ #Draw the background first
+ $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0 );
-sub render {
+ #Draw the laser
+ $app->draw_rect( [ $laser, $app->h / 2, 10, 2 ], [ 255, 0, 0, 255 ] );
- #Draw the background first
- $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0 );
+ #Draw our FPS on the screen so we can see
+ $app->draw_gfx_text( [ 10, 10 ], [ 255, 0, 255, 255 ], "FPS: $FPS" );
- #Draw the lazer
- $app->draw_rect( [ $lazer, $app->h / 2, 10, 2 ], [ 255, 0, 0, 255 ] );
+ $app->update();
+ }
- $app->draw_gfx_text( [ 10, 10 ], [ 255, 0, 255, 255 ], "FPS: $FPS" );
+ # Called at the end of each frame, wether we draw or not
+ sub calculate_fps_at_frame_end {
- $app->update();
-}
+ # Ticks are microseconds since load time
+ $end = SDL::get_ticks();
-my $fps_check = ( 1000 / 60 );
+ # We will average our frame rate over 10 frames, to give less erratic rates
+ if ( $frames < 10 ) {
-sub calculate_fps_at_frame_end {
+ #Count a frame
+ $frames++;
- $end = SDL::get_ticks();
+ #Calculate how long it took from the start
+ $delta_time += $end - $start;
+ }
+ else {
+
+ # Our frame rate is our Frames * 100 / Time Elapsed in us
+ $FPS = int( ( $frames * 100 ) / $delta_time );
+
+ # Reset our metrics
+ $frames = 0;
+ $delta_time = 0;
+ }
- if ( $frames < 10 ) {
- $frames++;
- $delta_time += $end - $start;
- }
- else {
- $FPS = int( ( $frames * 100 ) / $delta_time );
- $frames = 0;
- $delta_time = 0;
}
-}
+ while ( !$quit ) {
+
+ # Get the time for the starting of the frame
+ $start = SDL::get_ticks();
-while ( !$quit ) {
+ get_events();
- $start = SDL::get_ticks();
+ # If we are fixing the lower bounds of the frame rate
+ if ( $ARGV[1] ) {
- get_events();
+ # And our delta time is going too slow for frame check
+ if ( $delta_time > $fps_check ) {
- if ( $ARGV[1] ) {
- if ( $delta_time > $fps_check ) {
+ # Calculate our FPS from this
+ calculate_fps_at_frame_end();
- calculate_fps_at_frame_end();
- next;
+ # Skip rendering and collision detections
+ # The heavy functions in the game loop
+ next;
+
+ }
}
- }
- calculate_next_positions();
- render();
+ calculate_next_positions();
+ render();
- calculate_fps_at_frame_end();
+ # A normal frame with rendering actually performed
+ calculate_fps_at_frame_end();
- if ( $ARGV[0] ) {
- if ( $delta_time < $fps_check ) {
- SDL::delay( $fps_check - $delta_time );
+ # if we are fixing the upper bounds of the frame rate
+ if ( $ARGV[0] ) {
+
+ # and our delta time is going too fast compared to the frame check
+ if ( $delta_time < $fps_check ) {
+
+ # delay for the difference
+ SDL::delay( $fps_check - $delta_time );
+ }
}
- }
-}
+ }
View
12 code_listings/game_loop_simple.pl
@@ -15,8 +15,8 @@
#Don't need to quit yet
my $quit = 0;
- #Start lazer on the left
- my $lazer = 0;
+ #Start laser on the left
+ my $laser = 0;
sub get_events {
@@ -31,8 +31,8 @@
}
sub calculate_next_positions {
- $lazer++;
- $lazer = 0 if $lazer > $app->w();
+ $laser++;
+ $laser = 0 if $laser > $app->w();
}
sub render {
@@ -40,8 +40,8 @@
#Draw the background first
$app->draw_rect( [ 0, 0, $app->w, $app->h ], 0 );
- #Draw the lazer
- $app->draw_rect( [ $lazer, $app->h / 2, 10, 2 ], [ 255, 0, 0, 255 ] );
+ #Draw the laser
+ $app->draw_rect( [ $laser, $app->h / 2, 10, 2 ], [ 255, 0, 0, 255 ] );
$app->update();
View
30 src/04-game.pod
@@ -34,8 +34,8 @@ A practical example of this is a moving Lazer.
#Don't need to quit yet
my $quit = 0;
- #Start lazer on the left
- my $lazer = 0;
+ #Start laser on the left
+ my $laser = 0;
sub get_events{
my $event = SDL::Event->new();
@@ -50,21 +50,27 @@ A practical example of this is a moving Lazer.
}
sub calculate_next_positions{
- $lazer++;
+ # Move the Lazer over
+ $laser++;
+ # If the laser goes off the screen bring it back
+ $laser = 0 if $laser > $app->w();
+
}
sub render {
#Draw the background first
- $app->draw( [0,0,$app->w, $app->h], 0 );
+ $app->draw_rect( [0,0,$app->w, $app->h], 0 );
- #Draw the lazer
- $app->draw( [$lazer, $app->h/2, 10, 2], [255,0,0,255]);
+ #Draw the laser, in the middle height of the screen
+ $app->draw_rect( [$laser, $app->h/2, 10, 2], [255,0,0,255]);
$app->update();
}
+
+ # Until we quit stay looping
while(!$quit)
{
get_events();
@@ -132,8 +138,8 @@ read on to the problems below.
#Don't need to quit yet
my $quit = 0;
- #Start lazer on the left
- my $lazer = 0;
+ #Start laser on the left
+ my $laser = 0;
sub get_events {
@@ -148,9 +154,9 @@ read on to the problems below.
}
sub calculate_next_positions {
- $lazer++;
+ $laser++;
- $lazer = 0 if $lazer > $app->w;
+ $laser = 0 if $laser > $app->w;
}
sub render {
@@ -158,8 +164,8 @@ read on to the problems below.
#Draw the background first
$app->draw_rect( [ 0, 0, $app->w, $app->h ], 0 );
- #Draw the lazer
- $app->draw_rect( [ $lazer, $app->h / 2, 10, 2 ], [ 255, 0, 0, 255 ] );
+ #Draw the laser
+ $app->draw_rect( [ $laser, $app->h / 2, 10, 2 ], [ 255, 0, 0, 255 ] );
#Draw our FPS on the screen so we can see
$app->draw_gfx_text( [ 10, 10 ], [ 255, 0, 255, 255 ], "FPS: $FPS" );
Please sign in to comment.
Something went wrong with that request. Please try again.