Skip to content
Download

Text on screen

You want to show the scoreboard, the lives or a message that always stays in the same place on screen.

Create an object without a sprite (for example Hud), place it in the room and draw in onRenderGui:

#include <brasa/brasa.hpp>
#include <format>
using namespace brasa;
class Hud : public Entity {
public:
void onRenderGui() override {
drawText("PRIMEIRA FASE", 4, 4);
drawText(std::format("PONTOS {}", 1500), 4, 14, colors::yellow);
}
};
BRASA_ENTITY(Hud);
  • onRenderGui draws on top of everything, in screen coordinates: (0, 0) is the top left corner, even with the camera moving.
  • drawText(text, x, y) writes the text. The color is optional: colors::white (default), colors::yellow, colors::red, colors::green, colors::black or Color{r, g, b, a}.
  • std::format builds texts with numbers: std::format("VIDAS {}", 3) becomes VIDAS 3.
  • Each letter is 8 pixels wide. Accents and cedillas work: drawText("Pontuação", 4, 4).

Center. The text takes up letters × 8 pixels:

const std::string msg = "FIM DE JOGO";
drawText(msg, (roomWidth() - msg.size() * 8.0f) / 2.0f, 80);

Bigger letters and another font. A number after the color scales up Brasa’s font (2 = 16-pixel letters). With the name of a font imported in Resources and the size in pixels, the text uses that font:

drawText("Pontuação", 4, 4, colors::white, 2);
drawText("Fim de jogo", 4, 30, colors::yellow, "Titulos", 24);

Numbers with leading zeros: std::format("{:05}", 300) becomes 00300.

Rectangles and lines, for health bars and frames:

drawRect(4, 30, 50, 4, colors::red); // filled bar
drawRect(4, 30, 50, 4, colors::white, false); // outline only