Skip to content

Commit

Permalink
Added more comments to the example and another particle
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Thakore committed Aug 15, 2010
1 parent c726e5c commit 4e5e4fc
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions examples/SDLx/SDLx_C_Object.pl
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,61 @@
use SDL::Events;

use SDLx::Controller::Object;
my $app = SDLx::App->new( w => 200, h => 200, title => "timestep" );
my $app = SDLx::App->new( w => 200, h => 200, title => "timestep" );

#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
my $constant = SDLx::Controller::Object->new( x => 0, y => 20, v_x => 20 );

my $spring = SDLx::Controller::Object->new(x=> 100, y => 100);
#NO need to send an acceleration for x,y or rotation
$constant->set_acceleration( sub { return ( 0, 0, 0 ) } );

#a hooke's law acceleration for the spring
my $accel = sub {
my ( $t, $state ) = @_;
my $k = 10;
my $b = 1;
my $ax = ( ( -1 * $k ) * ($state->x) - $b * $state->v_x );

return($ax,0,0)
};
$spring->set_acceleration( $accel);


my $event = sub
{
return 0 if $_[0]->type == SDL_QUIT;
return 1;
my ( $t, $state ) = @_;
my $k = 10;
my $b = 1;
my $ax = ( ( -1 * $k ) * ( $state->x ) - $b * $state->v_x );

return ( $ax, 0, 0 );
};
$spring->set_acceleration($accel);

my $render = sub
{
my $state = shift;
$app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x0 );
$app->draw_rect( [ 100 - $state->x , $state->y, 2, 2 ], 0xFF0FFF );
$app->update();
#This is how we will render the spring. Notice the x, and y are not tied to how they will show on the screen
my $render = sub {
my $state = shift;
$app->draw_rect( [ 100 - $state->x, $state->y, 2, 2 ], 0xFF0FFF );
};

#an event handler to exit
my $event = sub {
return 0 if $_[0]->type == SDL_QUIT;
return 1;
};


$app->add_event_handler($event);

#clear the screen
$app->add_show_handler( sub{ $app->draw_rect([0,0,$app->w, $app->h], 0x000000)} );

#add the spring
$app->add_object( $spring, $render );

#add the constant_velocity
$app->add_object(
$constant,
sub {
my $state = shift;
$app->draw_rect( [ $state->x, $state->y, 4, 4 ], 0xFFFFFF );
}
);

#add the final update
$app->add_show_handler( sub{ $app->update() } );

$app->run_test();


Expand Down

0 comments on commit 4e5e4fc

Please sign in to comment.