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

Author Topic: Load Texture from another file doesnt work ? [Resolved]  (Read 2543 times)

0 Members and 1 Guest are viewing this topic.

Weyrd

  • Newbie
  • *
  • Posts: 2
    • View Profile
Load Texture from another file doesnt work ? [Resolved]
« on: February 05, 2021, 07:39:45 pm »
Hello, i just started to use SFML and i try to load all my Sprite in another .cpp from where I draw them.
But the Texture not load when i try to display the Sprite


I have no problem when i reload the Texture in the main, so i was wondering if it was possible to load Texture in anothers functions/files

Here my code, but if the awnser is "no", no need to read it
(there are far too many pointer because i tried lots of solution xD)

createSprite.cpp
vector<Character*>* createChar() {
        sf::Texture test;
        test.loadFromFile("blabla.png");

        sf::Sprite*  spritePlayer = new Sprite;
        spritePlayer->setTexture(test);

        std::vector<Character*>* allChar= new vector<Character*>;

        Character *player = new  Character(spritePlayer);

        allChar->push_back(player);

        return allChar;
}

Main.cpp
class Character
{
public:
        Sprite* sprite;
        class Character(Sprite* sprite);
};

int main {
    Character* player = *createChar().front();

    while (window.isOpen()) {
        ...
        window.draw(*player->sprite);
        ...
    }
}
 


Thanks in advance !

« Last Edit: February 05, 2021, 09:40:33 pm by Weyrd »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Load Texture from another file doesnt work ?
« Reply #1 on: February 05, 2021, 08:41:55 pm »
This is what is commonly referred to as the white square problem. The Sprites and Textures tutorial on the SFML website has a section about it: https://www.sfml-dev.org/tutorials/2.5/graphics-sprite.php#the-white-square-problem.

In short, the problem is that you're creating a local texture variable.  The sprite doesn't copy the texture data into itself. It simply holds a pointer to the texture. Therefore the texture needs to exist for as long as the sprite is using it. In your case, once the createChar() function ends the texture will go out of scope and be destroyed, leaving your sprite pointing at invalid memory.

Weyrd

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Load Texture from another file doesnt work ? [Resolved]
« Reply #2 on: February 05, 2021, 09:48:47 pm »
Okay thank you, i now use a texture manager to keep load textures  !

someone else is looking for the solution :
TexturesManage.h
class TextureManager
{
private:
    /* Array of textures used */
    std::map<std::string, sf::Texture> textures;

public:
    void loadTexture(const std::string& name, const std::string& filename);
    sf::Texture& getRef(const std::string& texture);
    TextureManager(){}
};

TexturesMnager.cpp
#include <SFML/Graphics.hpp>
#include <map>
#include <string>
#include "TexturesManager.h"


void TextureManager::loadTexture(const std::string& name, const std::string& filename) {
        sf::Texture tex;
        tex.loadFromFile(filename);
        this->textures[name] = tex;

        return;
}

sf::Texture& TextureManager::getRef(const std::string& texture) {
        return this->textures.at(texture);
}

Game.h
#include "Header.h"
#include "TexturesManager.h"

using namespace sf;
using namespace std;

class Game {
private:
        void loadTexture();
        Sprite* createSprite(string textureName);
public:
        RenderWindow window;
        TextureManager texmgr;

        class Game(VideoMode VideoMode, string& title);
        void _run();
};
Game.cpp
void Game::loadTexture() {
        texmgr.loadTexture("blabla", "Sprites/blabla.png");
       
}

Sprite* Game::createSprite(string textureName) {
        sf::Sprite* sprite = new Sprite;
        sprite->setTexture(this->texmgr.getRef(textureName));
        return sprite;
}

and to use functions :
void Game::_run() {
    this->loadTexture();
    Sprite randomName = this->createSprite("blabla");
}

 

anything