Now game clears cursor at exit on ^C
parent
22d3cdeb43
commit
71cfceee4b
|
@ -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)
|
||||
|
|
2
Makefile
2
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)
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef __PLATFORM_GAME_H__
|
||||
#define __PLATFORM_GAME_H__
|
||||
|
||||
void platformGameInit(void);
|
||||
|
||||
#endif /* __PLATFORM_GAME_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__ */
|
||||
|
|
|
@ -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, ' ');
|
||||
|
|
|
@ -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 */
|
|
@ -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 */
|
||||
|
|
Loading…
Reference in New Issue