PageRenderTime 29ms CodeModel.GetById 16ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/SDLx/SDLx_C_Interface.pl

http://github.com/PerlGameDev/SDL
Perl | 65 lines | 38 code | 17 blank | 10 comment | 2 complexity | 0f72f2091765c164fed76ea0b427a2b0 MD5 | raw file
 1use strict;
 2use warnings;
 3use Time::HiRes qw( time sleep );
 4use SDL;
 5use SDLx::App;
 6use SDL::Event;
 7use SDL::Events;
 8
 9use SDLx::Controller::Interface;
10my $app = SDLx::App->new( w => 200, h => 200, title => "timestep", delay => 10 );
11
12#The initial x and y for this object.
13my $spring = SDLx::Controller::Interface->new( x => 100, y => 100 );
14
15#we have a constant x velocity of 20
16my $constant = SDLx::Controller::Interface->new( x => 0, y => 20, v_x => 20 );
17
18#NO need to send an acceleration for x,y or rotation
19$constant->set_acceleration( sub { return ( 0, 0, 0 ) } );
20
21#a hooke's law acceleration for the spring
22my $accel = sub {
23	my ( $t, $state ) = @_;
24	my $k  = 10;
25	my $b  = 1;
26	my $ax = ( ( -1 * $k ) * ( $state->x ) - $b * $state->v_x );
27	return ( $ax, 0, 0 );
28};
29$spring->set_acceleration($accel);
30
31#This is how we will render the spring. Notice the x, and y are not tied to how they will show on the screen
32my $render = sub {
33	my $state = shift;
34	$app->draw_rect( [ 100 - $state->x, $state->y, 2, 2 ], 0xFF0FFF );
35};
36
37#an event handler to exit
38my $event = sub {
39	$_[1]->stop if $_[0]->type == SDL_QUIT;
40};
41
42
43$app->add_event_handler($event);
44
45#clear the screen
46$app->add_show_handler( sub { $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x000000 ) } );
47
48#add the spring
49$spring->attach($app, $render );
50
51#add the constant_velocity
52$constant->attach( $app,
53	sub {
54		my $state = shift;
55		$app->draw_rect( [ $state->x, $state->y, 4, 4 ], 0xFFFFFF );
56	}
57);
58
59#add the final update
60$app->add_show_handler( sub { $app->update() } );
61
62$app->run();
63
64
65