Files
Frostbite2D/.opencode/plans/switch_stb_fix.md
Lenheart cb9f497fbb feat(资源加载): 替换stb_image为SDL_image并添加脚本解析功能
- 将stb_image替换为SDL_image以解决Switch平台兼容性问题
- 添加PVF资源包解析器和脚本解析器功能
- 修改各平台配置文件添加SDL_image依赖
- 更新纹理加载逻辑使用SDL_image API
- 新增脚本解析相关类用于处理游戏脚本数据
2026-03-18 04:18:57 +08:00

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;
  }
  
  // ... 其余代码保持不变
}

测试步骤

  1. 应用修复
  2. 重新构建 Switch 版本: xmake build -p switch
  3. 在 Switch 上运行生成的 NRO 文件
  4. 验证图片能正常加载

预期结果

  • stbi_load 不再崩溃
  • 图片能正常加载和显示
  • 程序能正常运行