SFML community forums

Help => Graphics => Topic started by: Dark Goomba on March 22, 2014, 10:15:54 pm

Title: Organizing Code with Load Texture
Post by: Dark Goomba on March 22, 2014, 10:15:54 pm
Hello everyone.
I am still a beginner on sfml. But I was wondering if there's a way to separate all the messy texture.loadFromFile("picture") with the rest of the code. Because Main.cpp is getting overloaded with this:

     sf::Texture texGoomba;
     sf::Sprite sprGoomba;
     if (!texGoomba.loadFromFile("goomba.png"))
          std::cout << "Error: goomba.png could not load" << std::endl;
     sprGoomba.setTexture(texGoomba);
     sprGoomba.setPosition(0,0);

So I tried putting all of that on to resource.cpp as a static member function,

     //resource.cpp
     void CResource::loadGoomba()
     {
          sf::Texture texGoomba;
          ...
     }

Though it's loaded on resource.cpp, main.cpp doesn't seem to recognize sprGoomba.
Well, if there's any better way to do this, any help would be appreciated.
Title: Re: Organizing Code with Load Texture
Post by: Jebbs on March 23, 2014, 12:22:15 am
  //resource.cpp
     void CResource::loadGoomba()
     {
          sf::Texture texGoomba;
          ...
     }

Well, declaring the sf::Texture and sf::Sprite inside a function means that they are now local to that function. The stuff in your main will never be able to see them.

Make sure you have a good understanding of C++ before you dig too deep into SFML.


Also, don't forget about the code tags. It makes things much easier to look at. ;)
Title: Re: Organizing Code with Load Texture
Post by: Jesper Juhl on March 23, 2014, 03:14:09 am
I think this is what you want: http://www.bromeon.ch/libraries/thor/v2.0/tutorial-resources.html