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

Author Topic: Sprite images are all white squares  (Read 4124 times)

0 Members and 1 Guest are viewing this topic.

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Sprite images are all white squares
« on: August 02, 2011, 10:28:48 am »
So for some reason, all the images I am loading for my sprites are being turned into white squares.  They are at the appropriate position in space, and they even change color if I want them to, so I know they are there, but they remain nothing more than blank squares of whatever color I set them at.

The way I've done it is I have two containers, one for tile images and one for object images.  Each container contains each image only once, and all instances of objects or tiles take their image from these containers.  Within the constructor for the Baby class (one of the object classes), I've included this code:
Code: [Select]

m_Sprite.SetImage(pLevel->GetMobImage(1));
m_Sprite.SetPosition(m_X, m_Y);
cout << m_X << ", " << m_Y << endl;
m_Sprite.SetColor(sf::Color(255, 0, 0));


Note that Baby inherits from Mob.  Mob contains only the sf::Sprite m_Sprite; declaration, however, as well as the following draw function:

Code: [Select]
void Mob::Draw(sf::RenderWindow* App)
{
    App->Draw(m_Sprite);
}


Level::GetMobImage() is defined as follows:

Code: [Select]
sf::Image Level::GetMobImage(int image)
{
    if (image >= m_MobImages.size())
    {
        cout << "ERROR!  No image this far into the vector!\n";
    }
    return m_MobImages[image];
}


Where m_MobImages is the aforementioned container of sf::Images.

---

So, blank squares.

I first thought my code was too convoluted.  Yet I am able to change the color of my sprites, and they definitely have the same size as the images they are supposed to be displaying (some are 9x9, others 15x15, others 32x32).

But then I tested out the other sf::Image using class, Tile, and realized that the tiles that form the background of the scene are also being drawn as blank white squares, even if I give them squiggly black lines as art (I hadn't noticed until then because the tiles had always been blank, and since they did color in using sf::Sprite::SetColor, I thought all was well).

I tried an inefficient hack, just to see if I could get it to work, by having instances of Baby have their own sf::Image data member and load its contents directly from a file themselves, without the whole container system.  I did so by including the following in the constructor for Baby:

Code: [Select]
sf::Image Image;
Image.LoadFromFile("Baby.png");
m_Sprite.SetImage(Image);
m_Sprite.SetPosition(m_X, m_Y);
m_Sprite.SetColor(sf::Color(255, 0, 0));


Again, all I get are red squares.

Can anyone point me in the right direction?

---

This was a similar post about something that seemed similar a few months ago, but reading it didn't really help me solve this, as s/he seemed to be trying to do something different than I am.

http://www.sfml-dev.org/forum/viewtopic.php?t=4493&sid=f9feaa1f0a8d93be858aed89b385873a

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite images are all white squares
« Reply #1 on: August 02, 2011, 10:35:43 am »
Do you call GetMobImage after all images are loaded, or are the calls mixed?
Laurent Gomila - SFML developer

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Sprite images are all white squares
« Reply #2 on: August 02, 2011, 10:40:42 am »
Afterwards; the images are all loaded during the constructor for the Level class, which is called before the main loop, which is the only place the the Baby instances can be created (and thus the only place GetMobImage gets called).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite images are all white squares
« Reply #3 on: August 02, 2011, 10:42:59 am »
Is your Level instance copied (maybe accidentally) after sprites have been created? To make sure that it doesn't happen, make your Level class inherit from sf::NonCopyable.
Laurent Gomila - SFML developer

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Sprite images are all white squares
« Reply #4 on: August 02, 2011, 11:18:34 am »
Hm... I've mostly solved the problem, now!  GetMobImage was returning an sf::Image, but I changed it such that it returned a pointer to an sf::Image and then used that instead to set the image (dereferencing it, of course).  That worked - the graphics now display properly.  

The only problem now is that the entire rectangle that contains the sprite's data is being turned red, rather than just the sprite's art.  I exported the image from InkScape as a .png - perhaps I should try something else to get the alpha to carry across?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite images are all white squares
« Reply #5 on: August 02, 2011, 11:42:56 am »
If you don't want to bother with alpha channel, you can create it from a color with the Image::CreateMaskFromColor function.
Laurent Gomila - SFML developer

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Sprite images are all white squares
« Reply #6 on: August 02, 2011, 06:05:48 pm »
Ah, that's pretty much the same as what I used in PyGame back when I was learning programming.  Thanks for the help!

 

anything