Hack #16. Interactive Graphical Apps
Paint pretty pictures with Perl.
People often see Perl as a general-purpose language: you start by using it to write short scripts, do administrative tasks, or text processing. If you happen to appreciate it, you end up enjoying its flexibility and power to perform almost anything that doesn't require the speed of compiled binaries.
Consider instead the number one requirement of games. Unless you're exclusively a fan of card games, you'd say "CPU power." Fortunately a crazy guy named David J. Goehrig had the mysterious idea to bind the functions of the C low-level graphical library SDL for the Perl language. The result is an object-oriented approach to SDL called sdlperl.
Blitting with SDL Perl
With SDL you will manipulate surfaces. These are rectangular images, and the most common operation is to copy one onto another; this is blitting.[2] To implement a basic image loader with SDL Perl in just four non-comment lines of code, write:
use SDL::App; # open a 640x480 window for your application our $app = SDL::App->new(-width => 640, -height => 480); # create a surface out of an image file specified on the command-line our $img = SDL::Surface->new( -name => $ARGV[0] ); # blit the surface onto the window of your application $img->blit( undef, $app, undef ); # flush all pending screen updates $app->flip( ); # sleep for 3 seconds to let the user view the image sleep 3;
You might wonder how to perform positioning and cropping during a blit. In the previous ...
Get Perl Hacks now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.