Hi All,
I'm trying to draw sprites that use an image as a texture, but when drawing some of the sprites do not draw properly and render as white blocks. Also, sometimes on a RenderWindow.draw an EXC_ARITHMETIC (code EXC_i386_DIV) signal is raised.
Screen shot of a 10x10 grid of sprites drawn with a single source image:
http://imgur.com/1CH9u9FThe sf::Image pointer is being stored in an unordered_map (imageMap) which is a game state member variable.
Code for addImageResource:
bool PlayState::addImageResource(const std::string imagePath)
{
// Check if image already exists
if (imageMap.find(imagePath) != imageMap.end())
return true;
// Load image from file
sf::Image *img = new sf::Image();
if (!img->loadFromFile(imagePath))
return false;
// Insert pointer and return insert success
return imageMap.insert(std::make_pair(imagePath, img)).second;
}
Code for getResourceImage:
sf::Image * PlayState::getImageResource(const std::string imagePath)
{
auto it = imageMap.find(imagePath);
if (it == imageMap.end())
return nullptr; // Image not found
else
return it->second; // Return value from iterator
}
PlayState constructor:
PlayState::PlayState(sf::RenderWindow& window, sf::View& camera) :
gameWindow (window), gameCamera(camera)
{
/* Load images */
if (!addImageResource("Block.png"))
std::cout << "Failed to load Block.png" << std::endl;
/* Create blocks */
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
blocks.emplace_back(sf::Vector2f(i * 30.0f, j * 30.0f), getImageResource("Block.png"));
}
Code for Block constructor: (Note: sprite and texture are both member variables of Block)
Block::Block(sf::Vector2f position, sf::Image *image)
{
if (image != nullptr)
texture.loadFromImage(*image);
sprite.setTexture(texture, true);
sprite.setPosition(position);
}
Draw method (also the code where EXC_ARITHMETIC comes from):
void Block::draw(sf::RenderWindow& window)
{
window.draw(sprite);
}
I've made sure that every Block is getting the correct pointer to the image, and the Image is not deleted until a cleanup method is called for the game state. I'm not sure what would cause these issues but any help would be appreciated.
Thanks