save code
This commit is contained in:
+57
-17
@@ -47,9 +47,44 @@ void CppAnimationFinishedCallback()
|
||||
std::cout << "call CppAnimationFinishedCallback\n";
|
||||
}
|
||||
|
||||
void CppCallback(const string& motion_type)
|
||||
void CppCallback()
|
||||
{
|
||||
g_app->changeMotion(motion_type.c_str(), CppAnimationFinishedCallback, true);
|
||||
g_app->changeMotionList(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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -62,23 +97,25 @@ int main() {
|
||||
|
||||
InitArg init_arg;
|
||||
Motion motion;
|
||||
motion.type = "4";
|
||||
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");
|
||||
//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;
|
||||
|
||||
@@ -103,8 +140,11 @@ int main() {
|
||||
std::cout << "Warning: Empty motion string" << std::endl;
|
||||
}
|
||||
|
||||
vector<string> s_motions;
|
||||
json j = motions["4"];
|
||||
s_motions.push_back(j.dump());
|
||||
|
||||
app.preReadyMotion(motion_str, CppCallback);
|
||||
app.preLoadMotionList(s_motions, CppCallback);
|
||||
app.mainLoop();
|
||||
//try {
|
||||
// app.mainLoop();
|
||||
|
||||
Reference in New Issue
Block a user