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

Author Topic: Organizing Code with Load Texture  (Read 1634 times)

0 Members and 1 Guest are viewing this topic.

Dark Goomba

  • Newbie
  • *
  • Posts: 9
    • View Profile
Organizing Code with Load Texture
« 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.
Goombas are mushrooms.

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Organizing Code with Load Texture
« Reply #1 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. ;)
DSFML - SFML for the D Programming Language.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Organizing Code with Load Texture
« Reply #2 on: March 23, 2014, 03:14:09 am »