Draw Text really slow

Linux specific forum
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Draw Text really slow

Post by Shield »

hmm strange... :?

Can't help further though because it seems to be a PB / Linux related issue (as you said before
the later versions the performance was better) and I don't use Linux.

Sorry.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Draw Text really slow

Post by idle »

If you think about it it's reasonable to assume Drawtext will be slow when it's using true type fonts.
There's a lot of math going on to create the symbol. Make a glyph cache with a map

something like this

Code: Select all

Structure glyph 
  glyph.i
  refcount.i  
  width.i
  height.i
EndStructure     

Global NewMap glyphs.glyph() 

Procedure GlyphCache()
tf=LoadFont(#PB_Any,"arial",14,0)
For a = 32 To 126 
  timg = CreateImage(#PB_Any,1,1)
  StartDrawing(ImageOutput(timg))
  DrawingFont(FontID(tf))
     width = TextWidth(Chr(a))
     height = TextHeight(Chr(a))
  StopDrawing()
  FreeImage(timg)
  If width And height 
     glyphs(Chr(a))\glyph = CreateImage(#PB_Any,width,height,24)
     StartDrawing(ImageOutput(glyphs(Chr(a))\glyph))
       DrawingMode(#PB_2DDrawing_Default)
       DrawingFont(FontID(tf))
       DrawText(0,0,Chr(a),RGB(255,0,0),0)
     StopDrawing()
     glyphs(Chr(a))\width = width 
     glyphs(Chr(a))\height = height 
   EndIf
 Next 
EndProcedure 

Procedure myDrawtext(output.i,text.s,x,y)
  Protected chr.s
  len = Len(text)
  If StartDrawing(output) 
    For a = 1 To len 
      chr = Mid(text,a,1)
      DrawImage(ImageID(glyphs(chr)\glyph),x,y)
      x+glyphs(chr)\width 
    Next
    StopDrawing()
  EndIf  
EndProcedure  

GlyphCache() 
OpenWindow(0,0,0,800,600,"test")

Repeat 
  ev = WindowEvent()
  myDrawtext( WindowOutput(0),"test",Random(700)+50,Random(500)+50)
  Delay(20)
Until ev =#PB_Event_CloseWindow   

Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Brujah
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Nov 03, 2003 7:45 pm
Location: Germany
Contact:

Re: Draw Text really slow

Post by Brujah »

We did a similar solution for our game.
We create sprites for sentences as we do not have THAT many of them.
And we save them in a map as well so they have to be created only once.
At gamestart we create the most used once so ingame we do not get any more slowdowns.

Problem here still is that it looks strange in a lot of places as antialiasing and stuff is missing. (No problem on black background but in f.e. the tile where we display on a scrolling background it looks strange)
I solved that by displaying the sprite in black first with offsets of -1 in all directions.
But still it does not look as good as it should :(
Post Reply