Let's say we have
sf::Vector2i v;
We get
error: conversion from 'sf::Event::MouseMoveEvent' to non-scalar type 'sf::Vector2i {aka sf::Vector2<int>}' requested
when doing
v = event.mouseMove;
The following works fine, but is ugly
v = sf::Vector2i(event.mouseMove.x, event.mouseMove.y)
There also is no explicit conversion.
Is there a nicer way of doing this?
Thanks,
Kim
PS: A bit off-topic, but why does Vector2 have no parenthesis overload?
Assuming you can use C++11, you can use:
v = { event.mouseMove.x, event.mouseMove.y };
which is less ugly.
Since both types there are both int, it'll work fine. However, to convert, you can either construct a new vector:
sf::Vector2f floatVector;
// ...
floatVector = sf::Vector2f(intVector);
or cast the values directly (again, from C++11):
sf::Vector2f floatVector;
// ...
floatVector = { static_cast<float>(intVector.x), static_cast<float>(intVector.y) };
I just wanted to add that the idea of casting from one vector to another just makes it more "ugly":
static_cast<sf::Vector2f>(intVector)
vs
sf::Vector2f(intVector)
I presume by "ugly", you mean more characters...
Casting from a float to an int vector often is also a sign of wrong usage. Why do you need integer positions when you have floating point values?[...]
Just to be clear, there was no conversion from float to int. The original post was about creating a vector from components and the conversion in my reply (from int to float) was in response to their question about conversion.
I agree with you that casting from float to int can be more troublesome than originally expected. 3.9f casted to int would be 3 (as you already mentioned) but even rounding the float first doesn't guarantee it rounding upwards. round(3.9f) might result in a value very slightly below 4 so would still cast to in as 3.
That said, it's possible to fix this: static_cast<int>(std::round(3.9f) + 0.5f);. Well, that is how I would do it ;D