From c732a25dc17a740d0aa8b76ca2e405b39f89d54d Mon Sep 17 00:00:00 2001 From: John Landers Date: Mon, 15 Jan 2024 11:35:03 -0600 Subject: [PATCH] still working on rotation, gotta quit going crazy lol --- src/rewrite/m_block.c | 35 +++++++++++++++++++++++++++++++++-- src/rewrite/m_block.h | 2 ++ src/rewrite/m_tetromino.c | 29 ++++++++++++++++++++++++++++- src/rewrite/m_tetromino.h | 1 + src/rewrite/tc_input.c | 8 +++++++- src/rewrite/tc_input.h | 3 ++- src/rewrite/tc_main.c | 3 ++- 7 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/rewrite/m_block.c b/src/rewrite/m_block.c index 24ad784..f4525f6 100644 --- a/src/rewrite/m_block.c +++ b/src/rewrite/m_block.c @@ -1,7 +1,6 @@ #include "m_block.h" #include -#include #include #include @@ -366,4 +365,36 @@ int M_B_Generate_Block_Id(void) { return rand(); } -int M_B_Can_Spawn_Blocks(void) { return _can_spawn_tetromino_flag; } \ No newline at end of file +int M_B_Can_Spawn_Blocks(void) { return _can_spawn_tetromino_flag; } + +/* TODO: make this update around point */ +block_t *M_B_Rotate_Block_Around_Point(block_t *block_to_rotate, + point_t rotation_point) { + point_offset_t offset, transposed_offset; + block_t *updated_block, + *origin_block = M_B_Get_Block_At_Point(rotation_point); + + offset.x_offset = block_to_rotate->point.x - rotation_point.x; + offset.y_offset = block_to_rotate->point.y - rotation_point.y; + + if (offset.x_offset > 0 || offset.x_offset < 0) { + transposed_offset.x_offset = offset.y_offset; + transposed_offset.y_offset = offset.x_offset * -1; + } else { + transposed_offset.x_offset = offset.y_offset; + transposed_offset.y_offset = offset.x_offset; + } + + printf("Offset: %d,%d\n", transposed_offset.x_offset, + transposed_offset.y_offset); + + /* This code is duplicated many places */ + updated_block = M_B_Get_Block_At_Offset(origin_block, transposed_offset); + updated_block->id = block_to_rotate->id; + M_B_Set_Block_Type(updated_block, block_to_rotate->type); + block_to_rotate->id = -1; + M_B_Set_Block_Type(block_to_rotate, bt_Empty); + printf("\nAddr %p != %p\n", updated_block, block_to_rotate); + + return updated_block; +} diff --git a/src/rewrite/m_block.h b/src/rewrite/m_block.h index 12b5367..b9e92c1 100644 --- a/src/rewrite/m_block.h +++ b/src/rewrite/m_block.h @@ -29,5 +29,7 @@ void M_B_On_Block_Spawn(void (*callback)(block_t **blocks)); int M_B_Point_Intersects_Static_Block(point_t point, int id); int M_B_Generate_Block_Id(void); int M_B_Can_Spawn_Blocks(void); +block_t *M_B_Rotate_Block_Around_Point(block_t *block_to_rotate, + point_t rotation_point); #endif /* M_BLOCK_H_ */ diff --git a/src/rewrite/m_tetromino.c b/src/rewrite/m_tetromino.c index f53b187..af9ace5 100644 --- a/src/rewrite/m_tetromino.c +++ b/src/rewrite/m_tetromino.c @@ -1,9 +1,11 @@ #include "m_tetromino.h" #include "m_block.h" #include +#include /* Blocks are gauranteed to be in order top left to bottom right */ block_t *_tetromino_blocks[BLOCKS_WITHIN_A_TETROMINO]; +point_t origin_point; int registered_flag = 0; void M_T_Update_Tetromino(void) { M_T_Tetromino_Fall(); } @@ -14,6 +16,7 @@ void M_T_Tetromino_Fall(void) { for (i = BLOCKS_WITHIN_A_TETROMINO - 1; i >= 0; i--) { _tetromino_blocks[i] = M_B_Move_Block_Down(_tetromino_blocks[i]); } + origin_point.y++; } } @@ -25,15 +28,16 @@ void M_T_Register_Falling_Blocks(block_t *blocks[BLOCKS_WITHIN_A_TETROMINO]) { for (i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) { _tetromino_blocks[i] = blocks[i]; } + origin_point = _tetromino_blocks[0]->point; } void M_T_Move_Tetromino_Left(void) { int i; - printf("test"); if (registered_flag && M_B_Can_Move_Blocks_Left(_tetromino_blocks)) { for (i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) { _tetromino_blocks[i] = M_B_Move_Block_Left(_tetromino_blocks[i]); } + origin_point.x--; } } @@ -43,5 +47,28 @@ void M_T_Move_Tetromino_Right(void) { for (i = BLOCKS_WITHIN_A_TETROMINO - 1; i >= 0; i--) { _tetromino_blocks[i] = M_B_Move_Block_Right(_tetromino_blocks[i]); } + origin_point.x++; } } + +int M_T_Block_Compare_Function(const void *a, const void *b) { + const block_t *block_a = a, *block_b = b; + return block_a < block_b; +} + +void M_T_Rotate_Tetromino(void) { + int i; + printf("\n\n"); + for (i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) { + printf("%d: %p\n", i, (_tetromino_blocks[i])); + } + if (registered_flag) { + for (i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) { + _tetromino_blocks[i] = + M_B_Rotate_Block_Around_Point(_tetromino_blocks[i], origin_point); + } + qsort(_tetromino_blocks, BLOCKS_WITHIN_A_TETROMINO, sizeof(block_t *), + M_T_Block_Compare_Function); + } + printf("\n\n"); +} diff --git a/src/rewrite/m_tetromino.h b/src/rewrite/m_tetromino.h index 58279bb..d6d83c0 100644 --- a/src/rewrite/m_tetromino.h +++ b/src/rewrite/m_tetromino.h @@ -8,5 +8,6 @@ void M_T_Tetromino_Fall(void); void M_T_Register_Falling_Blocks(block_t **blocks); void M_T_Move_Tetromino_Left(void); void M_T_Move_Tetromino_Right(void); +void M_T_Rotate_Tetromino(void); #endif /* M_TETROMINO_H_ */ diff --git a/src/rewrite/tc_input.c b/src/rewrite/tc_input.c index 26973bc..6222788 100644 --- a/src/rewrite/tc_input.c +++ b/src/rewrite/tc_input.c @@ -2,7 +2,7 @@ #include "m_tetromino.h" #include "raylib.h" -void TC_Process_Input(void) { +void TC_Process_Input_Per_Tick(void) { /* Was having issues with raylib input, wasn't having these issues before but I figured out that I can just cast to a char to get the result I want :) @@ -15,3 +15,9 @@ void TC_Process_Input(void) { M_T_Move_Tetromino_Right(); } } + +void TC_Process_Input_Per_Frame(void) { + if ((char)IsKeyPressed(KEY_SPACE)) { + M_T_Rotate_Tetromino(); + } +} diff --git a/src/rewrite/tc_input.h b/src/rewrite/tc_input.h index c899047..972ceb7 100644 --- a/src/rewrite/tc_input.h +++ b/src/rewrite/tc_input.h @@ -1,6 +1,7 @@ #ifndef TC_INPUT_H_ #define TC_INPUT_H_ -void TC_Process_Input(void); +void TC_Process_Input_Per_Tick(void); +void TC_Process_Input_Per_Frame(void); #endif /* TC_INPUT_H_ */ diff --git a/src/rewrite/tc_main.c b/src/rewrite/tc_main.c index e56561c..8bf436d 100644 --- a/src/rewrite/tc_main.c +++ b/src/rewrite/tc_main.c @@ -50,8 +50,9 @@ void TC_Game_Loop(void) { while (!TC_Close_Window()) { while (M_B_Can_Spawn_Blocks()) { R_Draw_Ui(); + TC_Process_Input_Per_Frame(); if (tick_rate == 0) { - TC_Process_Input(); + TC_Process_Input_Per_Tick(); M_T_Update_Tetromino(); M_B_Update_Blocks(); updated_blocks = M_B_Get_Blocks(&n_updated_blocks);