Minor window/bg cleanup

This commit is contained in:
GriffinR 2021-02-18 10:00:07 -05:00
parent 02c1fbd733
commit d62f89b20c
6 changed files with 56 additions and 54 deletions

View File

@ -1,5 +1,5 @@
filler_03002F58 gUnusedWindowVar1
filler_03002F5C gUnusedWindowVar2
gTransparentTileNumber gTransparentTileNumber
filler_03002F64 gUnusedWindowVar3
gUnknown_03002F70 gWindowBgTilemapBuffers

View File

@ -22,7 +22,7 @@ struct BgControl
u8 unknown_2; u8 unknown_2;
u8 unknown_3; u8 unknown_3;
} configs[4]; } configs[NUM_BACKGROUNDS];
u16 bgVisibilityAndMode; u16 bgVisibilityAndMode;
}; };
@ -39,8 +39,8 @@ struct BgConfig2
}; };
static struct BgControl sGpuBgConfigs; static struct BgControl sGpuBgConfigs;
static struct BgConfig2 sGpuBgConfigs2[4]; static struct BgConfig2 sGpuBgConfigs2[NUM_BACKGROUNDS];
static u32 sDmaBusyBitfield[4]; static u32 sDmaBusyBitfield[NUM_BACKGROUNDS];
u32 gUnneededFireRedVariable; u32 gUnneededFireRedVariable;
@ -70,7 +70,7 @@ void ResetBgControlStructs(void)
struct BgConfig zeroedConfig = sZeroedBgControlStruct; struct BgConfig zeroedConfig = sZeroedBgControlStruct;
int i; int i;
for (i = 0; i < 4; i++) for (i = 0; i < NUM_BACKGROUNDS; i++)
{ {
bgConfigs[i] = zeroedConfig; bgConfigs[i] = zeroedConfig;
} }
@ -259,7 +259,7 @@ static void SetBgAffineInternal(u8 bg, s32 srcCenterX, s32 srcCenterY, s16 dispC
return; return;
break; break;
case 2: case 2:
if (bg < 2 || bg > 3) if (bg < 2 || bg >= NUM_BACKGROUNDS)
return; return;
break; break;
case 0: case 0:
@ -290,7 +290,7 @@ static void SetBgAffineInternal(u8 bg, s32 srcCenterX, s32 srcCenterY, s16 dispC
bool8 IsInvalidBg(u8 bg) bool8 IsInvalidBg(u8 bg)
{ {
if (bg > 3) if (bg >= NUM_BACKGROUNDS)
return TRUE; return TRUE;
else else
return FALSE; return FALSE;
@ -306,7 +306,7 @@ void ResetBgsAndClearDma3BusyFlags(u32 leftoverFireRedLeafGreenVariable)
int i; int i;
ResetBgs(); ResetBgs();
for (i = 0; i < 4; i++) for (i = 0; i < NUM_BACKGROUNDS; i++)
{ {
sDmaBusyBitfield[i] = 0; sDmaBusyBitfield[i] = 0;
} }
@ -325,7 +325,7 @@ void InitBgsFromTemplates(u8 bgMode, const struct BgTemplate *templates, u8 numT
for (i = 0; i < numTemplates; i++) for (i = 0; i < numTemplates; i++)
{ {
bg = templates[i].bg; bg = templates[i].bg;
if (bg < 4) if (bg < NUM_BACKGROUNDS)
{ {
SetBgControlAttributes(bg, SetBgControlAttributes(bg,
templates[i].charBaseIndex, templates[i].charBaseIndex,
@ -351,7 +351,7 @@ void InitBgFromTemplate(const struct BgTemplate *template)
{ {
u8 bg = template->bg; u8 bg = template->bg;
if (bg < 4) if (bg < NUM_BACKGROUNDS)
{ {
SetBgControlAttributes(bg, SetBgControlAttributes(bg,
template->charBaseIndex, template->charBaseIndex,
@ -1239,7 +1239,7 @@ u32 GetBgType(u8 bg)
bool32 IsInvalidBg32(u8 bg) bool32 IsInvalidBg32(u8 bg)
{ {
if (bg > 3) if (bg >= NUM_BACKGROUNDS)
return TRUE; return TRUE;
else else
return FALSE; return FALSE;

View File

@ -4,12 +4,12 @@
#include "bg.h" #include "bg.h"
#include "blit.h" #include "blit.h"
u32 filler_03002F58; u32 gUnusedWindowVar1;
u32 filler_03002F5C; u32 gUnusedWindowVar2;
// This global is set to 0 and never changed. // This global is set to 0 and never changed.
u8 gTransparentTileNumber; u8 gTransparentTileNumber;
u32 filler_03002F64; u32 gUnusedWindowVar3;
void *gUnknown_03002F70[4]; void *gWindowBgTilemapBuffers[NUM_BACKGROUNDS];
extern u32 gUnneededFireRedVariable; extern u32 gUnneededFireRedVariable;
#define WINDOWS_MAX 32 #define WINDOWS_MAX 32
@ -38,22 +38,22 @@ bool16 InitWindows(const struct WindowTemplate *templates)
u8* allocatedTilemapBuffer; u8* allocatedTilemapBuffer;
int allocatedBaseBlock; int allocatedBaseBlock;
for (i = 0; i < 0x4; ++i) for (i = 0; i < NUM_BACKGROUNDS; ++i)
{ {
bgTilemapBuffer = GetBgTilemapBuffer(i); bgTilemapBuffer = GetBgTilemapBuffer(i);
if (bgTilemapBuffer != NULL) if (bgTilemapBuffer != NULL)
gUnknown_03002F70[i] = nullsub_8; gWindowBgTilemapBuffers[i] = nullsub_8;
else else
gUnknown_03002F70[i] = bgTilemapBuffer; gWindowBgTilemapBuffers[i] = bgTilemapBuffer;
} }
for (i = 0; i < 0x20; ++i) for (i = 0; i < WINDOWS_MAX; ++i)
{ {
gWindows[i].window = sDummyWindowTemplate; gWindows[i].window = sDummyWindowTemplate;
gWindows[i].tileData = NULL; gWindows[i].tileData = NULL;
} }
for (i = 0, allocatedBaseBlock = 0, bgLayer = templates[i].bg; bgLayer != 0xFF && i < 0x20; ++i, bgLayer = templates[i].bg) for (i = 0, allocatedBaseBlock = 0, bgLayer = templates[i].bg; bgLayer != 0xFF && i < WINDOWS_MAX; ++i, bgLayer = templates[i].bg)
{ {
if (gUnneededFireRedVariable == 1) if (gUnneededFireRedVariable == 1)
{ {
@ -62,7 +62,7 @@ bool16 InitWindows(const struct WindowTemplate *templates)
return FALSE; return FALSE;
} }
if (gUnknown_03002F70[bgLayer] == NULL) if (gWindowBgTilemapBuffers[bgLayer] == NULL)
{ {
attrib = GetBgAttribute(bgLayer, BG_ATTR_METRIC); attrib = GetBgAttribute(bgLayer, BG_ATTR_METRIC);
@ -79,19 +79,19 @@ bool16 InitWindows(const struct WindowTemplate *templates)
for (j = 0; j < attrib; ++j) for (j = 0; j < attrib; ++j)
allocatedTilemapBuffer[j] = 0; allocatedTilemapBuffer[j] = 0;
gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; gWindowBgTilemapBuffers[bgLayer] = allocatedTilemapBuffer;
SetBgTilemapBuffer(bgLayer, allocatedTilemapBuffer); SetBgTilemapBuffer(bgLayer, allocatedTilemapBuffer);
} }
} }
allocatedTilemapBuffer = AllocZeroed((u16)(0x20 * (templates[i].width * templates[i].height))); allocatedTilemapBuffer = AllocZeroed((u16)(WINDOWS_MAX * (templates[i].width * templates[i].height)));
if (allocatedTilemapBuffer == NULL) if (allocatedTilemapBuffer == NULL)
{ {
if ((GetNumActiveWindowsOnBg(bgLayer) == 0) && (gUnknown_03002F70[bgLayer] != nullsub_8)) if ((GetNumActiveWindowsOnBg(bgLayer) == 0) && (gWindowBgTilemapBuffers[bgLayer] != nullsub_8))
{ {
Free(gUnknown_03002F70[bgLayer]); Free(gWindowBgTilemapBuffers[bgLayer]);
gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; gWindowBgTilemapBuffers[bgLayer] = allocatedTilemapBuffer;
} }
return FALSE; return FALSE;
@ -140,7 +140,7 @@ u16 AddWindow(const struct WindowTemplate *template)
return 0xFF; return 0xFF;
} }
if (gUnknown_03002F70[bgLayer] == NULL) if (gWindowBgTilemapBuffers[bgLayer] == NULL)
{ {
attrib = GetBgAttribute(bgLayer, BG_ATTR_METRIC); attrib = GetBgAttribute(bgLayer, BG_ATTR_METRIC);
@ -154,19 +154,19 @@ u16 AddWindow(const struct WindowTemplate *template)
for (i = 0; i < attrib; ++i) for (i = 0; i < attrib; ++i)
allocatedTilemapBuffer[i] = 0; allocatedTilemapBuffer[i] = 0;
gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; gWindowBgTilemapBuffers[bgLayer] = allocatedTilemapBuffer;
SetBgTilemapBuffer(bgLayer, allocatedTilemapBuffer); SetBgTilemapBuffer(bgLayer, allocatedTilemapBuffer);
} }
} }
allocatedTilemapBuffer = AllocZeroed((u16)(0x20 * (template->width * template->height))); allocatedTilemapBuffer = AllocZeroed((u16)(WINDOWS_MAX * (template->width * template->height)));
if (allocatedTilemapBuffer == NULL) if (allocatedTilemapBuffer == NULL)
{ {
if ((GetNumActiveWindowsOnBg(bgLayer) == 0) && (gUnknown_03002F70[bgLayer] != nullsub_8)) if ((GetNumActiveWindowsOnBg(bgLayer) == 0) && (gWindowBgTilemapBuffers[bgLayer] != nullsub_8))
{ {
Free(gUnknown_03002F70[bgLayer]); Free(gWindowBgTilemapBuffers[bgLayer]);
gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; gWindowBgTilemapBuffers[bgLayer] = allocatedTilemapBuffer;
} }
return 0xFF; return 0xFF;
} }
@ -233,10 +233,10 @@ void RemoveWindow(u8 windowId)
if (GetNumActiveWindowsOnBg(bgLayer) == 0) if (GetNumActiveWindowsOnBg(bgLayer) == 0)
{ {
if (gUnknown_03002F70[bgLayer] != nullsub_8) if (gWindowBgTilemapBuffers[bgLayer] != nullsub_8)
{ {
Free(gUnknown_03002F70[bgLayer]); Free(gWindowBgTilemapBuffers[bgLayer]);
gUnknown_03002F70[bgLayer] = 0; gWindowBgTilemapBuffers[bgLayer] = 0;
} }
} }
@ -251,16 +251,16 @@ void FreeAllWindowBuffers(void)
{ {
int i; int i;
for (i = 0; i < 4; ++i) for (i = 0; i < NUM_BACKGROUNDS; ++i)
{ {
if (gUnknown_03002F70[i] != NULL && gUnknown_03002F70[i] != nullsub_8) if (gWindowBgTilemapBuffers[i] != NULL && gWindowBgTilemapBuffers[i] != nullsub_8)
{ {
Free(gUnknown_03002F70[i]); Free(gWindowBgTilemapBuffers[i]);
gUnknown_03002F70[i] = NULL; gWindowBgTilemapBuffers[i] = NULL;
} }
} }
for (i = 0; i < 0x20; ++i) for (i = 0; i < WINDOWS_MAX; ++i)
{ {
if (gWindows[i].tileData != NULL) if (gWindows[i].tileData != NULL)
{ {
@ -610,7 +610,7 @@ u16 AddWindow8Bit(const struct WindowTemplate *template)
u8* memAddress; u8* memAddress;
u8 bgLayer; u8 bgLayer;
for (windowId = 0; windowId < 32; windowId++) for (windowId = 0; windowId < WINDOWS_MAX; windowId++)
{ {
if (gWindows[windowId].window.bg == 0xFF) if (gWindows[windowId].window.bg == 0xFF)
break; break;
@ -618,7 +618,7 @@ u16 AddWindow8Bit(const struct WindowTemplate *template)
if (windowId == WINDOWS_MAX) if (windowId == WINDOWS_MAX)
return 0xFF; return 0xFF;
bgLayer = template->bg; bgLayer = template->bg;
if (gUnknown_03002F70[bgLayer] == 0) if (gWindowBgTilemapBuffers[bgLayer] == 0)
{ {
u16 attribute = GetBgAttribute(bgLayer, BG_ATTR_METRIC); u16 attribute = GetBgAttribute(bgLayer, BG_ATTR_METRIC);
if (attribute != 0xFFFF) if (attribute != 0xFFFF)
@ -629,17 +629,17 @@ u16 AddWindow8Bit(const struct WindowTemplate *template)
return 0xFF; return 0xFF;
for (i = 0; i < attribute; i++) // if we're going to zero out the memory anyway, why not call AllocZeroed? for (i = 0; i < attribute; i++) // if we're going to zero out the memory anyway, why not call AllocZeroed?
memAddress[i] = 0; memAddress[i] = 0;
gUnknown_03002F70[bgLayer] = memAddress; gWindowBgTilemapBuffers[bgLayer] = memAddress;
SetBgTilemapBuffer(bgLayer, memAddress); SetBgTilemapBuffer(bgLayer, memAddress);
} }
} }
memAddress = Alloc((u16)(0x40 * (template->width * template->height))); memAddress = Alloc((u16)(0x40 * (template->width * template->height)));
if (memAddress == NULL) if (memAddress == NULL)
{ {
if (GetNumActiveWindowsOnBg8Bit(bgLayer) == 0 && gUnknown_03002F70[bgLayer] != nullsub_9) if (GetNumActiveWindowsOnBg8Bit(bgLayer) == 0 && gWindowBgTilemapBuffers[bgLayer] != nullsub_9)
{ {
Free(gUnknown_03002F70[bgLayer]); Free(gWindowBgTilemapBuffers[bgLayer]);
gUnknown_03002F70[bgLayer] = NULL; gWindowBgTilemapBuffers[bgLayer] = NULL;
} }
return 0xFF; return 0xFF;
} }

View File

@ -70,9 +70,9 @@ void BlitBitmapRectToWindow4BitTo8Bit(u8 windowId, const u8 *pixels, u16 srcX, u
void CopyWindowToVram8Bit(u8 windowId, u8 mode); void CopyWindowToVram8Bit(u8 windowId, u8 mode);
extern struct Window gWindows[]; extern struct Window gWindows[];
extern void* gUnknown_03002F70[]; extern void* gWindowBgTilemapBuffers[];
extern u32 filler_03002F58; extern u32 gUnusedWindowVar1;
extern u32 filler_03002F5C; extern u32 gUnusedWindowVar2;
extern u32 filler_03002F64; extern u32 gUnusedWindowVar3;
#endif // GUARD_WINDOW_H #endif // GUARD_WINDOW_H

View File

@ -50,6 +50,8 @@
#define BG_TILE_H_FLIP(n) (0x400 + (n)) #define BG_TILE_H_FLIP(n) (0x400 + (n))
#define BG_TILE_V_FLIP(n) (0x800 + (n)) #define BG_TILE_V_FLIP(n) (0x800 + (n))
#define NUM_BACKGROUNDS 4
// text-mode BG // text-mode BG
#define OBJ_VRAM0 (VRAM + 0x10000) #define OBJ_VRAM0 (VRAM + 0x10000)
#define OBJ_VRAM0_SIZE 0x8000 #define OBJ_VRAM0_SIZE 0x8000

View File

@ -9,7 +9,7 @@ gUnneededFireRedVariable:
gTransparentTileNumber: gTransparentTileNumber:
.space 1 .space 1
.align 4 .align 4
gUnknown_03002F70: gWindowBgTilemapBuffers:
.space 16 .space 16
@ ../gflib/text.o @ ../gflib/text.o
.align 4 .align 4