Skip to content

Commit

Permalink
Document gflib and libmagb other than save.c
Browse files Browse the repository at this point in the history
  • Loading branch information
citrusbolt committed Feb 20, 2024
1 parent 3a7f51b commit b114559
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 67 deletions.
14 changes: 7 additions & 7 deletions payload/include/gflib/bg.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ extern bool8 gBgTilemapBufferTransferScheduled[];

void ResetGpuBuffers(void);
void DoGpuUpdateAndTilemapTransfers(void);
void CopyToBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, const u16 * src);
void CopyFromBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, u16 * dest);
void SetBgTilemapBufferPaletteRect(int bgNum, int left, int top, int width, int height, int paletteNum);
void SetBgTilemapBufferTileAt(int bgNum, int x, int y, u16 tileNum);
void FillBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, u16 tileNum);
void CopyRectWithinBgTilemapBuffer(int bgNum, int srcLeft, int srcTop, int width, int height, int destLeft, int destTop);
void SetBgPos(int bgNum, s32 x, s32 y);
void CopyToBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, const u16 * src);
void CopyFromBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u16 * dest);
void SetBgTilemapBufferPaletteRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u32 paletteNum);
void SetBgTilemapBufferTileAt(u32 bgNum, u32 x, u32 y, u16 tileNum);
void FillBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u16 tileNum);
void CopyRectWithinBgTilemapBuffer(u32 bgNum, u32 srcLeft, u32 srcTop, s32 width, u32 height, u32 destLeft, u32 destTop);
void SetBgPos(u32 bgNum, s32 x, s32 y);

#endif //GFLIB_BG_H
4 changes: 2 additions & 2 deletions payload/include/gflib/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
void AgbMain(void);
extern IntrFunc gIntrTable[14u];

void SetIntrFunc(int i, IntrFunc func);
void SetIntrFunc(u32 i, IntrFunc func);
void SetVBlankCallback(IntrFunc cb);
u32 GetFrameTotal(void);
void DelayFrames(u32 a0);
void DelayFrames(u32 frames);

#endif //GFLIB_INIT_H
12 changes: 6 additions & 6 deletions payload/include/gflib/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ void ResetSprites(void);
void UpdateSprites(void);
void DoOamBufferTransfer(void);
struct Sprite * AddSprite(s32 x, s32 y, const struct Subsprites * subsprites);
void MoveSpriteToHead(struct Sprite * a0);
void SetSpritePos(struct Sprite * r0, s32 r1, s32 r2);
void AddSpritePos(struct Sprite * r0, s32 r1, s32 r2);
void SetSpritePaletteNum(struct Sprite * r0, s32 r1);
void SetSpriteTileOffset(struct Sprite * r0, s32 r1);
void SetSpriteInvisible(struct Sprite * r0, s32 r1);
void MoveSpriteToHead(struct Sprite * sprite);
void SetSpritePos(struct Sprite * sprite, s32 x, s32 y);
void AddSpritePos(struct Sprite * sprite, s32 x, s32 y);
void SetSpritePaletteNum(struct Sprite * sprite, s32 paletteNum);
void SetSpriteTileOffset(struct Sprite * sprite, s32 tileOffset);
void SetSpriteInvisible(struct Sprite * sprite, s32 invisible);

#endif //GFLIB_SPRITE_H
2 changes: 1 addition & 1 deletion payload/include/gflib/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void DrawGlyphOnWindow_NoShadow(struct Window *window, u32 glyphId, u32 fgColor)
void DrawGlyphOnWindow_WithShadow(struct Window *window, u32 glyphId, u32 fgColor, u32 shadowColor);
struct Window *AddWindow(u32 windowId, const struct Window *template);
void GenerateFontHalfrowLookupTable(u32 * buffer);
void ClearWindowCharBuffer(struct Window * window, u32 a1);
void ClearWindowCharBuffer(struct Window * window, u32 tile);
void FillWindowCharBufferRect(struct Window * window, s32 x, s32 y, s32 width, s32 height, u32 fillValue);
void SetTextColor(struct Window * window, u8 fg, u8 shadow);
void TextWindowNextLine(struct Window * window);
Expand Down
2 changes: 1 addition & 1 deletion payload/include/libpmagb/berry.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ u32 GetEnigmaBerryChecksum(struct EnigmaBerry * enigmaBerry);
bool32 IsEnigmaBerryValid(void);
const struct Berry *GetBerryInfo(u8 berry);
const u8 *ItemId_GetName(u16 itemId);
void CopyItemName(u32 itemId, u8 *string, const u8 * berry_str);
void CopyItemName(u32 itemId, u8 *string, const u8 * berryString);

#endif //PMAGB_BERRY_H
35 changes: 18 additions & 17 deletions payload/src/gflib/bg.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "global.h"
#include "gflib/bg.h"

u16 gBgHofsBuffer[4];
u16 gBgVofsBuffer[4];
bool8 gBgTilemapBufferTransferScheduled[4];
u16 gBgHofsBuffer[NUM_BACKGROUNDS];
u16 gBgVofsBuffer[NUM_BACKGROUNDS];
bool8 gBgTilemapBufferTransferScheduled[NUM_BACKGROUNDS];
u32 filler1;
u32 filler2;
u32 filler3;
Expand All @@ -13,8 +13,9 @@ void DoGpuUpdateAndTilemapTransfers(void)
s32 i;
u16 bgCnt;
void * screenBase;
size_t size;
u32 size;
s32 r2;

REG_BG0HOFS = gBgHofsBuffer[0];
REG_BG1HOFS = gBgHofsBuffer[1];
REG_BG2HOFS = gBgHofsBuffer[2];
Expand All @@ -23,7 +24,7 @@ void DoGpuUpdateAndTilemapTransfers(void)
REG_BG1VOFS = gBgVofsBuffer[1];
REG_BG2VOFS = gBgVofsBuffer[2];
REG_BG3VOFS = gBgVofsBuffer[3];
for (i = 0; i < 4; i++)
for (i = 0; i < NUM_BACKGROUNDS; i++)
{
if (gBgTilemapBufferTransferScheduled[i])
{
Expand All @@ -47,9 +48,9 @@ void ResetGpuBuffers(void)
gBgTilemapBufferTransferScheduled[0] = 0;
}

void CopyToBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, const u16 * src)
void CopyToBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, const u16 * src)
{
int x;
s32 x;
u16 * ptr = (void *)(gBGTilemapBuffers + (bgNum << 12) + (top << 6) + (left << 1));

while (height)
Expand All @@ -63,9 +64,9 @@ void CopyToBgTilemapBufferRect(int bgNum, int left, int top, int width, int heig
}
}

void CopyFromBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, u16 * dest)
void CopyFromBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u16 * dest)
{
int x;
s32 x;
u16 * ptr = (void *)(gBGTilemapBuffers + (bgNum << 12) + (top << 6) + (left << 1));

while (height)
Expand All @@ -79,9 +80,9 @@ void CopyFromBgTilemapBufferRect(int bgNum, int left, int top, int width, int he
}
}

void SetBgTilemapBufferPaletteRect(int bgNum, int left, int top, int width, int height, int paletteNum)
void SetBgTilemapBufferPaletteRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u32 paletteNum)
{
int x;
s32 x;
u16 * ptr = (void *)(gBGTilemapBuffers + (bgNum << 12) + (top << 6) + (left << 1));

while (height)
Expand All @@ -96,15 +97,15 @@ void SetBgTilemapBufferPaletteRect(int bgNum, int left, int top, int width, int
}
}

void SetBgTilemapBufferTileAt(int bgNum, int x, int y, u16 tileNum)
void SetBgTilemapBufferTileAt(u32 bgNum, u32 x, u32 y, u16 tileNum)
{
u16 * ptr = (void *)(gBGTilemapBuffers + (bgNum << 12) + (y << 6) + (x << 1));
*ptr = tileNum;
}

void FillBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, u16 tileNum)
void FillBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u16 tileNum)
{
int x;
s32 x;
u16 * ptr = (void *)(gBGTilemapBuffers + (bgNum << 12) + (top << 6) + (left << 1));

while (height)
Expand All @@ -118,9 +119,9 @@ void FillBgTilemapBufferRect(int bgNum, int left, int top, int width, int height
}
}

void CopyRectWithinBgTilemapBuffer(int bgNum, int srcLeft, int srcTop, int width, int height, int destLeft, int destTop)
void CopyRectWithinBgTilemapBuffer(u32 bgNum, u32 srcLeft, u32 srcTop, s32 width, u32 height, u32 destLeft, u32 destTop)
{
int x;
s32 x;
const u16 * srcPtr;
u16 * destPtr;

Expand All @@ -139,7 +140,7 @@ void CopyRectWithinBgTilemapBuffer(int bgNum, int srcLeft, int srcTop, int width
}
}

void SetBgPos(int bgNum, s32 x, s32 y)
void SetBgPos(u32 bgNum, s32 x, s32 y)
{
gBgHofsBuffer[bgNum] = x;
gBgVofsBuffer[bgNum] = y;
Expand Down
14 changes: 10 additions & 4 deletions payload/src/gflib/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ u32 GetFrameTotal(void)
return gVBlankCounter;
}

void DelayFrames(u32 a0)
void DelayFrames(u32 frames)
{
for (; a0 != 0; a0--)
for (; frames != 0; frames--)
{
UpdateSprites();
VBlankIntrWait();
Expand All @@ -51,17 +51,20 @@ void DelayFrames(u32 a0)

void InitIntr(void)
{
int i;
u32 i;

gVBlankCallback = NULL;

for (i = 0; i < 14u; i++)
gIntrTable[i] = IntrDummy;

if (VBlankIntr != NULL)
gIntrTable[1] = VBlankIntr;
else
gIntrTable[1] = IntrDummy;
}

void SetIntrFunc(int i, IntrFunc func)
void SetIntrFunc(u32 i, IntrFunc func)
{
if (func != NULL)
gIntrTable[i] = func;
Expand All @@ -81,12 +84,15 @@ void VBlankIntr(void)
{
if (EnableSoundVSync(0))
m4aSoundVSync();

DoOamBufferTransfer();
DoGpuUpdateAndTilemapTransfers();
gVBlankCounter++;
INTR_CHECK = INTR_FLAG_VBLANK;

if (gVBlankCallback != NULL)
gVBlankCallback();

if (EnableSoundVSync(1))
m4aSoundMain();
}
Expand Down
6 changes: 4 additions & 2 deletions payload/src/gflib/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ static u16 sKeyRepeatTimer;
void ReadKeys(void)
{
u32 keyInput;
u16 * prevKeys_p = &gHeldKeys;
u16 newKeys = (keyInput = REG_KEYINPUT ^ KEYS_MASK) & ~*prevKeys_p;
u16 * prevKeys = &gHeldKeys;
u16 newKeys = (keyInput = REG_KEYINPUT ^ KEYS_MASK) & ~*prevKeys;
gNewKeys = newKeys;

if (gHeldKeys != keyInput)
{
gNewAndRepeatedKeys = keyInput;
Expand All @@ -29,6 +30,7 @@ void ReadKeys(void)
{
gNewAndRepeatedKeys = 0;
}

gHeldKeys = keyInput;
}

Expand Down
27 changes: 14 additions & 13 deletions payload/src/gflib/sound.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
#include "global.h"
#include "gflib/sound.h"

static u8 gUnknown_02021368;
static bool8 isSoundVsync;
static u8 sIsSoundVSyncPaused;
static bool8 sIsSoundVsync;

void InitSound(void)
{
gUnknown_02021368 = 0;
isSoundVsync = 0;
sIsSoundVSyncPaused = 0;
sIsSoundVsync = FALSE;
m4aSoundInit();
}

void PauseSoundVSync(void)
{
gUnknown_02021368 = 1;
isSoundVsync = 0;
sIsSoundVSyncPaused = 1;
sIsSoundVsync = FALSE;
}

bool32 EnableSoundVSync(u32 a0)
bool32 EnableSoundVSync(u32 enable)
{
if (a0 && gUnknown_02021368)
isSoundVsync = 1;
return isSoundVsync;
if (enable && sIsSoundVSyncPaused)
sIsSoundVsync = TRUE;

return sIsSoundVsync;
}

void PlaySE(u16 song)
Expand Down Expand Up @@ -54,8 +55,8 @@ bool8 sub_020093AC(void)
void FadeOutSE(u16 song, u16 _speed)
{
struct MusicPlayerInfo * mPlayInfo;

u16 speed = _speed >> 4;

if (speed == 0)
speed = 1;

Expand Down Expand Up @@ -89,12 +90,12 @@ void SetSEPitch(u16 song, s16 pitch)

void SoundVSyncOff(void)
{
isSoundVsync = 0;
sIsSoundVsync = FALSE;
m4aSoundVSyncOff();
}

void SoundVSyncOn(void)
{
m4aSoundVSyncOn();
isSoundVsync = 1;
sIsSoundVsync = TRUE;
}
14 changes: 10 additions & 4 deletions payload/src/gflib/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ static inline void *GetAddr(struct Window *window)
{
void *vaddr = window->vramCharBase + (window->currentY / 8) * window->width * 32;
u32 x = window->currentX;

vaddr += 32 * (x / 8);
// Needed to match.
x++;x--;
x++;
x--;

return vaddr;
}

Expand All @@ -30,6 +33,7 @@ void DrawGlyphOnWindow_NoShadow(struct Window * window, u32 glyphId, u32 fgColor
xpixel = 8 - (window->currentX & 7);
glyphData = window->glyphGfx + glyphId * window->glyphSize;
curpos--;

for (i = 0; i < window->glyphSize; i++)
{
if (i != 8)
Expand Down Expand Up @@ -66,6 +70,7 @@ void DrawGlyphOnWindow_WithShadow(struct Window *window, u32 glyphId, u32 fgColo

xpixel = 8 - (window->currentX & 7);
glyphData = window->glyphGfx + (glyphId * window->glyphSize * 2);

for (i = 0; i < window->glyphSize; i++)
{
vaddr = window->vramCharBase + ((window->currentY + i) >> 3) * window->width * 32 + (32 * (window->currentX >> 3));
Expand Down Expand Up @@ -101,9 +106,9 @@ void DrawGlyphOnWindow_WithShadow(struct Window *window, u32 glyphId, u32 fgColo
static inline void TextWindowFillTileBufferForText_Inline(struct Window *win)
{
s32 i, j;

u16 *addr = (void *) IWRAM_START + (win->bg << 12);
u16 blockVal = win->baseBlock;

for (i = win->top; i < win->top + win->height; i++)
{
for (j = win->left; j < win->left + win->width; j++)
Expand Down Expand Up @@ -153,6 +158,7 @@ void GenerateFontHalfrowLookupTable(u32 *ptr)
s32 i;

sFontHalfrowLookupTablePtr = ptr;

for (i = 0; i <= 0xFF; i++)
{
u32 r1 = 0;
Expand All @@ -178,9 +184,9 @@ void GenerateFontHalfrowLookupTable(u32 *ptr)
}
}

void ClearWindowCharBuffer(struct Window *window, u32 a1)
void ClearWindowCharBuffer(struct Window *window, u32 fillValue)
{
CpuFill16(a1, window->vramCharBase, (window->width * window->height) * 32);
CpuFill16(fillValue, window->vramCharBase, (window->width * window->height) * 32);
TextWindowSetXY(window, 0, 0);
}

Expand Down
Loading

0 comments on commit b114559

Please sign in to comment.