docs: update resources API docs
This commit is contained in:
66
docs/api/resources/resource-file-system/read-resource.md
Normal file
66
docs/api/resources/resource-file-system/read-resource.md
Normal file
@@ -0,0 +1,66 @@
|
||||
# ResourceFileSystem::ReadResource
|
||||
|
||||
## 方法签名
|
||||
|
||||
```cpp
|
||||
Containers::Array<Core::uint8> ReadResource(const Containers::String& relativePath) const;
|
||||
```
|
||||
|
||||
## 所属类
|
||||
|
||||
`XCEngine::Resources::ResourceFileSystem`
|
||||
|
||||
## 描述
|
||||
|
||||
读取指定相对路径的资源内容,以字节数组形式返回。该方法会首先查找资源的绝对路径,然后从文件系统读取数据。
|
||||
|
||||
此方法线程安全。
|
||||
|
||||
## 参数
|
||||
|
||||
| 参数名 | 类型 | 描述 |
|
||||
|--------|------|------|
|
||||
| `relativePath` | `const Containers::String&` | 资源的相对路径 |
|
||||
|
||||
## 返回值
|
||||
|
||||
- `Containers::Array<Core::uint8>` - 资源数据的字节数组。如果读取失败或资源不存在,返回空数组。
|
||||
|
||||
## 示例
|
||||
|
||||
```cpp
|
||||
#include "Resources/ResourceFileSystem.h"
|
||||
|
||||
using namespace XCEngine::Resources;
|
||||
|
||||
ResourceFileSystem& fs = ResourceFileSystem::Get();
|
||||
fs.Initialize("C:/Game/Resources");
|
||||
fs.AddDirectory("C:/Game/Assets");
|
||||
|
||||
// 读取文本资源
|
||||
auto textData = fs.ReadResource("shaders/forward.glsl");
|
||||
if (textData.Size() > 0) {
|
||||
printf("Shader size: %zu bytes\n", textData.Size());
|
||||
// 处理着色器源码
|
||||
}
|
||||
|
||||
// 读取二进制资源
|
||||
auto texData = fs.ReadResource("textures/player/diffuse.png");
|
||||
if (texData.Size() > 0) {
|
||||
// 使用图像数据
|
||||
const uint8_t* pixels = texData.Data();
|
||||
size_t width = *reinterpret_cast<const size_t*>(pixels);
|
||||
}
|
||||
|
||||
// 读取不存在的资源
|
||||
auto invalidData = fs.ReadResource("nonexistent/file.txt");
|
||||
if (invalidData.Empty()) {
|
||||
printf("Resource not found or read error\n");
|
||||
}
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
- 返回的数组直接包含文件的原始字节数据
|
||||
- 当前实现仅支持从文件系统读取,档案读取待实现
|
||||
- 读取失败时返回空数组,可通过 `Empty()` 方法检查
|
||||
Reference in New Issue
Block a user