Skip to content
Hapaxia edited this page Dec 14, 2016 · 6 revisions

Introduction

Simple and small class to draw lines of any thickness from one point to another.

Thin lines are infinitly thin and always represented as a single pixel wide.
Thick lines are finitely thick and can also be textured.

Usage

Declaration

  • sw::Line line;
    creates a Line with a white colour, zero thickness and both positions at (0, 0).

  • sw::Line line(startPosition, endPosition);
    creates a Line with a white colour, zero thickness and positions as specified by parameters.

  • sw::Line line(startPosition, endPosition, thickness);
    creates a Line with a white colour and with the thickness and positions as specified by parameters.

  • sw::Line line(startPosition, endPosition, thickness, color);
    creates a Line with the colour, thickness and positions as specified by parameters.

Drawing

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

Transformations

This class inherits from sf::Transformable so it has all the usual SFML transformations available, if you need them.

Manipulation

  • setColor(sf::Color color);
    sets the colour of the line to the specified sf::Color, regardless of whether it's a so-called thick line or infinitely thin line.

  • setThickness(thickness)
    changes the width of the line. If the value passed to this method is zero, the line becomes "infinitely thin" and is rendered as a pixel-wide line, otherwise it's rendered as a quad with the given thickness. This is a templated method so the thickness value can be any type that can be cast to a float. This also applies to the thickness parameter in the constructor.

  • setPoints(sf::Vector2f startPosition, sf::Vector2f endPosition);
    sets both positions of the line to the specified sf::Vector2fs.

  • setPoint(unsigned int pointIndex, sf::Vector2f position);
    sets the position of the single point specified by the pointIndex to the specified sf::Vector2f. A pointIndex of 0 represents the start point and 1 represent the end point. To be more clear in your code (or to match a preference of style), there is a variety of options for specifying this ID. See the information below.

The start point ID can be specified as:
line.getStartIndex(), line.PointIndex.Start, line.Start (using line as the object name), or
sw::Line::PointIndex::Start, sw::Line::Start, or 0.

Similarly, the end point ID can be specified as:
line.getEndIndex(), line.PointIndex.End, line.End (using line as the object name), or
sw::Line::PointIndex::End, sw::Line::End, or 1.

The examples will use the first option.

Texture

  • setTexture(sf::Texture)
    sets the sf::Texture to use for the line if it is a thick line. If the texture is omitted, no texture will be used.

  • setTextureRect(sf::FloatRect textureRect)
    sets the rectangle (sf::FloatRect) of the texture to use for a thick line. Note that the line's texture is applied as if the line is drawn horizontally, left-to-right.

Bounds

This class inherits from sf::Transformable so all the usual SFML transformations (setPosition, move, rotate, setOrigin, scale etc.) can be applied to the line. Local and global bounds can also be retrieved from the line via methods that are similarly named to SFML's methods.
They are: getLocalBounds() and getGlobalBounds().
Local bounds form the bounding rectangle of the line before SFML's transformations have been applied.
Global bounds form the bounding rectangle of the line after SFML's transformations have been applied.
The SFML transformations are based on the origin and may cause unexpected effects if the origin is not explicitly set to where it should be (usually one of the line's ends or its centre).

Note: the bounds take into account the width of the line.

Information

  • getPoint(unsigned int pointIndex)
    returns an sf::Vector2f representing the position of the point specified by pointIndex, which is an unsigned int and can take all of the same values as the pointIndex in setPoint() above.

  • getColor()
    returns an sf::Color representing the line's current colour.

  • getTexture()
    returns a const reference to the texture used by the thick line. Only use if the Line has a texture set.

  • getTextureRect()
    returns the texture rectangle.

Simple Example

#include <SFML/Graphics.hpp>
#include <SelbaWard/Line.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Line simple example");
    sw::Line line({ 5.f, 5.f }, { 600.f, 450.f });
    sw::Line thickLine({ 300.f, 500.f }, { 400.f, 20.f }, 50, sf::Color::Red);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(thickLine);
        window.draw(line);
        window.display();
    }
    return EXIT_SUCCESS;
}

The code above displays:
Simple Example

(Line v1.2)

Clone this wiki locally