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

Author Topic: Graphical glitch appearing  (Read 84 times)

0 Members and 2 Guests are viewing this topic.

Missqu

  • Newbie
  • *
  • Posts: 4
    • View Profile
Graphical glitch appearing
« on: March 03, 2025, 06:35:48 am »
Hello, I experience graphical glitch. I attached 2 screenshots, 1 with and 1 without graphical bug.
minimal code looks something like so
Quote

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
sf::Texture default_texture("my default texture.png");
struct Sprite
{
    explicit Sprite() {}
    explicit Sprite(const sf::Texture& _texture) : sprite_(_texture) {}
    std::optional<sf::Sprite> sprite_ = std::nullopt;
};
Sprite first_sprite;
std::vector<Sprite> sprites;

  void init()
      {
        first_sprite.sprite_->setTexture(default_texture);
        for (int i =0; i < 5;++i)
        {
          sprites.push_back(first_sprite);
        }
    }
int main()
{
    //init

    sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML window");
    init();
    while (window.isOpen())
    {
        while (const std::optional event = window.pollEvent())
        {
            // Close window: exit
            if (event->is<sf::Event::Closed>())
                window.close();
        }
        window.clear();
        for (int i =0; i < sprites.size(); ++i)
        {
          if (sprites.sprite_.has_value())
          {
            window.draw(sprites.sprite_.value());
          }
        }
        window.display();
    }
}
Whether or not the glitch appears I seem to be able to reliable reproduce based on the size of vector of sprites. (For examples I push_back 5 Sprites it appears, I do 7 it doesn't.
 
The wrapper for sf::Sprite is necessary I can't go around it.
OS: archlinux
Why is this happening? What might be the cause? I am at loss here, I seem to be lacking some knowledge. Might be because I copy the sprite, but I had no issue in the past.
« Last Edit: March 03, 2025, 06:38:17 am by Missqu »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11154
    • View Profile
    • development blog
    • Email
Re: Graphical glitch appearing
« Reply #1 on: March 03, 2025, 08:44:08 am »
Looks like your second screenshot didn't make it. ;D

You shouldn't be using SFML resources at a global scope. This can and eventually will lead to problems with global initialization or destruction order. Generally, the use of globals are not recommended.

When you push_back the sprite, you create a copy of it. Make sure that your struct will correctly copy its members as well.
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Missqu

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Graphical glitch appearing
« Reply #2 on: March 03, 2025, 11:00:46 pm »
I made a mistake, I actually store sf::Texture in class as unordered map, like so:
Quote
class ResourceManager
{
public:
    ResourceManager();



    sf::Texture* load(std::string _path);
    sf::Texture* get(std::string _path);

private:
    std::unordered_map<std::string, std::unique_ptr<sf::Texture>> textures_;
};
sf::Texture* ResourceManager::load(std::string _path)
{
   sf::Texture texture_temp;
   if (!texture_temp.loadFromFile(_path))
   {
      std::cerr << "Error loading texture " << _path << std::endl;
      return nullptr;
   }

      textures_[_path] = std::make_unique<sf::Texture>(_path);

   return textures_[_path].get();
}

sf::Texture* ResourceManager::get(std::string _path)
{
   return textures_[_path].get();
}
I also attached second screenshot. My bad should have double checked things.
« Last Edit: March 03, 2025, 11:02:28 pm by Missqu »

Hapax

  • Hero Member
  • *****
  • Posts: 3410
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Graphical glitch appearing
« Reply #3 on: March 03, 2025, 11:53:08 pm »
If you want to avoid the copying (mentioned above by eXpl0it3r), you could skip the "first sprite" temp and construct it directly in the vector since you already have a constructor ready:
sprites.emplace_back(default_texture);
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything