Hi,
I want to create a element at the mouse position. I order to keep it within the window I want to clamp the mouse position to the windows dimensions. Maybe I am just being stupid here, but I can't get it to work...
Here is the code I am using:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <algorithm>
template <typename T>
bool operator <(const sf::Vector2<T>& left, const sf::Vector2<T>& right)
{
return (left.x < right.x) && (left.y < right.y);
}
template <typename T>
bool operator >(const sf::Vector2<T>& left, const sf::Vector2<T>& right)
{
return (left.x > right.x) && (left.y > right.y);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Example");
// run the program as long as the window is open
while (window.isOpen())
{
// check all the windows events
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left)
{
auto mousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window));
const auto windowSize = sf::Vector2f(window.getSize());
// clamp to window size
mousePosition = std::min(windowSize, std::max(sf::Vector2f(0.f, 0.f), mousePosition));
std::cout << mousePosition.x << "x" << mousePosition.y << std::endl;
}
}
// clear the window to black
window.clear();
// display the windows content
window.display();
}
return 0;
}
I am getting these compilation errors for the
std::max and
std::min templates: no match for 'operator<' (operand types are 'const sf::Vector2<float>' and 'const sf::Vector2<float>')
and operator> respectively. I don't really know whats wrong since I do define these operators above. Maybe somebody can tell me whats wrong.
This also got me wondering: Is there a reason why these operators are not defined by SFML?
Thanks in advance,
Foaly