home *** CD-ROM | disk | FTP | other *** search
/ DarkBASIC - The Ultimate 3D Game Creator / PCactive 8 CD1 - DarkBasic.iso / SOFTWARE / DEMOS / Wordwrap / wordwrap.dba < prev   
Encoding:
Text File  |  2000-04-27  |  2.1 KB  |  83 lines

  1. `Here's a function to display text within specified margins.
  2. dim imageflag(1)
  3.  
  4. a$="Insert some funky old test string.  " 
  5. for j=1 to 82
  6.     wrap(a$)
  7. next j
  8.  
  9. if imageflag(1)=1 then delete image 99
  10. undim imageflag(1)
  11. end
  12.  
  13. FUNCTION wrap(a$)
  14.     `Change these 6 values as needed, all values in pixels.  
  15.     `Make left & right margins > 0, make top & bottom margins 
  16.     `multiples of text height, which by default is 16.
  17.     screenwidth=640 : screenheight=480
  18.     leftmargin=25 : rightmargin=25
  19.     topmargin=16 : bottommargin=16*9
  20.  
  21.     aheight=text height(a$)
  22.     if xover<leftmargin then xover=leftmargin
  23.     if ydown<topmargin then ydown=topmargin
  24.     nextline$=""
  25.  
  26.     do
  27.         alen=len(a$)
  28.         `get rid of any spaces at beginning of a$...
  29.         while left$(a$,1)=" "
  30.             dec alen
  31.             a$=right$(a$,alen)
  32.         endwhile
  33.  
  34.         awidth=text width(a$)
  35.         room=screenwidth-rightmargin-xover
  36.         `if there's room on the line, display a$, exit do-loop...
  37.         if awidth<room
  38.             text xover,ydown,a$
  39.             xover=xover+awidth
  40.             exit
  41.         endif
  42.  
  43.         `...otherwise go backwards along a$, 
  44.         `moving the last character into nextline$ 
  45.         `until a$ is short enough to fit...
  46.         while awidth>room
  47.             nextline$=right$(a$,1)+nextline$
  48.             dec alen
  49.             a$=left$(a$,alen)
  50.             awidth=text width(a$)
  51.         endwhile
  52.  
  53.         `then count back until it hits a space, 
  54.         `so you don't break mid-word...
  55.         righ$=right$(a$,1)
  56.         while righ$<>" " and alen>0
  57.             nextline$=righ$+nextline$
  58.             dec alen
  59.             a$=left$(a$,alen)
  60.             righ$=right$(a$,1)
  61.         endwhile
  62.  
  63.         `print a$ to screen...
  64.         text xover,ydown,a$
  65.         `and make nextline$ the new a$...
  66.         a$=nextline$ : nextline$=""
  67.         xover=leftmargin : ydown=ydown+aheight
  68.  
  69.         `scroll it off the top if necessary...
  70.         if ydown=>screenheight-bottommargin
  71.             x2=screenwidth-rightmargin : y2=screenheight-bottommargin
  72.             if y2>screenheight then y2=screenheight
  73.             get image 99,leftmargin,topmargin+aheight,x2,y2
  74.             paste image 99,leftmargin,topmargin
  75.             ink 0,0
  76.             box leftmargin,screenheight-bottommargin-aheight,x2,y2
  77.             ink rgb(255,255,255),0
  78.             ydown=screenheight-bottommargin-aheight
  79.             imageflag(1)=1
  80.         endif
  81.     loop
  82. ENDFUNCTION
  83.