Page 1 of 1

custom BitMap fonts vs. DrawText()?

Posted: Thu Oct 16, 2003 11:47 am
by vanleth
I can't beat PB's DrawText() speed with the DisplaySprite() command in fullscreen for making gamefonts.
My approach was to have 1 sprite containing all the letters, then clip the sprite for a specific letter.
Anyone have a good workaround to do this trick faster?

Best regards
Van

Posted: Thu Oct 16, 2003 12:39 pm
by vanleth
I'll better post some examples.

Here is the font sprite

Image

And here is some test code. It's very noticeable that my routine is slow

Code: Select all

InitSprite() : InitKeyboard()

OpenScreen(800,600,16,"Bitmap Font")
; font sprite letters is 6 in width and 10 in height
LoadSprite(0, "debugfont.bmp" , 0 )

Procedure bitmap_text(x.l, y.l, string_buffer.s)
  a=0
  Repeat
    buff.b = PeekB(@string_buffer+a)
    ClipSprite(0,(buff-32)*6, 0, 6, 10)
    DisplaySprite(0, x+(a*6), y)
    a+1
  Until buff = 0
EndProcedure

Repeat
  FlipBuffers() : ClearScreen(0,0,0)

  old_time = TimeGetTime_()
  For a = 0 To 2000
    bitmap_text(100,100,"Bitmap Font")
  Next
  bitmapfont_time = TimeGetTime_() - old_time
  
  old_time = TimeGetTime_()
  StartDrawing(ScreenOutput())
  DrawingMode(1) : FrontColor(200,200,200)
    For a = 0 To 2000
      Locate(100, 112) : DrawText("Fred's Font")
    Next  
  pbfont_time = TimeGetTime_() - old_time
  
  ; show results
    Locate(0,0) : DrawText("Bitmap "+Str(bitmapfont_time))
    Locate(0,12) : DrawText("PureBasic "+Str(pbfont_time))
  StopDrawing()

  ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)

Posted: Thu Oct 16, 2003 1:57 pm
by LarsG
I have noticed this as well.. too bad PB doesn't support TTF fonts..
(at least, I didn't get them to work)

-Lars

Posted: Wed Oct 29, 2003 5:58 am
by chris_b
vanleth wrote:here is some test code. It's very noticeable that my routine is slow
Your code doesn't account for the time taken to do StartDrawing() and StopDrawing() - if you can avoid using any 2DDrawing stuff in your main loop then the font sprite method will be probably as fast

Posted: Wed Oct 29, 2003 11:13 am
by LarsG
chris_b wrote:
vanleth wrote:here is some test code. It's very noticeable that my routine is slow
Your code doesn't account for the time taken to do StartDrawing() and StopDrawing() - if you can avoid using any 2DDrawing stuff in your main loop then the font sprite method will be probably as fast
No no, Chris_b.. If you'd look at the code, you'd see that he is not using any Start/Stop-drawing commands during the bitmap_text test... I have come to the same results as Vanleth as well... If only we could get almost as fast text as the standard one, with TTF fonts, then we could forget most of the bitmap fonts.. :) (unless you want some colored fonts)

-Lars

Posted: Wed Oct 29, 2003 12:13 pm
by Psychophanta
A NOTE, Talking about speed:

MSWinAPI TimeGetTime_() and GetTickCount_() funcs return samething, but this last is much faster :wink:

Posted: Wed Oct 29, 2003 12:16 pm
by LarsG
Psychophanta wrote:A NOTE, Talking about speed:

MSWinAPI TimeGetTime_() and GetTickCount_() funcs do samething, but this last is about twice faster :wink:
Yeah.. I used GetTickCount_() when I tested the speed...
(but in this example, it's not really that important..)

-Lars

Posted: Wed Oct 29, 2003 12:22 pm
by Psychophanta
I know, but i am really obsessionate 8O with speed .

Posted: Wed Oct 29, 2003 6:01 pm
by chris_b
LarsG wrote:If you'd look at the code, you'd see that he is not using any Start/Stop-drawing commands during the bitmap_text test
What I am suggesting is that

Code: Select all

Repeat

  FlipBuffers()
  ClearScreen(0,0,0)

  Display_some_bitmap_text_using_sprites

  ExamineKeyboard() 

Until KeyboardPushed(#PB_Key_Escape)
is probably similar speed to

Code: Select all

Repeat

  FlipBuffers()
  ClearScreen(0,0,0)

  StartDrawing(ScreenOutput())
    Draw_some_text
  StopDrawing()

ExamineKeyboard() 

Until KeyboardPushed(#PB_Key_Escape)
due to the time taken for the Start/Stop drawing functions.

Of course it depends on the amount of text displayed at a time, whether you use DrawSprite or DrawTransparentSprite, etc. But for game use where you display small amount of text on the screen (like score and lives remaining) I think vanleth's bitmap_text function is fine.

If you are displaying more text at a time - like for example the speech boxes in RPG style games - then you can maybe use the bitmap_text function to draw to a seperate sprite once with the usebuffer function and then only display that one sprite within the main loop.

Posted: Wed Oct 29, 2003 8:02 pm
by vanleth
Well my test code was just to point out that Freds DrawText() routines are hard to beat speed wise.

Actually it got me abit curious on how the DrawText() command works internally.

Posted: Wed Oct 29, 2003 8:11 pm
by Psychophanta
Don't you prefer to ask yourself how work internally ClipSprite() and DisplaySprite()?
Might be DrawText() is good made, but ClipSprite() and DisplaySprite() too slow :twisted: .