mirror of
https://github.com/johndoe6345789/SDL3CPlusPlus.git
synced 2026-04-25 14:15:02 +00:00
Implement signal handling to gracefully stop the application
This commit is contained in:
18
src/main.cpp
18
src/main.cpp
@@ -5,6 +5,8 @@
|
||||
#include <rapidjson/writer.h>
|
||||
#include <rapidjson/prettywriter.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <csignal>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
@@ -20,8 +22,23 @@
|
||||
#include "app/sdl3_app.hpp"
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
namespace sdl3cpp::app {
|
||||
std::atomic<bool> g_signalReceived{false};
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
void SignalHandler(int signal) {
|
||||
if (signal == SIGINT || signal == SIGTERM) {
|
||||
sdl3cpp::app::g_signalReceived.store(true);
|
||||
}
|
||||
}
|
||||
|
||||
void SetupSignalHandlers() {
|
||||
std::signal(SIGINT, SignalHandler);
|
||||
std::signal(SIGTERM, SignalHandler);
|
||||
}
|
||||
|
||||
std::filesystem::path FindScriptPath(const char* argv0) {
|
||||
std::filesystem::path executable;
|
||||
if (argv0 && *argv0 != '\0') {
|
||||
@@ -294,6 +311,7 @@ void WriteRuntimeConfigJson(const RuntimeConfig& runtimeConfig,
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
SDL_SetMainReady();
|
||||
SetupSignalHandlers();
|
||||
try {
|
||||
AppOptions options = ParseCommandLine(argc, argv);
|
||||
sdl3cpp::app::TraceLogger::SetEnabled(options.traceEnabled);
|
||||
|
||||
Reference in New Issue
Block a user