This is the current code we use to convert one image full of blocks to seperated ones, but we only get a white block. Using sfml 1.6.
How we use it:
Initializer:
if(AddSpriteSheet(std::string("BlockSolid.png"), 16, 16))
{
std::cout << "its working, cheese-pie.";
}
PlayState.cpp:
sf::Sprite* tempSprite = tc.getTexture(std::string("BlockSolid.png"));
playerSprite = &(tempSprite[0]);
TextureContainer.cpp:
bool TextureContainer::AddTexture(std::string &fileName)
{
sf::Image image;
bool success = image.LoadFromFile(fileName);
sf::Sprite *sprite = new sf::Sprite();
sprite->SetImage(image);
textureMap.emplace(fileName, sprite);
return success;
}
bool TextureContainer::AddSpriteSheet(std::string &fileName, int spriteWidth, int spriteHeight)
{
sf::Image image;
bool success = image.LoadFromFile(fileName);
int width = image.GetWidth()/spriteWidth;
int height = (image.GetHeight()/spriteHeight);
sf::Sprite *sprite = new sf::Sprite[width*height];
sf::Image tempImage(16, 16);
for (int x = 0; x+1 <= width; x++)
{
for (int y = 0; y+1 <= height; y ++)
{
tempImage.Copy(image, 16, 0, sf::IntRect(x*spriteWidth, y*spriteHeight, (x + 1)*spriteWidth, (y + 1)*spriteHeight), true);
sprite[x+y*width].SetImage(tempImage);
}
}
textureMap.emplace(fileName, sprite);
return success;
}