Files
Frostbite2D/.opencode/plans/switch_asset_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

75 lines
2.1 KiB
Markdown

# Switch 平台 Asset 类崩溃修复计划
## 问题描述
调用 `Sprite::createFromFile("assets/player.png")` 时崩溃,甚至连 printf 都没执行。崩溃发生在 `Asset::readBinaryFile()` 内部的 `fs::exists()` 调用上。
## 问题根因分析
Switch 平台上的 `std::filesystem` 实现可能不稳定或不完整,导致 `fs::exists()` 调用直接崩溃。
## 修复方案
修改 `Asset::readBinaryFile()` 函数,**移除文件存在性检查**,直接尝试打开文件。这样可以避免调用有问题的 `fs::exists()`
### 修复: 修改 asset.cpp
**文件**: `Frostbite2D/src/frostbite2D/utils/asset.cpp`
**变更**:
- 移除 `readBinaryFile()` 中的 `exists()` 检查
- 直接尝试打开文件,通过 `file.is_open()` 来判断是否成功
**代码修改**:
```cpp
bool Asset::readBinaryFile(const std::string &path,
std::vector<uint8> &outData) {
std::string fullPath = resolveFullPath(path);
// 移除 fs::exists() 检查,直接尝试打开文件
std::ifstream file(toPath(fullPath), std::ios::in | std::ios::binary);
if (!file.is_open()) {
return false;
}
file.seekg(0, std::ios::end);
auto size = file.tellg();
file.seekg(0, std::ios::beg);
outData.resize(static_cast<size_t>(size));
file.read(reinterpret_cast<char *>(outData.data()), size);
file.close();
return true;
}
```
### 可选: 同时修复 readTextFile
为了保持一致性,也可以对 `readTextFile()` 做同样的修改:
```cpp
bool Asset::readTextFile(const std::string &path, std::string &outContent) {
std::string fullPath = resolveFullPath(path);
// 移除 fs::exists() 检查,直接尝试打开文件
std::ifstream file(toPath(fullPath), std::ios::in | std::ios::binary);
if (!file.is_open()) {
return false;
}
std::ostringstream ss;
ss << file.rdbuf();
outContent = ss.str();
file.close();
return true;
}
```
## 测试步骤
1. 应用修复
2. 重新构建 Switch 版本: `xmake build -p switch`
3. 在 Switch 上运行生成的 NRO 文件
4. 验证图片能正常加载
## 预期结果
- 不再崩溃
- 文件能正常读取
- 图片能正常加载和显示