Move platform specific code & make sleep platform independent

fix-mingw
Nakidai 2023-11-25 13:24:01 +03:00
parent 9933de02ac
commit 49bd6ee329
7 changed files with 70 additions and 31 deletions

View File

@ -10,6 +10,8 @@ add_executable(csnake
src/screen.c src/screen.c
src/input.c src/input.c
src/player.c src/player.c
src/platform.c
src/sleep.c
) )
set_target_properties(csnake PROPERTIES C_STANDARD 99) set_target_properties(csnake PROPERTIES C_STANDARD 99)

View File

@ -4,7 +4,7 @@ INCLUDE = -Iinclude
RM = rm -f RM = rm -f
SRCDIR = src SRCDIR = src
OBJDIR = obj 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)) OBJ = $(addprefix $(OBJDIR)/,$(SRC:.c=.o))
all: $(OUT) all: $(OUT)

11
include/platform.h Normal file
View File

@ -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__ */

6
include/sleep.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __SLEEP_H__
#define __SLEEP_H__
void sleepMS(int msec);
#endif /* __SLEEP_H__ */

View File

@ -2,18 +2,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
#ifdef _WIN32
#include <Windows.h>
#include <process.h>
#else
#include <pthread.h>
#endif
#include "input.h" #include "input.h"
#include "screen.h" #include "screen.h"
#include "player.h" #include "player.h"
#include "food.h" #include "food.h"
#include "config.h" #include "config.h"
#include "platform.h"
#include "sleep.h"
void drawPlayer(Player *player, Screen *screen) void drawPlayer(Player *player, Screen *screen)
{ {
@ -32,19 +28,6 @@ Food generateFood(Player *player)
return food; 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) int main(int argc, char **argv)
{ {
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
@ -59,12 +42,7 @@ int main(int argc, char **argv)
bool stopped = false; bool stopped = false;
InputArgs input_args = (InputArgs){ key, running }; InputArgs input_args = (InputArgs){ key, running };
#ifdef _WIN32 threadCreate(input, &input_args);
_beginthread(input, 0, &input_args);
#else
pthread_t input_thread;
pthread_create(&input_thread, NULL, input, &input_args);
#endif
while (*running) while (*running)
{ {
screenSet(screen, ' '); screenSet(screen, ' ');
@ -75,11 +53,7 @@ int main(int argc, char **argv)
for (i = 0; i < SIZE*2; ++i) putchar('-'); for (i = 0; i < SIZE*2; ++i) putchar('-');
printf("\nScore: %d\n", player->score); printf("\nScore: %d\n", player->score);
#ifdef _WIN32 sleepMS(1000);
Sleep(1000L);
#else
nanosleep(&(struct timespec){.tv_sec = 1}, NULL);
#endif
switch (*key) switch (*key)
{ {
case 'q': case 'q':

32
src/platform.c Normal file
View File

@ -0,0 +1,32 @@
#ifdef _WIN32
#include <Windows.h>
#include <process.h>
#else
#include <stdio.h>
#include <pthread.h>
#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

14
src/sleep.c Normal file
View File

@ -0,0 +1,14 @@
#include <time.h>
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);
}