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

Author Topic: problem using texture [solved]  (Read 1745 times)

0 Members and 1 Guest are viewing this topic.

sc

  • Newbie
  • *
  • Posts: 7
    • View Profile
problem using texture [solved]
« on: March 20, 2016, 10:56:12 am »
Hi guys !

I have been playing around with the tilemap class ( http://www.sfml-dev.org/tutorials/2.3/graphics-vertex-array.php#example-tile-map ), and everything is working fine : I can display 2 'test' layers in one of the viewports I set up.

I next wanted to display the tileset texture that was loaded inside the tilemap class in a second viewport. But here, I ran into trouble and I don't understand why ...

class TileMap : public sf::Drawable, public sf::Transformable {
   public:
        bool load() {
           if (!texTileSet.loadFromFile("maps/newTileSet.png")) {
                return false;
           }
           //...
        }

        sf::Texture getTileSet() { //THIS IS NOT WORKING
           return texTileSet;
        }

        //...

   private:
        sf::Texture texTileSet;
        //...
}


Main.cpp
int main() {
   //...
   TileMap tileMap;
   if (!tileMap.load()) {
        return -1;
   }

   sf::Sprite sprite;
   sprite.setTexture(tileMap.getTileSet());

   window.draw(sprite);
   //...
}
 

The result of this code : "not_ok.png" (no errors or crashes).

On the other hand, if I reload the texture inside main, all is fine.
Main.cpp
int main() {
   //...
   TileMap tileMap;
   if (!tileMap.load()) {
        return -1;
   }

   sf::Texture texTileSet;
   if (!texTileSet.loadFromFile("maps/newTileSet.png")) { //THIS IS OK
           return false;
   }

   sf::Sprite sprite;
   sprite.setTexture(tileMap.getTileSet());

   window.draw(sprite);
   //...
}
 

The result of this code : "ok.png".

What am I doing wrong here ?   :-[
Thanks !
« Last Edit: March 20, 2016, 01:27:26 pm by sc »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: problem using texture
« Reply #1 on: March 20, 2016, 11:49:29 am »
You return a copy of the texture.

Try this instead:

const sf::Texture& getTileSet()
Laurent Gomila - SFML developer

sc

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: problem using texture
« Reply #2 on: March 20, 2016, 01:26:51 pm »
You were absolutely right !

Coming from Java, it is a bit of a strange concept that a getter also returns a copy ...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: problem using texture [solved]
« Reply #3 on: March 20, 2016, 02:22:01 pm »
In C++, you have copy semantics everywhere, unless you explicitly specify otherwise.

Another concept that Java doesn't have is const-correctness: you should consider declaring functions that don't change the *this object as const.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

sc

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: problem using texture [solved]
« Reply #4 on: March 20, 2016, 02:30:34 pm »
Ah ... OK.
Thank you very much for the tips !