Skip to content

Instantly share code, notes, and snippets.

@awwaiid
Created March 10, 2010 02:59
Show Gist options
  • Save awwaiid/327467 to your computer and use it in GitHub Desktop.
Save awwaiid/327467 to your computer and use it in GitHub Desktop.
Coro+SDL Demo
#!/usr/bin/perl
use strict;
use SDL;
use SDL::App;
use SDL::Game::Rect;
use SDL::Event;
use SDL::Events;
use Coro;
use Coro::EV;
use AnyEvent;
my $app = SDL::App->new(
-title => 'rectangle',
-width => 640,
-height => 480,
);
my $rect = SDL::Rect->new( 0,0, $app->w, $app->h);
my $pixel_format = $app->format;
my $blue_pixel = SDL::Video::map_RGB( $pixel_format, 0x00, 0x00, 0xff );
my $red_pixel = SDL::Video::map_RGB( $pixel_format, 0xf0, 0x00, 0x33 );
my $green_pixel = SDL::Video::map_RGB( $pixel_format, 0x00, 0xf0, 0x33 );
# Initial background
SDL::Video::fill_rect( $app, $rect, $blue_pixel );
# Make an async box
sub make_box {
my ($speed, $color, $initial_x, $initial_y) = @_;
my $color = SDL::Video::map_RGB( $pixel_format, int rand 256, int rand 256, int rand 256 );
async {
my $grect = SDL::Game::Rect->new($initial_x, $initial_y, 10, 10);
my $x_direction = 1;
my $y_direction = 1;
while(1) {
#$grect = $grect->move($x_direction,$y_direction);
$grect->x($grect->x + $x_direction);
$grect->y($grect->y + $y_direction);
# print "X: " . $grect->x . " Y: " . $grect->y . " speed: $speed\n";
$x_direction = -1*$x_direction if $grect->x > 630 || $grect->x < 1;
$y_direction = -1*$y_direction if $grect->y > 470 || $grect->y < 1;
SDL::Video::fill_rect( $app, $grect, $color );
# SDL::Video::update_rect($app, 0, 0, 640, 480);
my $done = AnyEvent->condvar;
my $delay = AnyEvent->timer( after => $speed, cb => sub { $done->send; } );
$done->recv;
}
};
}
# Asyncronous code #3 -- watch for events
async {
my $event = SDL::Event->new();
print "Waiting for event...\n";
while(1) {
while( SDL::Events::poll_event($event) )
{
print "Got event!\n";
if($event->type == SDL_QUIT) {
print "All done!\n";
exit;
}
if($event->type == SDL_MOUSEBUTTONDOWN) {
print "New Box Time!\n";
my $tmp = $red_pixel;
$red_pixel = $green_pixel;
$green_pixel = $tmp;
make_box(0.01, $red_pixel, (int rand 630) + 1, (int rand 470) + 1);
}
# cede();
# my $done = AnyEvent->condvar;
# my $delay = AnyEvent->timer( after => 0.00000001, cb => sub { $done->send; cede();} );
# # print "... waiting ...\n";
# $done->recv;
}
# cede();
my $done = AnyEvent->condvar;
my $delay = AnyEvent->timer( after => 0.00000001, cb => sub { $done->send; cede();} );
$done->recv;
}
};
# Redraw the screen about 60 times a second
async {
while(1) {
my $done = AnyEvent->condvar;
my $delay = AnyEvent->timer( after => 0.033, cb => sub { $done->send; cede();} );
$done->recv;
SDL::Video::update_rect($app, 0, 0, 640, 480);
}
};
# Fast moving red box
#make_box(0.001, $red_pixel, 1, 1);
my $count = $ARGV[0] || 1;
make_box(0.001, $red_pixel, (int rand 630) + 1, (int rand 470) + 1) for 1..$count;
# Slow moving green
# make_box(0.1, $green_pixel, 50, 1);
print "Looping...\n";
EV::loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment