diff options
| author | Preston Pan <preston@nullring.xyz> | 2023-02-06 22:55:20 -0800 |
|---|---|---|
| committer | Preston Pan <preston@nullring.xyz> | 2023-02-06 22:55:20 -0800 |
| commit | 0f726414cc0026691854bda9567ef959c7a2a976 (patch) | |
| tree | 2ddea95a79be29b865a97e3a84e32cefd165313a | |
| parent | d9b0f00ebec0c90a70bc823f8d3eb5af1128a605 (diff) | |
added new stuff
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | block.h | 2 | ||||
| -rw-r--r-- | tetris.c | 45 | ||||
| -rw-r--r-- | tetris.h | 4 | ||||
| -rw-r--r-- | vector.c | 2 | ||||
| -rw-r--r-- | vector.h | 2 |
6 files changed, 50 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ead8445 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/*.swp @@ -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 @@ -3,7 +3,12 @@ #include <signal.h> #include <sys/ioctl.h> #include <string.h> +#include <time.h> +#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; } @@ -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 @@ -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) { @@ -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); |
