Fontmachine- resizing fonts
Monkey Forums/Monkey Beginners/Fontmachine- resizing fonts
| ||
I've started using FontMachine and can now happily display bitmap text strings in my game. However, I can't find any mention anywhere of a way to set the font size programmatically. There's GetFontHeight but no corresponding set method. I really need this functionality so I can scale the font size according to device size so I'm hoping that I'm just being stupid... Can anyone help? Thanks, Ian |
| ||
One of the hassles with bitmap fonts is you need to provide multiple font sets for the various resolutions you want to support. So, for example, you'll need one set each for low, medium and high resolutions, and maybe a 4th set for "ultra high retina" display. In the FontMachine editor you'll need to create your font and then "render it out" in multiple sizes, a different filename/set for each one. Then in Monkey you load-in/use an appropriate set depending on the target device's current display resolution. The other option is to provide a single bitmap font set but use "scaling" when different text sizes are required. This will, however, have performance and quality penalties. |
| ||
Ah, thanks for the reply. I did suspect that generating multiple sets might be the answer. However, you also mention scaling. I know that scaling can blow performance but my game's a turn-based strategy game and, even though I'm scaling everything at the moment, I can still maintain a steady FPS. So, if I wanted to scale a font, how would I do that? |
| ||
Take into account that scaling does also affect font drawing, so usually providing a single big font and scaling down when needed gives good results |
| ||
Thanks, Ziggy, that's reassuring. However, I know that DrawImage, for example, has an overload for scaling, but DrawText doesn't. So how do I scale the font? I'm not (knowingly) using a matrix and I'm unclear how the Scale command works. |
| ||
I'm using this: |
| ||
@Volker: You can use the align parameter in the DrawText function like this:ffont.DrawText(text,0,0,eDrawAlign.LEFT) ffont.DrawText(text,0,0,eDrawAlign.RIGHT) ffont.DrawText(text,0,0,eDrawAlign.CENTER) So your function could be: Function DrawTextFM:Void(text:String, x:Int, y:Int, scale:Float = 1, xalign:Bool = False) ' uses global ffont =fontmachine font PushMatrix() Transform(scale,0,0,scale,x,y) if xalign=false ffont.DrawText(text,0,0) Else ffont.DrawText(text,0,0, eDrawAlign.CENTER) endif PopMatrix() End Function Or even more high level: Function DrawTextFM:Void(text:String, x:Int, y:Int, scale:Float = 1, xalign:Int = eDrawAlign.LEFT) ' uses global ffont =fontmachine font PushMatrix() Transform(scale,0,0,scale,x,y) ffont.DrawText(text, 0, 0, xalign) PopMatrix() End Function 'handy overload if you want to use a bool to determine "centered" status: Function DrawTextFM:Void(text:String, x:Int, y:Int, scale:Float = 1, centered:Bool = False) Local align:Int If centered Then align = eDrawAlign.CENTER Else align = eDrawAlign.LEFT DrawTextFM(text, x, y, scale, align) End Function In case better alignment calculation is added to the module, your function will beneffit from it too. Otherwise you're duplicating functionality |
| ||
Thanks everyone, this is much appreciated - I hadn't thought of using alignment. I'll have a go at this tomorrow: fixing another problem atm! |
| ||
Brill - works great. Many thanks. Now to combine it with a scrolling message box... :) |