forked from nakidai/csnake
1
0
Fork 0

Now game clears cursor at exit on ^C

Nakidai Perumenei 2023-12-17 03:07:48 +03:00
parent 22d3cdeb43
commit 71cfceee4b
7 changed files with 45 additions and 5 deletions

View File

@ -12,6 +12,7 @@ add_executable(csnake
src/player.c src/player.c
src/sleep.c src/sleep.c
src/platform/getch.c src/platform/getch.c
src/platform/game.c
) )
set_target_properties(csnake PROPERTIES C_STANDARD 11) set_target_properties(csnake PROPERTIES C_STANDARD 11)

View File

@ -4,7 +4,7 @@ INCLUDE = -Iinclude
RM = rm -f RM = rm -f
SRCDIR = src SRCDIR = src
OBJDIR = obj 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)) OBJ = $(addprefix $(OBJDIR)/,$(SRC:.c=.o))
DEFLDFLAGS = $(shell if echo "" | cc -E -dM -xc - | grep __FreeBSD__ > /dev/null 2>&1; then echo "-lpthread"; fi) DEFLDFLAGS = $(shell if echo "" | cc -E -dM -xc - | grep __FreeBSD__ > /dev/null 2>&1; then echo "-lpthread"; fi)

6
include/platform/game.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __PLATFORM_GAME_H__
#define __PLATFORM_GAME_H__
void platformGameInit(void);
#endif /* __PLATFORM_GAME_H__ */

View File

@ -7,7 +7,9 @@
inline int getchInit(void) { return 0; } inline int getchInit(void) { return 0; }
#else #else
int getch(void); int getch(void);
int getchInit(void); void getchInit(void);
void getchResetTerminalStateHandler(int sig);
void getchResetTerminalState(void);
#endif /* _WIN32 */ #endif /* _WIN32 */
#endif /* __GETCH_H__ */ #endif /* __GETCH_H__ */

View File

@ -11,6 +11,7 @@
#include "sleep.h" #include "sleep.h"
#include "platform/thread.h" #include "platform/thread.h"
#include "platform/screen.h" #include "platform/screen.h"
#include "platform/game.h"
void drawPlayer(Player player, Screen screen) void drawPlayer(Player player, Screen screen)
{ {
@ -32,6 +33,7 @@ Food generateFood(Player player)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
platformGameInit();
Player player; playerCreate(&player, DOWN, DEFX, DEFY, 0); Player player; playerCreate(&player, DOWN, DEFX, DEFY, 0);
Screen screen; screenCreate(&screen, SIZE, SIZE, ' '); Screen screen; screenCreate(&screen, SIZE, SIZE, ' ');

24
src/platform/game.c Normal file
View File

@ -0,0 +1,24 @@
#ifdef _WIN32
void platformGameInit(void) {}
#else
#include <signal.h>
#include <stdlib.h>
#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 */

View File

@ -24,15 +24,20 @@ int getch(void)
return (int)buf; return (int)buf;
} }
static void resetTerminalState(void) void getchResetTerminalState(void)
{ {
tcsetattr(0, TCSANOW, &DefaultState); tcsetattr(0, TCSANOW, &DefaultState);
} }
int getchInit(void) void getchResetTerminalStateHandler(int sig)
{
getchResetTerminalState();
_Exit(sig);
}
void getchInit(void)
{ {
tcgetattr(0, &DefaultState); tcgetattr(0, &DefaultState);
return atexit(resetTerminalState);
} }
#endif /* !_WIN32 */ #endif /* !_WIN32 */