Meta::Widget::Gtk::Sprite is a Perl module to do C64 style sprites.
init Gnome "test.pl";
my $mw = new Gtk::Window( "toplevel" );
my($canvas) = Gnome::Canvas->new() ;
my $croot = $canvas->root;
my $sprites = new Meta::Widget::Gtk::Sprite($croot);
my $p1 = $sprites->create("./player1.xpm", 100, 0);
$sprites->slide_to_time($p1,5000, 100, 100);
my $p2 = $sprites->create("./player2.xpm", 0, 0);
$sprites->slide_to_speed($p2,10, 100, 100);
Sprite is a module to bring back the simple graphics programming of the C64 (hopefully without the lookslikearse component). You can declare pictures to be 'sprites' on the canvas, and then move them around and crash them into each other.
The canvas is the Gnome::Canvas object. You have to have a Gtk::Canvas object before starting Sprite.
new Meta::Widget::Gtk::Sprite( $canvas_root );
The new method takes one argument, the canvas root object for the canvas you want to draw on.
You may obtain the canvas root from your canvas like this:
my $croot = $canvas->root;
$sprite_number = $sprites->create("/path/to/filename", 10, 20);
Create will load an image file (right now, only xpm format) from disk and make a sprite out of it. The two numbers are the x and y position on the canvas.
$sprites->show( $sprite_number );
Makes the sprite appear on the canvas
$sprites->hide( $sprite_number );
Make the sprite picture disappear from the canvas. Note that it can still collide with other sprites. If you don't want it to hit anything, move it out of the way or ignore it in your own collision handler.
$sprites->destroy( $sprite_number );
Completely destroys a sprite.
$sprites->move_to( $sprite_number, 10, 20 );
Teleports the sprite named in $sprite_number to the position given immediately. Contrast slide_to_xxx functions.
$sprites->slide_to_time( $sprite_number, $time, 10, 20 );
Will make the sprite $sprite_number 'slide' across the canvas to the position 10, 20. It will take $time seconds to do so. Slow speeds will appear jerky.
$sprites->slide_to_speed( $sprite_number, $speed, 10, 20);
Will 'slide' the sprite $sprite_number to the position 10, 20. It will move at a speed of $speed pixels per second.
Returns the x and y coordinates of $sprite_number
$sprites->velocity( $sprite_number, 5, 6);
Sets the speed of $sprite_number. The numbers are the x and y speeds. Negative numbers will make the sprite go backwards.
$sprites->set_collision_handler ( &collision_handler );
Name a function that will be called when two sprites collide. Note that the collision detection system is extremely crappy right now. It turns out that it is very difficult to efficiently detect collisions.
Your function will be called like this:
collision_handler( $sprite_number, $sprite_number);
where the two sprite numbers are the two sprites that collided. Multiple sprites colliding will cause many collision handler callbacks.
Note well that if you set the collision handler Sprite.pm will check every single sprite for collisions every animation loop. I haven't optimised this, so you will notice a massive slowdown as you add more sprites.
To switch collisions checking off, set the handler to undef:
$sprites->set_collision_handler ( undef );