News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

GetTextWidth( ) is being weird

Started by Draco9898, September 24, 2005, 01:22:31 PM

Previous topic - Next topic

Draco9898

I'm trying to break a string into parts and reassemble them on the screen correctly, however, It seems GetTextWidth() Isn't getting the correct size on some characters when you change a text into a bitmap. Take a look:

PlayBASIC Code: [Select]
Dim GameFont(0)
CreateFont(26,1)


ThisString$="Some Level Title"
Length=Len(ThisString$)
X#=(ScreenW#/2)-(GetTextWidth(ThisString$)/2)
For X=1 To Length
ThisString2$=Mid$(ThisString$,X,1)
Text 2+GetTextWidth(ThisString2$),2+(X*22),ThisString2$
Next X
Sync: WaitKey

Wait 1000
SetFont 2

ThisString$="Some Level Title"
Length=Len(ThisString$)
X#=(ScreenW#/2)-(GetTextWidth(ThisString$)/2)
For X=1 To Length
ThisString2$=Mid$(ThisString$,X,1)
Text 32+GetTextWidth(ThisString2$),2+(X*22),ThisString2$
Next X
Sync: WaitKey



`Load the Font And shade it
Psub CreateFont(FontSize#,Color)
ThisNum=GetArrayElements(GameFont(),1)+1
If ThisNum=22
DeleteFont 1
EndIf
ReDim GameFont(ThisNum+1)
GameFont(ThisNum+1)=GetFreeFont()
LoadFont "Ariel",GameFont(ThisNum+1),FontSize#,1
MakeBitmapFont GameFont(ThisNum+1),$2F
TempImage=GetFreeImage(): CreateImage TempImage,FontSize#,FontSize#
RenderToImage TempImage
Color#=0
For X=1 To FontSize#
If X>(FontSize#*0.25)
Color#=Color#+((255/(FontSize#/2)))
If Color#>255 Then Color#=255
EndIf
`Red
If Color=1 Then LineC 0,0+x,FontSize#,0+x,RGB(255,Int(Color#),Int(Color#))
`Gold
If Color=2 Then LineC 0,0+x,FontSize#,0+x,RGB(255,255,Int(Color#))
`Blue
If Color=3 Then LineC 0,0+x,FontSize#,0+x,RGB(Int(Color#),Int(Color#),255)
Next X
RenderToScreen
BlendBitmapFont GameFont(ThisNum+1),TempImage
DeleteImage TempImage
EndPsub

DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

#1
The Arial font has variable width characters.  


PlayBASIC Code: [Select]
Dim GameFont(0)
CreateFont(26,1)


ThisString$="Some Level Title"
Length=Len(ThisString$)
X#=(ScreenW#/2)-(GetTextWidth(ThisString$)/2)
For X=1 To Length
ThisString2$=Mid$(ThisString$,X,1)
Text 2+GetTextWidth(ThisString2$),2+(X*22),ThisString2$
Next X
Sync: WaitKey

Wait 1000
SetFont 2

ThisString$="Some Level Title"
Length=Len(ThisString$)
X#=(ScreenW#/2)-(GetTextWidth(ThisString$)/2)
For X=1 To Length
ThisString2$=Mid$(ThisString$,X,1)

; Text 32+GetTextWidth(ThisString2$),2+(X*22),ThisString2$

; center around width
CenterText 32,2+(X*22),ThisString2$

; right justify
Text 80-GetTextWidth(ThisString2$),2+(X*22),ThisString2$

Next X
Sync: WaitKey



`Load the Font And shade it
Psub CreateFont(FontSize#,Color)
ThisNum=GetArrayElements(GameFont(),1)+1
If ThisNum=22
DeleteFont 1
EndIf
ReDim GameFont(ThisNum+1)
GameFont(ThisNum+1)=GetFreeFont()
LoadFont "Ariel",GameFont(ThisNum+1),FontSize#,1
MakeBitmapFont GameFont(ThisNum+1),$2F
TempImage=GetFreeImage(): CreateImage TempImage,FontSize#,FontSize#
RenderToImage TempImage
Color#=0
For X=1 To FontSize#
If X>(FontSize#*0.25)
Color#=Color#+((255/(FontSize#/2)))
If Color#>255 Then Color#=255
EndIf
`Red
If Color=1 Then LineC 0,0+x,FontSize#,0+x,RGB(255,Int(Color#),Int(Color#))
`Gold
If Color=2 Then LineC 0,0+x,FontSize#,0+x,RGB(255,255,Int(Color#))
`Blue
If Color=3 Then LineC 0,0+x,FontSize#,0+x,RGB(Int(Color#),Int(Color#),255)
Next X
RenderToScreen
BlendBitmapFont GameFont(ThisNum+1),TempImage
DeleteImage TempImage
EndPsub



Draco9898

thanks kevin, but is there a way I can draw the letters in a horizontal fashion with the correct widths?
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

look closely, the widths are correct.  If the weren't it wouldn't render any text correctly.