Hey, hope you guys can help. I've recently started rewriting my SDL driven RTS game to SFML (due to various reasons). I am trying to write a class to draw out the map from a tileset, but its just displaying a bunch of white squares on the screen. Now, I have read before that this might be due to the texture being destroyed while sprites are still using it (a case of declaring the texture locally), but it appears that my texture is still there safe and sound. To avoid loading textures constantly I am keeping my sprites in a vector, don't know if that matters.
Here is the code in question:
C++
void TerrainMap::drawMap()
{
unsigned int i;
unsigned int j;
sf::Sprite tmp;
for(j = 0; j < (this->sizeY); j++)
{
for(i = 0; i < (this->sizeX); i++)
{
tmp = mDatabase.getSpriteId(mapArray[i][j]); /* Fetching Sprite from Database*/
tmp.setPosition(sf::Vector2f(i*90,j*90)); /* Dislocating Sprite */
this->mWindow.draw(tmp);
std::cout << "Placing Sprite" << std::endl;
}
}
}
sf::Sprite SpriteDatabase::getSpriteId(unsigned int id) const
{
std::cout << "Returning Sprite\n" << std::endl;
return spriteIndex[id];
}
void SpriteDatabase::loadSprite(sf::Sprite sprite) /* Push into vector container */
{
std::cout << "Loading Sprite\n" << std::endl;
spriteIndex.push_back(sprite);
}
bool SpriteDatabase::preloadDatabase()
{
sf::Sprite sprite;
if(!spriteIndex.empty())
return false; /* If sprites have already been loaded */
if(!texture.loadFromFile("gfxPack.png"))
return false; /* If texture failed to load */
sprite.setTexture(texture);
sprite.setTextureRect(sf::IntRect(0,0,89,90)); /* Lime Grass */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(90,0,179,90)); /* Rough Sand */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(180,0,269,90)); /* Pure Sand */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(270,0,359,90)); /* Shallow Water */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(360,0,449,90)); /* Dirt */
loadSprite(sprite);
return true;
}
int main()
{
SpriteDatabase sprDB;
sf::RenderWindow window(sf::VideoMode(800,600), "Window");
TerrainMap x(window, sprDB);
x.setSize(12,12);
x.loadMap("example.map");
window.setVerticalSyncEnabled(true);
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Black);
x.drawMap();
window.display();
}
return 0;
}
Headers:
#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
/* Sprite Database -------------------------------- */
/* : Each Sprite has a unique id. ----------------- */
/* : use getSpriteId to return the sprite object -- */
/* ------------------------------------------------ */
/* LIST OF OBJECTS -------------------------------- */
/* 0. TERRAIN TEXTURE - Lime Grass ---------------- */
/* 1. TERRAIN TEXTURE - Rough Sand ---------------- */
/* 2. TERRAIN TEXTURE - Pure Sand ----------------- */
/* 3. TERRAIN TEXTURE - Shallow Water ------------- */
/* 4. TERRAIN TEXTURE - Dirt ---------------------- */
/* ------------------------------------------------ */
/* ------------------------------------------------ */
class SpriteDatabase
{
public:
SpriteDatabase();
sf::Sprite getSpriteId(unsigned int id) const;
private:
void loadSprite(sf::Sprite sprite);
bool preloadDatabase();
std::vector<sf::Sprite> spriteIndex;
sf::Texture texture;
};
class TerrainMap
{
public:
TerrainMap(sf::RenderWindow& window, const SpriteDatabase& database) : mWindow(window), mDatabase(database) {};
void setSize(unsigned int x, unsigned int y);
void loadMap(const char* file);
void drawMap();
private:
sf::RenderWindow& mWindow;
const SpriteDatabase& mDatabase;
unsigned int mapArray[64][64];
unsigned int sizeX;
unsigned int sizeY;
};