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

In reply to SDL Console Text Box by shotgunefx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.