SFML community forums

Help => System => Topic started by: SeriousITGuy on January 29, 2015, 04:13:57 pm

Title: [Solved] Contruction sf::Vector2f from sf::FloatRect - Wrong values
Post by: SeriousITGuy on January 29, 2015, 04:13:57 pm
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!
Title: Re: Contruction sf::Vector2f from sf::FloatRect - Wrong values
Post by: Hapax 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;
}
Title: Re: Contruction sf::Vector2f from sf::FloatRect - Wrong values
Post by: SeriousITGuy 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!  ;)
Title: Re: [Solved] Contruction sf::Vector2f from sf::FloatRect - Wrong values
Post by: Hapax on January 31, 2015, 04:07:25 pm
No worries. I didn't even know this happened until I saw your problem ;D
Title: Re: [Solved] Contruction sf::Vector2f from sf::FloatRect - Wrong values
Post by: Jesper Juhl 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