sf::Texture as class member doesn't work?

前端 未结 1 1504
北恋
北恋 2020-12-04 02:16

Heyy, I want to draw a sprite in my SFML application but its texture is always white when I use an image and a texture that are class members

Class members:

相关标签:
1条回答
  • 2020-12-04 03:09

    This is called the white square problem.

    Basically, at some point, your object is copied but the copy constructor doesn't update the copied sprite texture to use the copied texture, and the original texture is destroyed so the copied sprite doesn't have a valid texture anymore.

    A quick fix can simply run the initialisation code inside the copy constructor and copy assignment operator.


    BTW

    myimg_texture.create(icon.width, icon.height);
    myimg_texture.update(myimg_image);
    

    can directly use icon.pixelData instead of myimg_image and thus you don't need an sf::Image at all.

    Or you can do as follow if you need the sf::Image for another purpose:

    myimg_texture.loadFromImage(myimg_image);
    
    0 讨论(0)
提交回复
热议问题