Skip to content

Bitmap Text

Hapaxia edited this page Feb 20, 2025 · 9 revisions

Introduction

A bitmap text object that displays text using Selba Ward's Bitmap Font.

Usage

Declaration

sw::BitmapText text;

Usage

Usage of Bitmap Text is similar to SFML's text object: you set which font to use (in this case, it's a Selba Ward Bitmap Font), set its string, colour, scale, and all the other transformations available to standard SFML transformable objects (e.g. position, rotation, etc.). Bitmap Text also allows you to set the text's tracking - the global letter spacing.
Bitmap Text includes Bitmap Font.

  • setBitmapFont(sw::BitmapFont&)
    sets which bitmap font to use. If the parameter is omitted, a font is no longer used (the text no longer displays anything until a font is set).

  • setString(std::string&)
    sets the string of the text.

  • setColor(sf::Color&)
    sets the sf::Color with which to multiply the font texture (white is the original texture).

  • setScale(sf::Vector2u)
    setScale(unsigned int scaleX, unsigned int scaleY)
    sets the scale of the bitmap text.

  • setScale(unsigned int)
    sets both x and y scale of the bitmap text identically.

  • setTracking(int)
    sets the tracking i.e. the global letter spacing. Negative values are also accepted.

  • getColor()
    returns the current sf::Color that is multiplied with the font texture.

  • getTracking()
    returns a signed integer representing the current tracking.

  • getLocalBounds()
    returns an sf::FloatRect that contains the Bitmap Text* before any SFML transformations.

  • getGlobalBounds()
    returns an sf::FloatRect that contains the Bitmap Text* after any SFML transformations.

* The bounds are taken from the entire rectangular glyphs, not just the visible parts. This means that transparent parts of glyphs count as a part of that glyph/character and are therefore included in the bounds calculations.

Exceptions

Bitmap Text does not throw any exceptions itself.

Displaying

This is a drawable text class and can be drawn similarly to any SFML drawable: renderTarget.draw(bitmapText);

Simple Example

#include <SFML/Graphics.hpp>
#include <SelbaWard/BitmapText.hpp>
#include <iostream>
int main()
{
    sf::RenderWindow window(sf::VideoMode({ 250u, 40u }), "Bitmap Font/Bitmap Text simple example");
    sw::BitmapFont font;
    sw::BitmapText text;
    sf::Texture fontTexture;
    if (!fontTexture.loadFromFile("resources/Selba Ward Bitmap Font 0001.png")) // available from https://github.com/Hapaxia/SelbaWard/tree/master/examples/resources
    {
        std::cerr << "Could not load font texture." << std::endl;
        return EXIT_FAILURE;
    }
    font.setExternalTexture(fontTexture);
    font.setNumberOfTilesPerRow(16u);
    font.setDefaultTextureRect({ { 0, 0 }, { 8, 8 } });
    text.setBitmapFont(font);
    text.setString("This is a bitmap font.");
    text.setPosition({ 10.f, 20.f });
    while (window.isOpen())
    {
        while (const auto event{ window.pollEvent() }) if (event->is<sf::Event::Closed>()) window.close();
        window.clear();
        window.draw(text);
        window.display();
    }
    return EXIT_SUCCESS;
}

The code above displays a single string displayed using a Bitmap Font:
Simple Example

Note: the texture for this example is available, along with more examples, in the examples folder, although you can use your own images.

(Bitmap Text v1.1)

Clone this wiki locally