Download - Game Development with SDL and Perl

Transcript
Page 1: Game Development with SDL and Perl

Game DevelopmentWith Perl and SDL

- press start -

Page 2: Game Development with SDL and Perl

Perl

SDL

Page 3: Game Development with SDL and Perl

Perl

SDL

Page 4: Game Development with SDL and Perl

Perl

SDL

Page 5: Game Development with SDL and Perl

Simple DirectMedia Layer

Page 6: Game Development with SDL and Perl

low level access

Page 7: Game Development with SDL and Perl

Video

Page 8: Game Development with SDL and Perl

Audio

Page 9: Game Development with SDL and Perl

Input Devices

Page 10: Game Development with SDL and Perl

cross-platform

Page 11: Game Development with SDL and Perl

Multimedia App

libSDL

DirectX GDI

MS Windows

framebuffer Xlib

Linux

Quartz

OS X

...

Page 12: Game Development with SDL and Perl

LGPL v2

Page 13: Game Development with SDL and Perl

Round 1

F IGH T!. . .

Page 14: Game Development with SDL and Perl

1:1 API

Page 15: Game Development with SDL and Perl

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!

Page 16: Game Development with SDL and Perl

Too low level

Page 17: Game Development with SDL and Perl

Not Perlish at all!

Page 18: Game Development with SDL and Perl

SDLx::* Sugar

Page 19: Game Development with SDL and Perl

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!

Page 20: Game Development with SDL and Perl

The Game Loop

Page 21: Game Development with SDL and Perl

while ( $running ) { get_events(); update_game(); render_scene();}

Page 22: Game Development with SDL and Perl

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 );}

Page 23: Game Development with SDL and Perl
Page 24: Game Development with SDL and Perl

SDLx::App->run;

Page 25: Game Development with SDL and Perl

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;

Page 26: Game Development with SDL and Perl

Avenger

Page 27: Game Development with SDL and Perl

Avenger

Page 28: Game Development with SDL and Perl

sugary syntax

Page 29: Game Development with SDL and Perl

use Avenger title => 'My Game';

update { ... };

show { ... };

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

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

start;

Page 30: Game Development with SDL and Perl

Box2D

integration

Page 31: Game Development with SDL and Perl

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;

Page 32: Game Development with SDL and Perl

Simple games, easy.

Complex games, possible!

Page 33: Game Development with SDL and Perl

use Avenger;use MyGame;

start 'MainScreen';

Page 34: Game Development with SDL and Perl

package MyGame::MainScreen;use Avenger;

load { ... };

unload { ... };

update { ... };

event { ... };

show { ... };

1;

Page 35: Game Development with SDL and Perl

Other SDLx Goodies

Page 36: Game Development with SDL and Perl

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

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

show { $menu->render( app ) };

Page 37: Game Development with SDL and Perl

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.');

Page 38: Game Development with SDL and Perl

my $music = SDLx::Music->new;

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

$music->play( 'intro' );

Page 39: Game Development with SDL and Perl

my $sprite = SDLx::Sprite::Animated->new;

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

show { $sprite->draw( app ) };

Page 40: Game Development with SDL and Perl

HUGE TODO LIST!

Page 41: Game Development with SDL and Perl

github.com/PerlGameDev

#sdl (irc.perl.org)

[email protected]

@SDLPerl

Page 42: Game Development with SDL and Perl

Thank You

For Playing !

[email protected]

@garu_rj