Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Spritesheet algorithm not working  (Read 2552 times)

0 Members and 1 Guest are viewing this topic.

gustav9797

  • Newbie
  • *
  • Posts: 4
    • View Profile
[SOLVED] Spritesheet algorithm not working
« on: February 10, 2013, 10:44:12 pm »
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:
Code: [Select]
Initializer:
if(AddSpriteSheet(std::string("BlockSolid.png"), 16, 16))
{
std::cout << "its working, cheese-pie.";
}
PlayState.cpp:
Code: [Select]
sf::Sprite* tempSprite = tc.getTexture(std::string("BlockSolid.png"));
playerSprite = &(tempSprite[0]);
TextureContainer.cpp:
Code: [Select]
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;
}
« Last Edit: February 11, 2013, 11:09:49 pm by gustav9797 »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Spritesheet algorithm not working
« Reply #1 on: February 10, 2013, 11:04:27 pm »
Keep your image alive.
It's asked A LOT in the forums...
« Last Edit: February 10, 2013, 11:06:24 pm by G. »

ostkaka

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Spritesheet algorithm not working
« Reply #2 on: February 10, 2013, 11:07:00 pm »
Keep your image alive.
It's asked A LOT in the forums...
TY. -.-
[EDIT]
But no... It didn't really help. Why doesn't this work?
Code: [Select]
tempImage.Copy(image, 16, 0, sf::IntRect(x*spriteWidth, y*spriteHeight, (x + 1)*spriteWidth, (y + 1)*spriteHeight), true);
« Last Edit: February 10, 2013, 11:18:03 pm by ostkaka »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Spritesheet algorithm not working
« Reply #3 on: February 11, 2013, 01:08:58 am »
There are at least 2 flaws in TextureContainer.cpp. (btw, who are you lol?)
The tutorial says this: "a sprite only points to an external image it doesn't own one".
Which means that:
- when your image is deleted (end of scope where you declared it) your sprite doesn't have an image anymore, hence the white box
- if your image is modified, it affects every sprites pointing to this image

ostkaka

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Spritesheet algorithm not working
« Reply #4 on: February 11, 2013, 01:27:04 am »
There are at least 2 flaws in TextureContainer.cpp. (btw, who are you lol?)
The tutorial says this: "a sprite only points to an external image it doesn't own one".
Which means that:
- when your image is deleted (end of scope where you declared it) your sprite doesn't have an image anymore, hence the white box
- if your image is modified, it affects every sprites pointing to this image
oh, ty... :o I actually know that. :-\

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Spritesheet algorithm not working
« Reply #5 on: February 11, 2013, 03:58:57 am »
So what do you actually want?  :o ??? ::)
Maybe I'm wrong, I'll let other people try.
« Last Edit: February 11, 2013, 04:01:27 am by G. »

 

anything