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

Author Topic: Sprite is showing up white !  (Read 2173 times)

0 Members and 1 Guest are viewing this topic.

Athomield

  • Newbie
  • *
  • Posts: 10
    • View Profile
Sprite is showing up white !
« on: December 28, 2014, 12:36:14 pm »
Hi I have a vector of sprites created in real-time and they are added to it (to be more specific instantiating bullets) and every one of the uses the same texture created in a constructor of another class that contains them, the texture is fine because when I use it on static sprites it works fine, but this array of sprites refuses to get any texture what so ever and stays white all the time.
Any help is appreciated.

EDIT:I even tried to put the texture in main() before the main loop and assign it to every bullet before drawing it, same thing. I'm afraid this is a bad bug.


void PlayerSpaceShip::DrawBullets(sf::RenderWindow &win)
{
        for(int i = 0;i < bulletShot.size();i++)
        {
                bulletShot[i].Draw(win);
        }
}


void Projectile::SetPosition(int x, int y)
{
        m_sprite.setOrigin(m_sprite.getLocalBounds().width/2,m_sprite.getLocalBounds().height/2);
        m_sprite.setPosition(x,y);
}

void Projectile::SetTexture(sf::Texture texture)
{
        m_sprite.setTexture(texture);
}

void Projectile::Draw(sf::RenderWindow &win)
{
        win.draw(m_sprite);
}

void Projectile::Move()
{
        m_sprite.move(direction);
}
 
« Last Edit: December 28, 2014, 03:40:16 pm by Athomield »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10901
    • View Profile
    • development blog
    • Email
AW: Sprite is showing up white !
« Reply #1 on: December 28, 2014, 12:50:01 pm »
Provide a minimal and complete code example. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Laurent Gomila - SFML developer

Athomield

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite is showing up white !
« Reply #3 on: December 28, 2014, 01:18:32 pm »
The thing is, the texture exists fine in the memory, when I set it to the player him self becomes the bullet, plus the player texture is loaded within the same scope and still exists.These set of sprites particularly refuse to take any texture.
Also note that the playerspaceship class doesnt get destroyed during run-time.
« Last Edit: December 28, 2014, 01:22:35 pm by Athomield »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10901
    • View Profile
    • development blog
    • Email
AW: Sprite is showing up white !
« Reply #4 on: December 28, 2014, 01:25:42 pm »
That's why I said to provide a minimal and complete example. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Athomield

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite is showing up white !
« Reply #5 on: December 28, 2014, 01:28:58 pm »
I will update the code above, and add what's missing :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10901
    • View Profile
    • development blog
    • Email
AW: Sprite is showing up white !
« Reply #6 on: December 28, 2014, 01:37:29 pm »
I didn't notice that you edited the first post.
Anyways your issue is as far as I can tell in Projectile::SetTexture. You pass a the texture by value, thus creating a temporary texture that just lives within the SetTexture function and gets destroyed afterwards. And within the function you assign the short living temporary texture to the sprite, thus when drawing the sprite the temporary texture will long have been destroyed.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Sprite is showing up white !
« Reply #7 on: December 28, 2014, 01:44:56 pm »
You also have another copy which will invalidate the texture pointer: when you add a projectile to the vector.
Laurent Gomila - SFML developer

Athomield

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite is showing up white !
« Reply #8 on: December 28, 2014, 01:51:12 pm »
@eXpl0it3r you're a hero member :D it worked by passing in a pointer instead.
@Laurent adding it to the vector didn't cause me a trouble for now I hope it doesn't
And thank you for your time and efforts :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10901
    • View Profile
    • development blog
    • Email
AW: Sprite is showing up white !
« Reply #9 on: December 28, 2014, 01:56:45 pm »
You probably want to pass it as reference instead. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Athomield

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite is showing up white !
« Reply #10 on: December 28, 2014, 02:00:50 pm »
Yep, that works too !

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Sprite is showing up white !
« Reply #11 on: December 28, 2014, 02:06:35 pm »
You're right, forget my comment I read your code too quickly ;)
Laurent Gomila - SFML developer

 

anything