TGC Codebase Backup



Word wrap by Phaelax

27th Aug 2005 8:01
Summary

wraps words to a given width



Description

displays text over a box, the words are wrapped to the width of that box. Words are not split creating the ugly block paragraph format(newspaper) but split up between words.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    sync on


thing$ = "Hello, my name is Ozark. We are the elephant people that live in the mountains. If you wish to find the magic key, you must first do something for us. Go to the nearest cave, and there you will find a magical and mystical potatoe head.  Bring this back to us and we will tell you where you can find the super duper magic key of Rasberry!"

width = 100

repeat
   cls

   if mouseclick() = 1 then inc width
   if mouseclick() = 2 then dec width
   if width < 80 then width = 80

   talkBox(mousex(),mousey(),thing$,width,rgb(200,255,0),rgb(0,0,255))

   sync
until spacekey() = 1



`============================================
`Displays a box and text wrapped to the box.
`The height of the box depends on how many
`lines the text is broken down to.
`(X,Y) Top-left corner of box position
`"S" text to display
`"Width" width of the box
`"tColor" is text color
`"bColor" is box color
`============================================
function talkBox(x as integer, y as integer, s as string, width as integer, tColor as dword, bColor as dword)
   sTemp as string
   sTemp = s
   lines = 1
   while text width(sTemp) > width
      length = len(sTemp)
      for i = 1 to length
         if mid$(sTemp, i) = " "
            wIndex = i
         endif
         if text width(left$(sTemp,i)) > width
            inc lines
            exit
         endif
      next i
      sTemp = right$(sTemp,length-wIndex)
   endwhile

   height = text height(s) * lines
   ink bColor,0
   box x,y,x+width,y+height
   ink tColor,0
   wrapText(x,y,s,width)
endfunction


`============================================
`Wraps and displays text using the specified
`width as the boundary. Words are not broken
`up when wrapped.
`============================================
function wrapText(x as integer, y as integer, s as string, width as integer)
   repeat
      length = len(s)
      wIndex = 0
      for i = 1 to length
         if mid$(s,i) = " "
            wIndex = i
         endif
         if text width(left$(s,i)) > width
            exit
         endif
      next i
      text x,y,left$(s,wIndex-1)
      y = y + text height(s)
      s = right$(s,length-wIndex)
   until text width(s) < width
   text x,y,s
endfunction