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.
What do you mean by same syntax?
If I store the sprites in an array declared in main(), as opposed to within my SpriteSheet class, it works fine.
ie. window.draw(*pSprites[0][0]);
So yes, [0 ][0 ] contains a valid sprite.
My error:
Program received signal SIGSEGV, Segmentation fault.
In sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&) () (C:\Windows\sfml-graphics-2.dll)
Even the follow example gives me a segfault when creating an sf::RenderWindow
#include <memory>
#include <SFML/Graphics.hpp>
using namespace std;
int main() {
sf::Event event;
sf::RenderWindow testWindow(sf::VideoMode(800, 600, 32), "Test");
while(testWindow.isOpen()) {
sf::Event event;
while(testWindow.pollEvent(event)) {
if(event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
testWindow.close();
}
testWindow.clear();
testWindow.display();
}
return 0;
}