feat(资源管理): 添加NPK文件格式支持及精灵创建功能

实现NPK文件格式的解析和缓存管理,支持从NPK文件中加载精灵图像
添加Sprite::createFromNpk方法用于从NPK创建精灵
修改Texture和Sprite相关方法以支持const数据
添加zlib依赖用于NPK文件解压
优化Asset::listFilesWithExtension的扩展名匹配逻辑
This commit is contained in:
2026-03-20 04:21:42 +08:00
parent 29dec1f64b
commit 18111dae6b
11 changed files with 646 additions and 55 deletions

View File

@@ -14,8 +14,9 @@
#include <frostbite2D/resource/script_parser.h>
#include <frostbite2D/audio/audio_system.h>
#include <frostbite2D/audio/sound.h>
#include <frostbite2D/audio/music.h>
#include <frostbite2D/audio/sound.h>
#include <frostbite2D/resource/npk_archive.h>
using namespace frostbite2D;
@@ -42,59 +43,59 @@ int main(int argc, char **argv) {
// 尝试加载精灵
auto sprite = Sprite::createFromFile("assets/player.png");
if (sprite) {
sprite->SetPosition(100, 100);
sprite->SetPosition(0, 0);
menuScene->AddActor(sprite);
SDL_Log("Sprite created and added to scene");
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create sprite from file!");
}
auto &archive = PvfArchive::get();
if (archive.open("assets/Script.pvf")) {
archive.init();
// 文件内容读取
if (auto rawData = archive.getFileRawData("region/balmayer_north.rgn")) {
ScriptParser parser(*rawData, "script/example.bin");
// // 方式1迭代解析
// while (!parser.isEnd()) {
// if (auto value = parser.next()) {
// switch (value->type) {
// case ScriptValueType::Integer:
// SDL_Log("Integer: %d", value->intValue);
// break;
// case ScriptValueType::Float:
// SDL_Log("Float: %f", value->floatValue);
// break;
// case ScriptValueType::String:
// SDL_Log("String: %s", value->stringValue.c_str());
// break;
// default:
// break;
// }
// }
// }
// auto &archive = PvfArchive::get();
// if (archive.open("assets/Script.pvf")) {
// archive.init();
// // 文件内容读取
// if (auto rawData = archive.getFileRawData("region/balmayer_north.rgn")) {
// ScriptParser parser(*rawData, "script/example.bin");
// // // 方式1迭代解析
// // while (!parser.isEnd()) {
// // if (auto value = parser.next()) {
// // switch (value->type) {
// // case ScriptValueType::Integer:
// // SDL_Log("Integer: %d", value->intValue);
// // break;
// // case ScriptValueType::Float:
// // SDL_Log("Float: %f", value->floatValue);
// // break;
// // case ScriptValueType::String:
// // SDL_Log("String: %s", value->stringValue.c_str());
// // break;
// // default:
// // break;
// // }
// // }
// // }
// // 方式2批量解析
// parser.reset();
auto allValues = parser.parseAll();
for (const auto &value : allValues) {
// 处理 value
switch (value.type) {
case ScriptValueType::Integer:
SDL_Log("Integer: %d", value.intValue);
break;
case ScriptValueType::Float:
SDL_Log("Float: %f", value.floatValue);
break;
case ScriptValueType::String:
SDL_Log("String: %s", value.stringValue.c_str());
break;
default:
break;
}
}
}
}
// // // 方式2批量解析
// // parser.reset();
// auto allValues = parser.parseAll();
// for (const auto &value : allValues) {
// // 处理 value
// switch (value.type) {
// case ScriptValueType::Integer:
// SDL_Log("Integer: %d", value.intValue);
// break;
// case ScriptValueType::Float:
// SDL_Log("Float: %f", value.floatValue);
// break;
// case ScriptValueType::String:
// SDL_Log("String: %s", value.stringValue.c_str());
// break;
// default:
// break;
// }
// }
// }
// }
AudioSystem::get().init();
AudioSystem::get().setMasterVolume(1.0f);
@@ -108,6 +109,19 @@ int main(int argc, char **argv) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load background music!");
}
NpkArchive &npk = NpkArchive::get();
npk.setImagePackDirectory("assets/ImagePacks2");
npk.setDefaultImg("sprite/interface/base.img", 0);
npk.init();
auto sprite1 = Sprite::createFromNpk("sprite/newtitle/nangua.img", 0);
if (sprite1) {
sprite1->SetPosition(0, 0);
menuScene->AddActor(sprite1);
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create sprite from NPK!");
}
app.run();
app.shutdown();