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

Author Topic: Sprite doesn't display  (Read 625 times)

0 Members and 1 Guest are viewing this topic.

Fance

  • Newbie
  • *
  • Posts: 3
    • View Profile
Sprite doesn't display
« on: May 09, 2025, 09:29:09 pm »
I feel like I'm going insane, I'm making a game for a university exam and the current feature that I'm implementing is a heart item that heals the player, currently, the heart is dropped each time an enemy is killed by the player but for some reason the first 5 hearts that spawn don' t have their sprite drawn, they do exist as they can heal the player but the sprite isn't drawn on screen, after I kill like 5 enemies the hearts that are dropped after have their sprite drawn correctly.

header:
struct Heart: Updatable{
    sf::Sprite sprite;
    sf::Texture texture;
    sf::Vector2f position;
    Player* player;

    Heart(Player* player, sf::Vector2f position);
   
    bool update(float delta) override;
    void draw(sf::RenderWindow& window) override;
};
 

source:
Heart::Heart(Player* player, sf::Vector2f position):
    position(position),
    texture(heart_sprite),
    sprite(texture),
    player(player){
        //sprite = sf::Sprite(texture);
        sprite.setOrigin({7.f, 5.5});
        sprite.setScale(player_scale);
        sprite.setPosition(position);
}

bool Heart::update(float delta){
    if(dist(position, player->position) < 140){
        player->heal();
        return true;
    }
    return false;
}

void Heart::draw(sf::RenderWindow& window){
    window.draw(sprite);
}
 

Both the Player and the Enemies load the texture and the sprite the same way and they work properly, if I uncomment that line in the constructor it works perfectly but it seems like a very stupid solution to me.
Thanks to everyone in advance for the help.

kimci86

  • Full Member
  • ***
  • Posts: 130
    • View Profile
Re: Sprite doesn't display
« Reply #1 on: May 10, 2025, 01:30:09 am »
This is probably because the texture is after the sprite in your Heart type. It implies the texture is initialized after the sprite, regardless of the texture being before the sprite in Heart constructor's initializer list. So you are initializing the sprite with a texture not yet initialized. The sprite constructor internally queries the texture size, which is an undefined behavior on an uninitialized texture.

To fix it, you could move the texture before the sprite in Heart type.

But even then, storing the texture referenced by the sprite in the same structure is a bad idea. It creates potential lifetime issues.
Consider the following pseudo-code:
Heart h1{player, position}; // h1.sprite references h1.texture
{
    Heart h2{player, position};
    h1 = h2; // h1.sprite now references h2.texture
} // h2 is destroyed
// h1.sprite references a texture that does not exist anymore -> bug

In addition, copying the texture in each Heart instance is unnecessary.
You could simply have a single texture instance, which is heart_sprite apparently in your code, and use it to initialize your sprites. This would solve both the initialization order issue and lifetime issue.

Fance

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite doesn't display
« Reply #2 on: May 10, 2025, 03:02:23 am »
Thank you, I'll let you know if it works once I implement it, also, just to clarify, "heart_sprite" isn't a texture, it's just the file path to the .png (bad nomenclature on my part). Could you elaborate a bit more on the long term issue? I didn't quite understand it, and thank you again.

Fance

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite doesn't display
« Reply #3 on: May 10, 2025, 08:31:03 am »
Can confirm it now works properly, thank you very much, didn't know the order of declaration mattered, thank you very much.

 

anything