From 897e414bd087e4078405d549fec47dff6d760afd Mon Sep 17 00:00:00 2001 From: Ian C Date: Tue, 29 Nov 2005 23:10:44 +0000 Subject: Tidied up interfaces and added CentreOn() --- bitmapfont.mod/bitmapfont.bmx | 384 +++++++++++++++++++++--------------------- 1 file changed, 188 insertions(+), 196 deletions(-) diff --git a/bitmapfont.mod/bitmapfont.bmx b/bitmapfont.mod/bitmapfont.bmx index fd70671..6e9b5a0 100644 --- a/bitmapfont.mod/bitmapfont.bmx +++ b/bitmapfont.mod/bitmapfont.bmx @@ -1,196 +1,188 @@ -Rem -bbdoc: noddybox.bitmapfont -EndRem -Module noddybox.bitmapfont - -ModuleInfo "Framework: Simple Bitmap Font Routines" -ModuleInfo "Copyright: Public Domain" -ModuleInfo "Author: Ian Cowburn" -ModuleInfo "Version: $Revision$" - -' $Id$ - -Import brl.Max2D -Import brl.Basic - - -Strict - -Rem -bbdoc: Defines a bitmap font -about: The @TBitmapFont object works from BMF Files, which are created using a tool called BitmapFontEd available at -about: http://www.noddybox.demon.co.uk/free/index-csharp.html
-about: Note that when colours are referenced they simply modify the colours in the font images as @SetColor() would do on a norml image.
-about: When drawing characters the current scale, alpha and rotation settings are applied. -EndRem -Type TBitmapFont - - Const NOCHR=95 - - Field img:TImage[NOCHR] - Field height:Int[NOCHR] - Field width:Int[NOCHR] - Field is_fixed:Int - Field max_width:Int - Field max_height:Int - - Rem - bbdoc: Loads a font from the supplied BMF file. - returns: The created font, or null if the BMF file couldn't be loaded. - about: The @image_flags are passed onto @CreateImage() when the images for the font are being created. - EndRem - Function Load:TBitmapFont(path:String, image_flags:Int) - Local fnt:TBitmapFont - Local str:TStream - Local magic:String - Local f,x,y - - str=ReadStream(path) - - If (Not str) Then Return Null - - fnt=New TBitmapFont - - magic=ReadString(str,4) - - If (magic<>"BMF1") - CloseStream(str) - Return Null - EndIf - - fnt.max_width=-1 - fnt.max_height=-1 - - fnt.is_fixed=Readint(str) - - For f=0 Until NOCHR - fnt.width[f]=Readint(str) - fnt.height[f]=Readint(str) - - fnt.max_width=Max(fnt.max_width,fnt.width[f]) - fnt.max_height=Max(fnt.max_height,fnt.height[f]) - - fnt.img[f]=CreateImage(fnt.width[f],fnt.height[f],1,image_flags) - - Local pm:TPixmap=LockImage(fnt.img[f]) - - For x=0 Until fnt.width[f] - For y=0 Until fnt.height[f] - WritePixel(pm,x,y,Readint(str)) - Next - Next - - UnlockImage(fnt.img[f]) - Next - - CloseStream(str) - - Return fnt - End Function - - Rem - bbdoc: Draws coloured text. - about: Draws @txt at the supplied @x,@y co-ordinates. @red,@green and @blue control the colour. - EndRem - Method DrawColoured(txt:String, x:Int, y:Int, red:Int, green:Int, blue:Int) - Local f,r,g,b,c - Local xs#,ys# - - GetScale(xs,ys) - - GetColor(r,g,b) - SetColor(red,green,blue) - - For f=0 Until Len(txt) - c=txt[f]-32 - DrawImage(img[c],x,y) - x:+width[c]*xs - Next - - SetColor(r,g,b) - End Method - - Rem - bbdoc: Draws white text. - about: Draws @txt at the supplied @x,@y co-ordinates in white. - EndRem - Method Draw(txt:String, x:Int, y:Int) - DrawColoured(txt,x,y,255,255,255) - End Method - - Rem - bbdoc: Draws centred, coloured text. - about: Draws @txt centred at the supplied @y co-ordinate. @red,@green and @blue control the colour. - EndRem - Method CentreColoured(txt:String, y:Int, red:Int, green:Int, blue:Int) - If is_fixed - DrawColoured(txt,GraphicsWidth()/2-Len(txt)*width[0]/2,y,red,green,blue) - Else - DrawColoured(txt,GraphicsWidth()/2-TextWidth(txt)/2,y,red,green,blue) - EndIf - End Method - - Rem - bbdoc: Draws centred, white text. - about: Draws @txt centred at the supplied @y co-ordinate in white. - EndRem - Method Centre(txt:String, y:Int) - If is_fixed - DrawColoured(txt,GraphicsWidth()/2-Len(txt)*width[0]/2,y,255,255,255) - Else - DrawColoured(txt,GraphicsWidth()/2-TextWidth(txt)/2,y,255,255,255) - EndIf - End Method - - Rem - bbdoc: Width of the supplied string. - returns: The length of the string @txt in pixels. - about: The current scale settings are taken into account. - EndRem - Method TextWidth:Int(txt:String) - Local w - Local xs#,ys# - - GetScale(xs,ys) - - For Local f=0 Until Len(txt) - w:+width[txt[f]-32]*xs - Next - Return w - End Method - - Rem - bbdoc: Height of the supplied string. - returns: The height of the string @txt in pixels. - about: The current scale settings are taken into account. - EndRem - Method TextHeight:Int(txt:String) - Local h:Int=0 - Local xs#,ys# - - GetScale(xs,ys) - - For Local f=0 Until Len(txt) - h=Max(h,height[txt[f]-32]*ys) - Next - - Return h - End Method - - Rem - bbdoc: Width of the largest character. - returns: The width in pixels of the largest character in the font. - EndRem - Method MaxWidth:Int() - Return max_width - End Method - - Rem - bbdoc: Height of the largest character. - returns: The height in pixels of the largest character in the font. - EndRem - Method MaxHeight:Int() - Return max_height - End Method - -End Type +Rem +bbdoc: noddybox.bitmapfont +EndRem +Module noddybox.bitmapfont + +ModuleInfo "Framework: Simple Bitmap Font Routines" +ModuleInfo "Copyright: Public Domain" +ModuleInfo "Author: Ian Cowburn" +ModuleInfo "Version: $Revision$" + +' $Id$ + +Import brl.Max2D +Import brl.Basic + + +Strict + +Rem +bbdoc: Defines a bitmap font +about: The @TBitmapFont object works from BMF Files, which are created using a tool called BitmapFontEd available at +http://www.noddybox.demon.co.uk/free/index-csharp.html
+Note that when colours are referenced they simply modify the colours in the font images as @SetColor() would do on a norml image.
+When drawing characters the current scale, alpha and rotation settings are applied. +EndRem +Type TBitmapFont + + Const NOCHR=95 + + Field img:TImage[NOCHR] + Field height:Int[NOCHR] + Field width:Int[NOCHR] + Field is_fixed:Int + Field max_width:Int + Field max_height:Int + + Rem + bbdoc: Loads a font from the supplied BMF file. + returns: The created font, or null if the BMF file couldn't be loaded. + about: The @image_flags are passed onto @CreateImage() when the images for the font are being created. + EndRem + Function Load:TBitmapFont(path:String, image_flags:Int) + Local fnt:TBitmapFont + Local str:TStream + Local magic:String + Local f,x,y + + str=ReadStream(path) + + If (Not str) Then Return Null + + fnt=New TBitmapFont + + magic=ReadString(str,4) + + If (magic<>"BMF1") + CloseStream(str) + Return Null + EndIf + + fnt.max_width=-1 + fnt.max_height=-1 + + fnt.is_fixed=ReadInt(str) + + For f=0 Until NOCHR + fnt.width[f]=ReadInt(str) + fnt.height[f]=ReadInt(str) + + fnt.max_width=Max(fnt.max_width,fnt.width[f]) + fnt.max_height=Max(fnt.max_height,fnt.height[f]) + + fnt.img[f]=CreateImage(fnt.width[f],fnt.height[f],1,image_flags) + + Local pm:TPixmap=LockImage(fnt.img[f]) + + For x=0 Until fnt.width[f] + For y=0 Until fnt.height[f] + WritePixel(pm,x,y,ReadInt(str)) + Next + Next + + UnlockImage(fnt.img[f]) + Next + + CloseStream(str) + + Return fnt + End Function + + Rem + bbdoc: Draws text. + about: Draws @txt at the supplied @x, @y co-ordinates. @red, @green and @blue control the colour. + EndRem + Method Draw(txt:String, x:Int, y:Int, red:Int=255, green:Int=255, blue:Int=255) + Local f,r,g,b,c + Local xs#,ys# + + GetScale(xs,ys) + + GetColor(r,g,b) + SetColor(red,green,blue) + + For f=0 Until Len(txt) + c=txt[f]-32 + DrawImage(img[c],x,y) + x:+width[c]*xs + Next + + SetColor(r,g,b) + End Method + + Rem + bbdoc: Draws centred text. + about: Draws @txt centred at the supplied @y co-ordinate. @red, @green and @blue control the colour. + EndRem + Method Centre(txt:String, y:Int, red:Int=255, green:Int=255, blue:Int=255) + If is_fixed + Draw(txt,GraphicsWidth()/2-Len(txt)*width[0]/2,y,red,green,blue) + Else + Draw(txt,GraphicsWidth()/2-TextWidth(txt)/2,y,red,green,blue) + EndIf + End Method + + Rem + bbdoc: Draws text centred on a position. + about: Draws @txt centred on the supplied @x and @y co-ordinate. @red, @green and @blue control the colour. + EndRem + Method CentreOn(txt:String, x:Int, y:Int, red:Int=255, green:Int=255, blue:Int=255) + If is_fixed + Draw(txt,x-Len(txt)*width[0]/2,y-TextHeight(txt),red,green,blue) + Else + Draw(txt,x-TextWidth(txt)/2,y-TextHeight(txt),red,green,blue) + EndIf + End Method + + Rem + bbdoc: Width of the supplied string. + returns: The length of the string @txt in pixels. + about: The current scale settings are taken into account. + EndRem + Method TextWidth:Int(txt:String) + Local w + Local xs#,ys# + + GetScale(xs,ys) + + For Local f=0 Until Len(txt) + w:+width[txt[f]-32]*xs + Next + Return w + End Method + + Rem + bbdoc: Height of the supplied string. + returns: The height of the string @txt in pixels. + about: The current scale settings are taken into account. + EndRem + Method TextHeight:Int(txt:String) + Local h:Int=0 + Local xs#,ys# + + GetScale(xs,ys) + + For Local f=0 Until Len(txt) + h=Max(h,height[txt[f]-32]*ys) + Next + + Return h + End Method + + Rem + bbdoc: Width of the largest character. + returns: The width in pixels of the largest character in the font. + EndRem + Method MaxWidth:Int() + Return max_width + End Method + + Rem + bbdoc: Height of the largest character. + returns: The height in pixels of the largest character in the font. + EndRem + Method MaxHeight:Int() + Return max_height + End Method + +End Type -- cgit v1.2.3