Guest User

Untitled

a guest
Sep 30th, 2026
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | Source Code | 0 0
  1. void Graphics::beginRenderLoop(
  2.     std::vector<Pass *> &graphicsPasses,
  3.     std::vector<Pass *> &computePasses,
  4.     std::function<void(double time, double deltaTime)> updateFn)
  5. {
  6.     Global::g_gui_thread = std::thread([&]() {
  7.         while (!glfwWindowShouldClose(Global::g_window)) {
  8.             if (Global::g_swapchain_dirty)
  9.                 recreateSwapchain(graphicsPasses);
  10.  
  11.             uint32_t frame;
  12.  
  13.             auto r = acquireSwapchainImage(&frame);
  14.  
  15.             if (r == VK_ERROR_OUT_OF_DATE_KHR) {
  16.                 //vkQueueWaitIdle(Global::g_queue);
  17.                 recreateSwapchain(graphicsPasses);
  18.                 continue;
  19.             }
  20.  
  21.             render(frame, graphicsPasses, computePasses);
  22.             r = presentImage(frame);
  23.  
  24.             if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR)
  25.                 recreateSwapchain(graphicsPasses);
  26.         }
  27.  
  28.         Global::g_window_running = false;
  29.     });
  30.  
  31.     uint64_t frame = 0;
  32.  
  33.     double t = 0.0;
  34.     double dt = 1000 / 60.0;
  35.     auto currTime = std::chrono::steady_clock::now();
  36.     double accumulator = 0.0;
  37.  
  38.     // Main loop
  39.     while (1) {
  40.         if (!Global::g_window_running)
  41.             break;
  42.  
  43.         auto newTime = std::chrono::steady_clock::now();
  44.         auto frameTime = std::chrono::duration<double, std::milli>(newTime - currTime).count();
  45.         currTime = newTime;
  46.  
  47.         accumulator += frameTime;
  48.  
  49.         while (accumulator >= dt) {
  50.             //processing
  51.             updateFn(t, dt);
  52.  
  53.             accumulator -= dt;
  54.             t += dt;
  55.         }
  56.  
  57.         frame++;
  58.         glfwPollEvents();
  59.         std::this_thread::sleep_for(std::chrono::duration<double, std::milli>(dt));
  60.     }
  61.  
  62.     Global::g_gui_thread.join();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment