From 592d7ae8bcfcf59bb73f19ab71e901ed75a236f4 Mon Sep 17 00:00:00 2001 From: cellos51 Date: Tue, 5 Aug 2025 14:35:52 -0400 Subject: [PATCH] Add affine background color functionality and integrate into game loop --- graphics/affine_background_gfx.png | Bin 2983 -> 2959 bytes include/affine_background.h | 16 ++++++++ source/affine_background.c | 63 +++++++++++++++++++++++++++++ source/game.c | 19 ++------- source/main.c | 45 ++------------------- 5 files changed, 85 insertions(+), 58 deletions(-) create mode 100644 include/affine_background.h create mode 100644 source/affine_background.c diff --git a/graphics/affine_background_gfx.png b/graphics/affine_background_gfx.png index 7a567dc88ab88fa6a85a61877c8a4ab43c2946fd..4abff4b34d35f12f2434af43a1683347f104c73b 100644 GIT binary patch literal 2959 zcmajh4Nwzj8VB&jQ%O-U_N1tYinQWKJv#+N#JC3$BRYs6$bd?uy(20WXaVsO$XW@0 zE2Gw)UuCsaK|G)*h!uoY)Z?I%6%jDDB%)}5gl=#NNno>k@0;}QnBnL#!)*4=vmbf# z%m3NK8#joYon|^A2;v;Nc6B6zIDnT92yY_TZ2h7r5$>f$ih>N}T~gdW$J0Q!8$~>Z$*@ixhhp1CE=zPWWl4WEHQjWqgbKLz532Ul4j;2+ z@o?Iod1Vb^*SaJ%!$eze@AA&V2lRZk_D53Oj4uv0`>J9+v(9^?m>-sS@O!_8X3x)> z@^Io5>LYwsEj;d&K)E!L;)i&w=d2o0k4Igz%va7jv5$D2`b~}To)@#%{YVm37(MrK z8+An{IBWK;lrQbj-J1;Wd+-H$;IZ^cZ!YQ{tP5Q%XIrhRYTCp3vKy=4@a(#oVZaS` z;R2bk`#3r?n0iUmWxZrXSDUuLAVcRwXUSPtYe4z^?5bp6xvr}Dp;jwy!plv|sdlms ztCDZ-w^(A{YA3S-h01IqE^Gq_L^C_B`Sce=PC75+~AyUTeEYa_QJK z94AWABP8r7IA57A4_p=W6`V>pQ~+)%!-`JJ|Gza6;8IoWf>rQ)Xd%C}zIPGlRBX3J z3C@cZe{eRRVwfcJ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IS8wz$Zp_C8f_GHz5~pD4t=|xLe_7k(tl>*>*Ct?nBC0SQ<5!LM@Qt(o9 zqXD8p(;Z$SMQfrsB3$*>0D)b`QmwLsdwAZ=(QY&teWw34A1!Qhi_#A{dZL~+F(6Cp zrv1HHw7Ul2@=p-XVpCgjm^s-J3u=TZqib6&L!Xh7I9ce^q-0v?Yehe|(6<%|FPK48 z4>Hptn)EwnJyDh|q;lp0axNg}0y1$fApb`JNz>0Bj(i)eD_a8ogrUf-F=6$b1Nnad DhT-E# diff --git a/include/affine_background.h b/include/affine_background.h new file mode 100644 index 0000000..31440c0 --- /dev/null +++ b/include/affine_background.h @@ -0,0 +1,16 @@ +#ifndef AFFINE_BACKGROUND_H +#define AFFINE_BACKGROUND_H + +#include + +#include "graphic_utils.h" + +#define AFFINE_BG_PAL_LEN 5 +#define AFFINE_BG_PB (PAL_ROW_LEN * 15) // This isn't really a palette bank, just the starting index of the palette +#define AFFINE_BG_DEFAULT_COLOR 0x3E4B + +void affine_background_init(); +void affine_background_update(); +void affine_background_set_color(COLOR color); + +#endif // AFFINE_BACKGROUND_H \ No newline at end of file diff --git a/source/affine_background.c b/source/affine_background.c new file mode 100644 index 0000000..6752be2 --- /dev/null +++ b/source/affine_background.c @@ -0,0 +1,63 @@ +#include "affine_background.h" +#include "affine_background_gfx.h" + +#include "graphic_utils.h" + +BG_AFFINE bgaff; +AFF_SRC_EX asx = {32<<8, 64<<8, 120, 80, 0x0100, 0x0100, 0}; + +void affine_background_init() +{ + unsigned int correctedTiles[affine_background_gfxTilesLen / 4]; + + for (int i = 0; i < affine_background_gfxTilesLen / 4; i++) + { + correctedTiles[i] = affine_background_gfxTiles[i] | 0xF0F0F0F0; + } + + memcpy16(&tile8_mem[AFFINE_BG_CBB], correctedTiles, affine_background_gfxTilesLen/2); + GRIT_CPY(&se_mem[AFFINE_BG_SBB], affine_background_gfxMap); + memcpy16(&pal_bg_mem[AFFINE_BG_PB], affine_background_gfxPal, AFFINE_BG_PAL_LEN); + + bgaff = bg_aff_default; + + affine_background_set_color(AFFINE_BG_DEFAULT_COLOR); +} + +void affine_background_update() +{ + static uint timer = 0; + timer++; + + // These values are not permament, just the current configuration for the affine background + asx.tex_x += 5; + asx.tex_y += 12; + asx.sx = (lu_sin(timer * 100)) >> 8; // Scale the sine value to fit in a s16 + asx.sx += 256; // Add 256 to the sine value to make it positive + asx.sy = (lu_sin(timer * 100 + 0x4000)) >> 8; // Scale the sine value to fit in a s16 + asx.sy += 256; // Add 256 to the sine value to make it positive + bg_rotscale_ex(&bgaff, &asx); + REG_BG_AFFINE[2] = bgaff; +} + +void affine_background_set_color(COLOR color) +{ + memcpy16(&pal_bg_mem[AFFINE_BG_PB], affine_background_gfxPal, AFFINE_BG_PAL_LEN); + for (int i = 0; i < AFFINE_BG_PAL_LEN; i++) + { + COLOR old_color; + memcpy16(&old_color, &pal_bg_mem[AFFINE_BG_PB] + i, 1); // Copy the color from the background palette + + int r, g, b; + r = (old_color >> 10) & 0b11111; // Extract red component + g = (old_color >> 5) & 0b11111; // Extract green component + b = old_color & 0b11111; // Extract blue component + + int brightness = (r + g + b) / 3; // Calculate brightness + + const float brightness_divergence = 0.03f; // The difference in brightness between each color in the palette + const float brightness_modifier = -0.4f; // The overall brightness modifier for the palette + + clr_adj_brightness(&pal_bg_mem[AFFINE_BG_PB] + i, &color, 1, float2fx(brightness_modifier + (brightness * brightness_divergence))); // Adjust brightness for the palette colors + } +} \ No newline at end of file diff --git a/source/game.c b/source/game.c index e1e4e82..0f74e88 100644 --- a/source/game.c +++ b/source/game.c @@ -11,6 +11,7 @@ #include "hand_analysis.h" #include "blind.h" #include "joker.h" +#include "affine_background.h" #include "graphic_utils.h" #include "tonc_video.h" #include "audio_utils.h" @@ -430,22 +431,7 @@ void change_background(int id) { main_bg_se_copy_rect(BOSS_BLIND_TITLE_SRC_RECT, TOP_LEFT_BLIND_TITLE_POINT); - const int palette_length = 5; - memcpy16(&pal_bg_mem[16 * 15], affine_background_gfxPal, palette_length); - for (int i = 0; i < palette_length; i++) - { - COLOR old_color = 0; - memcpy16(&old_color, &pal_bg_mem[16 * 15] + i, 1); // Copy the color from the background palette - - int r, g, b; - r = (old_color >> 10) & 0b11111; // Extract red component - g = (old_color >> 5) & 0b11111; // Extract green component - b = old_color & 0b11111; // Extract blue component - - COLOR new_color = blind_get_color(BOSS_BLIND, BLIND_MAIN_COLOR_INDEX); - int brightness = (r + g + b) / 3; // Calculate brightness - clr_adj_brightness(&pal_bg_mem[16 * 15] + i, &new_color, 1, float2fx(-0.6f + (brightness * 0.05f))); // Adjust brightness for the palette colors - } + affine_background_set_color(blind_get_color(BOSS_BLIND, BLIND_SHADOW_COLOR_INDEX)); } bg_copy_current_item_to_top_left_panel(); @@ -2004,6 +1990,7 @@ void game_round_end() tte_erase_rect_wrapper(BLIND_REWARD_RECT); tte_erase_rect_wrapper(BLIND_REQ_TEXT_RECT); obj_hide(playing_blind_token->obj); + affine_background_set_color(AFFINE_BG_DEFAULT_COLOR); state = 5; timer = 0; } diff --git a/source/main.c b/source/main.c index 2cb29fc..76abc07 100644 --- a/source/main.c +++ b/source/main.c @@ -7,6 +7,7 @@ #include "game.h" #include "blind.h" #include "joker.h" +#include "affine_background.h" #include "graphic_utils.h" // Graphics @@ -17,29 +18,6 @@ #include "soundbank.h" #include "soundbank_bin.h" -#define MAP_AFF_SIZE 0x0100 -BG_AFFINE bgaff; -AFF_SRC_EX asx = {32<<8, 64<<8, 120, 80, 0x0100, 0x0100, 0}; - - -void init_map() // Temp function derived from libtonc example -{ - // Grit is fucking garbage and doesn't allow you to have a palette offset, they all get loaded at 0x0000. I have to manually change each tile's palette bank - - unsigned int correctedTiles[affine_background_gfxTilesLen / 4]; - for (int i = 0; i < affine_background_gfxTilesLen / 4; i++) - { - correctedTiles[i] = affine_background_gfxTiles[i] | 0xF0F0F0F0; // I brute forced this. I have no idea how this would and does work, but it does. We'll just leave this here for now. - // Actually for future reference, each number seems to equate to an XY on the palette, so 0xF0F0F0F0 would just push everything down 15 colors, since the second number isn't set. - } - - memcpy16(&tile8_mem[AFFINE_BG_CBB], correctedTiles, affine_background_gfxTilesLen); - GRIT_CPY(&se_mem[AFFINE_BG_SBB], affine_background_gfxMap); - memcpy16(&pal_bg_mem[16 * 15], affine_background_gfxPal, 5); - - bgaff = bg_aff_default; -} - void init() { irq_init(NULL); @@ -93,9 +71,8 @@ void init() REG_DISPCNT = DCNT_MODE1 | DCNT_OBJ_1D | DCNT_BG0 | DCNT_BG1 | DCNT_BG2 | DCNT_OBJ | DCNT_WIN0 | DCNT_WIN1; - init_map(); // Initialize the map for the background - // Initialize subsystems + affine_background_init(); sprite_init(); card_init(); blind_init(); @@ -103,24 +80,9 @@ void init() game_init(); } -void background_update() // This needs to be called before update() and draw() because setting REG_BG_AFFINE during an HBlank can cause issues -{ - static uint timer = 0; - timer++; - - // These values are not permament, just the current configuration for the affine background - asx.tex_x += 5; - asx.tex_y += 12; - asx.sx = (lu_sin(timer * 100)) >> 8; // Scale the sine value to fit in a s16 - asx.sx += 256; // Add 256 to the sine value to make it positive - asx.sy = (lu_sin(timer * 100 + 0x4000)) >> 8; // Scale the sine value to fit in a s16 - asx.sy += 256; // Add 256 to the sine value to make it positive - bg_rotscale_ex(&bgaff, &asx); - REG_BG_AFFINE[2]= bgaff; -} - void update() { + affine_background_update(); game_update(); } @@ -138,7 +100,6 @@ int main() VBlankIntrWait(); mmFrame(); key_poll(); - background_update(); update(); draw(); }