diff --git a/CMakeLists.txt b/CMakeLists.txt index ad9634e..af519c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ add_executable(csnake src/player.c src/sleep.c src/platform/getch.c + src/platform/game.c ) set_target_properties(csnake PROPERTIES C_STANDARD 11) diff --git a/Makefile b/Makefile index a55765b..09072e9 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ INCLUDE = -Iinclude RM = rm -f SRCDIR = src OBJDIR = obj -SRC = main.c screen.c input.c player.c sleep.c platform/getch.c +SRC = main.c screen.c input.c player.c sleep.c platform/getch.c platform/game.c OBJ = $(addprefix $(OBJDIR)/,$(SRC:.c=.o)) DEFLDFLAGS = $(shell if echo "" | cc -E -dM -xc - | grep __FreeBSD__ > /dev/null 2>&1; then echo "-lpthread"; fi) diff --git a/include/platform/game.h b/include/platform/game.h new file mode 100644 index 0000000..57bb2c0 --- /dev/null +++ b/include/platform/game.h @@ -0,0 +1,6 @@ +#ifndef __PLATFORM_GAME_H__ +#define __PLATFORM_GAME_H__ + +void platformGameInit(void); + +#endif /* __PLATFORM_GAME_H__ */ diff --git a/include/platform/getch.h b/include/platform/getch.h index 15c66fe..50ba078 100644 --- a/include/platform/getch.h +++ b/include/platform/getch.h @@ -7,7 +7,9 @@ inline int getchInit(void) { return 0; } #else int getch(void); -int getchInit(void); +void getchInit(void); +void getchResetTerminalStateHandler(int sig); +void getchResetTerminalState(void); #endif /* _WIN32 */ #endif /* __GETCH_H__ */ diff --git a/src/main.c b/src/main.c index 11bcfeb..80ccd28 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,7 @@ #include "sleep.h" #include "platform/thread.h" #include "platform/screen.h" +#include "platform/game.h" void drawPlayer(Player player, Screen screen) { @@ -32,6 +33,7 @@ Food generateFood(Player player) int main(int argc, char **argv) { srand((unsigned int)time(NULL)); + platformGameInit(); Player player; playerCreate(&player, DOWN, DEFX, DEFY, 0); Screen screen; screenCreate(&screen, SIZE, SIZE, ' '); diff --git a/src/platform/game.c b/src/platform/game.c new file mode 100644 index 0000000..122d186 --- /dev/null +++ b/src/platform/game.c @@ -0,0 +1,24 @@ +#ifdef _WIN32 +void platformGameInit(void) {} +#else + +#include +#include + +#include "platform/getch.h" + +void platformGameInit(void) +{ + getchInit(); + + atexit(getchResetTerminalState); + + signal(SIGINT, getchResetTerminalStateHandler); + signal(SIGABRT, getchResetTerminalStateHandler); + signal(SIGFPE, getchResetTerminalStateHandler); + signal(SIGILL, getchResetTerminalStateHandler); + signal(SIGSEGV, getchResetTerminalStateHandler); + signal(SIGTERM, getchResetTerminalStateHandler); +} + +#endif /* _WIN32 */ diff --git a/src/platform/getch.c b/src/platform/getch.c index 954a204..78bd421 100644 --- a/src/platform/getch.c +++ b/src/platform/getch.c @@ -24,15 +24,20 @@ int getch(void) return (int)buf; } -static void resetTerminalState(void) +void getchResetTerminalState(void) { tcsetattr(0, TCSANOW, &DefaultState); } -int getchInit(void) +void getchResetTerminalStateHandler(int sig) +{ + getchResetTerminalState(); + _Exit(sig); +} + +void getchInit(void) { tcgetattr(0, &DefaultState); - return atexit(resetTerminalState); } #endif /* !_WIN32 */