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:
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user