refactor: add window::stop() and remove quit flag

Added `window::stop()` (sets GLFW close flag). Updated
signal/timer handlers and Q key check to call it instead of
using a separate `quit` bool in main().

This encapsulates the close state in the window class (no
more external flag + manual checks). The render loop is now
simpler.

(The process_signals lambda and ASIO duration timer are
retained.)
This commit is contained in:
2026-05-05 23:45:46 +02:00
parent 3f78d0978d
commit c3860cc1d3
3 changed files with 13 additions and 7 deletions
+6
View File
@@ -83,4 +83,10 @@ auto window::raw() const -> GLFWwindow* {
return m_window; return m_window;
} }
auto window::stop() -> void {
if (m_window) {
glfwSetWindowShouldClose(m_window, GLFW_TRUE);
}
}
} }
+1
View File
@@ -17,6 +17,7 @@ public:
auto swap_buffers() -> void; auto swap_buffers() -> void;
auto poll_events() -> void; auto poll_events() -> void;
auto raw() const -> GLFWwindow*; auto raw() const -> GLFWwindow*;
auto stop() -> void;
private: private:
GLFWwindow* m_window = nullptr; GLFWwindow* m_window = nullptr;
+6 -7
View File
@@ -50,10 +50,9 @@ auto main(int argc, char const* argv[]) -> int {
// signal handling + optional duration timer (via ASIO) // signal handling + optional duration timer (via ASIO)
asio::io_context io; asio::io_context io;
asio::signal_set signals(io, SIGINT, SIGTERM); asio::signal_set signals(io, SIGINT, SIGTERM);
bool quit = false;
signals.async_wait([&](auto, auto) { signals.async_wait([&](auto, auto) {
quit = true; win.stop();
io.stop(); io.stop();
}); });
@@ -61,9 +60,9 @@ auto main(int argc, char const* argv[]) -> int {
if (max_duration_seconds > 0.0f) { if (max_duration_seconds > 0.0f) {
duration_timer.expires_after(std::chrono::milliseconds( duration_timer.expires_after(std::chrono::milliseconds(
static_cast<long long>(max_duration_seconds * 1000.0f))); static_cast<long long>(max_duration_seconds * 1000.0f)));
duration_timer.async_wait([&](auto ec) { duration_timer.async_wait([&win](auto ec) {
if (!ec) { if (!ec) {
quit = true; win.stop();
} }
}); });
} }
@@ -78,11 +77,11 @@ auto main(int argc, char const* argv[]) -> int {
while (!win.should_close()) { while (!win.should_close()) {
process_signals(); process_signals();
auto now = std::chrono::steady_clock::now(); if (glfwGetKey(win.raw(), GLFW_KEY_Q) == GLFW_PRESS) {
if (quit || glfwGetKey(win.raw(), GLFW_KEY_Q) == GLFW_PRESS) { win.stop();
break;
} }
auto now = std::chrono::steady_clock::now();
auto dt = std::chrono::duration<float>(now - prev).count(); auto dt = std::chrono::duration<float>(now - prev).count();
prev = now; prev = now;