From 1ff14f7cbcc0178b38d9a604ec475e4d4401411c Mon Sep 17 00:00:00 2001 From: Nakidai Date: Fri, 24 Nov 2023 23:58:37 +0300 Subject: [PATCH] Add windows support in threads and sleep --- src/input.c | 4 ++-- src/main.c | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/input.c b/src/input.c index 0df48bd..192d566 100644 --- a/src/input.c +++ b/src/input.c @@ -1,14 +1,14 @@ #include "input.h" #include #include -#ifdef WIN32 +#ifdef _WIN32 #include #else #include #include #endif -#ifdef WIN32 +#ifdef _WIN32 #define getch _getch #else int getch(void) diff --git a/src/main.c b/src/main.c index 3caf612..dc4f10f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,13 @@ #include #include #include -#include #include +#ifdef _WIN32 +#include +#include +#else +#include +#endif #include "input.h" #include "screen.h" @@ -27,10 +32,18 @@ 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) { @@ -48,7 +61,11 @@ int main(int argc, char **argv) bool stopped = false; InputArgs input_args = (InputArgs){ key, running }; +#ifdef _WIN32 + _beginthread(input, 0, &input_args); +#else pthread_create(&input_thread, NULL, input, &input_args); +#endif while (*running) { screenSet(screen, ' '); @@ -59,7 +76,11 @@ 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 switch (*key) { case 'q':