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] [EINKLAPPEN] 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] [EINKLAPPEN] = 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] [EINKLAPPEN] 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.
|