feat: add ASIO signal handling for graceful shutdown
- Add asio dependency for async signal handling - Register SIGINT and SIGTERM to quit the application - Poll signals each frame for non-blocking shutdown - Q key and Ctrl+C both cleanly exit the program
This commit is contained in:
@@ -3,6 +3,10 @@
|
||||
|
||||
#include "cbt/window.hpp"
|
||||
|
||||
#include <csignal>
|
||||
|
||||
#include <asio.hpp>
|
||||
|
||||
#include "glad/glad.h"
|
||||
|
||||
auto main(int, char const*[]) -> int {
|
||||
@@ -12,8 +16,23 @@ auto main(int, char const*[]) -> int {
|
||||
return 1;
|
||||
}
|
||||
|
||||
asio::io_context io;
|
||||
asio::signal_set signals(io, SIGINT, SIGTERM);
|
||||
bool quit = false;
|
||||
|
||||
signals.async_wait([&](auto, auto) {
|
||||
quit = true;
|
||||
io.stop();
|
||||
});
|
||||
|
||||
auto process_signals = [&]() -> void {
|
||||
while (io.poll()) {}
|
||||
};
|
||||
|
||||
while (!w.should_close()) {
|
||||
if (glfwGetKey(static_cast<GLFWwindow*>(w.raw()), GLFW_KEY_Q) == GLFW_PRESS) {
|
||||
process_signals();
|
||||
|
||||
if (quit || glfwGetKey(static_cast<GLFWwindow*>(w.raw()), GLFW_KEY_Q) == GLFW_PRESS) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user