From 49bd6ee3292560ae56eb6006a79a14f435645258 Mon Sep 17 00:00:00 2001 From: Nakidai Date: Sat, 25 Nov 2023 13:24:01 +0300 Subject: [PATCH] Move platform specific code & make sleep platform independent --- CMakeLists.txt | 2 ++ Makefile | 2 +- include/platform.h | 11 +++++++++++ include/sleep.h | 6 ++++++ src/main.c | 34 ++++------------------------------ src/platform.c | 32 ++++++++++++++++++++++++++++++++ src/sleep.c | 14 ++++++++++++++ 7 files changed, 70 insertions(+), 31 deletions(-) create mode 100644 include/platform.h create mode 100644 include/sleep.h create mode 100644 src/platform.c create mode 100644 src/sleep.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e1d85c6..c290f3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,8 @@ add_executable(csnake src/screen.c src/input.c src/player.c + src/platform.c + src/sleep.c ) set_target_properties(csnake PROPERTIES C_STANDARD 99) diff --git a/Makefile b/Makefile index 1109b7e..c374147 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 +SRC = main.c screen.c input.c player.c platform.c sleep.c OBJ = $(addprefix $(OBJDIR)/,$(SRC:.c=.o)) all: $(OUT) diff --git a/include/platform.h b/include/platform.h new file mode 100644 index 0000000..8795e76 --- /dev/null +++ b/include/platform.h @@ -0,0 +1,11 @@ +#ifndef __PLATFORM_H__ +#define __PLATFORM_H__ + +void resetCoordinates(void); +#ifdef _WIN32 +void threadCreate(void (*function)(void *), void *args); +#else +void threadCreate(void *(*function)(void *), void *args); +#endif + +#endif /* __PLATFORM_H__ */ diff --git a/include/sleep.h b/include/sleep.h new file mode 100644 index 0000000..d4190cf --- /dev/null +++ b/include/sleep.h @@ -0,0 +1,6 @@ +#ifndef __SLEEP_H__ +#define __SLEEP_H__ + +void sleepMS(int msec); + +#endif /* __SLEEP_H__ */ diff --git a/src/main.c b/src/main.c index f772ae6..31e047f 100644 --- a/src/main.c +++ b/src/main.c @@ -2,18 +2,14 @@ #include #include #include -#ifdef _WIN32 -#include -#include -#else -#include -#endif #include "input.h" #include "screen.h" #include "player.h" #include "food.h" #include "config.h" +#include "platform.h" +#include "sleep.h" void drawPlayer(Player *player, Screen *screen) { @@ -32,19 +28,6 @@ Food generateFood(Player *player) return food; } -#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 - int main(int argc, char **argv) { srand((unsigned int)time(NULL)); @@ -59,12 +42,7 @@ int main(int argc, char **argv) bool stopped = false; InputArgs input_args = (InputArgs){ key, running }; -#ifdef _WIN32 - _beginthread(input, 0, &input_args); -#else - pthread_t input_thread; - pthread_create(&input_thread, NULL, input, &input_args); -#endif + threadCreate(input, &input_args); while (*running) { screenSet(screen, ' '); @@ -75,11 +53,7 @@ int main(int argc, char **argv) for (i = 0; i < SIZE*2; ++i) putchar('-'); printf("\nScore: %d\n", player->score); -#ifdef _WIN32 - Sleep(1000L); -#else - nanosleep(&(struct timespec){.tv_sec = 1}, NULL); -#endif + sleepMS(1000); switch (*key) { case 'q': diff --git a/src/platform.c b/src/platform.c new file mode 100644 index 0000000..bfaa6ef --- /dev/null +++ b/src/platform.c @@ -0,0 +1,32 @@ +#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/sleep.c b/src/sleep.c new file mode 100644 index 0000000..943c45f --- /dev/null +++ b/src/sleep.c @@ -0,0 +1,14 @@ +#include + +long long int getMS() +{ + struct timespec ts; + timespec_get(&ts, TIME_UTC); + return ts.tv_sec * 1000 + ts.tv_nsec / 1000000; +} + +void sleepMS(int msec) +{ + long long int end = getMS() + msec; + while (getMS() < end); +}