#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
class SpriteDatabase
{
public:
SpriteDatabase();
~SpriteDatabase();
sf::Sprite & getSpriteId(unsigned int id);
private:
void loadSprite(sf::Sprite *sprite);
bool preloadDatabase();
std::vector<sf::Sprite*> spriteIndex;
sf::Texture texture;
};
sf::Sprite & SpriteDatabase::getSpriteId(unsigned int id)
{
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(!texture.loadFromFile("gfxPack.png"))
return false;
sprite = new sf::Sprite(texture, sf::IntRect(0,0,89,90));
loadSprite(sprite);
sprite = new sf::Sprite(texture, sf::IntRect(90,0,89,90));
loadSprite(sprite);
sprite = new sf::Sprite(texture, sf::IntRect(180,0,89,90));
loadSprite(sprite);
sprite = new sf::Sprite(texture, sf::IntRect(270,0,89,90));
loadSprite(sprite);
sprite = new sf::Sprite(texture, sf::IntRect(360,0,89,90));
loadSprite(sprite);
return true;
}
SpriteDatabase::~SpriteDatabase()
{
for (int = 0; i < spriteIndex.size(); i++)
{
delete spriteIndex[i];
}
spriteIndex.clear();
}
it work?
ADD:
Oh, Fuck...
You use sprite.setTextureRect(sf::IntRect(360,0,449,90));
1 parametr start texture rect x
2 parametr start texture rect y
3 parametr width texture rect
4 parametr height texture rect
sprite.setTextureRect(sf::IntRect(0,0,89,90)); /* Lime Grass */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(90,0,89,90)); /* Rough Sand */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(180,0,89,90)); /* Pure Sand */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(270,0,89,90)); /* Shallow Water */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(360,0,89,90)); /* Dirt */
loadSprite(sprite);