UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on September 28, 2005, 07:24:25 AM

Title: Custom Bitmap Fonts
Post by: kevin on September 28, 2005, 07:24:25 AM
  This code shows how you can import bitmap fonts into PlayBASIC 1.089.  While I could build the import function into PlayBASIC.   I think i'll leave it outside.   Since there's so many ways these types of bitmap fonts are set out.

Once imported, you can just use the font like normal  with Print/text etc.  It's a feature that's long overdue !

(note: This Example can also be found in the PlayBASIC example pack)
[pbcode]

; Build Chr/image map conversion string
  ChrMap$=          " !"+chr$(34)
  ChrMap$=ChrMap$+   "    '()   -./0123456789:  = ? abcdefghijklmnopqrstuvwxyz"
  LoadBitmapFont("..\..\gfx\Bitmap_Font.bmp",5,ChrMap$,30,16,17)


; Build Chr/image map converion string
  ChrMap$=" !"+chr$(34)+"#$%&'()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz"
  LoadBitmapFont("..\..\gfx\Fresh.png",10,ChrMap$,30,32,25)


   cx=getscreenwidth()/2
   Do

 Setfont 1   
 centertext cx,100,"...this is just the good old courier font..."

 Setfont 5
 centertext cx,150,"...this is an ok custom bitmap font..."
 centertext cx,200,"...custom bitmap font..."

 Setfont 10
 centertext cx,300,"...but this one is..."
 centertext cx,350,"...much nicer!!!..."
   
 sync
   loop
   


Function LoadBitmapFont(Filename$,FontIndex,ChrMap$,ChrIndex,ChrWidth,ChrHeight)

 ; get surrent render surface
   OldSurface=GetSurface()

; graba free image index
 TempImage=getfreeimage()
; attempt to load the image
   loadimage Filename$,Tempimage

 ; check if the image loaded OK   
   if GetImageStatus(TempImage)=true

     ` redirect all rendering/grabbing to the loaded image
 rendertoimage TempImage

    ; create the 'blank' bitmap font
   CreateBitmapFont FontIndex,ChrWidth,ChrHeight

 W=getsurfacewidth()
 H=getsurfaceHeight()
 
   ;Xpos=0
   ;Ypos=0
 For ThisChr=1 to len(ChrMap$)
    GrabChr=Mid(ChrMap$,ThisChr)
    GetFontChr FontIndex,GrabChr,Xpos,Ypos   
    xpos=xpos+Chrwidth
    if Xpos=>w
      Xpos=0
      Ypos=Ypos+ChrHeight
      if Ypos=>H then Exit
    endif     
 next
 DeleteImage tempimage   
   endif
   rendertoimage  OldSurface
EndFUnction


[/pbcode]

Title: Custom Bitmap Fonts
Post by: Jeku on October 12, 2005, 01:43:00 AM
Sweet!  Thanks!  This is just what I was looking for, for my new secret game ;)