Drawing Texts - sinusinu/Flora GitHub Wiki
Font
Texts can be drawn using a Font
. Font
requires TTF
font file to be loaded.
âšī¸ Same as Texture: Place any TrueType font file as font.ttf
on build folder.
Begin with Simple Program:
MyCore.cs
using Flora;
using Flora.Gfx;
using Flora.Util;
namespace FloraTest {
class MyCore : FloraCore {
Font font;
public override void Prepare() {
font = new Font(PathUtils.Relative("font.ttf"), 24);
}
public override void Pause() {
}
public override void Resume() {
}
public override void Resize(int width, int height) {
}
public override void Render(float delta) {
Gfx.Begin();
Gfx.Clear(0, 0, 0);
font.Draw("Hello Flora!", 0, 0);
Gfx.End();
}
public override void Cleanup() {
font.Dispose();
}
}
}
You will see the text "Hello Flora!" written on top-left corner of the screen.
new Font(PathUtils.Relative("font.ttf"), 24)
creates a font from file font.ttf
and size of 24.
To draw text using created font, call Font.Draw
. Notice that we are using Draw
from Font
, not Gfx
.
Font
generates and caches needed glyphs on the fly, so there is no need to pre-generate anything.
Because Font
also handles low-level resources, it must be disposed by calling Dispose()
function after use.
Advanced uses
Font
have some useful functions you can use.
Continue with above program:
using Flora;
using Flora.Gfx;
using Flora.Util;
namespace FloraTest {
class MyCore : FloraCore {
Font font;
public override void Prepare() {
font = new Font(PathUtils.Relative("font.ttf"), 24);
font.SetHinting(FontHinting.None);
}
public override void Pause() {
}
public override void Resume() {
}
public override void Resize(int width, int height) {
}
public override void Render(float delta) {
Gfx.Begin();
Gfx.Clear(0, 0, 0);
font.SetScale(0.33f);
font.SetColor(0.33f, 0.33f, 0.33f, 1f);
font.Draw("Hello Flora!", 0, 0);
font.SetScale(0.67f);
font.SetColor(0.67f, 0.67f, 0.67f, 1f);
font.Draw("Hello Flora!", 0, 0);
font.SetScale(1f);
font.SetColor(1f, 1f, 1f, 1f);
font.Draw("Hello Flora!", 0, 0);
Gfx.End();
}
public override void Cleanup() {
font.Dispose();
}
}
}
SetHinting
sets hinting of the Font
. Be aware that it clears the font cache, so repeated call will make the application slow down a lot.
Color and Scale of the Font
can be set using SetColor
and SetScale
respectively. These can be called in a loop.