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 the 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 current 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($text,0,$break_pos,'').$args{-breakchar}) ; }else{ # Done $args{-font}->print($args{-surface},$x,$y,substr($text,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; } } }