From 4b208ab1c44b4b6731358ea775f62da15fe480d4 Mon Sep 17 00:00:00 2001 From: Nakidai Date: Thu, 30 Nov 2023 17:52:46 +0300 Subject: [PATCH] Move some functions - Remove platform.c, instead use inlines - Some small functions in screen.c are now inline too - Make functions in sleep.c and input.c not specified in header static --- CMakeLists.txt | 1 - Makefile | 2 +- include/platform.h | 32 +++++++++++++++++++++++++++++--- include/screen.h | 15 +++++++++++++-- src/input.c | 2 +- src/platform.c | 32 -------------------------------- src/screen.c | 10 ---------- src/sleep.c | 2 +- 8 files changed, 45 insertions(+), 51 deletions(-) delete mode 100644 src/platform.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f68b63..bd923c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,6 @@ add_executable(csnake src/screen.c src/input.c src/player.c - src/platform.c src/sleep.c ) diff --git a/Makefile b/Makefile index d13877c..e911941 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 platform.c sleep.c +SRC = main.c screen.c input.c player.c sleep.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.h b/include/platform.h index 8795e76..9592c71 100644 --- a/include/platform.h +++ b/include/platform.h @@ -1,11 +1,37 @@ #ifndef __PLATFORM_H__ #define __PLATFORM_H__ -void resetCoordinates(void); #ifdef _WIN32 -void threadCreate(void (*function)(void *), void *args); +#include +#include #else -void threadCreate(void *(*function)(void *), void *args); +#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/screen.h b/include/screen.h index 8fb5dd1..6d03c97 100644 --- a/include/screen.h +++ b/include/screen.h @@ -1,6 +1,8 @@ #ifndef __SCREEN_H__ #define __SCREEN_H__ +#include + typedef char Point; typedef struct screen_t { @@ -12,8 +14,17 @@ typedef struct screen_t Screen *screenCreate(int width, int height, Point fill_value); void screenFree(Screen *screen); -Point *screenGetPoint(Screen *screen, int x, int y); void screenShow(Screen *screen); -void screenSet(Screen *screen, char fill_value); + +static inline Point *screenGetPoint(Screen *screen, int x, int y) +{ + return screen->screen + x + (y * screen->width); +} + +static inline void screenSet(Screen *screen, Point fill_value) +{ + memset(screen->screen, fill_value, screen->width * screen->height * sizeof(char)); +} + #endif /* __SCREEN_H__ */ diff --git a/src/input.c b/src/input.c index f970dfb..ba4391b 100644 --- a/src/input.c +++ b/src/input.c @@ -11,7 +11,7 @@ #ifdef _WIN32 #define getch _getch #else -int getch(void) +static int getch(void) { char buf = 0; struct termios old = { 0 }; diff --git a/src/platform.c b/src/platform.c deleted file mode 100644 index bfaa6ef..0000000 --- a/src/platform.c +++ /dev/null @@ -1,32 +0,0 @@ -#ifdef _WIN32 -#include -#include -#else -#include -#include -#endif - -#ifdef _WIN32 -void resetCoordinates(void) -{ - HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleCursorPosition(output, (COORD){0}); -} -#else -void resetCoordinates(void) -{ - printf("\e[1;1H\e[2J"); -} -#endif - -#ifdef _WIN32 -void threadCreate(void (*function)(void *), void *args) -{ - _beginthread(function, 0, args); -} -#else -void threadCreate(void *(*function)(void *), void *args) -{ - pthread_create(&(pthread_t){0}, 0, function, args); -} -#endif diff --git a/src/screen.c b/src/screen.c index 514af70..8103e29 100644 --- a/src/screen.c +++ b/src/screen.c @@ -21,11 +21,6 @@ void screenFree(Screen *screen) free(screen); } -Point *screenGetPoint(Screen *screen, int x, int y) -{ - return screen->screen + x + (y * screen->width); -} - void screenShow(Screen *screen) { int x, y, i; @@ -43,8 +38,3 @@ void screenShow(Screen *screen) putchar('\n'); } } - -void screenSet(Screen *screen, Point fill_value) -{ - memset(screen->screen, fill_value, screen->width * screen->height * sizeof(char)); -} diff --git a/src/sleep.c b/src/sleep.c index 943c45f..fce4c13 100644 --- a/src/sleep.c +++ b/src/sleep.c @@ -1,6 +1,6 @@ #include -long long int getMS() +static long long int getMS() { struct timespec ts; timespec_get(&ts, TIME_UTC);