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

Author Topic: Singleton with RenderWindow member and static SFML libs  (Read 1742 times)

0 Members and 1 Guest are viewing this topic.

syndicatedragon

  • Newbie
  • *
  • Posts: 3
    • View Profile
Singleton with RenderWindow member and static SFML libs
« on: July 26, 2013, 01:16:22 am »
I'm using Visual C++ 2010. The following code crashes when starting a debug session when linked to the SFML static debug libraries. It works FINE debugging when linked to the shared debug SFML libraries! Also, if you comment out the _window member, it works fine with the static libraries. Any ideas why?

#include "SFML\Graphics.hpp"

class Singleton {
protected:
        Singleton() { }
        static Singleton _singleton;
        sf::RenderWindow _window;
};

Singleton Singleton::_singleton;

int main(int argc, char *argv[])
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(shape);
        window.display();
    }
        return 0;
}

FRex

  • Hero Member
  • *****
  • Posts: 1843
    • View Profile
    • My GitHub Page
    • Email
Re: Singleton with RenderWindow member and static SFML libs
« Reply #1 on: July 26, 2013, 01:45:46 am »
You can't do that, windows can't be global or static or inside globals or statics because of some opengl things that happen behind the scenes.
ZipSavings, script to count rar/7z/zip savings: https://goo.gl/vvBj5M
LuaConsole: https://goo.gl/X4kRUk
FoxRaycaster: https://goo.gl/27nVS8
Small Games - Heart, Routing and Snek: https://goo.gl/15ZGWE https://goo.gl/k5gwWD https://goo.gl/4nKPnT
Botes - a notes app in Pascal: https://goo.gl/bzTqsi

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9558
    • View Profile
    • development blog
    • Email
Re: Singleton with RenderWindow member and static SFML libs
« Reply #2 on: July 26, 2013, 05:06:21 am »
You can't do that, windows can't be global or static or inside globals or statics because of some opengl things that happen behind the scenes.
In theory you can have global variables of it, but the initialization must happen in local space. Since I said theory, I mean theory. In practice you should not be using global variables and since singletons are kinda of the same, you shouldn't use them either. There are enough posts about this on the forum with detailed explanations and discussions for why etc., don't feel like repeating it yet again. :D
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

syndicatedragon

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Singleton with RenderWindow member and static SFML libs
« Reply #3 on: July 26, 2013, 04:48:55 pm »
OK, that's good to know. Honestly I had not seen any indication that it can't be used in this way in the tutorials or documentation.

I'm curious though, why does it work with the dynamic libs?

FRex

  • Hero Member
  • *****
  • Posts: 1843
    • View Profile
    • My GitHub Page
    • Email
Re: Singleton with RenderWindow member and static SFML libs
« Reply #4 on: July 26, 2013, 07:11:45 pm »
Order of initializing globals and statics like that(ie. not in functions, in functions statics are initialized on first function call) is not known at all so sometimes it works and sometimes not.
« Last Edit: July 26, 2013, 07:13:38 pm by FRex »
ZipSavings, script to count rar/7z/zip savings: https://goo.gl/vvBj5M
LuaConsole: https://goo.gl/X4kRUk
FoxRaycaster: https://goo.gl/27nVS8
Small Games - Heart, Routing and Snek: https://goo.gl/15ZGWE https://goo.gl/k5gwWD https://goo.gl/4nKPnT
Botes - a notes app in Pascal: https://goo.gl/bzTqsi

 

anything