another newbie question about "draw string" and cu

General FreeBASIC programming questions.
Tigra
Posts: 155
Joined: Jan 07, 2007 17:21

another newbie question about "draw string" and cu

Postby Tigra » Jan 21, 2008 2:13

I've tried the sample in the help doc, and I've search the forums for "draw string font" but found nothing conclusive about what is wrong with my code.

Here is a sample program:

Code: Select all

'' Define character range
Const FIRSTCHAR = Asc(" "), LASTCHAR = Asc("9")
Const NUMCHARS = (LASTCHAR - FIRSTCHAR) + 1

''~ Dim Shared myFont As Any Ptr
Dim Shared myFont As UByte Ptr = 0

if Command() = "" Then

   print "run with '1' to see 8-bit test, '2' for 32-bit test"

ElseIf Command() = "1" Then

   Screen 20, 8, 1
   cls

   myFont = ImageCreate(NUMCHARS * 8, 8+1, &hFFFFFF, 8)
   Dim As UByte Ptr p = myFont + IIf(myFont[0] = 7, 32, 4)
   '' Assign font buffer header.
   '' Header version
   p[0] = 0
   p[1] = FIRSTCHAR
   p[2] = LASTCHAR

   For i As Integer = FIRSTCHAR To LASTCHAR
       '' Here we could define a custom width for each letter, but for simplicity we use
       '' a fixed width of 8 since we are reusing the default font glyphs
       p[3 + i - FIRSTCHAR] = 8

       '' Create character onto custom font buffer by drawing using default font
       Draw String myFont, ((i - FIRSTCHAR) * 8, 1), Chr(i), 32 + (i Mod 24) + 24
   Next i

ElseIf Command() = "2" Then

   Screen 20, 32, 1
cls

   myFont = ImageCreate(NUMCHARS * 8, 8+1, &hFFFFFF, 32)
   Dim As UByte Ptr p = myFont + IIf(myFont[0] = 7, 32, 4)
   '' Assign font buffer header.
   '' Header version
   p[0] = 0
   p[1] = FIRSTCHAR
   p[2] = LASTCHAR

   For i As Integer = FIRSTCHAR To LASTCHAR
       '' Here we could define a custom width for each letter, but for simplicity we use
       '' a fixed width of 8 since we are reusing the default font glyphs
       p[3 + i - FIRSTCHAR] = 8

       '' Create character onto custom font buffer by drawing using default font
       Draw String myFont, ((i - FIRSTCHAR) * 8, 1), Chr(i), 32 + (i Mod 24) + 24
   Next i

End If

Draw String (20, 20), "13579", , , PSet
Draw String (20, 40), "13579", , myFont, PSet


Print "press a key"
GetKey()


In both cases only half of the character is shown. Can anyone suggest what is wrong with my code?

Before I more onto the more powerful lib's people have made, I'd like to get the simple case working first.

Tigra[/url]
ssjx
Posts: 34
Joined: Oct 23, 2007 8:51
Contact:

Postby ssjx » Jan 21, 2008 14:21

It looks like the image height is too small, if you increase it to 12 (or more) the text fits.
D.J.Peters
Posts: 7698
Joined: May 28, 2005 3:28

Postby D.J.Peters » Jan 21, 2008 14:36

wrong SCREEN syntax?
SCREENRES will work here

Joshy

Code: Select all

'' Define character range
Const FIRSTCHAR = Asc(" "), LASTCHAR = Asc("9")
Const NUMCHARS = (LASTCHAR - FIRSTCHAR) + 1

''~ Dim Shared myFont As Any Ptr
Dim Shared myFont As Ubyte Ptr = 0

If Command(1) = "" Then

        Print "run with '1' to see 8-bit test, '2' for 32-bit test"

Elseif Command(1) = "1" Then

        Screenres 640,480,8
        Cls

        myFont = ImageCreate(NUMCHARS * 8, 8+1, &hFFFFFF, 8)
        Dim As Ubyte Ptr p = myFont + IIf(myFont[0] = 7, 32, 4)
        '' Assign font buffer header.
        '' Header version
        p[0] = 0
        p[1] = FIRSTCHAR
        p[2] = LASTCHAR

        For i As Integer = FIRSTCHAR To LASTCHAR
                '' Here we could define a custom width for each letter, but for simplicity we use
                '' a fixed width of 8 since we are reusing the default font glyphs
                p[3 + i - FIRSTCHAR] = 8

                '' Create character onto custom font buffer by drawing using default font
                Draw String myFont, ((i - FIRSTCHAR) * 8, 1), Chr(i), 32 + (i Mod 24) + 24
        Next i

Elseif Command(1) = "2" Then

        Screenres 640,480,32
Cls

        myFont = ImageCreate(NUMCHARS * 8, 8+1, &hFFFFFF, 32)
        Dim As Ubyte Ptr p = myFont + IIf(myFont[0] = 7, 32, 4)
        '' Assign font buffer header.
        '' Header version
        p[0] = 0
        p[1] = FIRSTCHAR
        p[2] = LASTCHAR

        For i As Integer = FIRSTCHAR To LASTCHAR
                '' Here we could define a custom width for each letter, but for simplicity we use
                '' a fixed width of 8 since we are reusing the default font glyphs
                p[3 + i - FIRSTCHAR] = 8

                '' Create character onto custom font buffer by drawing using default font
                Draw String myFont, ((i - FIRSTCHAR) * 8, 1), Chr(i), 32 + (i Mod 24) + 24
        Next i

End If

Draw String (20, 20), "13579", , , Pset
Draw String (20, 40), "13579", , myFont, Pset


Print "press a key"
GetKey()
Tigra
Posts: 155
Joined: Jan 07, 2007 17:21

Postby Tigra » Jan 22, 2008 1:16

ssjx wrote:It looks like the image height is too small, if you increase it to 12 (or more) the text fits.


D.J.Peters wrote:wrong SCREEN syntax?
SCREENRES will work here


Both ssjx and D.J.Peters are right but for different reasons.

I tried ssjx's first. It worked but I did not understand why.

D.J.Peters' suggestion got me thinking that I was using Screen 20 which is 1024x768. Using ScreenRes 640, 480 made the program work as expected.

I went back to ssix's suggestion, though it was 13 not 12 - 1024 is 1.6 times the size of 640, so 8*1.6 = 12.8, I rounded it to 13. 12 shows that the font stopped at the bottom edge of the character, 13 (obviously) added an empty line.

I tried Screen 18 and Screen 12 - 18 gave the same results as 20, and 12 gave no output for test 1 and a runtime error for test 2.

So I'll stick with Screen 20 and a font height of 13.

Thanks ssjx and D.J. Peters

Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests