Hi there,
I'm using SFML 2.0 and have created a SpriteSheet class which inherits sf::Drawable. It contains a pointer array to store sprites created dynamically and I'm having trouble getting the sprites to draw.
SpriteSheet Declaration:
class SpriteSheet : public sf::Drawable {
public:
SpriteSheet(sf::Texture* texture, const int tileSize);
~SpriteSheet();
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
private:
int theTileSize;
int cols;
int rows;
sf::Texture *pTexture;
sf::Sprite *pSprites[16][16];
};
This is how I'm populating the sprites pointer array within the SpriteSheet constructor.
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
sf::IntRect rect(sf::Vector2i(x,y),sf::Vector2i(theTileSize, theTileSize));
pSprites[i][j] = new sf::Sprite(*pTexture, rect);
pSprites[i][j]->setPosition(x*tileSize,y*tileSize);
}
}
When I run my code with window.draw(SpriteSheet); I get segfault errors. This is my draw function. If I use the same syntax in main(), everything works fine.
void SpriteSheet::draw(sf::RenderTarget &target, sf::RenderStates states) const {
target.draw(*pSprites[0][0]);
}
Any ideas? I've only recently picked C++ back up again and I'm probably missing something glaringly obvious.
Thanks.