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
(https://cdn.discordapp.com/attachments/309034003980222467/807316940577177600/unknown.png)
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 !
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");
}