diff --git a/app/src/main/cpp/main.cpp b/app/src/main/cpp/main.cpp index 4bd32da..c7e260c 100644 --- a/app/src/main/cpp/main.cpp +++ b/app/src/main/cpp/main.cpp @@ -42,9 +42,9 @@ void android_main(struct android_app *pApp) { pApp->onAppCmd = handle_cmd; android_app_set_motion_event_filter(pApp, nullptr); - + long long _lastDrawFrameTime = chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count(); do { - auto ms = chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count(); + auto start_time = chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count(); // Process all pending events before running game logic. bool done = false; while (!done) { @@ -72,10 +72,16 @@ void android_main(struct android_app *pApp) { } if(application.isInited()) { - application.drawFrame(); + auto frameTime = (start_time - _lastDrawFrameTime); + _lastDrawFrameTime = chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count(); + application.drawFrame(frameTime); + } + auto end_time = chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count(); + auto function_time = (end_time - start_time); + if(function_time < 20) + { + this_thread::sleep_for(chrono::milliseconds((20-function_time))); } - auto end_ms = chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count(); - this_thread::sleep_for(chrono::milliseconds((end_ms-ms))); } while (!pApp->destroyRequested); application.cleanup(); } diff --git a/example/src/main/assets/底妆/基础上妆/2按压/区域/demo0.png b/example/src/main/assets/bottom_makeup/base_makeup/press2/area/pic0.png similarity index 100% rename from example/src/main/assets/底妆/基础上妆/2按压/区域/demo0.png rename to example/src/main/assets/bottom_makeup/base_makeup/press2/area/pic0.png diff --git a/example/src/main/assets/底妆/基础上妆/2按压/区域/demo0_ex.png b/example/src/main/assets/bottom_makeup/base_makeup/press2/area/pic0_thick.png similarity index 100% rename from example/src/main/assets/底妆/基础上妆/2按压/区域/demo0_ex.png rename to example/src/main/assets/bottom_makeup/base_makeup/press2/area/pic0_thick.png diff --git a/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/demo0.png b/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/demo0.png new file mode 100644 index 0000000..c9e5244 Binary files /dev/null and b/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/demo0.png differ diff --git a/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/demo0_thick.png b/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/demo0_thick.png new file mode 100644 index 0000000..4cbe056 Binary files /dev/null and b/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/demo0_thick.png differ diff --git a/example/src/main/assets/底妆/基础上妆/2按压/方向/pic0.png b/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic0.png similarity index 100% rename from example/src/main/assets/底妆/基础上妆/2按压/方向/pic0.png rename to example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic0.png diff --git a/example/src/main/assets/底妆/基础上妆/2按压/方向/pic0_thick.png b/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic0_thick.png similarity index 100% rename from example/src/main/assets/底妆/基础上妆/2按压/方向/pic0_thick.png rename to example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic0_thick.png diff --git a/example/src/main/assets/底妆/基础上妆/2按压/方向/pic1.png b/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic1.png similarity index 100% rename from example/src/main/assets/底妆/基础上妆/2按压/方向/pic1.png rename to example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic1.png diff --git a/example/src/main/assets/底妆/基础上妆/2按压/方向/pic1_thick.png b/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic1_thick.png similarity index 100% rename from example/src/main/assets/底妆/基础上妆/2按压/方向/pic1_thick.png rename to example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic1_thick.png diff --git a/example/src/main/assets/底妆/基础上妆/2按压/方向/pic2.png b/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic2.png similarity index 100% rename from example/src/main/assets/底妆/基础上妆/2按压/方向/pic2.png rename to example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic2.png diff --git a/example/src/main/assets/底妆/基础上妆/2按压/方向/pic2_thick.png b/example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic2_thick.png similarity index 100% rename from example/src/main/assets/底妆/基础上妆/2按压/方向/pic2_thick.png rename to example/src/main/assets/bottom_makeup/base_makeup/press2/direction/pic2_thick.png diff --git a/vulkan/Application.cpp b/vulkan/Application.cpp index 39e3400..b49e22d 100644 --- a/vulkan/Application.cpp +++ b/vulkan/Application.cpp @@ -127,8 +127,11 @@ void Application::mainLoop() #ifdef _WIN32 while (!glfwWindowShouldClose(window)) { + auto cur_time = getCurrentTimeMillis(); glfwPollEvents(); - drawFrame(); // 在这里调用 + auto functionTime = (cur_time - _lastDrawFrameTime); + _lastDrawFrameTime = getCurrentTimeMillis(); + drawFrame(functionTime); // 在这里调用 } #else @@ -448,7 +451,7 @@ void Application::createSyncObjects() } } -void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) +void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex, long long frameTime) { VkCommandBufferBeginInfo beginInfo{}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; @@ -473,7 +476,7 @@ void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t im vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); - render(commandBuffer); + render(commandBuffer, frameTime); // 结束渲染流程 vkCmdEndRenderPass(commandBuffer); @@ -483,7 +486,7 @@ void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t im } } -void Application::render(VkCommandBuffer commandBuffer) +void Application::render(VkCommandBuffer commandBuffer, long long frameTime) { // 绑定图形管线 vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline); @@ -492,7 +495,7 @@ void Application::render(VkCommandBuffer commandBuffer) vkCmdDraw(commandBuffer, 3, 1, 0, 0); } -void Application::drawFrame() +void Application::drawFrame(long long frameTime) { // 1. 等待前一帧完成 vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX); @@ -517,7 +520,7 @@ void Application::drawFrame() // 5. 记录命令缓冲区 vkResetCommandBuffer(commandBuffers[currentFrame], 0); - recordCommandBuffer(commandBuffers[currentFrame], imageIndex); + recordCommandBuffer(commandBuffers[currentFrame], imageIndex, frameTime); // 6. 提交命令缓冲区 VkSubmitInfo submitInfo{}; @@ -561,7 +564,6 @@ void Application::drawFrame() // 8. 前进到下一帧 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; - _lastDrawFrameTime = } void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator) { diff --git a/vulkan/Application.h b/vulkan/Application.h index 5f28180..a316774 100644 --- a/vulkan/Application.h +++ b/vulkan/Application.h @@ -37,10 +37,10 @@ public: void createPipelineLayout(); void createGraphicsPipeline(); void createRenderPass(); - void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex); + void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex, long long frameTime); void createSyncObjects(); - void drawFrame(); - virtual void render(VkCommandBuffer commandBuffer); + void drawFrame(long long frameTime); + virtual void render(VkCommandBuffer commandBuffer, long long frameTime); virtual bool isInited() { return inited; } void processWithVulkan(uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& out_texture, bool srgb); diff --git a/vulkan/FaceApp.cpp b/vulkan/FaceApp.cpp index 7374b9d..555ca00 100644 --- a/vulkan/FaceApp.cpp +++ b/vulkan/FaceApp.cpp @@ -4,7 +4,6 @@ - FaceApp* FaceApp::faceIns = nullptr; FaceApp::FaceApp(/* args */) @@ -515,8 +514,21 @@ void FaceApp::setup_descriptor_set() } -void FaceApp::render(VkCommandBuffer commandBuffer) +void FaceApp::render(VkCommandBuffer commandBuffer, long long frameTime) { + static long long game_time = 0; + game_time += frameTime; + long long actionTime = (1000 / _initArg.action_fps); + if (game_time > actionTime) + { + _curTexIndex++; + if (_curTexIndex >= _curMotion.png_num) + { + _curTexIndex = 0; + } + game_time = game_time - actionTime; + } + std::unique_lock lock(mtx); std::unique_lock lock_point(mtx_point); //Application::render(commandBuffer); @@ -527,7 +539,8 @@ void FaceApp::render(VkCommandBuffer commandBuffer) vkCmdPushConstants(commandBuffer, m_pipelineLayout_bg, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(float), &myFloatValue); - //vkCmdDraw(commandBuffer, 6, 1, 0, 0); + vkCmdDraw(commandBuffer, 6, 1, 0, 0); + vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, 1, &m_descriptor_sets[_curTexIndex], 0, NULL); @@ -604,8 +617,9 @@ void FaceApp::initVulkan() uploadVertexData(); last_update_time = getCurrentTimeMillis(); - faceAppInited = true; + changeMotion(_initArg.motion); + faceAppInited = true; #if _WIN32 std::vector floatArray; std::string& str = HardCodeData::Get().face_result_point_str; @@ -616,6 +630,7 @@ void FaceApp::initVulkan() } //ReceiveFacePoint(floatArray.data(), floatArray.size() / 3, 480, 480); + #endif } @@ -1003,8 +1018,15 @@ void FaceApp::changeMotion(Motion& motion) { string path = _curMotion.type + "/" + _curMotion.technique + "/" + _curMotion.step + "/" + _curMotion.show_type; string path_name = path + "/" + _curMotion.png_name + std::to_string(i) + ".png"; - loadTexture(path_name, m_texs[i], true); string path_name_ex = path + "/" + _curMotion.png_name + std::to_string(i) + "_thick.png"; +#ifdef _WIN32 + loadTextureExample(path_name, m_texs[i], true); + loadTextureExample(path_name_ex, m_texs_ex[i], true); +#else loadTexture(path_name, m_texs[i], true); + loadTexture(path_name, path_name_ex[i], true); +#endif // _WIN32 + + } } \ No newline at end of file diff --git a/vulkan/FaceApp.h b/vulkan/FaceApp.h index 572f625..47b9021 100644 --- a/vulkan/FaceApp.h +++ b/vulkan/FaceApp.h @@ -29,7 +29,7 @@ struct InitArg int action_fps; float zoom; Motion motion; - NLOHMANN_DEFINE_TYPE_INTRUSIVE(InitArg, action_fps, zoom) + NLOHMANN_DEFINE_TYPE_INTRUSIVE(InitArg, action_fps, zoom, motion); }; @@ -56,7 +56,7 @@ public: void initVulkan() override; - void render(VkCommandBuffer commandBuffer) override; + void render(VkCommandBuffer commandBuffer, long long frameTime) override; static FaceApp* Get() { return faceIns; } void update_face_vertex_buffer(float* pos, int pointCount); diff --git a/vulkan/main.cpp b/vulkan/main.cpp index bbd9be6..ee04497 100644 --- a/vulkan/main.cpp +++ b/vulkan/main.cpp @@ -2,16 +2,16 @@ #include "FaceApp.h" - int main() { InitArg init_arg; Motion motion; - motion.type = "底妆"; - motion.technique = "基础上妆"; - motion.step = "2按压"; - motion.show_type = "方向"; - motion.png_name = "pic"; - motion.png_num = 3; + motion.type = "bottom_makeup"; + motion.technique = "base_makeup"; + motion.step = "press2"; + motion.show_type = "direction"; + motion.png_name = "demo"; + motion.png_num = 1; +# init_arg.motion = motion; @@ -22,8 +22,6 @@ int main() { //motion.png_name = "pic"; //motion.png_num = 1; - //init_arg.motions.push_back(motion); - init_arg.action_fps = 3; init_arg.zoom = 1.5f; @@ -32,12 +30,13 @@ int main() { FaceApp app; app.SetInitArg(json_str.c_str()); app.initVulkan(); - try { - app.mainLoop(); - } catch (const std::exception& e) { - std::cerr << e.what() << std::endl; - return EXIT_FAILURE; - } + app.mainLoop(); + //try { + // app.mainLoop(); + //} catch (const std::exception& e) { + // std::cerr << e.what() << std::endl; + // return EXIT_FAILURE; + //} app.cleanup(); return EXIT_SUCCESS; } \ No newline at end of file