forked from nakidai/csnake
Move platform specific code & make sleep platform independent
parent
9933de02ac
commit
49bd6ee329
|
@ -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)
|
||||
|
|
2
Makefile
2
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)
|
||||
|
|
|
@ -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__ */
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef __SLEEP_H__
|
||||
#define __SLEEP_H__
|
||||
|
||||
void sleepMS(int msec);
|
||||
|
||||
#endif /* __SLEEP_H__ */
|
34
src/main.c
34
src/main.c
|
@ -2,18 +2,14 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
#include <process.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#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':
|
||||
|
|
|
@ -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
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue