Skip to content

Crosshair

Hapaxia edited this page Sep 27, 2020 · 4 revisions

Introduction

A class to draw an automatic crosshair.
Crosshair is automatically updated to be positioned at the mouse cursor position. If the mouse is outside of the specified window, Crosshair is not drawn.
The colour of the crosshair can be customised. The horizontal and vertical lines can have different colours.

Usage

Declaration

sw::Crosshair crosshair;
creates a crosshair that has the default colour of white with no assigned window

sw::Crosshair crosshair(sf::RenderWindow window)
creates a crosshair that has the default colour of white and assign the specified window. window can be an actual sf::RenderWindow object or omitted to clear the assignment

sw::Crosshair crosshair(sf::Color color)
creates a crosshair that has the colour of color with no assigned window

sw::Crosshair crosshair(sf::Color color, sf::RenderWindow window)
creates a crosshair that has the colour of color and assign the specified window. window can be an actual sf::RenderWindow object or omitted to clear the assignment

sw::Crosshair crosshair(sf::Color horizontalColor, sf::Color verticalColor)
creates a crosshair that has a horizontal line the colour of horizontalColor, a vertical line the colour of verticalColor with no assigned window

sw::Crosshair crosshair(sf::Color horizontalColor, sf::Color verticalColor, sf::RenderWindow window)
creates a crosshair that has a horizontal line the colour of horizontalColor, a vertical line the colour of verticalColor and assign the specified window. window can be an actual sf::RenderWindow object or omitted to clear the assignment

Drawing

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

Manipulation

Set-up

  • setWindow(sf::RenderWindow window)
    assigns the specified window. window can be an actual sf::RenderWindow object or omitted to clear the assignment

  • setColor(sf::Color color)
    sets the entire crosshair's colour to the specified sf::Color. This include both the horizontal and vertical lines (see below)

  • setHorizontalColor(sf::Color horizontalColor)
    sets the colour of the crosshair's horizontal line to the specified sf::Color

  • setVerticalColor(sf::Color verticalColor)
    sets the colour of the crosshair's vertical line to the specified sf::Color

Information

  • getPosition()
    returns a sf::Vector2f representing the current position of the crosshair's focus (using the co-ordinates of the window's view at the time of updating the vertices). This is approximately equivalent to window.mapPixelToCoords(sf::Mouse::getPosition(window));

Simple Example

#include <SFML/Graphics.hpp>
#include <SelbaWard/Crosshair.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(400, 300), "Crosshair simple example");
    sw::Crosshair crosshair(sf::Color::Green, sf::Color::Red, window);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(crosshair);
        window.display();
    }
    return EXIT_SUCCESS;
}

The code above displays:
Simple Example

(Crosshair v1.0)

Clone this wiki locally