1
Graphics / Re: Load Texture from another file doesnt work ? [Resolved]
« 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
TexturesMnager.cpp
Game.h
and to use functions :
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(){}
};
{
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);
}
#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#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();
};
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;
}
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");
}
this->loadTexture();
Sprite randomName = this->createSprite("blabla");
}