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

Author Topic: [Solved] Contruction sf::Vector2f from sf::FloatRect - Wrong values  (Read 4304 times)

0 Members and 1 Guest are viewing this topic.

SeriousITGuy

  • Full Member
  • ***
  • Posts: 123
  • Still learning...
    • View Profile
Hi Folks,

I have a pretty weird problem with contructing a sf::Vector2f from a sf::FloatRect, the x and y values of the vector are wrong. I create a sf::FloatRect for my game world, and want to create a sf::Vector2f for the spawn point of my character, which should spawn in the middle of the game world. I calculate the spawn position like this:

GameWorld::GameWorld( sf::RenderWindow& window)
  : _window( window )
[ ... ]
  , _worldBounds( 0.f, 0.f, 1600.f, 1200.f )
  //, _spawnPosition( 800.f, 600.f )
  , _spawnPosition( _worldBounds.width / 2.f, _worldBounds.height / 2.f )
 

The result should be 800, 600, but in reality is
_spawnPosition.x = -53687088.000000
_spawnPosition.y = -53687088.000000

Code architecture is based on the SFML Book code base.
Code Example
(click to show/hide)

Visual Studio 2013.4 with SFML 2.2 stable

I can't track down the source of the problem. Any thoughts?
Every suggestion welcome, I go crazy about this.

Cheers!
« Last Edit: January 30, 2015, 08:21:22 am by SeriousITGuy »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Contruction sf::Vector2f from sf::FloatRect - Wrong values
« Reply #1 on: January 29, 2015, 04:31:24 pm »
_spawnposition is created before _worldbounds so its initialisation is first.

This works:
#include <SFML/Graphics.hpp>
#include <iostream>

struct Test
{
        sf::FloatRect m_worldBounds;
        sf::Vector2f m_spawnPosition;
        Test():
                m_worldBounds(0.f, 0.f, 1600.f, 1200.f),
                m_spawnPosition(m_worldBounds.width / 2.f, m_worldBounds.height / 2.f)
        {
        }
};

int main()
{
        Test test;
        std::cout << test.m_worldBounds.width << "x" << test.m_worldBounds.height << std::endl;
        std::cout << "(" << test.m_spawnPosition.x << ", " << test.m_spawnPosition.y << ")" << std::endl;

        return EXIT_SUCCESS;
}

but this does not:
#include <SFML/Graphics.hpp>
#include <iostream>

struct Test
{
        sf::Vector2f m_spawnPosition; // wrong way around
        sf::FloatRect m_worldBounds; // wrong way around
        Test():
                m_worldBounds(0.f, 0.f, 1600.f, 1200.f),
                m_spawnPosition(m_worldBounds.width / 2.f, m_worldBounds.height / 2.f)
        {
        }
};

int main()
{
        Test test;
        std::cout << test.m_worldBounds.width << "x" << test.m_worldBounds.height << std::endl;
        std::cout << "(" << test.m_spawnPosition.x << ", " << test.m_spawnPosition.y << ")" << std::endl;
       
        return EXIT_SUCCESS;
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

SeriousITGuy

  • Full Member
  • ***
  • Posts: 123
  • Still learning...
    • View Profile
Re: Contruction sf::Vector2f from sf::FloatRect - Wrong values
« Reply #2 on: January 30, 2015, 08:21:09 am »
Doh, going over the code hundreds of times and I didn't notice the wrong order in the class declaration. Just one of those days I guess. Thanks, it works now!  ;)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Solved] Contruction sf::Vector2f from sf::FloatRect - Wrong values
« Reply #3 on: January 31, 2015, 04:07:25 pm »
No worries. I didn't even know this happened until I saw your problem ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Solved] Contruction sf::Vector2f from sf::FloatRect - Wrong values
« Reply #4 on: January 31, 2015, 04:15:16 pm »
The C++ standard is fairly explicit in mentioning that member initialization happens in declared order (regardless of the order you mention them in initialization lists).
The standard can be a bitch to read though, so if you want a more human readable explanation of the details, then read the "Initialization order" section of this page: http://en.cppreference.com/w/cpp/language/initializer_list
« Last Edit: January 31, 2015, 04:34:45 pm by Jesper Juhl »

 

anything