summary refs log tree commit diff
diff options
context:
space:
mode:
authorturnipgod2025-03-03 00:55:10 -0500
committerturnipgod2025-03-03 00:55:10 -0500
commitea048d60ebb07e8d6c729ef25e7321e1b564f8ce (patch)
tree703da1c7e3963f9dcd0a4f0bb49fc035eeb96b02
parent1e5e13bf16ac6b91b54bc8217606683e7085346a (diff)
piece queue rendering, hold piece
-rw-r--r--src/main.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
index 35cb460..e9eacbc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -41,6 +41,8 @@ int piece_queue[TETROMINO_COUNT*2] = {
 };
 int current_bag = 0;
 int current_piece_index = 0;
+int hold = 7;
+int held = 0;
 
 void draw_block(int x, int y, int width, int height, struct Color color)
 {
@@ -88,6 +90,56 @@ void render_matrix(void)
             }
         }
     }
+
+    /* draw piece hold */
+    draw_block(
+        SCREEN_WIDTH/2 + 1 - (BLOCK_SIZE),
+        matrix_origin_y + MATRIX_HEIGHT/2 * BLOCK_SIZE - 3 * BLOCK_SIZE,
+        4*BLOCK_SIZE,
+        4*BLOCK_SIZE,
+        BLACK
+    );
+    if (hold != 7) {
+        for (int i = 0; i < tetrominos[hold].size; i++) {
+            for (int j = 0; j < tetrominos[hold].size; j++) {
+                if (tetrominos[hold].directions[NORTH][j][i] == 1) {
+                    draw_block(
+                        ((SCREEN_WIDTH/2 + 1 - (BLOCK_SIZE)) + (i * BLOCK_SIZE)) + 1
+                               + ((hold != I && hold != O)?(BLOCK_SIZE/2):0)
+                        ,matrix_origin_y + (MATRIX_HEIGHT/2 * BLOCK_SIZE) + ((j-3) * BLOCK_SIZE)
+                        ,BLOCK_SIZE - 2
+                        ,BLOCK_SIZE - 2
+                        ,tetrominos[hold].color
+                    );
+                }
+            }
+        }
+    }
+
+    /* draw piece queue */
+    draw_block(
+        SCREEN_WIDTH/2 + 1 - (BLOCK_SIZE),
+        matrix_origin_y + MATRIX_HEIGHT/2 * BLOCK_SIZE - 20 * BLOCK_SIZE,
+        4*BLOCK_SIZE,
+        (4*BLOCK_SIZE) * 4,
+        BLACK
+    );
+    for (int y = 0; y < 4; y++) {
+        for (int i = 0; i < tetrominos[piece_queue[(y+current_piece_index)%14]].size; i++) {
+            for (int j = 0; j < tetrominos[piece_queue[(y+current_piece_index)%14]].size; j++) {
+                if (tetrominos[piece_queue[(y+current_piece_index)%14]].directions[NORTH][j][i] == 1) {
+                    draw_block(
+                        ((SCREEN_WIDTH/2 + 1 - (BLOCK_SIZE)) + (i * BLOCK_SIZE)) + 1
+                           + ((piece_queue[(y+current_piece_index)%14] != I && piece_queue[(y+current_piece_index)%14] != O)?(BLOCK_SIZE/2):0)
+                        ,((matrix_origin_y + MATRIX_HEIGHT/2 * BLOCK_SIZE - 20 * BLOCK_SIZE) + ((j - 1) * BLOCK_SIZE)) + 1 + ((3 - y) * BLOCK_SIZE * 4) + BLOCK_SIZE/2
+                        ,BLOCK_SIZE - 2
+                        ,BLOCK_SIZE - 2
+                        ,tetrominos[piece_queue[(y+current_piece_index)%14]].color
+                    );
+                }
+            }
+        }
+    }
 }
 
 void shuffle_bag(int *bag)
@@ -139,6 +191,29 @@ void generation_phase(void)
     }
 }
 
+void handle_hold(void)
+{
+    if (IsKeyDown(KEY_SPACE) && !held) {
+        if (hold == 7) {
+            hold = current_piece.type;
+            current_piece_index++;
+            current_piece_index = current_piece_index % 14;
+            current_piece.type = current_piece_index;
+        } else {
+            int temp = current_piece.type;
+            current_piece.type = hold;
+            hold = temp;
+        }
+        current_piece.rotation = NORTH;
+        current_piece.pos_x = 4;
+        current_piece.pos_y = 20;
+        if (is_overlap(0,0)) {
+            CloseWindow();
+        }
+        held = 1;
+    }
+}
+
 void handle_rotate(void)
 {
     static int rotated_cw = 0;
@@ -235,6 +310,7 @@ void handle_fall(void)
     if (is_overlap(0, -1)) {
         move_piece_to_matrix();
         generation_phase();
+        held = 0;
         return;
     }
 
@@ -309,6 +385,7 @@ int main(void)
     while (!WindowShouldClose()) {
         handle_lr();
         handle_rotate();
+        handle_hold();
         handle_hard_drop();
         handle_fall();
         handle_line_clears();