Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Converting sf::Event::MouseMoveEvent to sf::Vector2i  (Read 3296 times)

0 Members and 1 Guest are viewing this topic.

kim366

  • Newbie
  • *
  • Posts: 35
    • View Profile
Converting sf::Event::MouseMoveEvent to sf::Vector2i
« on: November 14, 2016, 02:42:43 pm »
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?
« Last Edit: November 14, 2016, 02:46:04 pm by kim366 »

Hapax

  • Hero Member
  • *****
  • Posts: 3353
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Converting sf::Event::MouseMoveEvent to sf::Vector2i
« Reply #1 on: November 14, 2016, 03:13:20 pm »
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) };
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

kim366

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Converting sf::Event::MouseMoveEvent to sf::Vector2i
« Reply #2 on: November 14, 2016, 03:39:26 pm »
Okay, thanks, but it is not possible to cast it all at once, you do have to make a new vector from the x and y values?

Hapax

  • Hero Member
  • *****
  • Posts: 3353
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Converting sf::Event::MouseMoveEvent to sf::Vector2i
« Reply #3 on: November 14, 2016, 03:56:49 pm »
You need to create a new vector to convert as one (this is still pretty quick).

Casting, however, is not creating a new vector. It's simply casting each value and placing that casted value into the existing vector.
I casted each value separately because I tend to need to do that as I generally cast separately when it's not just a direct conversion. I would use the vector's constructor for that.
Statically casting the vector does work, though; it simply uses the vector's constructor to convert.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10838
    • View Profile
    • development blog
    • Email
Converting sf::Event::MouseMoveEvent to sf::Vector2i
« Reply #4 on: November 14, 2016, 04:28:38 pm »
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? "Converting" from float to int will simply cut off the decimal values, which isn't what you'd always want (3.9 is closer to 4 than to 3).
And finally if you want to convert from game space to screen space, you should use the mapCoordsToPixel function that takes the view into account.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Converting sf::Event::MouseMoveEvent to sf::Vector2i
« Reply #5 on: November 14, 2016, 06:58:46 pm »
MouseMoveEvent gives X and Y separately, so yes, if you want to create a Vector2i from these coordinates, you have to... create a Vector2i from these coordinates (the thing you're calling "ugly").

Quote
A bit off-topic, but why does Vector2 have no parenthesis overload?
I have no idea what you mean. An overload of the "parenthesis" operator () is defined when you need to "call" the object, so I guess this is not what you're talking about since it makes no sense to "call" vectors.
« Last Edit: November 14, 2016, 07:00:34 pm by Laurent »
Laurent Gomila - SFML developer

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Converting sf::Event::MouseMoveEvent to sf::Vector2i
« Reply #6 on: November 14, 2016, 09:22:07 pm »
Quote
Quote
A bit off-topic, but why does Vector2 have no parenthesis overload?
I have no idea what you mean. An overload of the "parenthesis" operator () is defined when you need to "call" the object, so I guess this is not what you're talking about since it makes no sense to "call" vectors.

I think he means: user-defined conversion
I would like a spanish/latin community...
Problems building for Android? Look here

Hapax

  • Hero Member
  • *****
  • Posts: 3353
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Converting sf::Event::MouseMoveEvent to sf::Vector2i
« Reply #7 on: November 15, 2016, 12:41:42 am »
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
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*