summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cetris.c39
-rw-r--r--src/cetris.h19
-rw-r--r--src/input.c34
-rw-r--r--src/input.h20
-rw-r--r--src/matrix.c12
-rw-r--r--src/matrix.h3
-rw-r--r--src/test.c4
-rw-r--r--src/types.h19
8 files changed, 81 insertions, 69 deletions
diff --git a/src/cetris.c b/src/cetris.c
index 7adf5b1..419fdaf 100644
--- a/src/cetris.c
+++ b/src/cetris.c
@@ -6,6 +6,9 @@
#include <assert.h>
#include "cetris.h"
+#include "types.h"
+#include "matrix.h"
+#include "input.h"
#ifdef BUILD_TESTS
#include "test.h"
@@ -15,8 +18,6 @@
static void init_piece_queue(struct cetris_game* g);
static void shuffle_queue(struct cetris_game* g);
-static void next_piece(struct cetris_game* g);
-static void wipe_board(struct cetris_game* g);
static void set_constants(struct cetris_game* g);
/* LEVEL DROP SPEED VALUES */
@@ -41,11 +42,11 @@ void init_game(struct cetris_game* g) {
g->current_index = 0;
- g->input.held_move = 0;
- g->input.prev_move = 0;
- g->input.next_move_tick = 0;
- g->input.can_rotate = 0;
- g->input.can_hard_drop = 0;
+ g->held_move = 0;
+ g->prev_move = 0;
+ g->next_move_tick = 0;
+ g->can_rotate = 0;
+ g->can_hard_drop = 0;
g->lines = 0;
g->level = 1;
@@ -233,43 +234,43 @@ void wipe_board(struct cetris_game* g) {
/* MOVEMENT FUNCTIONS */
void move_down(struct cetris_game* g) {
- if (g->input.held_move != DOWN) {
+ if (g->held_move != DOWN) {
move_current(g, basic_movements[DOWN]);
}
- g->input.held_move = DOWN;
+ g->held_move = DOWN;
}
void move_right(struct cetris_game* g) {
- if (g->input.held_move != RIGHT) {
+ if (g->held_move != RIGHT) {
move_current(g, basic_movements[RIGHT]);
}
- g->input.held_move = RIGHT;
+ g->held_move = RIGHT;
}
void move_left(struct cetris_game* g) {
- if (g->input.held_move != LEFT) {
+ if (g->held_move != LEFT) {
move_current(g, basic_movements[LEFT]);
}
- g->input.held_move = LEFT;
+ g->held_move = LEFT;
}
void move_hard_drop(struct cetris_game* g) {
- if (g->input.held_move != HARD_DROP) {
+ if (g->held_move != HARD_DROP) {
hard_drop(g);
}
- g->input.held_move = HARD_DROP;
+ g->held_move = HARD_DROP;
}
void rotate_clockwise(struct cetris_game* g) {
- if (g->input.held_move != ROTATE_CW) {
+ if (g->held_move != ROTATE_CW) {
rotate_matrix(g, 1);
}
- g->input.held_move = ROTATE_CW;
+ g->held_move = ROTATE_CW;
}
void rotate_counterclockwise(struct cetris_game* g) {
- if (g->input.held_move != ROTATE_CCW) {
+ if (g->held_move != ROTATE_CCW) {
rotate_matrix(g, 0);
}
- g->input.held_move = ROTATE_CCW;
+ g->held_move = ROTATE_CCW;
}
diff --git a/src/cetris.h b/src/cetris.h
index 1afc7ef..2690163 100644
--- a/src/cetris.h
+++ b/src/cetris.h
@@ -2,8 +2,7 @@
#include <stdint.h>
-#include "input.h"
-#include "matrix.h"
+#include "types.h"
#define CETRIS_BOARD_X 10
#define CETRIS_BOARD_Y 43
@@ -16,12 +15,6 @@
#define CETRIS_LINE_CLEAR_DELAY 40
#define CETRIS_WAIT_ON_CLEAR 0
-typedef struct {
- int8_t x;
- int8_t y;
-} vec2;
-
-
typedef enum {
O, I, S, Z, L, J, T
} type;
@@ -71,7 +64,12 @@ struct cetris_game {
struct tetrimino current;
uint8_t current_index;
- struct input_manager input;
+ /* input_manager */
+ input_t held_move;
+ input_t prev_move;
+ int next_move_tick;
+ uint8_t can_rotate;
+ uint8_t can_hard_drop;
/* internal game tick */
int tick;
@@ -90,6 +88,9 @@ struct cetris_game {
long int score;
};
+void next_piece(struct cetris_game* g);
+void wipe_board(struct cetris_game* g);
+
/* API PROTOTYPES FUNCTIONS */
void init_game(struct cetris_game* g);
diff --git a/src/input.c b/src/input.c
index a77f4df..e3b61b8 100644
--- a/src/input.c
+++ b/src/input.c
@@ -1,21 +1,27 @@
+#include <stdint.h>
+
#include "input.h"
+#include "types.h"
+#include "matrix.h"
+#include "cetris.h"
+
uint8_t handle_inputs(struct cetris_game* g) {
uint8_t did_move = 0;
- if (g->input.held_move && !g->input.next_move_tick) {
- if (g->input.held_move == RIGHT || g->input.held_move == LEFT) {
- if (g->input.prev_move == g->input.held_move) {
- g->input.next_move_tick = g->tick + CETRIS_DAS_PERIOD;
+ if (g->held_move && !g->next_move_tick) {
+ if (g->held_move == RIGHT || g->held_move == LEFT) {
+ if (g->prev_move == g->held_move) {
+ g->next_move_tick = g->tick + CETRIS_DAS_PERIOD;
} else {
- g->input.next_move_tick = g->tick + CETRIS_DAS_DELAY;
+ g->next_move_tick = g->tick + CETRIS_DAS_DELAY;
}
} else {
- g->input.next_move_tick = g->tick + CETRIS_DROP_PERIOD;
+ g->next_move_tick = g->tick + CETRIS_DROP_PERIOD;
}
}
- if (g->input.next_move_tick && g->tick >= g->input.next_move_tick) {
- switch (g->input.held_move) {
+ if (g->next_move_tick && g->tick >= g->next_move_tick) {
+ switch (g->held_move) {
case DOWN:
g->score++;
move_current(g, basic_movements[DOWN]);
@@ -31,14 +37,14 @@ uint8_t handle_inputs(struct cetris_game* g) {
}
if (did_move) {
- g->input.next_move_tick = 0;
- g->input.prev_move = g->input.held_move;
+ g->next_move_tick = 0;
+ g->prev_move = g->held_move;
}
return did_move;
}
-void clear_held_key(struct input_manager* input) {
- input->prev_move = 0;
- input->held_move = 0;
- input->next_move_tick = 0;
+void clear_held_key(struct cetris_game* g) {
+ g->prev_move = 0;
+ g->held_move = 0;
+ g->next_move_tick = 0;
}
diff --git a/src/input.h b/src/input.h
index 188ca71..9fceabe 100644
--- a/src/input.h
+++ b/src/input.h
@@ -2,24 +2,8 @@
#include <stdint.h>
+#include "types.h"
#include "cetris.h"
-typedef enum {
- DOWN = 1,
- RIGHT = 2,
- LEFT = 3,
- ROTATE_CCW = 4,
- ROTATE_CW = 5,
- HARD_DROP = 6
-} input_t;
-
-struct input_manager {
- input_t held_move;
- input_t prev_move;
- int next_move_tick;
- uint8_t can_rotate;
- uint8_t can_hard_drop;
-};
-
uint8_t handle_inputs(struct cetris_game* g);
-void clear_held_key(struct input_manager* input);
+void clear_held_key(struct cetris_game* g);
diff --git a/src/matrix.c b/src/matrix.c
index 796b4a8..1fabdce 100644
--- a/src/matrix.c
+++ b/src/matrix.c
@@ -1,6 +1,8 @@
#include <string.h>
#include "matrix.h"
+#include "types.h"
+#include "cetris.h"
/* SRS WALL KICK VALUES */
@@ -90,7 +92,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.mat);
+ int8_t check = check_new_matrix(g, g->current.m);
if (check <= 0) {
g->current.pos.y -= offset.y;
g->current.pos.x -= offset.x;
@@ -107,7 +109,7 @@ void overlay_current_matrix(struct cetris_game* g) {
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 (g->current.mat[y][x]) {
+ if (g->current.m[y][x]) {
g->board[r.x][r.y].occupied = 1;
g->board[r.x][r.y].c = g->current.c;
}
@@ -123,7 +125,7 @@ void hard_drop(struct cetris_game* g) {
while (!drop) {
g->current.pos.y++;
drop_count++;
- int8_t check = check_new_matrix(g, g->current.mat);
+ int8_t check = check_new_matrix(g, g->current.m);
if (check <= 0) {
g->current.pos.y--;
drop_count--;
@@ -181,7 +183,7 @@ void rotate_matrix(struct cetris_game* g, int clockwise) {
for (uint8_t x = 0; x < 4; x++) {
for (uint8_t y = 0; y < 4; y++) {
- if (g->current.mat[y][x]) {
+ 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);
@@ -256,7 +258,7 @@ void rotate_matrix(struct cetris_game* g, int clockwise) {
}
g->current.r = next;
- memcpy(g->current.mat, m, sizeof(piece_matrix));
+ memcpy(g->current.m, m, sizeof(piece_matrix));
wipe_board(g);
}
}
diff --git a/src/matrix.h b/src/matrix.h
index 57166cc..f75e1e0 100644
--- a/src/matrix.h
+++ b/src/matrix.h
@@ -2,10 +2,9 @@
#include <stdint.h>
+#include "types.h"
#include "cetris.h"
-typedef uint8_t piece_matrix[4][4];
-
const piece_matrix default_matrices[7];
const vec2 basic_movements[4];
diff --git a/src/test.c b/src/test.c
index fa7aaa8..cbd8424 100644
--- a/src/test.c
+++ b/src/test.c
@@ -58,9 +58,9 @@ void apply_test_board(struct cetris_game* g, enum tests t) {
default:
return;
}
- for (int y = BOARD_VISABLE; y < BOARD_Y; y++) {
+ for (int y = CETRIS_BOARD_VISABLE; y < CETRIS_BOARD_Y; y++) {
for (int x = 0; x < 10; x++) {
- if ((*board)[y - BOARD_VISABLE][x]) {
+ if ((*board)[y - CETRIS_BOARD_VISABLE][x]) {
g->board[x][y].occupied = 1;
g->board[x][y].constant = 1;
g->board[x][y].c = COLOR_I;
diff --git a/src/types.h b/src/types.h
new file mode 100644
index 0000000..2ce97c7
--- /dev/null
+++ b/src/types.h
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <stdint.h>
+
+typedef uint8_t piece_matrix[4][4];
+
+typedef struct {
+ int8_t x;
+ int8_t y;
+} vec2;
+
+typedef enum {
+ DOWN = 1,
+ RIGHT = 2,
+ LEFT = 3,
+ ROTATE_CCW = 4,
+ ROTATE_CW = 5,
+ HARD_DROP = 6
+} input_t;