Simple Bitmapfont

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

 

David

Betreff: Simple Bitmapfont

BeitragMo, Jul 26, 2004 14:23
Antworten mit Zitat
Benutzer-Profile anzeigen
Hi!

Hab ma eine ganz simples Beispiel für Bitmapfonts geschrieben, da ich mit den 32x1xxxxx Bitmaps, bei welchem alle Charakters in einer Reihe stehen, nicht wirklich zufrieden bin.

Also erstmal der Code zum erstellen des Bitmapfonts.

BlitzBasic: [AUSKLAPPEN]

Graphics 800, 600, 32
SetBuffer BackBuffer()

Const MAX_WIDTH = 512
Const MAX_HEIGHT = 512

Const FONT_NAME$ = "Arial"
Const FONT_SIZE = 32
Const FONT_BOLD = False
Const FONT_ITALIC= False
Const FONT_UNDERL= False

Const OFFSETX = 5
Const OFFSETY = 2
Const FIRST_CHAR = 32

fntCurr = LoadFont( FONT_NAME, FONT_SIZE, FONT_BOLD, FONT_ITALIC, FONT_UNDERL )
SetFont( fntCurr )

xPos = 0
yPos = 0
width = 0
height = FontHeight()
tmpWidth = StringWidth( Chr$( FIRST_CHAR ) )

img = CreateImage( MAX_WIDTH, MAX_HEIGHT, 1 )
SetBuffer ImageBuffer( img, 0 )

file = WriteFile( "c:\test.fnt" )

For i = FIRST_CHAR To 256
width = StringWidth( Chr$( i ) )
x = ( x + tmpWidth ) + OFFSETX

If ( x >= MAX_WIDTH - 10 ) Then
x = 0
y = ( y + height ) + OFFSETY
End If

Text x, y, Chr$( i )

WriteLine( file, Chr$( i ) + " = " + x + " " + y + " " + width + " " + height )

tmpWidth = width
Next

SaveImage( img, "c:\test.bmp" )
CloseFile( file )

SetBuffer BackBuffer()
DrawImage( img, ( GraphicsWidth() / 2 ) - MAX_WIDTH / 2 , ( GraphicsHeight() / 2 ) - MAX_HEIGHT / 2 )
Flip

WaitKey

End


Heraus kommt eine Bitmap und eine .fnt Datei welche die Charakterkoordinaten beinhält:

BlitzBasic: [AUSKLAPPEN]

= 13 0 8 32
! = 26 0 8 32
" = 39 0 10 32
# = 54 0 15 32
$ = 74 0 15 32
% = 94 0 24 32
& = 123 0 18 32
' = 146 0 5 32
( = 156 0 9 32
) = 170 0 9 32
;...


Und nu noch ein Beispiel für die Anwendung.

BlitzBasic: [AUSKLAPPEN]

Graphics 800, 600, 32
SetBuffer BackBuffer()

Const FONT_FILE$ = "Font\test.fnt"
Const FONT_BMP$ = "Font\test.bmp"

Type Font_t
Field x, y
Field width, height
End Type

Dim BmpFont.Font_t( 256 )
Global BmpImage = LoadImage( FONT_BMP )

Init()

While Not KeyHit( 1 )
PrintText( "Hallo Bitmapfont", 10, 10 )
Flip
Cls
Wend

Function CreateCharakter( idx, info$ )
BmpFont( idx ) = New Font_t
cnt = 0

info$ = Mid( info$, 5, Len( info$ ) )

While cnt <= 3
pos = Instr( info$, " ", 1 )

If ( pos = 0 )
pos = Len( info$ )
End If

Select cnt
Case 0
BmpFont( idx )\x = Mid( info$, 1, pos )

Case 1
BmpFont( idx )\y = Mid( info$, 1, pos )

Case 2
BmpFont( idx )\width = Mid( info$, 1, pos )

Case 3
BmpFont( idx )\height = Mid( info$, 1, pos )
End Select

info$ = Mid( info$, pos + 1, Len( info$ ) )
cnt = cnt + 1
Wend
End Function

Function Init()
file = OpenFile( FONT_FILE )

For i = 32 To 255
lne$ = ReadLine( file )
CreateCharakter( i, lne )
Next

CloseFile( file )
End Function

Function PrintText( txt$, x, y )
xPos = x

For i = 1 To Len( txt$ )
c.Font_t = BmpFont( Asc( Mid( txt$, i, 1 ) ) )
DrawImageRect( BmpImage, xPos, y, c\x, c\y, c\width, c\height )

xPos = xPos + c\width
Next
End Function

End


Hoffe irgendjemand kann Nutzen daraus ziehen.

grüße

P.S.: Der Code ist ohne jede Fehlerbehandlung, sollte bei Verwendung jedoch auf jeden Fall implementiert werden.

P.P.S.: Da ich das Beispiel recht schnell programmiert habe, ist der Code natürlich nicht optimal und kann bestimmt noch an vielen Stellen verbessert werden.
http://bl4ckd0g.funpic.de

Markus2

BeitragMo, Jul 26, 2004 19:44
Antworten mit Zitat
Benutzer-Profile anzeigen
Sowas in der Art habe ich mein ich schon hier gepostet aber
mit allen Zeichen , hättest dir gar nicht solche Mühe machen müssen Wink
Also Vorlage konnte man nen x beliebigen TrueType Font nehmen
und gespeichert habe ich die Pos. in eine xywh Datei .
Die ganze Routine war nur 5,5 KByte Smile
 

David

BeitragMo, Jul 26, 2004 20:20
Antworten mit Zitat
Benutzer-Profile anzeigen
Hi!

Ach weist du, so viel Mühe war das garnicht. Wink

grüße
http://bl4ckd0g.funpic.de

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group