Game
Development
With Perl and SDL



       - press start -
Perl
SDL
Perl
SDL
Perl
SDL
Simple DirectMedia Layer
low level access
Video
Audio
Input Devices
cross-platform
Multimedia App


                    libSDL


DirectX   GDI    framebuffer Xlib   Quartz

                                             ...
MS Windows            Linux         OS X
LGPL v2
Round 1   . . .




 F IGH T          !
1:1 API
use   strict;
use   warnings;
use   SDL;
use   SDL::Video;
use   SDL::Surface;
use   SDL::Rect;

SDL::init(SDL_INIT_VIDEO);
my ($screen_w, $screen_h) = (800, 600);
my $screen = SDL::Video::set_video_mode(
                $screen_w,
                $screen_h,
                32,
                SDL_ANYFORMAT
);

my $blue = SDL::Video::map_RGB($screen->format(), 0, 0, 255);
SDL::Video::fill_rect(
              $screen,
              SDL::Rect->new(15, 15, 100, 100),
              $blue
);
SDL::Video::update_rect($screen, 0, 0, $screen_w, $screen_h);

sleep 5; # so we have time to see!
Too low level
Not Perlish at all!
SDLx::* Sugar
use strict;
use warnings;

use SDLx::App;

my $app = SDLx::App->new(
        width => 800,
        height => 600,
);

$app->draw_rect(
        [15, 15, 100, 100], # rect
        [0,0,255,255],      # blue
);

$app->update;

sleep 5; # so we have time to see!
The Game Loop
while ( $running ) {
    get_events();
    update_game();
    render_scene();
}
while( $running ) {
    $loops = 0;
    while(
           get_tick_count() > $next_game_tick
           && $loops < $MAX_FRAMESKIP
    ) {
        get_events();
        update_game();

        $next_game_tick += $SKIP_TICKS;
        $loops++;
    }
    $interpolation = (     get_tick_count()
                         + $SKIP_TICKS - $next_game_tick
                     )
                     / $SKIP_TICKS;

    render_scene( $interpolation );
}
SDLx::App->run;
use SDLx::App;

my $app = SDLx::App->new;

$app->add_move_handler( &update );

$app->add_event_handler( &player_1 );
$app->add_event_handler( &player_2 );

$app->add_show_handler( &scene );

$app->run;
Avenger
Avenger
sugary syntax
use Avenger title => 'My Game';

update { ... };

show { ... };

event 'key_down' => sub { ... };

event 'mouse_left' => sub { ... };

start;
Box2D
integration
use Avenger title => 'My Game';
world->gravity( 0, -100 );

my $player = world->create_dynamic( x => 30, y => 100 );
my $floor = world->create_static(
                y => 50,
                w => app->w -100 ,
                h => 10,
);

update { world->update };

show {
    app->clear( [0, 0, 0, 255] );
    app->draw_rect( $floor->rect, [0, 255, 0, 255] );
    app->draw_rect( $player->rect, [255, 50, 0, 255] );
    app->update;
};

start;
Simple games, easy.
Complex games, possible!
use Avenger;
use MyGame;

start 'MainScreen';
package MyGame::MainScreen;
use Avenger;

load { ... };

unload { ... };

update { ... };

event { ... };

show { ... };

1;
Other SDLx Goodies
my $menu = SDLx::Widget::Menu->new;

$menu->items(
    'New Game'    =>   sub   {   ...   },
    'Load Game'   =>   sub   {   ...   },
    'Options'     =>   sub   {   ...   },
    'Quit'        =>   sub   {   ...   },
);

show { $menu->render( app ) };
my $text = SDLx::Text->new(
    font    => '/path/to/my/font.ttf',
    color   => 'white',
    h_align => 'center',
    shadow => 1,
    bold    => 1,
);

$text->write_to(
   app,
   'All your base are belong to us.'
);
my $music = SDLx::Music->new;

$music->data(
   intro => {
      loops   => 3,
      fade_in => 0.5,
      volume => 72,
   },
   gameover => {
      finished => sub { print 'Done!' },
   },
);

$music->play( 'intro' );
my $sprite = SDLx::Sprite::Animated->new;

$sprite->load( 'hero.png' )->start;

show { $sprite->draw( app ) };
HUGE TODO LIST!
github.com/PerlGameDev


    #sdl   (irc.perl.org)


sdl-devel-subscribe@perl.org


           @SDLPerl
Thank You
For Playing !

  garu@cpan.org
     @garu_rj

Game Development with SDL and Perl

  • 1.
    Game Development With Perl andSDL - press start -
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
    Multimedia App libSDL DirectX GDI framebuffer Xlib Quartz ... MS Windows Linux OS X
  • 12.
  • 13.
    Round 1 . . . F IGH T !
  • 14.
  • 15.
    use strict; use warnings; use SDL; use SDL::Video; use SDL::Surface; use SDL::Rect; SDL::init(SDL_INIT_VIDEO); my ($screen_w, $screen_h) = (800, 600); my $screen = SDL::Video::set_video_mode( $screen_w, $screen_h, 32, SDL_ANYFORMAT ); my $blue = SDL::Video::map_RGB($screen->format(), 0, 0, 255); SDL::Video::fill_rect( $screen, SDL::Rect->new(15, 15, 100, 100), $blue ); SDL::Video::update_rect($screen, 0, 0, $screen_w, $screen_h); sleep 5; # so we have time to see!
  • 16.
  • 17.
  • 18.
  • 19.
    use strict; use warnings; useSDLx::App; my $app = SDLx::App->new( width => 800, height => 600, ); $app->draw_rect( [15, 15, 100, 100], # rect [0,0,255,255], # blue ); $app->update; sleep 5; # so we have time to see!
  • 20.
  • 21.
    while ( $running) { get_events(); update_game(); render_scene(); }
  • 22.
    while( $running ){ $loops = 0; while( get_tick_count() > $next_game_tick && $loops < $MAX_FRAMESKIP ) { get_events(); update_game(); $next_game_tick += $SKIP_TICKS; $loops++; } $interpolation = ( get_tick_count() + $SKIP_TICKS - $next_game_tick ) / $SKIP_TICKS; render_scene( $interpolation ); }
  • 24.
  • 25.
    use SDLx::App; my $app= SDLx::App->new; $app->add_move_handler( &update ); $app->add_event_handler( &player_1 ); $app->add_event_handler( &player_2 ); $app->add_show_handler( &scene ); $app->run;
  • 26.
  • 27.
  • 28.
  • 29.
    use Avenger title=> 'My Game'; update { ... }; show { ... }; event 'key_down' => sub { ... }; event 'mouse_left' => sub { ... }; start;
  • 30.
  • 31.
    use Avenger title=> 'My Game'; world->gravity( 0, -100 ); my $player = world->create_dynamic( x => 30, y => 100 ); my $floor = world->create_static( y => 50, w => app->w -100 , h => 10, ); update { world->update }; show { app->clear( [0, 0, 0, 255] ); app->draw_rect( $floor->rect, [0, 255, 0, 255] ); app->draw_rect( $player->rect, [255, 50, 0, 255] ); app->update; }; start;
  • 32.
  • 33.
  • 34.
    package MyGame::MainScreen; use Avenger; load{ ... }; unload { ... }; update { ... }; event { ... }; show { ... }; 1;
  • 35.
  • 36.
    my $menu =SDLx::Widget::Menu->new; $menu->items( 'New Game' => sub { ... }, 'Load Game' => sub { ... }, 'Options' => sub { ... }, 'Quit' => sub { ... }, ); show { $menu->render( app ) };
  • 37.
    my $text =SDLx::Text->new( font => '/path/to/my/font.ttf', color => 'white', h_align => 'center', shadow => 1, bold => 1, ); $text->write_to( app, 'All your base are belong to us.' );
  • 38.
    my $music =SDLx::Music->new; $music->data( intro => { loops => 3, fade_in => 0.5, volume => 72, }, gameover => { finished => sub { print 'Done!' }, }, ); $music->play( 'intro' );
  • 39.
    my $sprite =SDLx::Sprite::Animated->new; $sprite->load( 'hero.png' )->start; show { $sprite->draw( app ) };
  • 40.
  • 41.
    github.com/PerlGameDev #sdl (irc.perl.org) sdl-devel-subscribe@perl.org @SDLPerl
  • 42.
    Thank You For Playing! garu@cpan.org @garu_rj