Skip to content

Instantly share code, notes, and snippets.

@awwaiid
Forked from kthakore/coroSDL.pl
Created March 9, 2010 15:08
Show Gist options
  • Save awwaiid/326678 to your computer and use it in GitHub Desktop.
Save awwaiid/326678 to your computer and use it in GitHub Desktop.
Err... see http://gist.github.com/327467 for the most up-to-date version
#!/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();
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 "COLOR SWAP TIME!\n";
my $tmp = $col_pixel;
$col_pixel = $hmm_pixel;
$hmm_pixel = $tmp;
}
cede();
}
cede();
}
};
print "Looping...\n";
EV::loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment