diff --git a/include/affine_background.h b/include/affine_background.h new file mode 100644 index 0000000..8e5f78f --- /dev/null +++ b/include/affine_background.h @@ -0,0 +1,17 @@ +#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 + +void affine_background_init(); +void affine_background_update(); +void affine_background_set_color(COLOR color); +// Must be called with an array of size at least AFFINE_BG_PAL_LEN +void affine_background_load_palette(const u16 *src); + +#endif // AFFINE_BACKGROUND_H diff --git a/include/graphic_utils.h b/include/graphic_utils.h index 2791791..8f75cf3 100644 --- a/include/graphic_utils.h +++ b/include/graphic_utils.h @@ -120,4 +120,22 @@ void tte_erase_rect_wrapper(Rect rect); */ void update_text_rect_to_right_align_num(Rect* rect, int num, int overflow_direction); -#endif //GRAPHIC_UTILS_H \ No newline at end of file +/*Copies 16 bit data from src to dst, applying a palette offset to the data. + * This is intended solely for use with tile8/8bpp data for dst and src. + * The palette offset allows the tiles to use a different location in the palette + * memory + * This is useful because grit always loads the palette to the beginning of + * pal_bg_mem[] + */ +void memcpy16_tile8_with_palette_offset(u16* dst, const u16* src, uint hwcount, u8 palette_offset); + +/*Copies 32 bit data from src to dst, applying a palette offset to the data. + * This is intended solely for use with tile8/8bpp data for dst and src. + * The palette offset allows the tiles to use a different location in the palette + * memory + * This is useful because grit always loads the palette to the beginning of + * pal_bg_mem[] + */ +void memcpy32_tile8_with_palette_offset(u32* dst, const u32* src, uint hwcount, u8 palette_offset); + +#endif //GRAPHIC_UTILS_H diff --git a/source/affine_background.c b/source/affine_background.c new file mode 100644 index 0000000..f9e15e7 --- /dev/null +++ b/source/affine_background.c @@ -0,0 +1,46 @@ +#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() +{ + memcpy32_tile8_with_palette_offset((u32*)&tile8_mem[AFFINE_BG_CBB], (const u32*)affine_background_gfxTiles, affine_background_gfxTilesLen/4, AFFINE_BG_PB); + GRIT_CPY(&se_mem[AFFINE_BG_SBB], affine_background_gfxMap); + affine_background_load_palette(affine_background_gfxPal); + + bgaff = bg_aff_default; +} + +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++) + { + clr_rgbscale(&pal_bg_mem[AFFINE_BG_PB] + i, &pal_bg_mem[AFFINE_BG_PB] + i, 1, color); + } +} + +void affine_background_load_palette(const u16 *src) +{ + memcpy16(&pal_bg_mem[AFFINE_BG_PB], src, AFFINE_BG_PAL_LEN); +} \ No newline at end of file diff --git a/source/game.c b/source/game.c index 8acd864..05f7a94 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" @@ -18,6 +19,7 @@ #include "background_gfx.h" #include "background_shop_gfx.h" #include "background_blind_select_gfx.h" +#include "affine_background_gfx.h" #include "soundbank.h" @@ -437,6 +439,8 @@ void change_background(int id) else if (current_blind == BOSS_BLIND) { main_bg_se_copy_rect(BOSS_BLIND_TITLE_SRC_RECT, TOP_LEFT_BLIND_TITLE_POINT); + + affine_background_set_color(blind_get_color(BOSS_BLIND, BLIND_SHADOW_COLOR_INDEX)); } bg_copy_current_item_to_top_left_panel(); @@ -1990,6 +1994,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_load_palette(affine_background_gfxPal); state = 5; timer = 0; } diff --git a/source/graphic_utils.c b/source/graphic_utils.c index 5311711..1d5bcb2 100644 --- a/source/graphic_utils.c +++ b/source/graphic_utils.c @@ -168,3 +168,22 @@ void update_text_rect_to_right_align_num(Rect* rect, int num, int overflow_direc } } +void memcpy16_tile8_with_palette_offset(u16* dst, const u16* src, uint hwcount, u8 palette_offset) +{ + const u16 offset = (((palette_offset) << 8) | (palette_offset)); + for (int i = 0; i < hwcount; i++) + { + // Copying u8 data twice across u16 data + dst[i] = src[i] + offset; + } +} + +void memcpy32_tile8_with_palette_offset(u32* dst, const u32* src, uint hwcount, u8 palette_offset) +{ + const u32 offset = (palette_offset << 24) | (palette_offset << 16) | (palette_offset << 8) | palette_offset; + for (int i = 0; i < hwcount; i++) + { + // Copying u8 data 4 times across u32 data + dst[i] = src[i] + offset; + } +} \ No newline at end of file diff --git a/source/main.c b/source/main.c index bd7b046..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. - } - - memcpy(&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(); }