diff --git a/code/apps/client/CMakeLists.txt b/code/apps/client/CMakeLists.txt index a31bde7..049e200 100644 --- a/code/apps/client/CMakeLists.txt +++ b/code/apps/client/CMakeLists.txt @@ -23,7 +23,9 @@ add_library(client-common STATIC source/network.c source/game.c source/main.c + source/signal_handling.c + header/signal_handling.h header/network.h header/platform.h header/game.h diff --git a/code/apps/client/header/signal_handling.h b/code/apps/client/header/signal_handling.h new file mode 100644 index 0000000..e015253 --- /dev/null +++ b/code/apps/client/header/signal_handling.h @@ -0,0 +1,4 @@ +#pragma once + +void sighandler_register(); +void sighandler_unregister(); diff --git a/code/apps/client/source/game.c b/code/apps/client/source/game.c index a6af58a..01b1ee4 100644 --- a/code/apps/client/source/game.c +++ b/code/apps/client/source/game.c @@ -6,7 +6,7 @@ void game_init() { } void game_shutdown() { - platform_shutdown(); + // } uint8_t game_is_running() { diff --git a/code/apps/client/source/main.c b/code/apps/client/source/main.c index b3c6bc8..53b318f 100644 --- a/code/apps/client/source/main.c +++ b/code/apps/client/source/main.c @@ -1,9 +1,11 @@ #define ZPL_IMPL #include "zpl.h" #include "game.h" +#include "signal_handling.h" int main(void) { + sighandler_register(); game_init(); while (game_is_running()) @@ -14,5 +16,6 @@ int main(void) } game_shutdown(); + sighandler_unregister(); return 0; } diff --git a/code/apps/client/source/network.c b/code/apps/client/source/network.c index 19e2912..a040392 100644 --- a/code/apps/client/source/network.c +++ b/code/apps/client/source/network.c @@ -1,4 +1,3 @@ -#define ZPL_IMPL #include "zpl.h" #define LIBRG_IMPL diff --git a/code/apps/client/source/platform_text.c b/code/apps/client/source/platform_text.c index e11dfd4..d64b1bd 100644 --- a/code/apps/client/source/platform_text.c +++ b/code/apps/client/source/platform_text.c @@ -1,18 +1,24 @@ #include "platform.h" +#include "zpl.h" #include +uint8_t is_running; + void platform_init() { printf("eco2d-cli\n"); + is_running = 1; } void platform_shutdown() { - printf("Bye!\n"); + printf("\nBye!\n"); + is_running = 0; } uint8_t platform_is_running() { - + return is_running; } void platform_render() { - // TODO + zpl_printf("o"); + zpl_sleep_ms(1000); } diff --git a/code/apps/client/source/signal_handling.c b/code/apps/client/source/signal_handling.c new file mode 100644 index 0000000..a7a3a3c --- /dev/null +++ b/code/apps/client/source/signal_handling.c @@ -0,0 +1,53 @@ +#include +#include "zpl.h" +#include "platform.h" +#ifdef ZPL_SYSTEM_WINDOWS + +static BOOL WINAPI _sighandler_win32_control_handler(DWORD control_type) +{ + switch (control_type) + { + case CTRL_C_EVENT: + case DBG_CONTROL_C: + platform_shutdown(); + return 0; + case CTRL_CLOSE_EVENT: + case CTRL_LOGOFF_EVENT: + case CTRL_BREAK_EVENT: + case CTRL_SHUTDOWN_EVENT: + platform_shutdown(); + return 1; + } + + return 0; +} +#else //POSIX complaint +#include +static void _sighandler_posix_signal_handler(int sig) { + platform_shutdown(); +} +#endif + +void sighandler_register() { + #ifdef ZPL_SYSTEM_WINDOWS + { + if (!SetConsoleCtrlHandler(_sighandler_win32_control_handler, 1)) { + zpl_printf("Could not set up signal handler!\n"); + } + } + #else // POSIX compliant + signal(SIGINT, &_sighandler_posix_signal_handler); + signal(SIGTERM, &_sighandler_posix_signal_handler); + #endif +} + +void sighandler_unregister() { + #ifdef ZPL_SYSTEM_WINDOWS + { + if (!SetConsoleCtrlHandler(_sighandler_win32_control_handler, 0)) { + zpl_printf("Could not uninstall signal handler!"); + } + } + #else // POSIX compliant + #endif +}