Skip to content

Instantly share code, notes, and snippets.

@plainspooky
Last active October 18, 2015 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plainspooky/dad05d191f74568f2d2e to your computer and use it in GitHub Desktop.
Save plainspooky/dad05d191f74568f2d2e to your computer and use it in GitHub Desktop.
This is a simple animation test program written in Perl with SDL.
#!/usr/bin/perl
use SDL;
use SDLx::App;
use SDL::Rect;
use SDL::Image;
use SDL::Event;
use SDL::Events;
use strict;
use warnings;
use constant STARS => 8;
use constant FRAMES => 6;
use constant W => 64;
use constant H => 64;
use constant MAX_X => 640-64;
use constant MAX_Y => 480-64;
my ( $i, $j, $k, $l, $mask );
my $events = SDL::Event->new();
my $app = SDLx::App->new(
title => "animation test in Perl/SDL",
width => 640,
height => 480,
depth => 32,
fullscreen => 0,
);
# download the images in:
# https://giovannireisnunes.files.wordpress.com/2015/10/animation_grid.png
# https://giovannireisnunes.files.wordpress.com/2015/10/animation_stars.png
my $background = SDL::Image::load('animation_grid.png');
my $background_rect = SDL::Rect->new(0,0,640,480);
my $images = SDL::Image::load('animation_stars.png');
my @shapes = ();
for ( $i=0; $i<FRAMES; $i++ ){
push @shapes, SDL::Rect->new(W*$i,0,W,H);
}
my @stars = ();
for ( $i=0; $i<STARS; $i++){
push @stars, { x=> int(MAX_X*rand(1)),
y=> int(MAX_Y*rand(1)),
ix=> 2+2*int(3*rand(1)),
iy=> 2+2*int(3*rand(1)),
f=> int(FRAMES*rand(1))
};
}
my $repeat=1;
sleep 4;
while ( $repeat ) {
SDL::Events::pump_events();
if ( SDL::Events::poll_event($events) ) {
$repeat=0 if $events->type == SDL_KEYDOWN;
}
SDL::Video::blit_surface($background, $background_rect, $app, $background_rect );
for ( $i=0; $i<STARS; $i++ ) {
$mask = SDL::Rect->new($stars[$i]{x},$stars[$i]{y},W,H);
SDL::Video::blit_surface($images, $shapes[$stars[$i]{f}], $app, $mask);
$stars[$i]{x} += $stars[$i]{ix};
$stars[$i]{y} += $stars[$i]{iy};
$stars[$i]{ix} = -$stars[$i]{ix} if ( $stars[$i]{x}>MAX_X or $stars[$i]{x}<0 );
$stars[$i]{iy} = -$stars[$i]{iy} if ( $stars[$i]{y}>MAX_Y or $stars[$i]{y}<0 );
$stars[$i]{f}++;
$stars[$i]{f}=0 if $stars[$i]{f}==FRAMES;
}
SDL::Video::update_rects($app, $background_rect);
$app->delay(30);
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment