An easier way to learn how to use the Haxe core and related technologies.
Tagged with: Kha
Drawing text in Kha requires a couple of steps:
Assets
directorydrawString
Here's a minimal example demonstrating these steps, assuming that you downloaded the Inconsolata
font into Assets/fonts/inconsolata_regular.ttf
:
package;
import kha.Framebuffer;
import kha.Scheduler;
import kha.System;
import kha.Image;
import kha.Scaler;
import kha.Color;
import kha.Font;
import kha.Assets;
class Unknown
{
private static var bgColor = Color.fromValue(0x26004d);
private var font:Font;
private var initialized:Bool = false;
public function new()
{
System.notifyOnRender(render);
Scheduler.addTimeTask(update, 0, 1 / 60);
Assets.loadEverything(function()
{
initialized = true;
font = Assets.fonts.Inconsolata_Regular;
});
}
public function render(framebuffer:Framebuffer): Void
{
if (!initialized)
{
return;
}
frames += 1;
// clear our backbuffer using graphics2
var g = framebuffer.g2;
g.begin(bgColor);
g.font = font;
g.fontSize = 24;
g.color = Color.fromValue(0xFF0000); // red text
g.drawString("Hello, world!", 50, 20); // Draw "Hello, World!" at (50, 20)
g.end();
}
}
This is an untested example, with lots of things removed (eg. back buffering). It demonstrates the main steps:
Assets.loadEverything
)