Permalink
Browse files

Tidied the code

  • Loading branch information...
kthakore
kthakore committed Aug 31, 2010
1 parent 64791fa commit 36a0ca5c0066b7c397dd73ab825d42d7a19aba51
View
1,074 Build.PL

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -9,7 +9,7 @@
use SDLx::Controller::Object;
my $app = SDLx::App->new( w => 200, h => 200, title => "timestep" );
-#The initial x and y for this object.
+#The initial x and y for this object.
my $spring = SDLx::Controller::Object->new( x => 100, y => 100 );
#we have a constant x velocity of 20
@@ -24,7 +24,7 @@
my $k = 10;
my $b = 1;
my $ax = ( ( -1 * $k ) * ( $state->x ) - $b * $state->v_x );
-
+
return ( $ax, 0, 0 );
};
$spring->set_acceleration($accel);
@@ -45,7 +45,7 @@
$app->add_event_handler($event);
#clear the screen
-$app->add_show_handler( sub{ $app->draw_rect([0,0,$app->w, $app->h], 0x000000)} );
+$app->add_show_handler( sub { $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x000000 ) } );
#add the spring
$app->add_object( $spring, $render );
@@ -55,12 +55,12 @@
$constant,
sub {
my $state = shift;
- $app->draw_rect( [ $state->x, $state->y, 4, 4 ], 0xFFFFFF );
+ $app->draw_rect( [ $state->x, $state->y, 4, 4 ], 0xFFFFFF );
}
);
#add the final update
-$app->add_show_handler( sub{ $app->update() } );
+$app->add_show_handler( sub { $app->update() } );
$app->run_test();
@@ -20,116 +20,120 @@
use SDLx::FPS;
SDL::init(SDL_INIT_VIDEO);
-my $display = SDL::Video::set_video_mode(800, 600, 32, SDL_HWSURFACE | SDL_HWACCEL);
+my $display = SDL::Video::set_video_mode( 800, 600, 32, SDL_HWSURFACE | SDL_HWACCEL );
my $layers = SDLx::LayerManager->new();
my $event = SDL::Event->new();
my $loop = 1;
my $last_click = Time::HiRes::time;
-my $fps = SDLx::FPS->new(fps => 60);
+my $fps = SDLx::FPS->new( fps => 60 );
my @selected_cards = ();
my $left_mouse_down = 0;
my @rects = ();
init_surfaces();
$layers->blit($display);
-SDL::Video::update_rect($display, 0, 0, 0, 0);
+SDL::Video::update_rect( $display, 0, 0, 0, 0 );
game();
-sub event_loop
-{
- my $handler = shift;
-
- SDL::Events::pump_events();
- while(SDL::Events::poll_event($event))
- {
- $left_mouse_down = 1 if $event->type == SDL_MOUSEBUTTONDOWN && $event->button_button == SDL_BUTTON_LEFT;
- $left_mouse_down = 0 if $event->type == SDL_MOUSEBUTTONUP && $event->button_button == SDL_BUTTON_LEFT;
-
- $handler->{on_quit}->() if defined $handler->{on_quit} && ($event->type == SDL_QUIT || ($event->type == SDL_KEYDOWN && $event->key_sym == SDLK_ESCAPE));
- $handler->{on_drop}->() if defined $handler->{on_drop} && $event->type == SDL_MOUSEBUTTONUP;
- $handler->{on_click}->() if defined $handler->{on_click} && $event->type == SDL_MOUSEBUTTONDOWN && Time::HiRes::time - $last_click >= 0.3;
-
- $last_click = Time::HiRes::time if $event->type == SDL_MOUSEBUTTONDOWN;
- }
+sub event_loop {
+ my $handler = shift;
+
+ SDL::Events::pump_events();
+ while ( SDL::Events::poll_event($event) ) {
+ $left_mouse_down = 1 if $event->type == SDL_MOUSEBUTTONDOWN && $event->button_button == SDL_BUTTON_LEFT;
+ $left_mouse_down = 0 if $event->type == SDL_MOUSEBUTTONUP && $event->button_button == SDL_BUTTON_LEFT;
+
+ $handler->{on_quit}->()
+ if defined $handler->{on_quit}
+ && ( $event->type == SDL_QUIT || ( $event->type == SDL_KEYDOWN && $event->key_sym == SDLK_ESCAPE ) );
+ $handler->{on_drop}->() if defined $handler->{on_drop} && $event->type == SDL_MOUSEBUTTONUP;
+ $handler->{on_click}->()
+ if defined $handler->{on_click}
+ && $event->type == SDL_MOUSEBUTTONDOWN
+ && Time::HiRes::time- $last_click >= 0.3;
+
+ $last_click = Time::HiRes::time if $event->type == SDL_MOUSEBUTTONDOWN;
+ }
}
-sub game
-{
- my @selected_cards = ();
- my $x = 0;
- my $y = 0;
- my $handler = {
- on_quit => sub {
- $loop = 0;
- },
- on_drop => sub {
- if(scalar @selected_cards) {
- my $layer = $layers->by_position($event->button_x, $event->button_y);
-
- if(defined $layer) {
- my @behind = @{$layer->behind};
- if(scalar @behind == 1
- && $behind[0]->data->{id} =~ m/^\w{2}$/) {
- $layer->foreground;
- $layers->detach_xy($behind[0]->pos->x + 8, $behind[0]->pos->y + 8);
- printf("to %s\n", $behind[0]->data->{id});
- }
- else {
- $layers->detach_back;
- }
- }
- else {
- $layers->detach_back;
- }
- }
- @selected_cards = ();
- },
- on_click => sub {
- unless(scalar @selected_cards) {
- my $layer = $layers->by_position($event->button_x, $event->button_y);
-
- if(defined $layer
- && $layer->data->{id} =~ m/^(white|black)$/) {
- @selected_cards = ($layer);
- $layers->attach($layer, $event->button_x, $event->button_y);
- }
- }
- },
- };
-
- while($loop) {
- event_loop($handler);
- @rects = @{$layers->blit($display)};
- SDL::Video::update_rect($display, 0, 0, 0, 0) if scalar @rects;
- $fps->delay;
- }
+sub game {
+ my @selected_cards = ();
+ my $x = 0;
+ my $y = 0;
+ my $handler = {
+ on_quit => sub {
+ $loop = 0;
+ },
+ on_drop => sub {
+ if ( scalar @selected_cards ) {
+ my $layer = $layers->by_position( $event->button_x, $event->button_y );
+
+ if ( defined $layer ) {
+ my @behind = @{ $layer->behind };
+ if ( scalar @behind == 1
+ && $behind[0]->data->{id} =~ m/^\w{2}$/ )
+ {
+ $layer->foreground;
+ $layers->detach_xy( $behind[0]->pos->x + 8, $behind[0]->pos->y + 8 );
+ printf( "to %s\n", $behind[0]->data->{id} );
+ } else {
+ $layers->detach_back;
+ }
+ } else {
+ $layers->detach_back;
+ }
+ }
+ @selected_cards = ();
+ },
+ on_click => sub {
+ unless ( scalar @selected_cards ) {
+ my $layer = $layers->by_position( $event->button_x, $event->button_y );
+
+ if ( defined $layer
+ && $layer->data->{id} =~ m/^(white|black)$/ )
+ {
+ @selected_cards = ($layer);
+ $layers->attach( $layer, $event->button_x, $event->button_y );
+ }
+ }
+ },
+ };
+
+ while ($loop) {
+ event_loop($handler);
+ @rects = @{ $layers->blit($display) };
+ SDL::Video::update_rect( $display, 0, 0, 0, 0 ) if scalar @rects;
+ $fps->delay;
+ }
}
sub init_surfaces {
- my $white_surface = SDL::Image::load('test/data/wood_light.png');
- my $black_surface = SDL::Image::load('test/data/wood_dark.png');
- my $white_button = SDL::Image::load('test/data/button_light.png');
- my $black_button = SDL::Image::load('test/data/button_dark.png');
- for my $x (0..7) {
- for my $y (0..7) {
- $layers->add(SDLx::Layer->new(
- ($x + $y) & 1 ? $white_surface : $black_surface,
- 144 + 64 * $x,
- 44 + 64 * $y,
- {id => chr(ord('A') + $x) . ($y + 1)}
- ));
- }
- }
-
- for my $x (0..7) {
- for my $y (0..7) {
- if($y < 2) {
- $layers->add(SDLx::Layer->new($white_button, 152 + 64 * $x, 52 + 64 * $y, {id => 'white'}));
- }
-
- if($y > 5) {
- $layers->add(SDLx::Layer->new($black_button, 152 + 64 * $x, 52 + 64 * $y, {id => 'black'}));
- }
- }
- }
+ my $white_surface = SDL::Image::load('test/data/wood_light.png');
+ my $black_surface = SDL::Image::load('test/data/wood_dark.png');
+ my $white_button = SDL::Image::load('test/data/button_light.png');
+ my $black_button = SDL::Image::load('test/data/button_dark.png');
+ for my $x ( 0 .. 7 ) {
+ for my $y ( 0 .. 7 ) {
+ $layers->add(
+ SDLx::Layer->new(
+ ( $x + $y ) & 1 ? $white_surface : $black_surface,
+ 144 + 64 * $x,
+ 44 + 64 * $y,
+ { id => chr( ord('A') + $x ) . ( $y + 1 ) }
+ )
+ );
+ }
+ }
+
+ for my $x ( 0 .. 7 ) {
+ for my $y ( 0 .. 7 ) {
+ if ( $y < 2 ) {
+ $layers->add( SDLx::Layer->new( $white_button, 152 + 64 * $x, 52 + 64 * $y, { id => 'white' } ) );
+ }
+
+ if ( $y > 5 ) {
+ $layers->add( SDLx::Layer->new( $black_button, 152 + 64 * $x, 52 + 64 * $y, { id => 'black' } ) );
+ }
+ }
+ }
}
@@ -61,7 +61,7 @@ sub init {
my $game = SDLx::Controller->new();
sub on_move {
- my $t = shift;
+ my $t = shift;
my $dt = shift;
$dt = $dt / 1000;
$ball->{x} += $ball->{x_vel} * $dt;
View
@@ -90,7 +90,7 @@ sub init {
my $game = SDLx::Controller->new( dt => 0.1 );
sub on_move {
- my $t = shift;
+ my $t = shift;
my $dt = shift;
$dt = $dt / 1000;
paddle_confine( $paddle, $dt, $app->h );
View
@@ -161,7 +161,7 @@ sub ACTION_build {
${ Alien::SDL::ConfigData->config('build_params') }{'title'} || 'n.a.'
);
$self->SUPER::ACTION_build;
- $self->ACTION_bundle;
+ $self->ACTION_bundle;
}
# both special to MacOS/Darwin, somebody should review whether it is still necessary
View
@@ -18,7 +18,7 @@ sub build_bundle {
my $bundle_contents = "SDLPerl.app/Contents";
system "mkdir -p \"$bundle_contents\"";
mkdir "$bundle_contents/MacOS", 0755;
- my $Perl = ($ENV{'FULLPERL'} or $^X or 'perl') ;
+ my $Perl = ( $ENV{'FULLPERL'} or $^X or 'perl' );
my $cflags; #= Alien::SDL->config('cflags');
$cflags = ' ' . `$Perl -MExtUtils::Embed -e ccopts`;
chomp($cflags);
View
@@ -10,6 +10,7 @@ use SDLx::Controller::Timer;
use Scalar::Util 'refaddr';
use SDLx::Controller::Object;
use SDLx::Controller::State;
+
# inside out, so this can work as the superclass of another
# SDL::Surface subclass
my %_delta;
@@ -73,27 +74,27 @@ sub run {
}
sub run_test {
- my $self = shift;
- my $quit = 0;
+ my $self = shift;
+ my $quit = 0;
my $t = 0.0;
my $dt = $_dt{ refaddr $self};
my $current_time = 0.0;
my $accumulator = 0.0;
- while ( !$_quit{ refaddr $self} ) {
+ while ( !$_quit{ refaddr $self} ) {
$self->_event;
- my $new_time = time;
- my $delta_time = $new_time - $current_time;
- $current_time = $new_time;
- $delta_time = 0.25 if ( $delta_time > 0.25 );
- $accumulator += $delta_time;
- while ( $accumulator >= $dt ) {
- $accumulator -= $dt;
- $self->_move( $dt, $t );
- $t += $dt;
- }
- $self->_show( $accumulator / $dt );
- }
+ my $new_time = time;
+ my $delta_time = $new_time - $current_time;
+ $current_time = $new_time;
+ $delta_time = 0.25 if ( $delta_time > 0.25 );
+ $accumulator += $delta_time;
+ while ( $accumulator >= $dt ) {
+ $accumulator -= $dt;
+ $self->_move( $dt, $t );
+ $t += $dt;
+ }
+ $self->_show( $accumulator / $dt );
+ }
}
@@ -112,9 +113,9 @@ sub _event {
sub _move {
my $self = shift;
my $delta_ticks = shift;
- my $t = shift;
+ my $t = shift;
foreach my $move_handler ( @{ $_move_handlers{ refaddr $self} } ) {
- $move_handler->($delta_ticks, $t);
+ $move_handler->( $delta_ticks, $t );
}
}
@@ -129,21 +130,18 @@ sub _show {
sub quit { $_quit{ refaddr $_[0] } = 1 }
sub add_object {
- my ($self, $obj, $render, @params ) = @_;
+ my ( $self, $obj, $render, @params ) = @_;
croak "Object is needed" unless $obj && $obj->isa('SDLx::Controller::Object');
- my $move = sub { $obj->update( $_[1], $_[0] ) } ;
- $self->add_move_handler( $move );
-
- if( $render )
- {
- my $show = sub { my $state = $obj->interpolate( $_[0] ); $render->($state, @params); };
- $self->add_show_handler( $show );
- }
- else
- {
- carp "Render callback not provide" ;
-
+ my $move = sub { $obj->update( $_[1], $_[0] ) };
+ $self->add_move_handler($move);
+
+ if ($render) {
+ my $show = sub { my $state = $obj->interpolate( $_[0] ); $render->( $state, @params ); };
+ $self->add_show_handler($show);
+ } else {
+ carp "Render callback not provide";
+
}
Oops, something went wrong.

0 comments on commit 36a0ca5

Please sign in to comment.