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

Author Topic: Problems with loading Texture in a seperate class  (Read 1202 times)

0 Members and 1 Guest are viewing this topic.

Crounus

  • Newbie
  • *
  • Posts: 1
    • View Profile
Problems with loading Texture in a seperate class
« on: January 18, 2013, 08:23:30 am »
Greetings,

I actually playing around with SFML 2.0 for learning purposes. Well you know how that works ... you writing everything in main(), see how it works and what is happening and so on.

Well, for now my little game is running. Loading the texture is working fine and I'm be able to move the texture around (which is exactly a sort of tileset) with my Arrow keys and at least, it animates, too. Next step for me would be to get a little bit more OO, so I created a new class and named it player_main and moved the essential things which have to do with the player (for e.g. loading texture of player, creating the animation, move player with arrow keys and so on) from my main to player_main. This worked well so far, too, up to the point where the texture should loaded.
In my player_main I have a function which is called "init()". This init function is called one time in my main function in fact at the same position as the texture loading was before. The only thing I've getting now is a white box instead of the loaded tileset.
Because of this, I've added the "EXIT_FAILURE" output to my "texture.loadformfile" line. Well, this EXIT_FAILURE just give me the output "1" but at least I've found out on this way that something is going wrong while loading the texture.

To get some more validity in this text, here are some snippets.

main.cpp
...
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML windows");

    //sf::Texture texture;
    //if (!texture.loadFromFile("/home/stefan/SFMLTest/img/Baurn_Moosader.png"))
    //    return EXIT_FAILURE;
   

    //Player class
    player_main *player = new player_main();
    player->init();
...
 

player_main.h
...
class player_main
{
    public:
        player_main();
        void init();
        void move_left();
        void move_right();
...
 

player_main.cpp
#include "player_main.h"
...
void player_main::init()
{
    std::cout << "init";
    //The texture which is the player useing
    sf::Texture texture;
    texture.loadFromFile("/home/stefan/SFMLTest/img/Baurn_Moosader.png");

    player_right.setSpriteSheet(texture);
    player_right.addFrame(sf::IntRect(0, 145, 30, 46));
    player_right.addFrame(sf::IntRect(32,145, 30, 46));
...
 

I've added the std::cout in the init function just to check I've the function is called at all. The curios thing about this is, that the "init" output line does not show up in the console. Only after closing the SFML window I get the output. As far as I can see this means the function does get executed after shutting down the SFML window?!

It would be awesome if someone could help me out here ...

Greetings
Crounus

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Problems with loading Texture in a seperate class
« Reply #1 on: January 18, 2013, 10:00:22 am »
Hi.
Your sf::Texture is local to your player_main::init function, so it is destroyed at the end of the function. That's why you get a white sprite. It's a very recurrent mistake, you could probably have found the answer by googling sfml white sprite. ^^

You have to keep your sf::Texture alive in memory. You can make it a member of player_main, or use some kind of texture manager, etc.
It's even written in the doc :
Quote
It is important to note that the sf::Sprite instance doesn't copy the texture that it uses, it only keeps a reference to it. Thus, a sf::Texture must not be destroyed while it is used by a sf::Sprite (i.e. never write a function that uses a local sf::Texture instance for creating a sprite).
Oh, and for your cout weird behavior, you probably need some std::flush or std::endl. (endl calls flush)
« Last Edit: January 18, 2013, 10:09:39 am by G. »