- 将stb_image替换为SDL_image以解决Switch平台兼容性问题 - 添加PVF资源包解析器和脚本解析器功能 - 修改各平台配置文件添加SDL_image依赖 - 更新纹理加载逻辑使用SDL_image API - 新增脚本解析相关类用于处理游戏脚本数据
2.3 KiB
2.3 KiB
stbi_load_from_memory 失败调试和修复计划
问题描述
stbi_load_from_memory() 执行失败,但不知道具体失败原因。
修复方案
1. 添加详细的调试信息
修改 texture.cpp,添加以下调试信息:
- 输出读取到的文件大小
- 输出 stbi 的失败原因(使用
stbi_failure_reason())
2. 修改代码
文件: Frostbite2D/src/frostbite2D/graphics/texture.cpp
变更:
Ptr<Texture> Texture::loadFromFile(const std::string& path) {
Asset& asset = Asset::get();
std::string resolvedPath = asset.resolveAssetPath(path);
std::vector<uint8> fileData;
SDL_Log("LoadQ");
if (!asset.readBinaryFile(path, fileData)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to read texture file: %s", resolvedPath.c_str());
return nullptr;
}
SDL_Log("LoadQ1: file size = %zu bytes", fileData.size());
// 检查文件是否为空
if (fileData.empty()) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Texture file is empty: %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);
SDL_Log("LoadQ2");
if (!data) {
const char* failureReason = stbi_failure_reason();
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load texture: %s, reason: %s", resolvedPath.c_str(), failureReason ? failureReason : "unknown");
return nullptr;
}
SDL_Log("LoadQ3: %dx%d, %d channels", width, height, channels);
// ... 其余代码保持不变
}
调试步骤
- 应用上述修改
- 重新编译并运行
- 查看日志输出,特别注意:
file size的值是多少stbi_failure_reason()输出的失败原因是什么
根据失败原因的可能解决方案
如果失败原因是 "png" 相关
- 检查 PNG 文件是否损坏
- 尝试用其他图片格式(如 BMP)测试
如果失败原因是 "out of memory"
- 检查图片是否太大
- 考虑调整 stbi 的内存分配设置
如果文件大小为 0
- 检查文件路径是否正确
- 检查 Asset 类的读取是否有问题
- 检查文件是否存在于 Switch 上
如果是其他原因
- 根据具体的失败原因进一步分析