# Switch 平台 stbi_load 崩溃修复计划 ## 问题描述 在 Switch 平台运行时,调用 `Sprite::createFromFile("assets/player.png")` 会在 `stbi_load` 这一行崩溃。 ## 修复方案(简化版) 只修改一个文件:使用 Asset 类读取文件到内存,再用 `stbi_load_from_memory` 加载。 ### 修复: 修改 texture.cpp **文件**: `Frostbite2D/src/frostbite2D/graphics/texture.cpp` **变更**: - 使用 `Asset::readBinaryFile()` 读取文件到内存 - 使用 `stbi_load_from_memory()` 替代 `stbi_load()` **代码修改**: ```cpp Ptr Texture::loadFromFile(const std::string& path) { Asset& asset = Asset::get(); std::vector fileData; if (!asset.readBinaryFile(path, fileData)) { std::string resolvedPath = asset.resolveAssetPath(path); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to read texture file: %s", resolvedPath.c_str()); return nullptr; } int width, height, channels; uint8* data = stbi_load_from_memory(fileData.data(), static_cast(fileData.size()), &width, &height, &channels, 0); if (!data) { std::string resolvedPath = asset.resolveAssetPath(path); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load texture: %s", resolvedPath.c_str()); return nullptr; } // ... 其余代码保持不变 } ``` ## 测试步骤 1. 应用修复 2. 重新构建 Switch 版本: `xmake build -p switch` 3. 在 Switch 上运行生成的 NRO 文件 4. 验证图片能正常加载 ## 预期结果 - stbi_load 不再崩溃 - 图片能正常加载和显示 - 程序能正常运行