Ok, so I had the idea that maybe loading the same texture so many times might be causing issues, so I created a texture manager class to load the texture once, and then pass a pointer to it to the Tile class constructor to be used in setting the texture of the sprite in the Tile class, and... IT WORKS! Here's the texture manager class I created.
TextureManager.h
#ifndef TEXTUREMANAGER_H
#define TEXTUREMANAGER_H
#include <SFML/Graphics.hpp>
#include <map>
class TextureManager {
private:
std::map<const char *, sf::Texture> textures;
public:
TextureManager();
virtual ~TextureManager();
void AddTexture(const char * filename, const char * identifier);
sf::Texture* GetTexture(const char * identifier);
};
#endif
TextureManager.cpp
#include "TextureManager.h"
#include <cstdlib>
TextureManager::TextureManager() {
}
TextureManager::~TextureManager() {
}
void TextureManager::AddTexture(const char * filename, const char * identifier) {
sf::Texture texture;
if(!texture.loadFromFile(filename)) {
// There was an error loading the image...
printf("Error loading image from file: %s", filename);
} else {
this->textures[identifier] = texture;
}
}
sf::Texture* TextureManager::GetTexture(const char * identifier) {
return &(this->textures[identifier]);
}
And then in the Init() function of my SFMLApp class (creates the SFML window and handles the game information), I simply do this...
// Load the textures into the texture manager.
this->textureManager.AddTexture("WoodTexture01.png", "Wood01");
And then, to load that texture into the sprite of each tile, I simply pass a pointer to the texture to the Tile class constructor, and do this...
if(texture != NULL) {
// Set the sprite's texture.
this->sprite.setTexture(*texture);
this->sprite.setPosition((this->cx - (Tile::TILE_SIZE / 2)), (this->cy - (Tile::TILE_SIZE / 2)));
this->sprite.setScale((Tile::TILE_SIZE / texture->getSize().x), (Tile::TILE_SIZE / texture->getSize().y));
}
Now... maybe I can finally get on with the rest of my tile engine?