You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SFML加载纹理显示白方块问题求助(RAII风格TextureHolder)

SFML纹理加载后显示白块的解决方案

核心问题:局部资源管理器被提前销毁

在Game类的构造函数中,TextureHolder texture_holder是局部变量,构造函数执行完毕后该对象会被销毁,其内部存储的所有纹理资源(由std::unique_ptr管理的sf::Texture)也会被自动释放。而m_spritePlane仅保存了纹理的引用,纹理销毁后Sprite失去有效数据,最终显示为白块。

修复步骤

1. 将TextureHolder改为Game类的成员变量

修改Game.hpp,在私有成员中添加资源管理器:

class Game final {
public:
    explicit            Game();
    void                run();
    ~Game() =           default;
private:
    void                processEvents();
    void                update(sf::Time deltaTime);
    void                render();
    void                handlePlayerInput(sf::Keyboard::Key , bool );

    sf::RenderWindow  m_window;
    TextureHolder     m_textureHolder; // 添加资源管理器成员
    sf::Sprite        m_spritePlane;

    std::bitset<MovementCount> m_isMoving;
};

同时删除无用的m_texturePlane成员变量。

2. 调整Game构造函数的实现

去掉局部的TextureHolder实例,直接使用类成员变量:

Game::Game() : m_window(sf::VideoMode(WIDTH, HEIGHT), TITLE) {
    m_window.setVerticalSyncEnabled(true);

    m_textureHolder.load(Textures::ID::Airplane, "/home/davit/CLionProjects/untitled/plane-animated-top-down-game-art.png");
    m_spritePlane.setTexture(m_textureHolder.get(Textures::ID::Airplane));
}

额外代码优化建议

1. 处理纹理查找失败的情况

当前TextureHolder::get函数未处理ID不存在的情况,会引发未定义行为,可添加错误处理:

sf::Texture &TextureHolder::get(const Textures::ID id) {
    const auto found = m_TextureMap.find(id);
    if (found == m_TextureMap.end()) {
        std::cerr << "Texture with ID " << static_cast<int>(id) << " not found!" << std::endl;
        static sf::Texture defaultTexture;
        return defaultTexture;
    }
    return *found->second;
}

const sf::Texture& TextureHolder::get(const Textures::ID id) const {
    const auto found = m_TextureMap.find(id);
    if (found == m_TextureMap.end()) {
        std::cerr << "Texture with ID " << static_cast<int>(id) << " not found!" << std::endl;
        static sf::Texture defaultTexture;
        return defaultTexture;
    }
    return *found->second;
}

2. 验证纹理文件路径

检查控制台输出的加载日志,确保纹理文件的绝对路径正确,避免因路径错误导致加载失败。


内容的提问来源于stack exchange,提问作者Davo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 09:49:56