In general, there is a code

#include <SFML\Graphics.hpp> #include <string> namespace game { const sf::Vector2u WINDOW_SIZE(256,240); int scaleCoeff = 2; class ImageResource { public: ImageResource(std::string url); sf::Image image; sf::Texture texture; sf::Sprite sprite; }; ImageResource::ImageResource(std::string url) { this->image.loadFromFile(url); this->texture.loadFromImage(this->image); this->sprite.setTexture(this->texture); } ImageResource logo("logo.png"); } int main() { sf::RenderWindow app(sf::VideoMode(game::WINDOW_SIZE.x, game::WINDOW_SIZE.y), "!Curse"); app.setSize(sf::Vector2u(game::WINDOW_SIZE.x * game::scaleCoeff, game::WINDOW_SIZE.y * game::scaleCoeff)); while (app.isOpen()) { sf::Event ev; while (app.pollEvent(ev)) { if (ev.type == sf::Event::Closed) app.close(); } app.clear(); app.draw(game::logo.sprite); app.display(); } } 

The fact is that the compiler does not swear at anything, but when you start the program, it just falls. I found out that the problem is in the ImageResource class. I even did not use pointers as the program crashes instantly.

  • Check at least write that "logo.png" exists. Does he even exist? - JaponDemon
  • Yes it exists - ishidex2
  • Navigate error handling where possible: assertions, try - catch. And that "the program lays down" absolutely not informative. - JaponDemon
  • Congratulations on static order initialization fiasco . Make the logo local variable so that the initialization order does not depend on the weather on Mars. - Croessmah
  • What does local mean? - ishidex2

1 answer 1

Do not create SFML global static objects. In this case, the problem is that the variable logo is global static. For static global variables located in different translation units, the initialization order is not defined. You stumbled upon the so-called static order initialization fiasco . It is not known what will happen before - initialization of the logo or initialization of SFML . In your case, apparently, the logo first initialized, and, accordingly, SFML , which is not yet ready for use, is SFML .

Make the logo variable a local variable in main , this will exclude the initialization of the logo to SFML , since when calling main all static global data is already initialized.

 //... int main() { game::ImageResource logo("logo.png"); //... app.draw(logo.sprite); //... } 

If you really need a global access point to a resource, you can wrap a static object in a function that returns a reference to the resource:

 namespace game { //... ImageResource & getLogo() { static ImageResource logo("logo.png"); return logo; } } //... int main() { //... app.draw(game::getLogo().sprite); //... } 

PS And do not forget about error handling.