From b822d2b36554a07875847693ead409386c04544e Mon Sep 17 00:00:00 2001 From: MeirGavish Date: Wed, 9 Jul 2025 04:27:10 +0300 Subject: [PATCH] Moved code to new graphic_utils.c/.h header and added Rect wrapper struct --- include/game.h | 19 ------------------- include/graphic_utils.h | 36 ++++++++++++++++++++++++++++++++++++ source/game.c | 19 ++++++------------- source/graphic_utils.c | 15 +++++++++++++++ source/main.c | 1 + 5 files changed, 58 insertions(+), 32 deletions(-) create mode 100644 include/graphic_utils.h create mode 100644 source/graphic_utils.c diff --git a/include/game.h b/include/game.h index fc752e5..7627bc8 100644 --- a/include/game.h +++ b/include/game.h @@ -1,25 +1,6 @@ #ifndef GAME_H #define GAME_H -// TODO: Move to dedicated header? -/* Reminder: - * Screen Base Block is the base for the screenblock entries i.e. tilemap - * Character Base Block is the base for the tiles themselves - */ -#define MAIN_BG_SBB 31 -#define MAIN_BG_CBB 1 -#define TTE_SBB 30 -#define TTE_CBB 0 -#define AFFINE_BG_SBB 2 -#define AFFINE_BG_CBB 2 - -#define SCREENBLOCK_ROW_LEN 32 - -#define ROUND_END_MENU_BOTTOM_SE 7 -#define ROUND_END_MENU_TOP_SE 20 -#define ROUND_END_MENU_LEFT_SE 9 -#define ROUND_END_MENU_WIDTH_SE 16 - #define MAX_HAND_SIZE 16 #define MAX_DECK_SIZE 52 #define MAX_SELECTION_SIZE 5 diff --git a/include/graphic_utils.h b/include/graphic_utils.h new file mode 100644 index 0000000..880865e --- /dev/null +++ b/include/graphic_utils.h @@ -0,0 +1,36 @@ +#ifndef GRAPHIC_UTILS_H +#define GRAPHIC_UTILS_H + +/* Reminder: + * Screen Base Block is the base for the screenblock entries i.e. tilemap + * Character Base Block is the base for the tiles themselves + */ +#define MAIN_BG_SBB 31 +#define MAIN_BG_CBB 1 +#define TTE_SBB 30 +#define TTE_CBB 0 +#define AFFINE_BG_SBB 2 +#define AFFINE_BG_CBB 2 + +/* A 1024 size screenblock is arranged in a grid of 32x32 block entries + * Interestingly since each block is 8x8 pixels, the 240x160 GBA screen + * is smaller than the screenblock, only the top left part of the screenblock + * is displayed on the screen. + */ +#define SCREENBLOCK_ROW_LEN 32 + +typedef struct +{ + int left; + int top; + int right; + int bottom; +} Rect; + +/* Clears a rect in the main background screenblock. + * The rect needs to be in screenblocks + */ +void main_bg_se_clear_rect(Rect rect); + + +#endif //GRAPHIC_UTILS_H \ No newline at end of file diff --git a/source/game.c b/source/game.c index 525ec95..51ac3db 100644 --- a/source/game.c +++ b/source/game.c @@ -9,6 +9,7 @@ #include "sprite.h" #include "card.h" #include "blind.h" +#include "graphic_utils.h" #include "background_gfx.h" #include "background_play_gfx.h" @@ -105,6 +106,10 @@ static inline Card *discard_pop() return discard_pile[discard_top--]; } + +// Consts +static const Rect ROUND_END_MENU_RECT = { 9, 7, 25, 21 }; + // General functions void sort_hand_by_suit() { @@ -295,18 +300,6 @@ enum HandType hand_get_type() return hand_type; } -// TODO: Add function to header with documentation -void main_bg_se_clear_rect(int bottom, int top, int left, int width) -{ - for (int y = bottom; y <= top; y++) - { - for (int x = 0; x < width; x++) - { - memset16(&se_mem[MAIN_BG_SBB][left + x + SCREENBLOCK_ROW_LEN*y], 0x000, 1); - } - } -} - void change_background(int id) { if (background == id) @@ -337,7 +330,7 @@ void change_background(int id) // Incoming hack! Clear the round end menu so that we can slowly display it with an animation later. The reason this isn't optimal is because the full background is already loaded into the vram at this point. // I'm just doing it this way because it's easier than doing some weird shit with Grit in order to get a proper tilemap. I'm not the biggest fan of Grit. - main_bg_se_clear_rect(ROUND_END_MENU_BOTTOM_SE, ROUND_END_MENU_TOP_SE, ROUND_END_MENU_LEFT_SE, ROUND_END_MENU_WIDTH_SE); + main_bg_se_clear_rect(ROUND_END_MENU_RECT); //tte_erase_rect(0, 0, 64, 48); // Clear top left corner where the blind stats are displayed tte_erase_rect(128, 128, 152, 160); // Clear the hand size/max size display diff --git a/source/graphic_utils.c b/source/graphic_utils.c new file mode 100644 index 0000000..aed72be --- /dev/null +++ b/source/graphic_utils.c @@ -0,0 +1,15 @@ +#include +#include + +#include "graphic_utils.h" + +void main_bg_se_clear_rect(Rect rect) +{ + for (int y = rect.top; y < rect.bottom; y++) + { + for (int x = rect.left; x < rect.right; x++) + { + memset16(&se_mem[MAIN_BG_SBB][x + SCREENBLOCK_ROW_LEN * y], 0x000, 1); + } + } +} diff --git a/source/main.c b/source/main.c index 957b2be..9cb1699 100644 --- a/source/main.c +++ b/source/main.c @@ -6,6 +6,7 @@ #include "card.h" #include "game.h" #include "blind.h" +#include "graphic_utils.h" // Graphics #include "deck_gfx.h"