From eda7badf4773b905a1335dda5033ea34c0481a47 Mon Sep 17 00:00:00 2001 From: Nakidai Date: Fri, 1 Dec 2023 22:39:38 +0300 Subject: [PATCH] Finally separate platform-specific code from independent --- CMakeLists.txt | 1 + Makefile | 9 ++++++--- include/input.h | 10 ++++------ include/platform.h | 37 ----------------------------------- include/platform/getch.h | 11 +++++++++++ include/platform/screen.h | 23 ++++++++++++++++++++++ include/platform/thread.h | 32 ++++++++++++++++++++++++++++++ src/input.c | 41 +++++---------------------------------- src/main.c | 3 ++- src/platform/getch.c | 24 +++++++++++++++++++++++ 10 files changed, 108 insertions(+), 83 deletions(-) delete mode 100644 include/platform.h create mode 100644 include/platform/getch.h create mode 100644 include/platform/screen.h create mode 100644 include/platform/thread.h create mode 100644 src/platform/getch.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 9292c45..ad9634e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable(csnake src/input.c src/player.c src/sleep.c + src/platform/getch.c ) set_target_properties(csnake PROPERTIES C_STANDARD 11) diff --git a/Makefile b/Makefile index e911941..6a2fc30 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 +SRC = main.c screen.c input.c player.c sleep.c platform/getch.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) @@ -14,13 +14,16 @@ all: $(OUT) obj: mkdir obj +platform: obj + mkdir obj/platform + $(OBJDIR)/%.o: $(SRCDIR)/%.c $(CC) -c -o $@ $< $(CFLAGS) $(INCLUDE) -$(OUT): obj $(OBJ) +$(OUT): obj platform $(OBJ) $(CC) -o $@ $(OBJ) $(LDFLAGS) $(DEFLDFLAGS) clean: - $(RM) $(OUT) $(OBJDIR)/* + $(RM) -r $(OUT) $(OBJDIR)/* .PHONY: default clean diff --git a/include/input.h b/include/input.h index d73a36a..e21bb2d 100644 --- a/include/input.h +++ b/include/input.h @@ -3,16 +3,14 @@ #include +#include "platform/thread.h" + typedef struct input_args_t { - int *out; + int *out; bool *alive; } InputArgs; -#ifdef _WIN32 -void input(void *vargp); -#else -void *input(void *vargp); -#endif +ThreadR input(void *vargp); #endif /* __INPUT_H__ */ diff --git a/include/platform.h b/include/platform.h deleted file mode 100644 index 9592c71..0000000 --- a/include/platform.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef __PLATFORM_H__ -#define __PLATFORM_H__ - -#ifdef _WIN32 -#include -#include -#else -#include -#include -#endif - -#ifdef _WIN32 -static inline void resetCoordinates(void) -{ - HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleCursorPosition(output, (COORD){0}); -} -#else -static inline void resetCoordinates(void) -{ - printf("\e[1;1H\e[2J"); -} -#endif - -#ifdef _WIN32 -static inline void threadCreate(void (*function)(void *), void *args) -{ - _beginthread(function, 0, args); -} -#else -static inline void threadCreate(void *(*function)(void *), void *args) -{ - pthread_create(&(pthread_t){0}, 0, function, args); -} -#endif - -#endif /* __PLATFORM_H__ */ diff --git a/include/platform/getch.h b/include/platform/getch.h new file mode 100644 index 0000000..4603264 --- /dev/null +++ b/include/platform/getch.h @@ -0,0 +1,11 @@ +#ifndef __GETCH_H__ +#define __GETCH_H__ + +#ifdef _WIN32 +#include +#define getch _getch +#else +int getch(void); +#endif /* _WIN32 */ + +#endif /* __GETCH_H__ */ diff --git a/include/platform/screen.h b/include/platform/screen.h new file mode 100644 index 0000000..7ee43b0 --- /dev/null +++ b/include/platform/screen.h @@ -0,0 +1,23 @@ +#ifndef __PLATFORM_SCREEN_H__ +#define __PLATFORM_SCREEN_H__ + +#ifdef _WIN32 +#include +#else +#include +#endif /* _WIN32 */ + +#ifdef _WIN32 +static inline void resetCoordinates(void) +{ + HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleCursorPosition(output, (COORD){0}); +} +#else +static inline void resetCoordinates(void) +{ + printf("\e[1;1H\e[2J"); +} +#endif /* _WIN32 */ + +#endif /* __PLATFORM_SCREEN_H__ */ diff --git a/include/platform/thread.h b/include/platform/thread.h new file mode 100644 index 0000000..a9451f3 --- /dev/null +++ b/include/platform/thread.h @@ -0,0 +1,32 @@ +#ifndef __THREAD_H__ +#define __THREAD_H__ + +#ifdef _WIN32 +#include +#else +#include +#include +#endif /* _WIN32 */ + +#ifdef _WIN32 +typedef void ThreadR; +#define ThreadReturn return +#else +typedef void* ThreadR; +#define ThreadReturn return NULL; +#endif /* _WIN32 */ +typedef ThreadR (*Thread)(void *); + +#ifdef _WIN32 +static inline void threadCreate(Thread function, void *args) +{ + _beginthread(function, 0, args); +} +#else +static inline void threadCreate(Thread function, void *args) +{ + pthread_create(&(pthread_t){0}, 0, function, args); +} +#endif /* _WIN32 */ + +#endif /* __THREAD_H__ */ diff --git a/src/input.c b/src/input.c index ba4391b..f467079 100644 --- a/src/input.c +++ b/src/input.c @@ -1,40 +1,11 @@ -#include "input.h" #include #include -#ifdef _WIN32 -#include -#else -#include -#include -#endif -#ifdef _WIN32 -#define getch _getch -#else -static int getch(void) -{ - char buf = 0; - struct termios old = { 0 }; - fflush(stdout); - if (tcgetattr(0, &old) < 0) perror("tcsetattr()"); - old.c_lflag &= ~ICANON; // local modes = Non Canonical mode - old.c_lflag &= ~ECHO; // local modes = Disable echo. - old.c_cc[VMIN] = 1; // control chars (MIN value) = 1 - old.c_cc[VTIME] = 0; // control chars (TIME value) = 0 (No time) - if (tcsetattr(0, TCSANOW, &old) < 0) perror("tcsetattr ICANON"); - if (read(0, &buf, 1) < 0) perror("read()"); - old.c_lflag |= ICANON; // local modes = Canonical mode - old.c_lflag |= ECHO; // local modes = Enable echo. - if (tcsetattr(0, TCSADRAIN, &old) < 0) perror ("tcsetattr ~ICANON"); - return (int)buf; -} -#endif +#include "input.h" +#include "platform/thread.h" +#include "platform/getch.h" -#ifdef _WIN32 -void input(void *vargp) -#else -void *input(void *vargp) -#endif +ThreadR input(void *vargp) { int *out = ((InputArgs *)vargp)->out; bool *alive = ((InputArgs *)vargp)->alive; @@ -43,7 +14,5 @@ void *input(void *vargp) { *out = getch(); } -#ifndef _WIN32 - return NULL; -#endif + ThreadReturn; } diff --git a/src/main.c b/src/main.c index 9a22049..30dcf0d 100644 --- a/src/main.c +++ b/src/main.c @@ -8,8 +8,9 @@ #include "player.h" #include "food.h" #include "config.h" -#include "platform.h" #include "sleep.h" +#include "platform/thread.h" +#include "platform/screen.h" void drawPlayer(Player *player, Screen *screen) { diff --git a/src/platform/getch.c b/src/platform/getch.c new file mode 100644 index 0000000..9828ccd --- /dev/null +++ b/src/platform/getch.c @@ -0,0 +1,24 @@ +#ifndef _WIN32 +#include +#include +#include + +int getch(void) +{ + char buf = 0; + struct termios old = { 0 }; + fflush(stdout); + if (tcgetattr(0, &old) < 0) perror("tcsetattr()"); + old.c_lflag &= ~ICANON; // local modes = Non Canonical mode + old.c_lflag &= ~ECHO; // local modes = Disable echo. + old.c_cc[VMIN] = 1; // control chars (MIN value) = 1 + old.c_cc[VTIME] = 0; // control chars (TIME value) = 0 (No time) + if (tcsetattr(0, TCSANOW, &old) < 0) perror("tcsetattr ICANON"); + if (read(0, &buf, 1) < 0) perror("read()"); + old.c_lflag |= ICANON; // local modes = Canonical mode + old.c_lflag |= ECHO; // local modes = Enable echo. + if (tcsetattr(0, TCSADRAIN, &old) < 0) perror ("tcsetattr ~ICANON"); + return (int)buf; +} + +#endif /* !_WIN32 */