home  forum  showcase  royalty-free  sign in  register   
 
   Forum forum >> BlitzCoder >> WrapText: a new text function for Blitz

Recent discussions

Sound Effects Events by megaphilx on Thu 6 Jan 2011 at 21:49:11
Latest updates by Heirauqs on Wed 5 Jan 2011 at 07:29:10
Bugs list by megaphilx on Wed 5 Jan 2011 at 03:46:36
What does PHP stand for? by sgvisual on Tue 23 Nov 2010 at 23:37:58
MegaPhilX To-Do List by megaphilx on Sat 13 Nov 2010 at 00:19:46
On-Topic Break Time by gosse on Fri 12 Nov 2010 at 17:58:47
gab to do list by Heirauqs on Fri 10 Sep 2010 at 07:37:11
gosse to do list by gosse on Fri 10 Sep 2010 at 06:27:45
Master Task List and Status by Heirauqs on Thu 9 Sep 2010 at 17:38:19
Borderlands Tweaks by gosse on Wed 28 Oct 2009 at 02:24:25

Back to BlitzCoderPage 1

 WrapText: a new text function for Blitz on Sun 10 Aug 2008 at 01:18:57
 

gosse
  Administrator
indieground.com
Posts: 89



Joined: Apr 2006
Location: Montreal, QC, CA

 

(reposted from Nuloen forum - Feb 15, 2006)
Here's a function I made for someone over CW.com. Does a great job, but should be used with a bitmap font for greater performances (especially justified text).
Code


; Environment Settings
Graphics 800, 600, 0, 2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
AppTitle "Wrap Text"

; Main loop
t = MilliSecs() + 1000
While Not KeyDown(1)

    ; Frame rate
    If MilliSecs() >= t Then
        lastFPS = FPS
        FPS = 0
        t = MilliSecs() + 1000
    End If
    FPS = FPS + 1
    
    ; Select mode
    If KeyHit(57) Then
        mode = (mode + 1) Mod 3
    End If
    If KeyHit(28) Then
        amode = (amode + 1) Mod 3
    End If
    If KeyHit(15) Then
        jmode = (jmode + 1) Mod 2
    End If
    
    ; Render
    Cls
    Text 0, 0, "Wrap mode: " + mode + " (press space to change)"
    Select mode
        Case 0
            Text 10, 15, "None"
        Case 1
            Text 10, 15, "Wrapped"
        Case 2
            Text 10, 15, "Direct"
    End Select
    Text 0, 30, "Align mode: " + amode + " (press enter to change)"
    Select amode
        Case 0
            Text 10, 45, "Left"
        Case 1
            Text 10, 45, "Center"
        Case 2
            Text 10, 45, "Right"
    End Select
    Text 0, 60, "Justified mode: " + jmode + " (press tab to change)"
    Select jmode
        Case 0
            Text 10, 75, "OFF"
        Case 1
            Text 10, 75, "ON"
    End Select
    Text 0, 90, "Text Height: " + h
    Text 0, 115, "FPS: " + lastFPS
    h = WrapText(100, 150, "Word wrap" + Chr$(13) + "From Wikipedia, the free encyclopedia" + Chr$(13) + "Word wrap refers to a feature supported by most text editors that allows them to insert soft returns (or hard returns for some text editors) at the right margins of a document. As the full view of the text would show excessively long text strings, word wrapping confines text to the viewable window, allowing text to be edited or read from top to bottom without any left to right scrolling." + Chr$(13) + "In word processors, word wrapping usually does not cause actual soft returns to be inserted into the actual document, but while editing the viewed text will be displayed as if soft returns had been inserted. If the user changes the margins, the editor will either automatically reposition the soft returns to ensure that all the text will 'flow' within the margins and remain visible, or provide the typist some convenient way to reposition the soft returns.", mode, amode, 400, jmode)
    Flip False

Wend

Const WRAP_MODE_NONE = 0
Const WRAP_MODE_WRAPPED = 1
Const WRAP_MODE_DIRECT = 2
Const ALIGN_MODE_LEFT = 0
Const ALIGN_MODE_CENTER = 1
Const ALIGN_MODE_RIGHT = 2
Const JUSTIFIED_OFF = 0
Const JUSTIFIED_ON = 1
Function WrapText%(x, y, txt$, mode = WRAP_MODE_NONE, align = ALIGN_MODE_LEFT, width = -1, justified = JUSTIFIED_OFF, height = -1)
    Local i, l, o, h
    Local w#, wx#
    Local c$
    l = Len(txt)
    If width < 0 And mode > WRAP_MODE_NONE Then
        RuntimeError "Cannot have no width with a wrap mode"
    End If
    If width < 0 And justified > JUSTIFIED_OFF Then
        RuntimeError "Cannot have no width and be justified"
    End If
    If height = -1 Then
        height = StringHeight(txt)
    End If
    Select mode
        Case WRAP_MODE_NONE
            For i = 1 To l
                If Mid(txt, i, 1) = Chr$(13) Or Mid(txt, i, 1) = Chr$(10) Then
                    h = WrapText(x, y, Left(txt, i - 1), WRAP_MODE_DIRECT, align, width, JUSTIFIED_OFF, height)
                    Return h+WrapText(x, y+height, Mid(txt, i + 1), mode, align, width, justified, height)
                End If
            Next
            Return WrapText(x, y, txt, WRAP_MODE_DIRECT, align, width, JUSTIFIED_OFF, height)
        Case WRAP_MODE_WRAPPED
            For i = 1 To l
                If Mid(txt, i, 1) = Chr$(13) Or Mid(txt, i, 1) = Chr$(10) Then
                    h = WrapText(x, y, Left(txt, i - 1), WRAP_MODE_DIRECT, align, width, JUSTIFIED_OFF, height)
                    Return h+WrapText(x, y+height, Mid(txt, i + 1), mode, align, width, justified, height)                
                End If
                If StringWidth(Left(txt, i - 1)) > width Then
                    i = i - 1
                    While i > 0 And Mid(txt, i, 1) <> " "
                        i = i - 1
                    Wend
                    If i = 0 Then
                        i = 1
                    End If
                    h = WrapText(x, y, Left(txt, i - 1), WRAP_MODE_DIRECT, align, width, justified, height)
                    Return h+WrapText(x, y+height, Mid(txt, i + 1), mode, align, width, justified, height)    
                End If
            Next
            Return WrapText(x, y, txt, WRAP_MODE_DIRECT, align, width, JUSTIFIED_OFF, height)
        Case WRAP_MODE_DIRECT
            Select justified
                Case JUSTIFIED_OFF
                    Select align
                        Case ALIGN_MODE_LEFT
                            Text x, y, txt
                        Case ALIGN_MODE_CENTER
                            Text x + width/2 - StringWidth(txt)/2, y, txt
                        Case ALIGN_MODE_RIGHT
                            Text x + width - StringWidth(txt), y, txt
                    End Select        
             Case JUSTIFIED_ON
                    w = (Float(width) - Float(StringWidth(txt))) / Float(l-1)
                    wx = x
                    For i = 1 To l
                        c$ = Mid(txt, i, 1)
                        Text wx, y, c
                        wx = wx + w + StringWidth(c)
                    Next
            End Select
            Return height
    End Select
End Function

-gosse

Back to BlitzCoderPage 1

1 guest online. 32 visitors in the last 24 hours.