From 0f726414cc0026691854bda9567ef959c7a2a976 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Mon, 6 Feb 2023 22:55:20 -0800 Subject: added new stuff --- .gitignore | 1 + block.h | 2 +- tetris.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- tetris.h | 4 ++++ vector.c | 2 +- vector.h | 2 +- 6 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ead8445 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/*.swp diff --git a/block.h b/block.h index 808fd88..c8c4743 100644 --- a/block.h +++ b/block.h @@ -1,6 +1,6 @@ #ifndef BLOCK_H #define BLOCK_H -typedef struct { +typedef struct BLOCK_STRUCT { enum { BLOCK_T, BLOCK_RL, /* L block with the bottom part facing diff --git a/tetris.c b/tetris.c index 88a99f3..8f8b43f 100644 --- a/tetris.c +++ b/tetris.c @@ -3,7 +3,12 @@ #include #include #include +#include +#include "tetris.h" +#include "conio.h" +#include "vector.h" +#include "block.h" #define UNUSED(x) (void)(x) volatile sig_atomic_t g_int_ctrl_c = 0; @@ -17,20 +22,54 @@ void interrupt_handler(int sig) { void parse_args(int argc, char **argv) { } -void die(void) { +void display_game(void) { } + int main (int argc, char **argv) { - parse_args(argc, argv); int width; int height; - + volatile int *last_input = calloc(1, sizeof(int)); + int *cancellation_token = calloc(1, sizeof(int)); + struct winsize win; + struct timespec time; + + /* store arguments in global variables */ + parse_args(argc, argv); + + /* get dimensions of board */ + ioctl(fileno(stdout), TIOCGWINSZ, &win); + width = (int)(win.ws_col); + height = (int)(win.ws_row); + /* handle SIGINT interrupts */ signal(SIGINT, interrupt_handler); + /* Hide cursor */ print("\033[?251"); + + /* prepare thread for getting input and accepting cancellation */ + thread_arg_t *thread_arg = malloc(sizeof(thread_arg_t)); + thread_arg->ret = (void *)last_input; + thread_arg->cancellation_token = cancellation_token; + /* Game Loop */ while (1) { + if (g_int_ctrl_c) { + goto EXIT; + } + display_game(); } + +EXIT: + /* cancel thread */ + *cancellation_token = 1; + printf("%sGame over! Your score was: %d%s\n", RED, g_score, COLOR_RESET); + /* unhide cursor */ + printf("\033[?25h"); + + /* free some values */ + free(cancellation_token); + free((int *)last_input); return 0; } diff --git a/tetris.h b/tetris.h index 3a201fa..9ea9fdc 100644 --- a/tetris.h +++ b/tetris.h @@ -1,5 +1,9 @@ #ifndef TETRIS_H #define TETRIS_H +typedef struct THREADARG_STRUCT { + int *cancellation_token; + void *ret; +} thread_arg_t; void interrupt_handler(int sig); void die(void); #endif diff --git a/vector.c b/vector.c index 846b361..34b30de 100644 --- a/vector.c +++ b/vector.c @@ -12,7 +12,7 @@ vectorp_t init_vectorp(vector_t* vectors, int length) { vectorP.length = length; return vectorP; } -bool vector_equals(vector_t v1, vector_t v2) { +int vector_equals(vector_t v1, vector_t v2) { return v1.mx == v2.mx && v1.my == v2.my; } vector_t vector_add(vector_t v1, vector_t v2) { diff --git a/vector.h b/vector.h index b4558a1..1e605a3 100644 --- a/vector.h +++ b/vector.h @@ -6,7 +6,7 @@ typedef struct Vector { } vector_t; vector_t init_vector(int mx, int my); -bool vector_equals(vector_t v1, vector_t v2); +int vector_equals(vector_t v1, vector_t v2); vector_t vector_add(vector_t v1, vector_t v2); vector_t vector_sub(vector_t v1, vector_t v2); -- cgit v1.3.1