Noting the lack of SDL::Console in the 2.x series, I decided to see whip up a sub to blit specified lines of an array of text to a specified location on screen. Truncating or continuing lines as specifed and needed.

Actually I see references to Games::Console::SDL, but nothing on CPAN.

This snippet also chokes perltidy v20031021.
Update 2006/07/24 Some version of SDL::TTFont have a bug with the width() method. Instead of returning the width, it now returns an array reference containing width and height. This causes an infinite loop as it tries to crop the line to fit the surface. Patch coming soon.
sub draw_text_box { my %args = ( -surface=> undef, # SDL Surface -font => undef , # SDL::TTFont -x => 0, # Start X -y => 0, # Start Y -w => 320, # Max Width -h => 200, # Max Height -text=> undef, # Array of text strings -startline=>0, # Start index of text strqings -linecount=>20, # Number of lines to display (line wrap might + effect this if enabled) -linewrap => 1, # Enable/Disable linewrap -breakchar=>">", # Char to display at word wrap or truncation @_ ); return unless defined ($args{-text}); foreach (qw/-x -y -w -h -startline -linecount/){ die "param [$_] not a number!" unless $args{$_} =~/^\d+$/; } my ($start,$end, $x,$y) = @args{ qw/-startline -linecount -x -y/ + }; my $fontheight = $args{-font}->height("abcDEF019"); # Might not +cover max height for all fonts my $combined_height = $args{-y} + $args{-h} - $fontheight; # Get + the total height my $wrap_height = defined $args{-breakchar} ? $args{-font}->width($args{-breakchar}) : 0 +; # break char width if any for (my $l = $start; $l < $start + $end; $l++){ my $text = $args{-text}->[$l]; my $textwidth = $args{-font}->width($text); # Get width of t +he rendered line if ($textwidth > 0){ # Skip if nothing to do if ( $textwidth < $args{-w}){ # Fits? Print it. $args{-font}->print($args{-surface},$x,$y,$text); }else{ # Wrap it. { last unless $text; # Nothing left, done. my $clen = length($text); # Get the length of curre +nt text my $break_pos = $textwidth - $wrap_height == 0 ? $clen : int( $clen * ( $args{-w} / ( +$textwidth- $wrap_height)) ); while( $args{-font}->width(substr($text,0,$break_pos)) > $args{-w} - $wrap_height and $break_pos ) { $break_pos --; } if ($clen > $break_pos){ # Breaks again $args{-font}->print($args{-surface},$x,$y,substr($te +xt,0,$break_pos,'').$args{-breakchar}) ; }else{ # Done $args{-font}->print($args{-surface},$x,$y,substr($te +xt,0,$break_pos,'')) ; last; } $textwidth = $args{-font}->width($text); # Done if past screen or truncating last if $y > $combined_height or ! $args{-linewrap}; $y+=$fontheight; redo; } } $y+=$fontheight; last if $y > $combined_height; } } }

Replies are listed 'Best First'.
Re: SDL Console Text Box
by zentara (Cardinal) on Feb 17, 2006 at 18:14 UTC
    Could you add a main section, to make a fully working example?

    I'm not really a human, but I play one on earth. flash japh
      Here you go. A hastily written example. Basically reads __DATA__ and the scrolls through the text back and forth. Press "ESC" to exit.
        It works, thanks.

        I'm not really a human, but I play one on earth. flash japh
      Sure. I'll try and get to it tonight.


      -Lee
      "To be civilized is to deny one's nature."
        Thanks, I tried to get it going, but my SDL isn't so good. I couldn't figure out how to pass args into the sub, kept getting errors.

        I'm not really a human, but I play one on earth. flash japh