-
Notifications
You must be signed in to change notification settings - Fork 27
Crosshair
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.
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
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.
-
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
-
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 towindow.mapPixelToCoords(sf::Mouse::getPosition(window));
#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:
(Crosshair v1.0)