Here you go. A hastily written example. Basically reads __DATA__ and the scrolls through the text back and forth. Press "ESC" to exit.
#!/usr/bin/perl use strict; use warnings; use SDL; use SDL::App; use SDL::Surface; use SDL::Event; use SDL::TTFont; my $count = 1; my @text; while (<DATA>){ chomp; push @text, $count++.": $_"; } my ($sl,$scount, $sinc)= (0,20,1); my ($tx,$ty,$tw,$th) = (10,20,300,200); # text box dimensions my $tex_rect = SDL::Rect->new(-x=>$tx,-y=>$ty,-w=>$tw,-h=>$th); my $TT_FONT = '/usr/share/fonts/truetype/verdana.ttf'; #Set to a valid + ttf font on your system my $app = new SDL::App( -title=>'Font test', -width=>800, -height=>600, -depth=>32, -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL, ); my $bgcolor = new SDL::Color(-r=>192, -g=>192, -b=>192); my $fgcolor = new SDL::Color(-r=>255, -g=>255, -b=>255); my $textcolor = new SDL::Color(-r=>0, -g=>0, -b=>100); my $font = new SDL::TTFont(-name=>$TT_FONT, -size=>10, -bg=>$textcolor +, -fg=>$fgcolor); my $actions = {}; &event_loop(); sub update_screen { $app->fill(0, $bgcolor); $app->fill($tex_rect, $textcolor); # Move the box within text back and forth if ($sl + $scount >= @text){ $sinc = -1; }elsif($sl <= 0){ $sinc = 1; } $sl+= $sinc; draw_text_box( -surface=>$app, -font=>$font, -x=>$tx, -y=>$ty, -w=>$tw, -h=>$th, -text=>\@text, -startline=>$sl, -linecount=>20, -linewrap=>1 ); $app->sync(); } sub draw_text_box { my %args = ( -surface=> undef, # SDL Surface -font => undef , # SDL::TTFont -x => 0, # Start X -y => 0, # Start Y -w => 100, # Max Width -h => 100, # Max Height -text=> undef, # Array of text strings -startline=>0, # Start index of text strqings -linecount=>100, # Number of lines to display (line wrap migh +t 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 $_ = [$args{$_}] not a valid range!" 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); $y+=$fontheight; # Done if past screen or truncating last if $y > $combined_height or ! $args{-linewrap}; redo; } } $y+=$fontheight; last if $y > $combined_height; } } } sub event_loop { my $event = new SDL::Event; MAIN_LOOP: while(1) { # Update Screen update_screen(); while ($event->poll) { my $type = $event->type(); last MAIN_LOOP if ($type == SDL_QUIT); last MAIN_LOOP if ($type == SDL_KEYDOWN && $event->key_nam +e() eq 'escape'); if ( exists($actions->{$type}) && ref( $actions->{$type} ) + eq "CODE" ) { $actions->{$type}->($event); } } $app->delay(100); } } __END__ Excerpt from "War of the Worlds" by H.G Wells A question of graver and universal interest is the possibility of another attack from the Martians. I do not think that nearly enough attention is being given to this aspect of the matter. At present the planet Mars is in conjunction, but with every return to opposition I, for one, anticipate a renewal of their adventure. In any case, we should be prepared. It seems to me that it should be possible to define the position of the gun from which the shots are discharged, to keep a sustained watch upon this part of the planet, and to anticipate the arrival of the next attack. In that case the cylinder might be destroyed with dynamite or artillery before it was sufficiently cool for the Martians to emerge, or they might be butchered by means of guns so soon as the screw opened. It seems to me that they have lost a vast advantage in the failure of their first surprise. Possibly they see it in the same light. Lessing has advanced excellent reasons for supposing that the Martians have actually succeeded in effecting a landing on the planet Venus. Seven months ago now, Venus and Mars were in alignment with the sun; that is to say, Mars was in opposition from the point of view of an observer on Venus. Subsequently a peculiar luminous and sinuous marking appeared on the unillumined half of the inner planet, and almost simultaneously a faint dark mark of a similar sinuous character was detected upon a photograph of the Martian disk. One needs to see the drawings of these appearances in order to appreciate fully their remarkable resemblance in character. At any rate, whether we expect another invasion or not, our views of the human future must be greatly modified by these events. We have learned now that we cannot regard this planet as being fenced in and a secure abiding place for Man; we can never anticipate the unseen good or evil that may come upon us suddenly out of space. It may be that in the larger design of the universe this invasion from Mars is not without its ultimate benefit for men; it has robbed us of that serene confidence in the future which is the most fruitful source of decadence, the gifts to human science it has brought are enormous, and it has done much to promote the conception of the commonweal of mankind. It may be that across the immensity of space the Martians have watched the fate of these pioneers of theirs and learned their lesson, and that on the planet Venus they have found a securer settlement. Be that as it may, for many years yet there will certainly be no relaxation of the eager scrutiny of the Martian disk, and those fiery darts of the sky, the shooting stars, will bring with them as they fall an unavoidable apprehension to all the sons of men. The broadening of men's views that has resulted can scarcely be exaggerated. Before the cylinder fell there was a general persuasion that through all the deep of space no life existed beyond the petty surface of our minute sphere. Now we see further. If the Martians can reach Venus, there is no reason to suppose that the thing is impossible for men, and when the slow cooling of the sun makes this earth uninhabitable, as at last it must do, it may be that the thread of life that has begun here will have streamed out and caught our sister planet within its toils. Dim and wonderful is the vision I have conjured up in my mind of life spreading slowly from this little seed bed of the solar system throughout the inanimate vastness of sidereal space. But that is a remote dream. It may be, on the other hand, that the destruction of the Martians is only a reprieve. To them, and not to us, perhaps, is the future ordained. I must confess the stress and danger of the time have left an abiding sense of doubt and insecurity in my mind. I sit in my study writing by lamplight, and suddenly I see again the healing valley below set with writhing flames, and feel the house behind and about me empty and desolate. I go out into the Byfleet Road, and vehicles pass me, a butcher boy in a cart, a cabful of visitors, a workman on a bicycle, children going to school, and suddenly they become vague and unreal, and I hurry again with the artilleryman through the hot, brooding silence. Of a night I see the black powder darkening the silent streets, and the contorted bodies shrouded in that layer; they rise upon me tattered and dog-bitten. They gibber and grow fiercer, paler, uglier, mad distortions of humanity at last, and I wake, cold and wretched, in the darkness of the night. I go to London and see the busy multitudes in Fleet Street and the Strand, and it comes across my mind that they are but the ghosts of the past, haunting the streets that I have seen silent and wretched, going to and fro, phantasms in a dead city, the mockery of life in a galvanised body. And strange, too, it is to stand on Primrose Hill, as I did but a day before writing this last chapter, to see the great province of houses, dim and blue through the haze of the smoke and mist, vanishing at last into the vague lower sky, to see the people walking to and fro among the flower beds on the hill, to see the sight-seers about the Martian machine that stands there still, to hear the tumult of playing children, and to recall the time when I saw it all bright and clear-cut, hard and silent, under the dawn of that last great day. . . . And strangest of all is it to hold my wife's hand again, and to think that I have counted her, and that she has counted me, among the dead.

-Lee
"To be civilized is to deny one's nature."

In reply to Re^2: SDL Console Text Box by shotgunefx
in thread 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.