Skip to main content

Text / UI

PixelStorm gives you two paths for drawing text:

  • Application::DrawText()
  • UI::Print()

Both routes end up in the same text queue. The difference is mostly convenience.

Anchors

Text drawing now supports anchors so you can center a label without manually adjusting its position.

AnchorEffect
TextAnchor::TopLeftdefault, no offset
TextAnchor::TopCentercenters horizontally
TextAnchor::TopRightaligns the top-right corner
TextAnchor::MiddleLeftcenters vertically
TextAnchor::MiddleCentercenters horizontally and vertically
TextAnchor::MiddleRightaligns the middle-right edge
TextAnchor::BottomLeftaligns the bottom-left corner
TextAnchor::BottomCentercenters horizontally and aligns the bottom
TextAnchor::BottomRightaligns the bottom-right corner

Application::DrawText

This is the base method.

ParameterUse
textstring to draw
positionposition in logical pixels
colorRGBA tint
scaletext scale
followCameraworld text or fixed HUD
anchoralignment relative to position

Rules

  • empty strings are ignored
  • text is queued and drawn later
  • the current default font is used

Example

app.DrawText("Loading...", Vec2(320.0f, 180.0f), Colors::White(), 1.0f, false, TextAnchor::MiddleCenter);

UI

UI is a static wrapper over the active application.

MethodUse
Bind(app)connects the application
Unbind()breaks the connection
Print(...)queues text

When To Use It

  • in scenes, so you do not have to pass Application & everywhere
  • in gameplay code where you want a quick HUD access point

Example

UI::Print(
"Press Interact to change scene",
Vec2(320.0f, 180.0f),
Colors::White(),
1.0f,
false,
TextAnchor::MiddleCenter);

World Text and Fixed HUD

CasefollowCamera
HUDfalse
NPC name labeltrue
contextual helpfalse

Mixed Example

UI::Print("Score: 120", Vec2(12.0f, 12.0f), Colors::White(), 1.0f, false);
UI::Print("Merchant", npc.Transform().GetPosition() + Vec2(0.0f, -24.0f), Colors::White(), 1.0f, true);

ToString() from pixelstorm/core/Math.h is useful here when you want to show values like positions or coordinates:

UI::Print("Pos: " + ToString(player.Transform().GetPosition()), Vec2(12.0f, 28.0f), Colors::White(), 1.0f, false);

Practical Notes

  • UI::Print() does nothing if UI::Bind(app) was not called
  • the default PixelStormMini.ttf font is loaded automatically by Application
  • if you replace the default font, remember to set it as the new default before drawing text
tip

If UI::Print() does not draw anything, first check that UI::Bind(app) was called and that a default font is available.