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

Author Topic: All sprites showing as white boxes.  (Read 4809 times)

0 Members and 1 Guest are viewing this topic.

JohnnyMccrum

  • Newbie
  • *
  • Posts: 3
    • View Profile
All sprites showing as white boxes.
« on: May 05, 2016, 05:23:42 pm »
Not sure what I'm doing wrong here.

when I display my textures with

Quote
window.draw(Spritesheet.at(1));

they all appear as white squares.

I have these variables in my main.h


Quote
//the main spritesheet.

Image sheetIMG;

//array to hold circles.
vector<CircleShape> Circles;

//array to hold sprites.
vector<Sprite> Spritesheet;

below is the offending code.




Quote
int LoadSpritesheet()
{
   //make sure spritesheet exists, then loads it into an image.

   Texture sheet;
   if (!sheet.loadFromFile("Sprites/A.png"))
   {
      return 0;
   }
   else
   {
      sheetIMG = sheet.copyToImage();
      MakeSprites();
      return 1;
   }
      

}

void MakeSprites()
{
   //turns the entire spritesheet into 8x8 modular sprites.

   Texture tex;

   int c = 0, r = 0;

   for (int i = 0; i <= (sheetIMG.getSize().x * sheetIMG.getSize().y) / 64; i++)
   {

      if (i == 125)
      {

         Sprite sprsdsds;
      }

      if (!tex.loadFromImage(sheetIMG, IntRect(0, 0, 8, 8)))
         break;
      else
      {
              Sprite spr;
              spr.setTexture(tex);
         Spritesheet.push_back(spr);
         c += 8;
         if (c == 16) { r++; };

      }
   }
   
   

         
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10194
    • View Profile
    • development blog
    • Email
Re: All sprites showing as white boxes.
« Reply #1 on: May 05, 2016, 05:30:09 pm »
Take a look at the official tutorial which explains this problem: The white square problem
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

JohnnyMccrum

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: All sprites showing as white boxes.
« Reply #2 on: May 05, 2016, 05:32:12 pm »
that doesn't really solve my issue though. How do I fix the pointer issue?

I'm rather new to pointers.

Surely sheetIMG should still store those textures as it's declared in my header?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10194
    • View Profile
    • development blog
    • Email
Re: All sprites showing as white boxes.
« Reply #3 on: May 05, 2016, 05:52:31 pm »
Get your favorite C++ resource or a good C++ book and read the chapter about references. Not really something we here can or should teach you. ;)

As long as you want to use the sprite, the textures need to exist. As such you should first think about who owns the textures and how can you save them for as long as the sprite uses it (a general solution would be to use something like Thor's resource holder).

Ah and if you aren't already, you should use a class instead of having global variables which will sooner than later blow up in your face. :D
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

JohnnyMccrum

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: All sprites showing as white boxes.
« Reply #4 on: May 05, 2016, 05:56:53 pm »
Thanks for being of absolutely no help.

Fixed it myself, there's absolutely no need to patronize me.

last time I post here.
« Last Edit: May 05, 2016, 06:00:53 pm by JohnnyMccrum »

Arcade

  • Full Member
  • ***
  • Posts: 226
    • View Profile
Re: All sprites showing as white boxes.
« Reply #5 on: May 05, 2016, 06:21:32 pm »
Glad you got it figured out, but I'm not sure why you took offense to eXpl0it3r's post. I thought he provided pretty solid advice. He explained exactly what your problem was (your texture was going out of scope while the sprite still needed it) and pointed out that Thor has classes to take care of resource handling for you if you are struggling with it.

Keep in mind that this is a SFML help forum, not a C++ help forum. That is why he pointed you towards other places to learn more about pointers and object lifetimes.

ZiyadCodes

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: All sprites showing as white boxes.
« Reply #6 on: February 21, 2022, 10:08:06 am »
I still don't understand the right way to fix the sprites appearing as white.
Here is my Bullet.cpp script:
(click to show/hide)


and here is my Bullet.h script:
(click to show/hide)

I even tried to do something like this in the update function:
(click to show/hide)

But still like it kind of worked but didn't at the same time. Sometimes the bullets would appear as a white box and sometimes they work with the texture and sometimes they work at the first second after they are shot and they turn to a white box, this is very confusing :'(

and my sprite still appears as a white square.
The only way I managed to get the sprite to work properly is when I put sprite.setTexture() in the update method.
Is there a correct way to fix this problem?
« Last Edit: February 21, 2022, 10:30:08 am by ZiyadCodes »

kojack

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Re: All sprites showing as white boxes.
« Reply #7 on: February 21, 2022, 11:05:52 am »
It depends on how you are using the Bullet objects.
This is just a guess about how you are handling it, but if you are pushing them into a vector, like std::vector<Bullet>, then that's the problem.
Sprites store a pointer to the texture you attach to them. This means the Texture object can't move in memory, the pointer to the texture in the sprite will break.
When an object is added to a vector, it makes a duplicate at a different location, so the texture pointer is not valid in the duplicate (points to the original Bullet, not the one in the vector). Also adding things to a vector will make the vector occasionally resize, which requires moving everything around (breaking the texture pointers again).
Although as I said, that's just a guess. :)

I find the easiest way to deal with Texture objects is to allocate them on the heap (using new) and handle them with pointers. This stops duplication and moving around in memory, so the binding from the sprite won't break.
I'd change the header to have:
sf::Texture *bulletTexture;

and the constructor to have:
bulletTexture = new sf::Texture;
bulletTexture->loadFromFile("Resources/Syringe.png");
bulletSprite.setTexture(*bulletTexture);

Even better would be to load the texture once somewhere (like a resource manager) and reuse it. Unlimited sprites can share a single texture. Right now every Bullet is reloading the png file and storing a copy of it's data.
(on a side note, I'd remove all the this-> bits, they aren't hurting but they also aren't doing anything)

ZiyadCodes

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: All sprites showing as white boxes.
« Reply #8 on: February 21, 2022, 06:22:51 pm »
Thank you so much. You helped me fix the problem ;D

THANKS     ~     THANKS     ~     THANKS     ~     THANKS     ~     THANKS     ~     THANKS     ~     THANKS