From 64bd072a1ef349fedc1364a221a19ce71c368e72 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Wed, 10 Apr 2019 17:16:27 -0400 Subject: add shorthand int types and cleanup --- core/include/cetris.h | 37 ++++++++++++++++--------------- core/include/input.h | 3 ++- core/include/matrix.h | 4 ++-- core/include/types.h | 15 ++++++++++--- core/src/cetris.c | 53 ++++++++++++++++++++++---------------------- core/src/input.c | 8 +++---- core/src/matrix.c | 46 +++++++++++++++++++------------------- core/src/test.c | 10 ++++----- frontends/opengl/meson.build | 4 ++++ 9 files changed, 98 insertions(+), 82 deletions(-) diff --git a/core/include/cetris.h b/core/include/cetris.h index 8264842..e51b8c4 100644 --- a/core/include/cetris.h +++ b/core/include/cetris.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "types.h" @@ -43,13 +44,13 @@ struct tetrimino { color c; piece_matrix m; vec2 pos; - int lock_tick; + u32 lock_tick; }; typedef struct { - uint8_t occupied; - uint8_t constant; - int remove_tick; + bool occupied; + bool constant; + u32 remove_tick; color c; } slot; @@ -62,30 +63,30 @@ struct cetris_game { /* current tetrimino */ struct tetrimino current; - uint8_t current_index; + u8 current_index; /* input_manager */ - uint8_t held_moves[7]; - int das_repeat; + bool held_moves[7]; input_t prev_das_move; - int das_move_tick; - int down_move_tick; + u8 das_repeat; + u32 das_move_tick; + u32 down_move_tick; /* internal game tick */ - int tick; - int next_drop_tick; + u32 tick; + u32 next_drop_tick; /* progress trackers */ - int lines; - uint8_t level; - uint8_t game_over; + u32 lines; + u32 level; + bool game_over; /* scoring flags */ - uint8_t tspin; - uint8_t mini_tspin; + bool tspin; + bool mini_tspin; - /* long int just incase */ - long int score; + /* score counter */ + u64 score; }; void next_piece(struct cetris_game* g); diff --git a/core/include/input.h b/core/include/input.h index 0092bb4..1199991 100644 --- a/core/include/input.h +++ b/core/include/input.h @@ -1,8 +1,9 @@ #pragma once #include +#include struct cetris_game; -uint8_t handle_inputs(struct cetris_game* g); +bool handle_inputs(struct cetris_game* g); void clear_held_key(struct cetris_game* g); diff --git a/core/include/matrix.h b/core/include/matrix.h index 3ab0252..5b6a74c 100644 --- a/core/include/matrix.h +++ b/core/include/matrix.h @@ -11,5 +11,5 @@ extern const vec2 basic_movements[4]; void move_current(struct cetris_game* g, vec2 offset); void overlay_current_matrix(struct cetris_game* g); void hard_drop(struct cetris_game* g); -void rotate_matrix(struct cetris_game* g, int clockwise); -int8_t check_new_matrix(struct cetris_game* g, piece_matrix m); +void rotate_matrix(struct cetris_game* g, bool clockwise); +i8 check_new_matrix(struct cetris_game* g, piece_matrix m); diff --git a/core/include/types.h b/core/include/types.h index 2ce97c7..a20773b 100644 --- a/core/include/types.h +++ b/core/include/types.h @@ -2,13 +2,22 @@ #include -typedef uint8_t piece_matrix[4][4]; +#define u8 uint8_t +#define u16 uint16_t +#define u32 uint32_t +#define u64 uint64_t +#define i8 int8_t +#define i16 int16_t +#define i32 int32_t +#define i64 int64_t typedef struct { - int8_t x; - int8_t y; + i8 x; + i8 y; } vec2; +typedef u8 piece_matrix[4][4]; + typedef enum { DOWN = 1, RIGHT = 2, diff --git a/core/src/cetris.c b/core/src/cetris.c index e6bbc8d..8e8244b 100644 --- a/core/src/cetris.c +++ b/core/src/cetris.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "cetris.h" #include "types.h" @@ -22,7 +23,7 @@ static void set_constants(struct cetris_game* g); /* LEVEL DROP SPEED VALUES */ -static const uint8_t level_drop_delay[20] = { +static const u32 level_drop_delay[20] = { 48, 43, 38, 33, 28, 23, 18, 13, 8, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 3 }; @@ -42,7 +43,7 @@ void init_game(struct cetris_game* g) { g->current_index = 0; - memset(g->held_moves, 0, sizeof(uint8_t) * 7); + memset(g->held_moves, 0, sizeof(bool) * 7); g->das_repeat = 0; g->prev_das_move = 0; g->das_move_tick = 0; @@ -50,10 +51,10 @@ void init_game(struct cetris_game* g) { g->lines = 0; g->level = 1; - g->game_over = 0; + g->game_over = false; - g->tspin = 0; - g->mini_tspin = 0; + g->tspin = false; + g->mini_tspin = false; g->score = 0; @@ -64,7 +65,7 @@ void init_game(struct cetris_game* g) { } void init_piece_queue(struct cetris_game* g) { - for (uint8_t i = 0; i < 7; i++) { + for (u8 i = 0; i < 7; i++) { switch (i) { case 0: g->piece_queue[i].t = O; @@ -103,9 +104,9 @@ void init_piece_queue(struct cetris_game* g) { } void shuffle_queue(struct cetris_game* g) { - for (uint8_t i = 0; i < 7; i++) { + for (u8 i = 0; i < 7; i++) { struct tetrimino t = g->piece_queue[i]; - uint8_t rand_index = rand() % 7; + u8 rand_index = rand() % 7; g->piece_queue[i] = g->piece_queue[rand_index]; g->piece_queue[rand_index] = t; } @@ -114,10 +115,10 @@ void shuffle_queue(struct cetris_game* g) { void update_game_tick(struct cetris_game* g) { if (g->game_over) return; - uint8_t did_move = 0; + bool did_move = false; if (g->tick == g->next_drop_tick) { move_current(g, basic_movements[DOWN]); - did_move = 1; + did_move = true; g->next_drop_tick = g->tick + level_drop_delay[g->level - 1]; } @@ -132,7 +133,7 @@ void update_game_tick(struct cetris_game* g) { } if (handle_inputs(g)) { - did_move = 1; + did_move = true; } if (did_move) wipe_board(g); @@ -145,7 +146,7 @@ void next_piece(struct cetris_game* g) { g->current = g->piece_queue[g->current_index]; if (check_new_matrix(g, g->current.m) <= 0) { - g->game_over = 1; + g->game_over = true; } g->current_index++; @@ -156,14 +157,14 @@ void next_piece(struct cetris_game* g) { } void set_constants(struct cetris_game* g) { - for (uint8_t x = 0; x < CETRIS_BOARD_X; x++) { - for (uint8_t y = 0; y < CETRIS_BOARD_Y; y++) { + for (u8 x = 0; x < CETRIS_BOARD_X; x++) { + for (u8 y = 0; y < CETRIS_BOARD_Y; y++) { if (g->board[x][y].occupied) g->board[x][y].constant = 1; } } } -void add_score(struct cetris_game* g, int lines) { +void add_score(struct cetris_game* g, u8 lines) { if (!g->tspin && !g->mini_tspin) { switch (lines) { case 1: g->score += 100 * g->level; break; @@ -178,40 +179,40 @@ void add_score(struct cetris_game* g, int lines) { case 2: g->score += 1200 * g->level; break; case 3: g->score += 1600 * g->level; break; } - g->tspin = 0; + g->tspin = false; } else if (g->mini_tspin) { switch (lines) { case 0: g->score += 100 * g->level; break; case 1: g->score += 200 * g->level; break; case 2: g->score += 400 * g->level; break; } - g->mini_tspin = 0; + g->mini_tspin = false; } } void wipe_board(struct cetris_game* g) { - uint8_t lines_cleared = 0; - for (uint8_t y = 0; y < CETRIS_BOARD_Y; y++) { - uint8_t clear_line = 1; - for (uint8_t x = 0; x < CETRIS_BOARD_X; x++) { + uint32_t lines_cleared = 0; + for (u8 y = 0; y < CETRIS_BOARD_Y; y++) { + bool clear_line = true; + for (u8 x = 0; x < CETRIS_BOARD_X; x++) { if (!g->board[x][y].constant) { memset(&g->board[x][y], 0, sizeof(slot)); } if (g->board[x][y].remove_tick && g->board[x][y].remove_tick <= g->tick) { memset(&g->board[x][y], 0, sizeof(slot)); - for (int8_t s = y - 1; s >= 0; s--) { + for (i8 s = y - 1; s >= 0; s--) { g->board[x][s + 1] = g->board[x][s]; } } if (!g->board[x][y].occupied || g->board[x][y].remove_tick > 0) { - clear_line = 0; + clear_line = false; } } if (clear_line) { lines_cleared++; - for (uint8_t x = 0; x < CETRIS_BOARD_X; x++) { + for (u8 x = 0; x < CETRIS_BOARD_X; x++) { g->board[x][y].remove_tick = g->tick + CETRIS_LINE_CLEAR_DELAY; } } @@ -250,11 +251,11 @@ void move_peice(struct cetris_game* g, input_t move) { break; } } - g->held_moves[move] = 1; + g->held_moves[move] = true; } void stop_holding(struct cetris_game* g, input_t move) { - g->held_moves[move] = 0; + g->held_moves[move] = false; if (move == RIGHT || move == LEFT) { g->das_move_tick = 0; g->das_repeat = 0; diff --git a/core/src/input.c b/core/src/input.c index 9fd9142..08d6219 100644 --- a/core/src/input.c +++ b/core/src/input.c @@ -6,7 +6,7 @@ #include "matrix.h" #include "cetris.h" -uint8_t handle_inputs(struct cetris_game* g) { +bool handle_inputs(struct cetris_game* g) { if ((g->held_moves[RIGHT] || g->held_moves[LEFT]) && !g->das_move_tick) { if (g->das_repeat == 0) { g->das_move_tick = g->tick + CETRIS_DAS_DELAY; @@ -19,7 +19,7 @@ uint8_t handle_inputs(struct cetris_game* g) { g->down_move_tick = g->tick + CETRIS_DROP_PERIOD; } - uint8_t did_move = 0; + bool did_move = false; if (g->das_move_tick && g->tick >= g->das_move_tick) { if (g->held_moves[RIGHT]) { if (g->prev_das_move == RIGHT || g->das_repeat == 0) g->das_repeat++; @@ -36,7 +36,7 @@ uint8_t handle_inputs(struct cetris_game* g) { } g->das_move_tick = 0; - did_move = 1; + did_move = true; } if (g->down_move_tick && g->tick >= g->down_move_tick) { @@ -45,7 +45,7 @@ uint8_t handle_inputs(struct cetris_game* g) { } g->down_move_tick = 0; - did_move = 1; + did_move = true; } return did_move; } diff --git a/core/src/matrix.c b/core/src/matrix.c index 5ee971b..204d920 100644 --- a/core/src/matrix.c +++ b/core/src/matrix.c @@ -94,7 +94,7 @@ void move_current(struct cetris_game* g, vec2 offset) { g->current.pos.y += offset.y; g->current.pos.x += offset.x; - int8_t check = check_new_matrix(g, g->current.m); + int32_t check = check_new_matrix(g, g->current.m); if (check <= 0) { g->current.pos.y -= offset.y; g->current.pos.x -= offset.x; @@ -108,8 +108,8 @@ void move_current(struct cetris_game* g, vec2 offset) { } void overlay_current_matrix(struct cetris_game* g) { - for (uint8_t y = 0; y < 4; y++) { - for (uint8_t x = 0; x < 4; x++) { + for (uint32_t y = 0; y < 4; y++) { + for (uint32_t x = 0; x < 4; x++) { vec2 r = (vec2){x + g->current.pos.x, y + g->current.pos.y}; if (g->current.m[y][x]) { g->board[r.x][r.y].occupied = 1; @@ -122,16 +122,16 @@ void overlay_current_matrix(struct cetris_game* g) { void hard_drop(struct cetris_game* g) { if (g->game_over) return; - uint8_t drop = 0; - uint8_t drop_count = 0; + bool drop = false; + u8 drop_count = 0; while (!drop) { g->current.pos.y++; drop_count++; - int8_t check = check_new_matrix(g, g->current.m); + i8 check = check_new_matrix(g, g->current.m); if (check <= 0) { g->current.pos.y--; drop_count--; - drop = 1; + drop = true; } } @@ -141,12 +141,12 @@ void hard_drop(struct cetris_game* g) { next_piece(g); } -void rotate_matrix(struct cetris_game* g, int clockwise) { +void rotate_matrix(struct cetris_game* g, bool clockwise) { if (g->game_over) return; if (g->current.t == O) return; rstate next; - int8_t wall_kick; + u8 wall_kick = 0; switch (g->current.r) { case INIT: if (clockwise) { @@ -185,11 +185,11 @@ void rotate_matrix(struct cetris_game* g, int clockwise) { piece_matrix m; memset(m, 0, sizeof(piece_matrix)); - for (uint8_t x = 0; x < 4; x++) { - for (uint8_t y = 0; y < 4; y++) { + for (u8 x = 0; x < 4; x++) { + for (u8 y = 0; y < 4; y++) { if (g->current.m[y][x]) { - uint8_t new_x = (clockwise) ? 1 - (y - 2) : 1 + (y - 2); - uint8_t new_y = (clockwise) ? 2 + (x - 1) : 2 - (x - 1); + u8 new_x = (clockwise) ? 1 - (y - 2) : 1 + (y - 2); + u8 new_y = (clockwise) ? 2 + (x - 1) : 2 - (x - 1); if (g->current.t == I) { if (clockwise) new_y--; @@ -202,9 +202,9 @@ void rotate_matrix(struct cetris_game* g, int clockwise) { } vec2 kick; - uint8_t set_current = 0; - uint8_t did_kick = 0; - for (uint8_t i = 0; i < 5; i++) { + bool set_current = false; + bool did_kick = false; + for (u8 i = 0; i < 5; i++) { if (g->current.t == I) { kick = srs_wall_kicks_i[wall_kick][i]; } else { @@ -213,8 +213,8 @@ void rotate_matrix(struct cetris_game* g, int clockwise) { g->current.pos.x += kick.x; g->current.pos.y += kick.y; if (check_new_matrix(g, m) > 0) { - set_current = 1; - if (i > 0) did_kick = 1; + set_current = true; + if (i > 0) did_kick = true; break; } else { g->current.pos.x -= kick.x; @@ -225,8 +225,8 @@ void rotate_matrix(struct cetris_game* g, int clockwise) { if (set_current) { /* check for tspin */ if (g->current.t == T) { - uint8_t did_tspin = 1; - for (uint8_t i = 0; i < 4; i++) { + u8 did_tspin = 1; + for (u8 i = 0; i < 4; i++) { g->current.pos.x += basic_movements[i].x; g->current.pos.y += basic_movements[i].y; @@ -249,9 +249,9 @@ void rotate_matrix(struct cetris_game* g, int clockwise) { } } -int8_t check_new_matrix(struct cetris_game* g, piece_matrix m) { - for (uint8_t x = 0; x < 4; x++) { - for (uint8_t y = 0; y < 4; y++) { +i8 check_new_matrix(struct cetris_game* g, piece_matrix m) { + for (u8 x = 0; x < 4; x++) { + for (u8 y = 0; y < 4; y++) { vec2 r = (vec2){g->current.pos.x + x, g->current.pos.y + y}; if (m[y][x]) { if (r.x > CETRIS_BOARD_X - 1 || r.x < 0) diff --git a/core/src/test.c b/core/src/test.c index 33badbd..c15156a 100644 --- a/core/src/test.c +++ b/core/src/test.c @@ -1,7 +1,7 @@ #include "test.h" #include "cetris.h" -int tspin_board[20][10] = { +u8 tspin_board[20][10] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, @@ -24,7 +24,7 @@ int tspin_board[20][10] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 } }; -int tspin_no_lines_board[20][10] = { +u8 tspin_no_lines_board[20][10] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, @@ -48,7 +48,7 @@ int tspin_no_lines_board[20][10] = { }; void apply_test_board(struct cetris_game* g, enum tests t) { - int (*board)[20][10]; + u8 (*board)[20][10]; switch (t) { case TSPIN: board = &tspin_board; @@ -59,8 +59,8 @@ void apply_test_board(struct cetris_game* g, enum tests t) { default: return; } - for (int y = CETRIS_BOARD_VISABLE; y < CETRIS_BOARD_Y; y++) { - for (int x = 0; x < 10; x++) { + for (u8 y = CETRIS_BOARD_VISABLE; y < CETRIS_BOARD_Y; y++) { + for (u8 x = 0; x < 10; x++) { if ((*board)[y - CETRIS_BOARD_VISABLE][x]) { g->board[x][y].occupied = 1; g->board[x][y].constant = 1; diff --git a/frontends/opengl/meson.build b/frontends/opengl/meson.build index 1821a94..ccf641a 100644 --- a/frontends/opengl/meson.build +++ b/frontends/opengl/meson.build @@ -5,6 +5,10 @@ glfw = dependency('glfw3') math = compiler.find_library('m') dl = compiler.find_library('dl') +configure_file(input: 'block.jpg', + output: 'block.jpg', + copy: true, install: false) + executable('cetris_gl', src, dependencies: [glfw, math, dl, cetris], include_directories: [cetris_inc, glad_inc], -- cgit v1.2.3-101-g0448