Add affine background color functionality and integrate into game loop

This commit is contained in:
cellos51
2025-08-05 14:35:52 -04:00
parent 46b53db8a0
commit 592d7ae8bc
5 changed files with 85 additions and 58 deletions
+63
View File
@@ -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
}
}
+3 -16
View File
@@ -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;
}
+3 -42
View File
@@ -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();
}