Skip to content

Instantly share code, notes, and snippets.

@kthakore
Created March 9, 2010 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kthakore/326668 to your computer and use it in GitHub Desktop.
Save kthakore/326668 to your computer and use it in GitHub Desktop.
#!/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 $col_pixel = SDL::Video::map_RGB( $pixel_format, 0xf0, 0x00, 0x33 );
my $hmm_pixel = SDL::Video::map_RGB( $pixel_format, 0x00, 0xf0, 0x33 );
# Initial background
SDL::Video::fill_rect( $app, $rect, $blue_pixel );
# Asyncronous code #1 -- slow growing red square
async {
my $grect = SDL::Game::Rect->new(10, 10, 30, 35);
foreach(0..80) {
$grect->x($_ );
$grect->centery($_ * 3);
$grect->size( ($_ / 40) * $_, ($_/38) * $_ );
SDL::Video::fill_rect( $app, $grect, $col_pixel );
SDL::Video::update_rect($app, 0, 0, 640, 480);
# SDL::delay(0); # Hmm... let stuff get drawn?
$grect = SDL::Game::Rect->new(10, 50, 30, 35);
$grect->x($_ );
$grect->centery($_ * 4);
$grect->size( ($_ / 40) * $_, ($_/38) * $_ );
SDL::Video::fill_rect( $app, $grect, $hmm_pixel );
SDL::Video::update_rect($app, 0, 0, 640, 480);
# SDL::delay(0); # Hmm... let stuff get drawn?
}
my $done = AnyEvent->condvar;
my $delay = AnyEvent->timer( after => 0.000001, cb => sub { $done->send; cede();} );
$done->recv;
};
# Asyncronous code #2 -- faster growing green square
async {
my $event = SDL::Event->new();
while( SDL::Events::poll_event($event) )
{
exit if $event->type == SDL_QUIT;
}
my $done = AnyEvent->condvar;
my $delay = AnyEvent->timer( after => 0.000001, cb => sub { $done->send; cede();} );
$done->recv;
};
print "Looping...\n";
EV::loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment