From 458ab270a8b7246a1a617e34c02aa74deca92ba6 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Mon, 15 Apr 2019 18:32:47 -0400 Subject: convert core to ansii c --- core/src/cetris.c | 41 ++++++++++++++++++++--------------- core/src/input.c | 5 +++-- core/src/matrix.c | 65 ++++++++++++++++++++++++++++++------------------------- core/src/test.c | 6 ++--- core/src/types.c | 8 +++++++ 5 files changed, 72 insertions(+), 53 deletions(-) create mode 100644 core/src/types.c (limited to 'core/src') diff --git a/core/src/cetris.c b/core/src/cetris.c index 4e404d6..aa0d105 100644 --- a/core/src/cetris.c +++ b/core/src/cetris.c @@ -1,4 +1,4 @@ -#include +//#include #include #include #include @@ -32,7 +32,7 @@ void init_game(cetris_game* g) { /* check for config errors */ assert(CETRIS_NEXT_PIECE_DELAY >= CETRIS_LINE_CLEAR_DELAY); - srand(time(NULL)); + //srand(time(NULL)); #ifdef BUILD_TESTS apply_test_board(g, TSPIN_NO_LINES); @@ -49,7 +49,8 @@ void init_game(cetris_game* g) { } void init_piece_queue(cetris_game* g) { - for (u8 i = 0; i < 7; i++) { + u8 i; + for (i = 0; i < 7; i++) { g->piece_queue[i].t = i; g->piece_queue[i].c = i + 1; memcpy(g->piece_queue[i].m, default_matrices[i], sizeof(piece_matrix)); @@ -57,12 +58,13 @@ void init_piece_queue(cetris_game* g) { g->piece_queue[i].lock_tick = 0; g->piece_queue[i].locked = false; g->piece_queue[i].ghost_y = 0; - g->piece_queue[i].pos = (vec2){CETRIS_INITIAL_X, CETRIS_INITIAL_Y - CETRIS_INITIAL_Y_OFFSET}; + g->piece_queue[i].pos = new_vec2(CETRIS_INITIAL_X, CETRIS_INITIAL_Y - CETRIS_INITIAL_Y_OFFSET); } } void shuffle_queue(cetris_game* g) { - for (u8 i = 0; i < 7; i++) { + u8 i; + for (i = 0; i < 7; i++) { tetrimino t = g->piece_queue[i]; u8 rand_index = rand() % 7; g->piece_queue[i] = g->piece_queue[rand_index]; @@ -71,6 +73,7 @@ void shuffle_queue(cetris_game* g) { } void update_game_tick(cetris_game* g) { + u8 did_move; if (g->game_over) return; g->tick++; @@ -81,11 +84,11 @@ void update_game_tick(cetris_game* g) { if (g->next_piece_tick) return; - bool did_move = false; + did_move = 0; if (g->tick >= g->next_drop_tick || !g->next_drop_tick) { if (g->next_drop_tick) { move_current(g, DOWN); - did_move = true; + did_move = 1; } if (g->level <= 20) { @@ -100,14 +103,14 @@ void update_game_tick(cetris_game* g) { g->current.pos.y++; if (check_matrix(g, &g->current.m) <= 0) { lock_current(g); - did_move = true; + did_move = 1; } g->current.pos.y--; g->current.lock_tick = 0; } if (handle_inputs(g)) { - did_move = true; + did_move = 1; } if (did_move) update_board(g); @@ -132,18 +135,19 @@ void next_piece(cetris_game* g) { } void lock_current(cetris_game* g) { - g->current.locked = true; - for (u8 x = 0; x < CETRIS_BOARD_X; x++) { - for (u8 y = 0; y < CETRIS_BOARD_Y; y++) { + u8 x, y; + for (x = 0; x < CETRIS_BOARD_X; x++) { + for (y = 0; y < CETRIS_BOARD_Y; y++) { if (g->board[x][y].occupied) g->board[x][y].constant = 1; } } + g->current.locked = true; update_board(g); } void make_ghosts(cetris_game* g) { u8 orig_y = g->current.pos.y; - while (true) { + while (1) { g->current.pos.y++; if (check_matrix(g, &g->current.m) <= 0) { g->current.ghost_y = g->current.pos.y - 1; @@ -156,10 +160,11 @@ void make_ghosts(cetris_game* g) { void update_board(cetris_game* g) { if (g->game_over) return; + u8 x, y, s; u8 lines_cleared = 0; - for (u8 y = 0; y < CETRIS_BOARD_Y; y++) { + for (y = 0; y < CETRIS_BOARD_Y; y++) { bool clear_line = true; - for (u8 x = 0; x < CETRIS_BOARD_X; x++) { + for (x = 0; x < CETRIS_BOARD_X; x++) { if (!g->board[x][y].constant) { memset(&g->board[x][y], 0, sizeof(slot)); } @@ -168,10 +173,10 @@ void update_board(cetris_game* g) { clear_line = false; } } - // remove tick only tracked on first block of line + /* remove tick only tracked on first block of line */ if (g->board[0][y].remove_tick && g->board[0][y].remove_tick <= g->tick) { - for (i8 s = y - 1; s >= 0; s--) { - for (u8 x = 0; x < CETRIS_BOARD_X; x++) { + for (s = 0; s < y; s++) { + for (x = 0; x < CETRIS_BOARD_X; x++) { g->board[x][s + 1] = g->board[x][s]; } } diff --git a/core/src/input.c b/core/src/input.c index 65dead1..c21b587 100644 --- a/core/src/input.c +++ b/core/src/input.c @@ -29,8 +29,9 @@ bool handle_inputs(cetris_game* g) { } if (g->held_moves[LEFT]) { - if (g->prev_das_move == LEFT || g->das_repeat == 0) g->das_repeat++; - else g->das_repeat = 0; + if (g->prev_das_move == LEFT || g->das_repeat == 0) { + g->das_repeat++; + } else g->das_repeat = 0; move_current(g, LEFT); g->prev_das_move = LEFT; } diff --git a/core/src/matrix.c b/core/src/matrix.c index caae107..2c9daee 100644 --- a/core/src/matrix.c +++ b/core/src/matrix.c @@ -6,27 +6,27 @@ /* SRS WALL KICK VALUES */ -// https://tetris.wiki/SRS +/* 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 */ }; /* DEFAULT MATRIX FOR EACH POSSIBLE TETRIMINO */ @@ -85,7 +85,7 @@ const piece_matrix default_matrices[7] = { /* MATRIX MODIFICATION */ const vec2 basic_movements[5] = { - {0, 0}, {0, 1}, {0, 1}, {1, 0}, {-1, 0} // NONE, DOWN, USER_DOWN, RIGHT, LEFT + {0, 0}, {0, 1}, {0, 1}, {1, 0}, {-1, 0} /* NONE, DOWN, USER_DOWN, RIGHT, LEFT */ }; void move_current(cetris_game* g, input_t move) { @@ -109,9 +109,10 @@ void move_current(cetris_game* g, input_t move) { } i8 check_matrix(cetris_game* g, piece_matrix* m) { - for (i8 y = 0; y < 4; y++) { - for (i8 x = 0; x < 4; x++) { - vec2 r = (vec2){x + g->current.pos.x, y + g->current.pos.y}; + i8 x, y; + for (y = 0; y < 4; y++) { + for (x = 0; x < 4; x++) { + vec2 r = new_vec2(x + g->current.pos.x, y + g->current.pos.y); if (r.y < 0) continue; if ((*m)[y][x]) { if (r.x >= CETRIS_BOARD_X || r.x < 0) return 0; @@ -125,9 +126,10 @@ i8 check_matrix(cetris_game* g, piece_matrix* m) { } void set_matrix(cetris_game* g, piece_matrix* m) { - for (i8 y = 0; y < 4; y++) { - for (i8 x = 0; x < 4; x++) { - vec2 r = (vec2){x + g->current.pos.x, y + g->current.pos.y}; + i8 x, y; + for (y = 0; y < 4; y++) { + for (x = 0; x < 4; x++) { + vec2 r = new_vec2(x + g->current.pos.x, y + g->current.pos.y); if ((*m)[y][x]) { if (r.y >= 0) { g->board[r.x][r.y].occupied = true; @@ -158,7 +160,8 @@ void hard_drop(cetris_game* g) { } } - g->score += 2 * drop_count; // 2 score for each hard-drop'd cell + /* 2 score for each hard-drop'd cell */ + g->score += 2 * drop_count; update_board(g); lock_current(g); @@ -167,7 +170,9 @@ void hard_drop(cetris_game* g) { void rotate_matrix(cetris_game* g, bool clockwise) { if (g->game_over || g->next_piece_tick) return; if (g->current.t == O) return; - + + u8 x, y, i; + rstate next = 0; u8 wall_kick = 0; switch (g->current.r) { @@ -208,8 +213,8 @@ void rotate_matrix(cetris_game* g, bool clockwise) { piece_matrix m; memset(m, 0, sizeof(piece_matrix)); - for (u8 x = 0; x < 4; x++) { - for (u8 y = 0; y < 4; y++) { + for (x = 0; x < 4; x++) { + for (y = 0; y < 4; y++) { if (g->current.m[y][x]) { u8 new_x = (clockwise) ? 1 - (y - 2) : 1 + (y - 2); u8 new_y = (clockwise) ? 2 + (x - 1) : 2 - (x - 1); @@ -230,7 +235,7 @@ void rotate_matrix(cetris_game* g, bool clockwise) { vec2 kick; bool set_current = false; bool did_kick = false; - for (u8 i = 0; i < 5; i++) { + for (i = 0; i < 5; i++) { if (g->current.t == I) { kick = srs_wall_kicks_i[wall_kick][i]; } else { @@ -255,7 +260,7 @@ void rotate_matrix(cetris_game* g, bool clockwise) { /* check for tspin */ if (g->current.t == T) { bool did_tspin = true; - for (int i = 1; i < 5; i++) { + for (i = 1; i < 5; i++) { g->current.pos.x += basic_movements[i].x; g->current.pos.y += basic_movements[i].y; diff --git a/core/src/test.c b/core/src/test.c index bad50d8..403823f 100644 --- a/core/src/test.c +++ b/core/src/test.c @@ -48,7 +48,7 @@ u8 tspin_no_lines_board[20][10] = { }; void apply_test_board(cetris_game* g, test t) { - u8 (*board)[20][10]; + u8 x, y, (*board)[20][10]; switch (t) { case TSPIN: board = &tspin_board; @@ -59,8 +59,8 @@ void apply_test_board(cetris_game* g, test t) { default: return; } - for (u8 y = 0; y < CETRIS_BOARD_Y; y++) { - for (u8 x = 0; x < 10; x++) { + for (y = 0; y < CETRIS_BOARD_Y; y++) { + for (x = 0; x < 10; x++) { if ((*board)[y][x]) { g->board[x][y].occupied = 1; g->board[x][y].constant = 1; diff --git a/core/src/types.c b/core/src/types.c new file mode 100644 index 0000000..381a14a --- /dev/null +++ b/core/src/types.c @@ -0,0 +1,8 @@ +#include "types.h" + +vec2 new_vec2(i8 x, i8 y) { + vec2 v; + v.x = x; + v.y = y; + return v; +} -- cgit v1.2.3-101-g0448