Switch to threads.h

pull/2/head
Nakidai 2023-10-30 02:51:46 +03:00
parent 758dbf9cc2
commit b611b54575
Signed by untrusted user who does not match committer: nakidai
GPG Key ID: 914675D395210A97
3 changed files with 7 additions and 9 deletions

View File

@ -9,6 +9,6 @@ typedef struct input_args_t
bool *alive; bool *alive;
} InputArgs; } InputArgs;
void *input(void *vargp); int input(void *vargp);
#endif /* __INPUT_H__ */ #endif /* __INPUT_H__ */

View File

@ -22,7 +22,7 @@ char getch(void)
return buf; return buf;
} }
void *input(void *vargp) int input(void *vargp)
{ {
char *out = ((InputArgs *)vargp)->out; char *out = ((InputArgs *)vargp)->out;
bool *alive = ((InputArgs *)vargp)->alive; bool *alive = ((InputArgs *)vargp)->alive;
@ -31,5 +31,5 @@ void *input(void *vargp)
{ {
*out = getch(); *out = getch();
} }
return NULL; return 0;
} }

View File

@ -1,8 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <pthread.h> #include <threads.h>
#include <unistd.h>
#include <time.h> #include <time.h>
#include "input.h" #include "input.h"
@ -33,7 +32,7 @@ int main(int argc, char **argv)
Player *player = playerCreate(DOWN, 0, 0, 0); Player *player = playerCreate(DOWN, 0, 0, 0);
Screen *screen = screenCreate(10, 10, ' '); Screen *screen = screenCreate(10, 10, ' ');
PlayerNode *node; PlayerNode *node;
pthread_t input_thread; thrd_t input_thread;
int head_x, head_y; int head_x, head_y;
Food food = generateFood(player); Food food = generateFood(player);
@ -41,7 +40,7 @@ int main(int argc, char **argv)
char *key = malloc(sizeof(char)); *key = 0; char *key = malloc(sizeof(char)); *key = 0;
InputArgs input_args = (InputArgs){ key, running }; InputArgs input_args = (InputArgs){ key, running };
pthread_create(&input_thread, NULL, input, &input_args); thrd_create(&input_thread, input, &input_args);
while (*running) while (*running)
{ {
switch (*key) switch (*key)
@ -56,7 +55,6 @@ int main(int argc, char **argv)
player->direction = DOWN; break; player->direction = DOWN; break;
case 'a': case 'a':
player->direction = LEFT; break; player->direction = LEFT; break;
sleep(1);
} }
if (playerDoTick(player, food)) if (playerDoTick(player, food))
@ -79,7 +77,7 @@ int main(int argc, char **argv)
*screenGetPoint(screen, food.x, food.y) = '@'; *screenGetPoint(screen, food.x, food.y) = '@';
screenShow(screen); screenShow(screen);
sleep(1); thrd_sleep(&(struct timespec){.tv_sec=1}, NULL);
} }
return 0; return 0;
} }