Files
2025-12-22 14:55:00 +08:00

186 lines
4.9 KiB
C++

#include "FaceApp.h"
std::wstring ReadUTF8File(const std::string& filePath)
{
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("无法打开UTF-8文件: " + filePath);
}
// 读取文件所有内容
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
file.close();
// 将UTF-8字符串转换为宽字符串
return AppBase::UTF8ToWideString(content);
}
std::wstring ReadFileWithChinesePath(const std::wstring& filePath) {
// 使用二进制模式打开文件
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open()) {
std::wcout << L"无法打开文件: " << filePath << std::endl;
return L"";
}
// 读取文件内容到std::string
std::string context;
file.seekg(0, std::ios::end);
context.resize(file.tellg());
file.seekg(0, std::ios::beg);
file.read(&context[0], context.size());
file.close();
return AppBase::UTF8ToWideString(context);
}
FaceApp* g_app;
void CppAnimationFinishedCallback()
{
std::cout << "call CppAnimationFinishedCallback\n";
}
void CppCallback()
{
vector<string> motions = {"4"};
g_app->changeMotionList(motions, CppAnimationFinishedCallback, true);
}
// 读取JSON文件并填充到motions容器中
void loadMotions(const std::string& filePath, std::map<std::string, Motion>& motions) {
// 读取json文件
std::ifstream i(filePath);
if (!i.is_open()) {
std::cerr << "无法打开文件" << std::endl;
return;
}
json j;
try {
i >> j;
}
catch (json::parse_error& ex) { // 捕获解析错误
std::cerr << "JSON解析错误: " << ex.what() << std::endl;
return;
}
// 遍历json对象并填充到motions中
for (auto& motionEntry : j.items()) {
Motion motion;
motion.name = motionEntry.key();
for (auto& frameEntry : motionEntry.value().items()) {
Frame frame;
frame.name = frameEntry.key();
frame.x = frameEntry.value()["x"].get<float>();
frame.y = frameEntry.value()["y"].get<float>();
motion.frames.push_back(frame);
}
motions[motion.getMotionId()] = motion;
}
}
bool threadRun = false;
// 线程要执行的函数
void threadFunction() {
int startTime = 0;
for (int i = 0; i < 500; ++i)
{
if (!threadRun)
{
break;
}
startTime += 100;
if (startTime == 10 * 1000)
{
vector<string> motions = { "5" };
g_app->changeMotionList(motions, CppAnimationFinishedCallback, true);
}
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 暂停200ms
}
}
int main() {
//std::string path = "chinese.txt";
//std::wstring chinese = ReadUTF8File(path);
//std::wstring chinese_path = AppBase::UTF8ToWideString("d:/测试目录/中文文件.txt");
//std::wstring content = ReadFileWithChinesePath(chinese_path);
InitArg init_arg;
Motion motion;
motion.name = "4";
//motion.png_names.push_back("4.png");
//for (int i = 1; i < 61; ++i)
//{
// if (i < 10)
// {
// motion.png_names.push_back("00" + std::to_string(i) + ".png");
// }
// else
// {
// motion.png_names.push_back("0" + std::to_string(i) + ".png");
// }
//
//}
std::map<string, Motion> motions;
loadMotions("example/src/main/assets/pic/motion_data_ex.json", motions);
//init_arg.motion = motion;
init_arg.action_fps = 10;
init_arg.zoom = 1.5f;
string json_str = json(init_arg).dump();
FaceApp app;
g_app = &app;
app.SetInitArg(json_str.c_str());
app.initVulkan();
app._running = true;
auto motion_json = json(motion);
if (!motion_json.is_object()) {
std::cout << "Invalid JSON structure" << std::endl;
}
std::string motion_str = motion_json.dump();
// 添加完整性检查
if (motion_str.empty()) {
std::cout << "Warning: Empty motion string" << std::endl;
}
MotionList s_motions;
s_motions.motions.push_back(motions["4"]);
s_motions.motions.push_back(motions["5"]);
//s_motions.motions.push_back(motions["13"]);
json j = s_motions;
auto s = j.dump();
app.preLoadMotionList(s, CppCallback);
threadRun = true;
std::thread t(threadFunction);
app.mainLoop();
threadRun = false;
t.join();
//try {
// app.mainLoop();
//} catch (const std::exception& e) {
// std::cerr << e.what() << std::endl;
// return EXIT_FAILURE;
//}
app.cleanup();
return EXIT_SUCCESS;
}