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

Author Topic: Casting integers for setPosition() properly  (Read 2982 times)

0 Members and 1 Guest are viewing this topic.

metafurionx

  • Newbie
  • *
  • Posts: 13
    • View Profile
Casting integers for setPosition() properly
« on: April 12, 2017, 04:35:57 am »
Using SFML 2.4.1, C++11. Basic question.
I want to position a sprite using integer variables.

Given
int x = 120, y = 300;
sf::Sprite my_sprite;
, should I do
my_sprite.setPosition(static_cast<float>(x), static_cast<float>(y));
or
my_sprite.setPosition(sf::Vector2f(sf::Vector2i(x, y)));
or another way?

And why should I do it that way?

Turbine

  • Full Member
  • ***
  • Posts: 102
    • View Profile
Re: Casting integers for setPosition() properly
« Reply #1 on: April 12, 2017, 08:08:17 am »
There's a few ways to go about this. Modern compilers generally support initialiser lists. So you could write:
Quote
sprite.setPosition({(float)x, (float)y});

There's also a compiler flag which lets the compiler automagically convert the int to float, although it's not recommended. (-fexceptions) Personally, I'd just keep the types native. Converting between the two comes at a cost.

The third way is to modify SFML's vector class and allow say - a template value.
« Last Edit: April 12, 2017, 09:59:36 am by Turbine »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Casting integers for setPosition() properly
« Reply #2 on: April 12, 2017, 10:20:06 am »
So you could write:
Quote
sprite.setPosition({(float)x, (float)y});

There's also a compiler flag which lets the compiler automagically convert the int to float, although it's not recommended. (-fexceptions) Personally, I'd just keep the types native. Converting between the two comes at a cost.

The third way is to modify SFML's vector class and allow say - a template value.
All three suggestions are bad.
Don't use C-casts.
Don't use compiler tricks.
Don't modify the SFML source code.

You do want to use floats for the entity logic, otherwise you can't create smooth movement (the resolution was just per pixel), so you probably want to use an sf::Vector2f.
Then before you draw you might want to have some conversion layer that will round the float position to integer positions.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Casting integers for setPosition() properly
« Reply #3 on: April 12, 2017, 08:16:05 pm »
If, after reading eXpl0it3r's advice, you still need to store as integer and convert to float for the position, I would probably use the first version (explicitly static_casting).
However, you should probably consider storing them as a sf::Vector2i to start with, which means the second version makes more sense:
sf::Vector2i position{ 120, 300 };
sf::Sprite my_sprite;
my_sprite.setPosition(sf::Vector2f(position));
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

metafurionx

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Casting integers for setPosition() properly
« Reply #4 on: April 12, 2017, 09:38:45 pm »
If, after reading eXpl0it3r's advice, you still need to store as integer and convert to float for the position, I would probably use the first version (explicitly static_casting).
However, you should probably consider storing them as a sf::Vector2i to start with, which means the second version makes more sense:
sf::Vector2i position{ 120, 300 };
sf::Sprite my_sprite;
my_sprite.setPosition(sf::Vector2f(position));
That's precisely the case. I shouldn't have loose variables for positions to start with.
Thanks a lot, you made it really clear. Gotta do some code cleaning now!