From 3b0e0f4788a3476ef848f1e54b0afae7a60bcdfe Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Sun, 5 Jan 2020 18:02:19 -0500 Subject: cleanup --- frontends/gl/ui.c | 150 ++++++++++++++++++++++++------------------------------ lib/cetris.c | 114 ++++++++++++++++++----------------------- lib/cetris.h | 4 ++ lib/rules.c | 3 -- 4 files changed, 120 insertions(+), 151 deletions(-) diff --git a/frontends/gl/ui.c b/frontends/gl/ui.c index b639e3f..c964a43 100644 --- a/frontends/gl/ui.c +++ b/frontends/gl/ui.c @@ -85,36 +85,32 @@ void load_scene(cetris_ui *ui) { void load_held_piece(cetris_ui *ui, GLfloat x, GLfloat y, GLfloat w, GLfloat h) { if (!ui->board.game.piece_held) return; - GLfloat block_width = w / 4.0f; - GLfloat block_height = h / 4.0f; + GLfloat block_width = w / 4.0f, block_height = h / 4.0f; - for (int s = 0; s < 4; s++) { - for (int j = 0; j < 4; j++) { - if ((ui->board.game.held.m[s]>>(3 - j))&1) { - new_rectangle(&ui->scene[ui->scene_count]); + ITER_MATRIX { + if ((ui->board.game.held.m[s]>>(3 - j))&1) { + new_rectangle(&ui->scene[ui->scene_count]); - GLfloat block_x = x + (j * block_width); - GLfloat block_y = y + (s * block_height); + GLfloat block_x = x + (j * block_width); + GLfloat block_y = y + (s * block_height); - if (ui->board.game.held.t == MINO_O) - block_x -= block_width / 1.5f; + if (ui->board.game.held.t == MINO_O) + block_x -= block_width / 1.5f; - if (ui->board.game.held.t == MINO_I) - block_x -= block_width / 1.5f; + if (ui->board.game.held.t == MINO_I) + block_x -= block_width / 1.5f; - set_block_texture(&ui->scene[ui->scene_count], ui->board.game.held.t); - update_rect(&ui->scene[ui->scene_count], block_x, block_y, - block_width, block_height, ui->window_width, ui->window_height); - - ui->scene[ui->scene_count++].texture = ui->board.skin.block.texture; - } + set_block_texture(&ui->scene[ui->scene_count], ui->board.game.held.t); + update_rect(&ui->scene[ui->scene_count], block_x, block_y, + block_width, block_height, ui->window_width, ui->window_height); + + ui->scene[ui->scene_count++].texture = ui->board.skin.block.texture; } } } void load_piece_queue(cetris_ui *ui, GLfloat x, GLfloat y, GLfloat w, GLfloat h) { - GLfloat block_width = w / 4.0f; - GLfloat block_height = w / 4.0f; + GLfloat block_width = w / 4.0f, block_height = w / 4.0f; for (int i = 0; i < 5; i++) { int mino, index = (ui->board.game.current_index + i); @@ -126,26 +122,24 @@ void load_piece_queue(cetris_ui *ui, GLfloat x, GLfloat y, GLfloat w, GLfloat h) mino = ui->board.game.next_queue[index]; } - for (int s = 0; s < 4; s++) { - for (int j = 0; j < 4; j++) { - if ((default_matrices[mino][s]>>(3 - j))&1) { - new_rectangle(&ui->scene[ui->scene_count]); + ITER_MATRIX { + if ((default_matrices[mino][s]>>(3 - j))&1) { + new_rectangle(&ui->scene[ui->scene_count]); - GLfloat block_x = x + (j * block_width); - GLfloat block_y = y + (h * (i / 5.0)) + (s * block_height); + GLfloat block_x = x + (j * block_width); + GLfloat block_y = y + (h * (i / 5.0)) + (s * block_height); - if (ui->board.game.held.t == MINO_O) - block_x -= block_width / 1.5f; + if (ui->board.game.held.t == MINO_O) + block_x -= block_width / 1.5f; - if (ui->board.game.held.t == MINO_I) - block_x -= block_width / 1.5f; + if (ui->board.game.held.t == MINO_I) + block_x -= block_width / 1.5f; - set_block_texture(&ui->scene[ui->scene_count], mino); - update_rect(&ui->scene[ui->scene_count], block_x, block_y, - block_width, block_height, ui->window_width, ui->window_height); + set_block_texture(&ui->scene[ui->scene_count], mino); + update_rect(&ui->scene[ui->scene_count], block_x, block_y, + block_width, block_height, ui->window_width, ui->window_height); - ui->scene[ui->scene_count++].texture = ui->board.skin.block.texture; - } + ui->scene[ui->scene_count++].texture = ui->board.skin.block.texture; } } } @@ -154,72 +148,60 @@ void load_piece_queue(cetris_ui *ui, GLfloat x, GLfloat y, GLfloat w, GLfloat h) // TODO: make this better void update_current_drawable(cetris_ui *ui, drawable_t *current, drawable_t* ghost) { refresh_current(ui); - int cur_piece = -1; - for (int s = 0; s < 4; s++) { - for (int j = 0; j < 4; j++) { - if ((ui->board.game.current.m[s]>>(3 - j))&1) { - cur_piece++; - - GLfloat block_x = ui->board.x_offset + - (j + ui->board.game.current.pos.x) * ui->board.block_width; - - if (ghost) { - GLfloat ghost_y = ui->board.y_offset + - (s + ui->board.game.current.ghost_y - ui->board.block_offset) - * ui->board.block_height; - - ghost[cur_piece].visable = true; - set_block_texture(&ghost[cur_piece], ui->board.game.current.t + 7); - update_rect(&ghost[cur_piece], block_x, ghost_y, - ui->board.block_width, ui->board.block_height, - ui->window_width, ui->window_height); - } - - if (s + ui->board.game.current.pos.y < ui->board.block_offset - 1) continue; - - GLfloat block_y = ui->board.y_offset + - (s + ui->board.game.current.pos.y - ui->board.block_offset) - * ui->board.block_height; - - current[cur_piece].visable = true; - set_block_texture(¤t[cur_piece], ui->board.game.current.t); - update_rect(¤t[cur_piece], block_x, block_y, - ui->board.block_width, ui->board.block_height, - ui->window_width, ui->window_height); + int cur_piece = -1; ITER_MATRIX { + if ((ui->board.game.current.m[s]>>(3 - j))&1) { + cur_piece++; + + GLfloat block_x = ui->board.x_offset + (j + ui->board.game.current.pos.x) + * ui->board.block_width; + + if (ghost) { + GLfloat ghost_y = ui->board.y_offset + (s + ui->board.game.current.ghost_y + - ui->board.block_offset) * ui->board.block_height; + ghost[cur_piece].visable = true; + set_block_texture(&ghost[cur_piece], ui->board.game.current.t + 7); + update_rect(&ghost[cur_piece], block_x, ghost_y, ui->board.block_width, + ui->board.block_height, ui->window_width, ui->window_height); } + + if (s + ui->board.game.current.pos.y < ui->board.block_offset - 1) continue; + + GLfloat block_y = ui->board.y_offset + (s + ui->board.game.current.pos.y + - ui->board.block_offset) * ui->board.block_height; + + current[cur_piece].visable = true; + set_block_texture(¤t[cur_piece], ui->board.game.current.t); + update_rect(¤t[cur_piece], block_x, block_y, ui->board.block_width, + ui->board.block_height, ui->window_width, ui->window_height); } } } void lock_current_drawable(cetris_ui *ui) { - int current = 0; - for (int s = 0; s < 4; s++) { - for (int j = 0; j < 4; j++) { - if ((ui->board.game.lock_event.m[s]>>(3 - j))&1) { - new_rectangle(&ui->board.board[ui->board.board_count + current]); + int current = 0; ITER_MATRIX { + if ((ui->board.game.lock_event.m[s]>>(3 - j))&1) { + new_rectangle(&ui->board.board[ui->board.board_count + current]); - GLfloat block_x = ui->board.x_offset + - (j + ui->board.game.lock_event.pos.x) * ui->board.block_width; + GLfloat block_x = ui->board.x_offset + + (j + ui->board.game.lock_event.pos.x) * ui->board.block_width; - GLfloat block_y = ui->board.y_offset + - (s + ui->board.game.lock_event.pos.y - ui->board.block_offset) - * ui->board.block_height; + GLfloat block_y = ui->board.y_offset + + (s + ui->board.game.lock_event.pos.y - ui->board.block_offset) + * ui->board.block_height; - set_block_texture(&ui->board.board[ui->board.board_count + current], ui->board.game.lock_event.t); - update_rect(&ui->board.board[ui->board.board_count + current], block_x, block_y, - ui->board.block_width, ui->board.block_height, - ui->window_width, ui->window_height); + set_block_texture(&ui->board.board[ui->board.board_count + current], ui->board.game.lock_event.t); + update_rect(&ui->board.board[ui->board.board_count + current], block_x, block_y, + ui->board.block_width, ui->board.block_height, + ui->window_width, ui->window_height); - ui->board.board_count++; - } + ui->board.board_count++; } } } void refresh_board(cetris_ui *ui) { ui->board.board_count = 0; - for (int s = 0; s < ui->board.game.config.board_x; s++) { for (int j = 0; j < ui->board.game.config.board_y; j++) { if (ui->board.game.board[s][j] & SLOT_OCCUPIED) { diff --git a/lib/cetris.c b/lib/cetris.c index 8be2b44..d8a175d 100644 --- a/lib/cetris.c +++ b/lib/cetris.c @@ -14,43 +14,38 @@ /* DEFAULT TETRIMINO */ CETRIS_EXPORT const piece_matrix default_matrices[7] = { - { 0b0000, 0b1100, 0b0110, 0b0000}, - { 0b0000, 0b0010, 0b1110, 0b0000}, - { 0b0000, 0b0110, 0b0110, 0b0000}, - { 0b0000, 0b0110, 0b1100, 0b0000}, - { 0b0000, 0b1111, 0b0000, 0b0000}, - { 0b0000, 0b1000, 0b1110, 0b0000}, + { 0b0000, 0b1100, 0b0110, 0b0000}, { 0b0000, 0b0010, 0b1110, 0b0000}, + { 0b0000, 0b0110, 0b0110, 0b0000}, { 0b0000, 0b0110, 0b1100, 0b0000}, + { 0b0000, 0b1111, 0b0000, 0b0000}, { 0b0000, 0b1000, 0b1110, 0b0000}, { 0b0000, 0b0100, 0b1110, 0b0000}}; /* SRS WALL KICK VALUES */ // https://tetris.wiki/SRS static const vec2 srs_wall_kicks[8][5] = { - {{0, 0}, {-1, 0}, {-1, 1}, {0, -2}, {-1, -2}}, // 0->R - {{0, 0}, {1, 0}, {1, -1}, {0, 2}, {1, 2}}, // R->0 - {{0, 0}, {1, 0}, {1, -1}, {0, 2}, {1, 2}}, // R->2 - {{0, 0}, {-1, 0}, {-1, 1}, {0, -2}, {-1, -2}}, // 2->R - {{0, 0}, {1, 0}, {1, 1}, {0, -2}, {1, -2}}, // 2->L - {{0, 0}, {-1, 0}, {-1, -1}, {0, 2}, {-1, 2}}, // L->2 - {{0, 0}, {-1, 0}, {-1, -1}, {0, 2}, {-1, 2}}, // L->0 - {{0, 0}, {1, 0}, {1, 1}, {0, -2}, {1, -2}} // 0->L + {{0, 0}, {-1, 0}, {-1, 1}, {0, -2}, {-1, -2}}, // 0->R + {{0, 0}, {1, 0}, {1, -1}, {0, 2}, {1, 2}}, // R->0 + {{0, 0}, {1, 0}, {1, -1}, {0, 2}, {1, 2}}, // R->2 + {{0, 0}, {-1, 0}, {-1, 1}, {0, -2}, {-1, -2}}, // 2->R + {{0, 0}, {1, 0}, {1, 1}, {0, -2}, {1, -2}}, // 2->L + {{0, 0}, {-1, 0}, {-1, -1}, {0, 2}, {-1, 2}}, // L->2 + {{0, 0}, {-1, 0}, {-1, -1}, {0, 2}, {-1, 2}}, // L->0 + {{0, 0}, {1, 0}, {1, 1}, {0, -2}, {1, -2}} // 0->L }; static const vec2 srs_wall_kicks_i[8][5] = { - {{0, 0}, {-2, 0}, {1, 0}, {-2, -1}, {1, 2}}, // 0->R - {{0, 0}, {2, 0}, {-1, 0}, {2, 1}, {-1, -2}}, // R->0 - {{0, 0}, {-1, 0}, {2, 0}, {-1, 2}, {2, -1}}, // R->2 - {{0, 0}, {1, 0}, {-2, 0}, {1, -2}, {-2, 1}}, // 2->R - {{0, 0}, {2, 0}, {-1, 0}, {2, 1}, {-1, -2}}, // 2->L - {{0, 0}, {-2, 0}, {1, 0}, {-2, -1}, {1, 2}}, // L->2 - {{0, 0}, {1, 0}, {-2, 0}, {1, -2}, {-2, 1}}, // L->0 - {{0, 0}, {-1, 0}, {2, 0}, {-1, 2}, {2, -1}} // 0->L + {{0, 0}, {-2, 0}, {1, 0}, {-2, -1}, {1, 2}}, // 0->R + {{0, 0}, {2, 0}, {-1, 0}, {2, 1}, {-1, -2}}, // R->0 + {{0, 0}, {-1, 0}, {2, 0}, {-1, 2}, {2, -1}}, // R->2 + {{0, 0}, {1, 0}, {-2, 0}, {1, -2}, {-2, 1}}, // 2->R + {{0, 0}, {2, 0}, {-1, 0}, {2, 1}, {-1, -2}}, // 2->L + {{0, 0}, {-2, 0}, {1, 0}, {-2, -1}, {1, 2}}, // L->2 + {{0, 0}, {1, 0}, {-2, 0}, {1, -2}, {-2, 1}}, // L->0 + {{0, 0}, {-1, 0}, {2, 0}, {-1, 2}, {2, -1}} // 0->L }; -static const vec2 basic_movements[5] = { - {0, 1}, // DOWN - {1, 0}, // RIGHT - {-1, 0} // LEFT +static const vec2 basic_movements[3] = { + {0, 1}, {1, 0}, {-1, 0} // DOWN, RIGHT, LEFT }; static void update_board(cetris_game *g); @@ -79,19 +74,17 @@ static void shuffle_queue(cetris_game *g) { } static int check_matrix(cetris_game *g, piece_matrix *m) { - for (uint8_t y = 0; y < 4; y++) { - for (uint8_t x = 0; x < 4; x++) { - vec2 r = (vec2){x + g->current.pos.x, y + g->current.pos.y}; - if (r.y < 0) - continue; - if (((*m)[y]>>(3 - x))&1) { - if (r.x >= g->config.board_x || r.x < 0) - return 0; - if (r.y >= g->config.board_y) - return -1; - if (g->board[r.x][r.y] & SLOT_OCCUPIED) - return -1; - } + ITER_MATRIX { + vec2 r = (vec2){j + g->current.pos.x, s + g->current.pos.y}; + if (r.y < 0) + continue; + if (((*m)[s]>>(3 - j))&1) { + if (r.x >= g->config.board_x || r.x < 0) + return 0; + if (r.y >= g->config.board_y) + return -1; + if (g->board[r.x][r.y] & SLOT_OCCUPIED) + return -1; } } return 1; @@ -213,12 +206,10 @@ static void next_piece(cetris_game *g) { } static void lock_current(cetris_game *g) { - for (int y = 0; y < 4; y++) { - for (int x = 0; x < 4; x++) { - if ((g->current.m[y]>>(3 - x))&1) { - g->board[g->current.pos.x + x][g->current.pos.y + y] |= SLOT_OCCUPIED; - g->board[g->current.pos.x + x][g->current.pos.y + y] |= g->current.t << 5; - } + ITER_MATRIX { + if ((g->current.m[s]>>(3 - j))&1) { + g->board[g->current.pos.x + j][g->current.pos.y + s] |= SLOT_OCCUPIED; + g->board[g->current.pos.x + j][g->current.pos.y + s] |= g->current.t << 5; } } @@ -253,18 +244,16 @@ static void hard_drop(cetris_game *g) { } static void rotate_matrix(cetris_game *g, piece_matrix *m, bool clockwise) { - for (uint8_t x = 0; x < 4; x++) { - for (uint8_t y = 0; y < 4; y++) { - if ((g->current.m[y]>>(3 - x))&1) { - uint8_t new_x = (clockwise) ? 1 - (y - 2) : 1 + (y - 2); - uint8_t new_y = (clockwise) ? 2 + (x - 1) : 2 - (x - 1); - - if (g->current.t == MINO_I) { - clockwise ? new_y-- : new_x++; - } - - (*m)[new_y] |= (uint8_t)0b1000 >> (new_x); + ITER_MATRIX { + if ((g->current.m[s]>>(3 - j))&1) { + uint8_t new_x = (clockwise) ? 1 - (s - 2) : 1 + (s - 2); + uint8_t new_y = (clockwise) ? 2 + (j - 1) : 2 - (j - 1); + + if (g->current.t == MINO_I) { + clockwise ? new_y-- : new_x++; } + + (*m)[new_y] |= (uint8_t)0b1000 >> (new_x); } } } @@ -275,13 +264,12 @@ static void rotate_piece(cetris_game *g, bool clockwise) { if (g->current.t == MINO_O) return; - uint8_t next = 0; - uint8_t wall_kick = 0; + uint8_t next = 0, wall_kick = 0; if (clockwise) { - next = (g->current.r + 1)%4; + next = (g->current.r + 1) % 4; wall_kick = g->current.r * 2; } else { - next = ((g->current.r - 1) + 4)%4; + next = ((g->current.r - 1) + 4) % 4; wall_kick = (next * 2) + 1; } @@ -291,8 +279,7 @@ static void rotate_piece(cetris_game *g, bool clockwise) { rotate_matrix(g, &m, clockwise); vec2 kick; - bool set_current = false; - bool did_kick = false; + bool set_current = false, did_kick = false; for (int i = 0; i < 4; i++) { if (g->current.t == MINO_I) { kick = srs_wall_kicks_i[wall_kick][i]; @@ -317,7 +304,7 @@ static void rotate_piece(cetris_game *g, bool clockwise) { /* check for tspin */ if (g->current.t == MINO_T) { bool did_tspin = true; - for (int i = 1; i < 5; i++) { + for (int i = 1; i < 3; i++) { g->current.pos.x += basic_movements[i].x; g->current.pos.y += basic_movements[i].y; @@ -481,7 +468,6 @@ CETRIS_EXPORT void move_piece(cetris_game *g, uint8_t move) { rotate_piece(g, 0); break; } - } CETRIS_EXPORT void init_game(cetris_game *g) { diff --git a/lib/cetris.h b/lib/cetris.h index b03f9fd..7e50460 100644 --- a/lib/cetris.h +++ b/lib/cetris.h @@ -22,6 +22,10 @@ typedef uint16_t ctick; #define CETRIS_ENABLE_DAS 1 +#define ITER_MATRIX \ + for (uint8_t j = 0; j < 4; j++) \ + for (uint8_t s = 0; s < 4; s++) + typedef struct { int8_t x; int8_t y; diff --git a/lib/rules.c b/lib/rules.c index 050a16e..31212d6 100644 --- a/lib/rules.c +++ b/lib/rules.c @@ -41,6 +41,3 @@ cetris_config tetris_ds_config = { .levels = &tetris_worlds_levels[0], .win_condition = marathon }; - - - -- cgit v1.2.3-101-g0448