diff --git a/cbt/window.cpp b/cbt/window.cpp index be3a9d6..6ac96dd 100644 --- a/cbt/window.cpp +++ b/cbt/window.cpp @@ -83,4 +83,10 @@ auto window::raw() const -> GLFWwindow* { return m_window; } +auto window::stop() -> void { + if (m_window) { + glfwSetWindowShouldClose(m_window, GLFW_TRUE); + } +} + } diff --git a/cbt/window.hpp b/cbt/window.hpp index 17168f6..2a37733 100644 --- a/cbt/window.hpp +++ b/cbt/window.hpp @@ -17,6 +17,7 @@ public: auto swap_buffers() -> void; auto poll_events() -> void; auto raw() const -> GLFWwindow*; + auto stop() -> void; private: GLFWwindow* m_window = nullptr; diff --git a/cuber.cpp b/cuber.cpp index 950938a..5da9cae 100644 --- a/cuber.cpp +++ b/cuber.cpp @@ -50,10 +50,9 @@ auto main(int argc, char const* argv[]) -> int { // signal handling + optional duration timer (via ASIO) asio::io_context io; asio::signal_set signals(io, SIGINT, SIGTERM); - bool quit = false; signals.async_wait([&](auto, auto) { - quit = true; + win.stop(); io.stop(); }); @@ -61,9 +60,9 @@ auto main(int argc, char const* argv[]) -> int { if (max_duration_seconds > 0.0f) { duration_timer.expires_after(std::chrono::milliseconds( static_cast(max_duration_seconds * 1000.0f))); - duration_timer.async_wait([&](auto ec) { + duration_timer.async_wait([&win](auto ec) { if (!ec) { - quit = true; + win.stop(); } }); } @@ -78,11 +77,11 @@ auto main(int argc, char const* argv[]) -> int { while (!win.should_close()) { process_signals(); - auto now = std::chrono::steady_clock::now(); - if (quit || glfwGetKey(win.raw(), GLFW_KEY_Q) == GLFW_PRESS) { - break; + if (glfwGetKey(win.raw(), GLFW_KEY_Q) == GLFW_PRESS) { + win.stop(); } + auto now = std::chrono::steady_clock::now(); auto dt = std::chrono::duration(now - prev).count(); prev = now;