Skip to content
Hapaxia edited this page Feb 20, 2025 · 6 revisions

Introduction

A class to draw a simple ring shape (annulus).
It's similar to SFML's basic sf::CircleShape but also allows a "hole" in that circle.
It can also draw just a sector of the ring rather than the entire ring, which appears as an arc with a thickness and possibly texture.
This object automatically updates whenever a change is made.

Usage

Declaration

sw::Ring ring;
creates a ring with the default radius of 0, default hole ratio of 0.5 and the default number of sides of 36

sw::Ring ring(float radius);
creates a ring with the radius specified and the default hole ratio of 0.5 and the default number of sides of 36

sw::Ring ring(float radius, float hole);
creates a ring with the radius and hole ratio that are specified, while the number of sides is the default of 36

sw::Ring ring(float radius, float hole, std::size_t numberOfSides);
creates a ring with the radius, hole ratio, and number of sides specified

Drawing

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

Transformations

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

Manipulation

Visual Representation

  • setRadius(float radius)
    sets the radius of the ring. This defines the size of the base circle

  • setHole(float hole)
    sets the hole ratio. This sets the ratio of the hole's radius to the base circle's radius. This value is designed to be used in the range of 0 to 1 but can accept any value, including greater than 1 and less than -1.

  • setNumberOfSides(std::size_t numberOfSides)
    sets the number of sides on the ring. This defines the number of edges used to approximate the circular shape and is used for both the outer and inner circles.

  • setSectorOffset(float sectorOffset)
    sets the offset (start point) of the sector from angle zero. This value is a ratio of the full ring and is there in the range 0 to 1. Defaults to 0 to display an entire ring

  • setSectorSize(float sectorSize)
    sets the size of the sector, which is used to find end point of the sector from offset. This value is a ratio of the full ring and is there in the range 0 to 1. Defaults to 1 to display an entire ring

  • setColor(sf::Color color)
    sets the sf::Color of the ring. If a texture is used, this colour is multiplied with the texture as is normal

Texture

  • setTexture(sf::Texture texture, bool resetRect)
    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 Ring 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 set the texture to a null pointer (nullptr or NULL). If resetRect is specified and true, the texture rectangle is automatically updated to match the new texture

  • setTextureRect(sf::IntRect textureRect)
    sets the texture rectangle to use. This rectangle is linked to the base circle and will be stretched to match if necessary.

Information

  • getRadius()
    returns the base circle's radius (float)

  • getHole()
    returns the hole's ratio to the base circle (float)

  • getNumberOfSides()
    returns the number of sides on the ring (std::size_t)

  • getColor()
    returns the colour of the ring (sf::Color)

  • getArea()
    returns the area of the ring (float)

  • getSectorOffset()
    returns the current sector offset (float)

  • getSectorSize()
    returns the current sector size (float)

  • getLocalBounds()
    returns an [sf::FloatRect] that contains all of the vertices (before any SFML transformations)

  • getGlobalBounds()
    returns an [sf::FloatRect] that contains all of the vertices (after any SFML transformations)

Simple Example

#include <SFML/Graphics.hpp>
#include <SelbaWard/Ring.hpp>
int main()
{
    sf::Texture texture;
    if (!texture.loadFromFile("resources/uv map.jpg"))
        return EXIT_FAILURE;
    sf::RenderWindow window(sf::VideoMode({ 200u, 200u }), "Ring simple example");
    sw::Ring ring(100.f);
    ring.setTexture(texture);
    while (window.isOpen())
    {
        while (const auto event{ window.pollEvent() }) if (event->is<sf::Event::Closed>()) window.close();
        window.clear();
        window.draw(ring);
        window.display();
    }
    return EXIT_SUCCESS;
}

The code above displays:
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.

(Ring v1.1)