Skip to content

Gallery Sprite

Hapaxia edited this page Feb 20, 2025 · 5 revisions

Introduction

A sprite class that allows you to store multiple "exhibits" (texture rectangles and anchor points) and change the displayed exhibit at any time. This can be useful to simplify using sprite sheets and can be helpful for animation.

The exhibit can be chosen by direct index (by specifying an exhibit number) or can be offset using addition and subtraction (e.g. +1 is next exhibit).

A bonus method is included that allows you to test if a point is touching/inside the sprite's outline. That is, the actual transformed rectangle of the sprite, not the axis-aligned bounding box.

Another bonus method allows you to specify a target size that will be used to set the sprite's scale so that the current (or any specified) exhibit will be displayed at that size.

Usage

Declaration

  • sw::GallerySprite sprite;
    creates a Gallery Sprite

  • sw::GallerySprite sprite(sf::Texture);
    creates a Gallery Sprite and sets the texture to use to be the passed sf::Texture (see setTexture below)

Drawing

This class inherits from sf::Drawable so it is drawn in the same way as all SFML drawables:
window.draw(sprite);
where window is an sf::RenderWindow.

Transformations

This class inherits from sf::Transformable so it has all the usual SFML transformations available.

Manipulation

Texture

  • setTexture(sf::Texture)
    takes an sf::Texture and stores a pointer to it. This means that the texture should exist (at its current memory position) as long as Gallery Sprite requires it. This texture can be changed whenever you like and should be reset if the current texture is moved in memory. You can also use this method to nullify the texture by omitting the parameter.

Exhibits

Exhibits are parts of a texture with an attached anchor. An exhibit is offset so that its anchor is at (0, 0) therefore anchors can be useful for aligning exhibits amongst other things.

Exhibits added range from exhibit number 1 to exhibit number 'number of exhibits'. That is, the exhibition number (for custom added exhibitions) starts at 1, not 0. However, the exhibit number zero can still be used; it always specifies the entire texture with a zeroed anchor. Therefore, any 'setting' methods cannot specify exhibit number zero as it cannot be modified. An attempt to use modify exhibit number zero will result in undefined behaviour.

sw::GallerySprite::Exhibit (hereafter shortened to just Exhibit) has two members: rectangle, which is the texture rectangle (sf::FloatRect) of the exhibit in the texture, and anchor, which is the exhibit's anchor (sf::Vector2f). The anchor is specified as an offset from the exhibit's top-left corner.

  • addExhibit(Exhibit exhibit)
    adds an exhibit to the gallery. Also returns the exhibit number. Note that specifying only an sf::FloatRect here would implicitly create an exhibit with that rectangle and a zero anchor.

  • setExhibit(std::size_t exhibitNumber, Exhibit exhibit)
    changes the exhibit specified by exhibitNumber to the supplied exhibit.

  • setRect(std::size_t exhibitNumber, sf::FloatRect rect)
    changes just the texture rectangle of the exhibit specified by exhibitNumber to the supplied rectangle (sf::FloatRect).

  • setAnchor(std::size_t exhibitNumber, sf::Vector2f anchor)
    changes just the anchor of the exhibit specified by exhibitNumber to the supplied anchor (sf::Vector2f).

Visual Representation

Gallery Sprite updates automatically whenever something is modified.

  • setColor()
    sets the colour of the Gallery Sprite. This colour, as with a standard sf::Sprite, is 'multiplied' with the texture. Note that if no texture exists, this becomes the colour of the resulting rectangle.

  • setScaleFromTargetSize(sf::Vector2f targetSize, std::size_t exhibitNumber)
    sets the scale of the Gallery Sprite so that the exhibit specified by exhibitNumber would be displayed at the targetSize. If exhibitNumber is omitted, the current exhibit is used).

Main

  • set(std::size_t exhibitNumber)
    sets the current exhibit to the exhibit specified by exhibitNumber

Offset Operators

Gallery Sprite overloads some operators to simplify changing exhibits. Note that no bounds checking is performed and using an exhibit number that does not exist results in undefined behaviour.

Only the operators shown here have been overloaded.

  • ++sprite
    increases the exhibit number by one. Also returns itself so that it can be used in an expression e.g ++sprite.getExhibit();. Note that only the prefix form is available.

  • --sprite
    decreases the exhibit number by one. Also returns itself so that it can be used in an expression e.g --sprite.getExhibit();. Note that only the prefix form is available.

  • sprite += 5
    increases the exhibit number by the number added to it.

  • sprite -= 5
    decreases the exhibit number by the number subtracted from it.

Information

Texture

  • getTexture()
    returns an const reference to the sf::Texture used by the Gallery Sprite.

Exhibits

  • getExhibit(std::size_t exhibitNumber)
    returns the Exhibit (sw::GallerySprite::Exhibit) specified by exhibitNumber. If exhibitNumber is omitted, exhibit zero is returned. Note that this is a copy of the stored exhibit.

  • getRect(std::size_t exhibitNumber)
    returns the texture rectangle (sf::FloatRect) of the exhibit specified by exhibitNumber. If exhibitNumber is omitted, exhibit zero is used. Note that this is a copy of the stored rectangle.

  • getAnchor(std::size_t exhibitNumber)
    returns the anchor (sf::Vector2f) of the exhibit specified by exhibitNumber. If exhibitNumber is omitted, exhibit zero is used. Note that this is a copy of the stored anchor.

Visual Representation

  • getColor()
    returns an sf::Color representing the colour of the Gallery Sprite.

  • getSize(std::size_t exhibitNumber)
    returns an sf::Vector2f representing the size of the exhibit specified by exhibitNumber (exhibit number zero being the entire texture). If exhibitNumber is omitted, the current exhibit is used.

Main

  • get()
    returns an unsigned int representing the number of the current exhibit

  • getNumberOfExhibits()
    returns an std::size_t representing the number of exhibits available. Note that exhibit number zero is not included.

Boundaries

  • getLocalBounds()
    returns an sf::FloatRect that contains the Gallery Sprite before any SFML transformations

  • getGlobalBounds()
    returns an sf::FloatRect that contains the Gallery Sprite after any SFML transformations

  • contains(sf::Vector2f point)
    returns a bool representing if the point (sf::Vector2f) is inside the sprite outline. Note that this is the actual transformed rectangle of the sprite, not the axis-aligned bounding box.

Simple Example

#include <SFML/Graphics.hpp>
#include <SelbaWard/GallerySprite.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode({ 150u, 100u }), "Gallery Sprite simple example");
    sf::Texture texture;
    if (!texture.loadFromFile("resources/Simple Tileset.png"))
        return EXIT_FAILURE;
    sw::GallerySprite sprite(texture); // create Gallery Sprite that uses 'texture'
    sprite.addExhibit({ { { 0.f, 0.f }, { 32.f, 32.f } } }); // add exhibit (number 1) representing top-left tile
    sprite.addExhibit({ { { 32.f, 0.f }, { 32.f, 32.f } } }); // add exhibit (number 2) representing top-right tile
    sprite.addExhibit({ { { 0.f, 32.f }, { 32.f, 32.f } } }); // add exhibit (number 3) representing bottom-left tile
    sprite.addExhibit({ { { 32.f, 32.f }, { 32.f, 32.f } } }); // add exhibit (number 4) representing bottom-right tile
    sf::Clock clock;
    while (window.isOpen())
    {
        while (const auto event{ window.pollEvent() }) if (event->is<sf::Event::Closed>()) window.close();
        sprite.set(clock.getElapsedTime().asMilliseconds() / 250 % 5); // sets exhibit based on clock, which cycles through exhibits
        window.clear();
        window.draw(sprite);
        window.display();
    }
    return EXIT_SUCCESS;
}

The code above displays cycles through all the exhibits, one per 250ms (quarter of a second). There are five exhibits to show here because exhibit zero is always the entire texture.

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

(GallerySprite v1.1)