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
fix-mingw
Nakidai 2023-11-30 17:52:46 +03:00
parent be37715af7
commit 4b208ab1c4
8 changed files with 45 additions and 51 deletions

View File

@ -10,7 +10,6 @@ add_executable(csnake
src/screen.c
src/input.c
src/player.c
src/platform.c
src/sleep.c
)

View File

@ -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)

View File

@ -1,11 +1,37 @@
#ifndef __PLATFORM_H__
#define __PLATFORM_H__
void resetCoordinates(void);
#ifdef _WIN32
void threadCreate(void (*function)(void *), void *args);
#include <Windows.h>
#include <process.h>
#else
void threadCreate(void *(*function)(void *), void *args);
#include <stdio.h>
#include <pthread.h>
#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__ */

View File

@ -1,6 +1,8 @@
#ifndef __SCREEN_H__
#define __SCREEN_H__
#include <string.h>
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__ */

View File

@ -11,7 +11,7 @@
#ifdef _WIN32
#define getch _getch
#else
int getch(void)
static int getch(void)
{
char buf = 0;
struct termios old = { 0 };

View File

@ -1,32 +0,0 @@
#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

View File

@ -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));
}

View File

@ -1,6 +1,6 @@
#include <time.h>
long long int getMS()
static long long int getMS()
{
struct timespec ts;
timespec_get(&ts, TIME_UTC);