- 将stb_image替换为SDL_image以解决Switch平台兼容性问题 - 添加PVF资源包解析器和脚本解析器功能 - 修改各平台配置文件添加SDL_image依赖 - 更新纹理加载逻辑使用SDL_image API - 新增脚本解析相关类用于处理游戏脚本数据
1.6 KiB
1.6 KiB
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()
代码修改:
Ptr<Texture> Texture::loadFromFile(const std::string& path) {
Asset& asset = Asset::get();
std::vector<uint8> 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<int>(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;
}
// ... 其余代码保持不变
}
测试步骤
- 应用修复
- 重新构建 Switch 版本:
xmake build -p switch - 在 Switch 上运行生成的 NRO 文件
- 验证图片能正常加载
预期结果
- stbi_load 不再崩溃
- 图片能正常加载和显示
- 程序能正常运行