holli has asked for the wisdom of the Perl Monks concerning the following question:

fellow monks,

i stumbled about the SDL::Library because i want to write a screensaver in perl. the docs are somewhat taciturn. so i decided to create a simple hello world. i assembled this:

use SDL::App; use SDL::Rect; use SDL::Event; use SDL::Constants; use SDL::Tool::Font; my $app = SDL::App->new ( -width => 640, -height => 480, -depth => 16, ); my $rect = SDL::Rect->new ( -height => 100, -width => 100, -x => 270, -y => 390, ); my $f = SDL::Tool::Font->new( -ttfont=>"Courier", ); my %actions = ( SDL_QUIT() => sub { exit(0); }, SDL_KEYDOWN() => sub { $f->print ($rect, 0, 0, "Key Pressed"); }, ); $app->loop(\%actions);

From what i´ve read this should work, but when i run it perl crashes. The exit code is -1073741819.

my os is winxp, perl is 5.8.6, SDL-library is 2.1

Replies are listed 'Best First'.
Re: Hello world using SDL::App
by jbrugger (Parson) on Dec 31, 2004 at 19:53 UTC
    I tried your code, and it seems to work until you press a key. Then it complains: Tool::Font::print requires a SDL::Surface, that could be found on CPAN.
    Some issues, i had to copy my ttf to the same folder as the test-script, and call it like -ttfont=>"Arial.ttf"

    you might have a look at: SDL::Tutorial to get it to work (see example below)

    #!/usr/bin/perl
    use SDL::App;
    use SDL::Rect;
    use SDL::Color;
    use SDL::Event;
    use SDL::Constants;
    use SDL::Tool::Font;

    my $app = SDL::App->new(
    -width => 640,
    -height => 480,
    -depth => 16,
    -title => "My HelloWold Killer APP",
    );

    my $rect = SDL::Rect->new(
    -height => 100,
    -width => 100,
    -x => 270,
    -y => 390,
    );

    my $color = SDL::Color->new(
    -r => 0x00,
    -g => 0x00,
    -b => 0xff,
    );

    $app->fill( $rect, $color );
    $app->update( $rect );

    my $f = SDL::Tool::Font->new( -ttfont=>"Arial.ttf", );
    my %actions = (
    SDL_QUIT() => sub { exit(0); },
    # SDL_KEYDOWN() => sub { $f->print ($rect, 0, 0, "Key Pressed"); },
    );
    $app->loop(\%actions);
Re: Hello world using SDL::App
by jbrugger (Parson) on Jan 22, 2005 at 15:09 UTC
    Hmm.. I could not help it, but this example and the code below might help you a bit more:
    #!/usr/bin/perl -w use strict; use SDL::App; use SDL::Rect; use SDL::Color; use SDL::Event; my $app = SDL::App->new( -width => 640, -height => 480, -depth => 16, -title => 'My COOL Perl-SDL application.', ); my $rect = SDL::Rect->new( -height => 100, -width => 100, -x => 0, -y => 0, ); my $oldRect = SDL::Rect->new( -height => 100, -width => 100, -x => 0, -y => 0, ); my @color; $color[0] = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0x00, ); $color[1] = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00, ); $color[2] = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00, ); $color[3] = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff, ); $app->fill( $rect, $color[1] ); $app->update( $rect ); my $bgcolor = $color[0]; my $col = 1; my $fgcolor = $color[$col]; sub draw_frame { my (%args) = @_; $app->fill( $oldRect, $bgcolor); $app->fill( $rect, $fgcolor ); $app->update( $oldRect, $rect ); } sub move() { my ($x,$y) = @_; $rect->x( $x ); $rect->y( $y ); draw_frame(); $oldRect->x( $x ); $oldRect->y( $y ); } sub key { my $key = shift; my $name = $key->key_name; print "Key Pressed: $name\n"; my $x=$rect->x; my $y=$rect->y; if ($name eq "left") { $x -= 10; $x = 540 if $x < 0; &move($x,$y); } elsif ($name eq "right") { $x += 10; $x = 0 if $x > 540; &move($x,$y); } elsif ($name eq "up") { $y -= 10; $y = 380 if $y < 0; &move($x,$y); } elsif ($name eq "down") { $y += 10; $y = 0 if $y > 380; &move($x,$y); } elsif ($name eq "c") { $col++; $col = 1 if $col > 3; $fgcolor = $color[$col]; draw_frame(); } elsif ($name eq "q") { exit(0); } } my %actions = ( SDL_QUIT() => sub { exit(0); }, SDL_KEYDOWN() => \&key, ); $app->loop(\%actions);