Polycode provides text rendering support using True Type and Open Type fonts via the Freetype library. You can render text to textures or use the SceneLabel entity to add text directly to your scenes.

Contents

Adding text to a scene.

You can use the SceneLabel class to add text as an entity directly to your scene. A SceneLabel is just a TYPE_VPLANE ScenePrimitive, that wraps the Label class, which is a Texture that can render text, with that texture set as the primitive's main texture.

To create a SceneLabel, we pass it the initial label text, the pixel height of the rendered text, the font name, antialiasing method and the actual label height in the scene. Polycode comes with 3 default fonts: sans, serif and mono. The two antialiasing options are ANTIALIAS_FULL and ANTIALIAS_NONE.

C++
Scene *scene = new Scene(Scene::SCENE_2D);
SceneLabel *label = new SceneLabel("Hello, World!", 64, "sans", Label::ANTIALIAS_FULL, 0.1);
scene->addChild(label);
Lua
scene = Scene(Scene.SCENE_2D)
label = SceneLabel("Hello, World!", 64, "sans", Label.ANTIALIAS_FULL, 0.1)
scene:addChild(label)

Loading custom fonts.

You can load your own True Type or Open Type fonts to use in labels. You can do this via the FontManager class in the CoreServices singleton. After you register the font, you can pass the name you register it with as the font name to a SceneLabel.

C++
CoreServices::getInstance()->getFontManager()->registerFont("MyFont", "MyFont.ttf");
SceneLabel *label = new SceneLabel("Hello, World!", 64, "MyFont", Label::ANTIALIAS_FULL, 0.1);
Lua
Services.FontManager:registerFont("CustomFont", "MyFont.ttf")
label = SceneLabel("Hello, World!", 64, "MyFont", Label.ANTIALIAS_FULL, 0.1)

Color text.

Since SceneLabels are subclassed from a SceneMesh, you can just set their color to set the color of the whole label. But if you want to set color to individual letters within the label, you can use the setColorForRange method of Label before calling setText().

C++
Scene *scene = new Scene(Scene::SCENE_2D);
SceneLabel *label = new SceneLabel("", 64, "sans", Label::ANTIALIAS_FULL, 0.1);
label->getLabel()->setColorForRange(Color(1.0, 0.0, 0.0, 1.0), 0, 5);
label->getLabel()->setColorForRange(Color(0.0, 0.0, 1.0, 1.0), 6, 12);
label->setText("Hello World!");
scene->addChild(label);
Lua
scene = Scene(Scene.SCENE_2D)
label = SceneLabel("", 64, "sans", Label.ANTIALIAS_FULL, 0.1)
label:getLabel():setColorForRange(Color(1.0, 0.0, 0.0, 1.0), 0, 5)
label:getLabel():setColorForRange(Color(0.0, 0.0, 1.0, 1.0), 6, 12)
label:setText("Hello World!")
scene:addChild(label)