From bb0bb13050aa149cd1dfdfdb958b1144aa497b3c Mon Sep 17 00:00:00 2001 From: mzxrules Date: Fri, 6 Sep 2024 14:32:11 -0400 Subject: [PATCH 1/4] Document gfxalloc.c --- src/code/flg_set.c | 19 +++++------ src/code/gfxalloc.c | 33 ++++++++++++++++--- src/code/z_debug.c | 19 +++++------ src/code/z_kankyo.c | 19 ++++++----- src/code/z_message.c | 28 ++++++++-------- src/code/z_play.c | 22 ++++++------- src/overlays/actors/ovl_En_Mag/z_en_mag.c | 17 +++++----- .../misc/ovl_kaleido_scope/z_kaleido_debug.c | 17 +++++----- .../misc/ovl_kaleido_scope/z_kaleido_scope.c | 19 +++++------ 9 files changed, 106 insertions(+), 87 deletions(-) diff --git a/src/code/flg_set.c b/src/code/flg_set.c index de5b2a3d3f0..d160f3008b1 100644 --- a/src/code/flg_set.c +++ b/src/code/flg_set.c @@ -68,8 +68,8 @@ void FlagSet_Update(PlayState* play) { GraphicsContext* gfxCtx = play->state.gfxCtx; Input* input = &play->state.input[0]; - Gfx* gfx; - Gfx* polyOpa; + Gfx* tempGfxDisp; + Gfx* lockedGfxDisp; OPEN_DISPS(gfxCtx, "../flg_set.c", 131); @@ -77,12 +77,11 @@ void FlagSet_Update(PlayState* play) { GfxPrint printer; s32 pad; - polyOpa = POLY_OPA_DISP; - gfx = Gfx_Open(polyOpa); - gSPDisplayList(OVERLAY_DISP++, gfx); + tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); + gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); GfxPrint_Init(&printer); - GfxPrint_Open(&printer, gfx); + GfxPrint_Open(&printer, tempGfxDisp); GfxPrint_SetColor(&printer, 250, 50, 50, 255); GfxPrint_SetPos(&printer, 4, 13); GfxPrint_Printf(&printer, entries[entryIdx].name); @@ -167,12 +166,12 @@ void FlagSet_Update(PlayState* play) { timer--; } - gfx = GfxPrint_Close(&printer); + tempGfxDisp = GfxPrint_Close(&printer); GfxPrint_Destroy(&printer); - gSPEndDisplayList(gfx++); - Gfx_Close(polyOpa, gfx); - POLY_OPA_DISP = gfx; + gSPEndDisplayList(tempGfxDisp++); + Gfx_Close(lockedGfxDisp, tempGfxDisp); + POLY_OPA_DISP = tempGfxDisp; } if (CHECK_BTN_ALL(input->press.button, BTN_L)) { diff --git a/src/code/gfxalloc.c b/src/code/gfxalloc.c index 79d8bb04e56..e9273d8abf3 100644 --- a/src/code/gfxalloc.c +++ b/src/code/gfxalloc.c @@ -1,14 +1,37 @@ #include "global.h" -Gfx* Gfx_Open(Gfx* gfx) { - return gfx + 1; +/** + * Creates a new temporary graphics display list pointer, using the memory reserved by gfxDisp + * + * @param gfxDisp is the display list yielding memory. It cannot be written to until Gfx_Close is called. + * @returns a new graphics display list pointer. + * + * @note This is used to give WORK_DISP more memory to write instructions without increasing the WORK_DISP buffer size. + */ +Gfx* Gfx_Open(Gfx* gfxDisp) { + // reserve space for a gSPBranchList command when Gfx_Close is called + return gfxDisp + 1; } -Gfx* Gfx_Close(Gfx* gfx, Gfx* dst) { - gSPBranchList(gfx, dst); - return dst; +/** + * Closes the graphics display list created by Gfx_Open. + * + * @param gfxDisp is the display list yielding memory. + * @param gfx is the graphics display list pointer that was created with Gfx_Open + * @returns gfxDisp's new position + */ +Gfx* Gfx_Close(Gfx* gfxDisp, Gfx* gfx) { + gSPBranchList(gfxDisp, gfx); + return gfx; } +/** + * Allocates a fixed size block of memory for graphics data on a graphics display list + * + * @param gfxP is a pointer to a graphics display list pointer + * @param size is the number of bytes to reserve + * @returns start pointer to the allocated memory + */ void* Gfx_Alloc(Gfx** gfxP, u32 size) { u8* ptr; Gfx* dst; diff --git a/src/code/z_debug.c b/src/code/z_debug.c index 963e934ff35..9f3d5a61deb 100644 --- a/src/code/z_debug.c +++ b/src/code/z_debug.c @@ -280,18 +280,17 @@ void Regs_DrawEditor(GfxPrint* printer) { * Draws the Reg Editor and Debug Camera text on screen */ void Debug_DrawText(GraphicsContext* gfxCtx) { - Gfx* gfx; - Gfx* opaStart; + Gfx* tempGfxDisp; + Gfx* lockedGfxDisp; GfxPrint printer; s32 pad; OPEN_DISPS(gfxCtx, "../z_debug.c", 628); GfxPrint_Init(&printer); - opaStart = POLY_OPA_DISP; - gfx = Gfx_Open(POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, gfx); - GfxPrint_Open(&printer, gfx); + tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); + gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); + GfxPrint_Open(&printer, tempGfxDisp); if ((OREG(0) == 1) || (OREG(0) == 8)) { DebugCamera_DrawScreenText(&printer); @@ -305,10 +304,10 @@ void Debug_DrawText(GraphicsContext* gfxCtx) { sDebugCamTextEntryCount = 0; - gfx = GfxPrint_Close(&printer); - gSPEndDisplayList(gfx++); - Gfx_Close(opaStart, gfx); - POLY_OPA_DISP = gfx; + tempGfxDisp = GfxPrint_Close(&printer); + gSPEndDisplayList(tempGfxDisp++); + Gfx_Close(lockedGfxDisp, tempGfxDisp); + POLY_OPA_DISP = tempGfxDisp; CLOSE_DISPS(gfxCtx, "../z_debug.c", 664); diff --git a/src/code/z_kankyo.c b/src/code/z_kankyo.c index 6a8026cdb91..c1bb7dcaea0 100644 --- a/src/code/z_kankyo.c +++ b/src/code/z_kankyo.c @@ -972,18 +972,19 @@ void Environment_Update(PlayState* play, EnvironmentContext* envCtx, LightContex #if OOT_DEBUG if (R_ENABLE_ARENA_DBG != 0 || CREG(2) != 0) { - Gfx* displayList; - Gfx* prevDisplayList; + Gfx* tempGfxDisp; + Gfx* lockedGfxDisp; OPEN_DISPS(play->state.gfxCtx, "../z_kankyo.c", 1682); - prevDisplayList = POLY_OPA_DISP; - displayList = Gfx_Open(POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, displayList); - Environment_PrintDebugInfo(play, &displayList); - gSPEndDisplayList(displayList++); - Gfx_Close(prevDisplayList, displayList); - POLY_OPA_DISP = displayList; + tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); + gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); + Environment_PrintDebugInfo(play, &tempGfxDisp); + + gSPEndDisplayList(tempGfxDisp++); + Gfx_Close(lockedGfxDisp, tempGfxDisp); + POLY_OPA_DISP = tempGfxDisp; + CLOSE_DISPS(play->state.gfxCtx, "../z_kankyo.c", 1690); } #endif diff --git a/src/code/z_message.c b/src/code/z_message.c index 29212d31820..71b823cc10f 100644 --- a/src/code/z_message.c +++ b/src/code/z_message.c @@ -3891,8 +3891,8 @@ void Message_DrawDebugText(PlayState* play, Gfx** p) { #endif void Message_Draw(PlayState* play) { - Gfx* plusOne; - Gfx* polyOpaP; + Gfx* tempGfxDisp; + Gfx* lockedGfxDisp; #if OOT_VERSION < GC_US s32 pad; #endif @@ -3906,21 +3906,21 @@ void Message_Draw(PlayState* play) { watchVar = gSaveContext.save.info.scarecrowLongSongSet; Message_DrawDebugVariableChanged(&watchVar, play->state.gfxCtx); if (BREG(0) != 0 && play->msgCtx.textId != 0) { - plusOne = Gfx_Open(polyOpaP = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, plusOne); - Message_DrawDebugText(play, &plusOne); - gSPEndDisplayList(plusOne++); - Gfx_Close(polyOpaP, plusOne); - POLY_OPA_DISP = plusOne; + tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); + gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); + Message_DrawDebugText(play, &tempGfxDisp); + gSPEndDisplayList(tempGfxDisp++); + Gfx_Close(lockedGfxDisp, tempGfxDisp); + POLY_OPA_DISP = tempGfxDisp; } #endif - plusOne = Gfx_Open(polyOpaP = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, plusOne); - Message_DrawMain(play, &plusOne); - gSPEndDisplayList(plusOne++); - Gfx_Close(polyOpaP, plusOne); - POLY_OPA_DISP = plusOne; + tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); + gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); + Message_DrawMain(play, &tempGfxDisp); + gSPEndDisplayList(tempGfxDisp++); + Gfx_Close(lockedGfxDisp, tempGfxDisp); + POLY_OPA_DISP = tempGfxDisp; CLOSE_DISPS(play->state.gfxCtx, "../z_message_PAL.c", 3582); } diff --git a/src/code/z_play.c b/src/code/z_play.c index 99bb0246813..22ceb7dbd98 100644 --- a/src/code/z_play.c +++ b/src/code/z_play.c @@ -1157,11 +1157,11 @@ void Play_Draw(PlayState* this) { gSPSegment(POLY_OPA_DISP++, 0x01, this->billboardMtx); if (!OOT_DEBUG || (R_HREG_MODE != HREG_MODE_PLAY) || R_PLAY_DRAW_COVER_ELEMENTS) { - Gfx* gfxP; - Gfx* sp1CC = POLY_OPA_DISP; + Gfx* tempGfxDisp; + Gfx* lockedGfxDisp; - gfxP = Gfx_Open(sp1CC); - gSPDisplayList(OVERLAY_DISP++, gfxP); + tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); + gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); if ((this->transitionMode == TRANS_MODE_INSTANCE_RUNNING) || (this->transitionMode == TRANS_MODE_INSTANCE_WAIT) || (this->transitionCtx.transitionType >= 56)) { @@ -1172,11 +1172,11 @@ void Play_Draw(PlayState* this) { SET_FULLSCREEN_VIEWPORT(&view); - View_ApplyTo(&view, VIEW_ALL, &gfxP); - this->transitionCtx.draw(&this->transitionCtx.instanceData, &gfxP); + View_ApplyTo(&view, VIEW_ALL, &tempGfxDisp); + this->transitionCtx.draw(&this->transitionCtx.instanceData, &tempGfxDisp); } - TransitionFade_Draw(&this->transitionFadeFlash, &gfxP); + TransitionFade_Draw(&this->transitionFadeFlash, &tempGfxDisp); #if PLATFORM_N64 if (gVisMonoColor.a != 0) @@ -1185,12 +1185,12 @@ void Play_Draw(PlayState* this) { #endif { gPlayVisMono.vis.primColor.rgba = gVisMonoColor.rgba; - VisMono_Draw(&gPlayVisMono, &gfxP); + VisMono_Draw(&gPlayVisMono, &tempGfxDisp); } - gSPEndDisplayList(gfxP++); - Gfx_Close(sp1CC, gfxP); - POLY_OPA_DISP = gfxP; + gSPEndDisplayList(tempGfxDisp++); + Gfx_Close(lockedGfxDisp, tempGfxDisp); + POLY_OPA_DISP = tempGfxDisp; } if (gTransitionTileState == TRANS_TILE_READY) { diff --git a/src/overlays/actors/ovl_En_Mag/z_en_mag.c b/src/overlays/actors/ovl_En_Mag/z_en_mag.c index 6d992e457f8..afd57b2fb35 100644 --- a/src/overlays/actors/ovl_En_Mag/z_en_mag.c +++ b/src/overlays/actors/ovl_En_Mag/z_en_mag.c @@ -803,20 +803,19 @@ void EnMag_DrawInner(Actor* thisx, PlayState* play, Gfx** gfxP) { void EnMag_Draw(Actor* thisx, PlayState* play) { s32 pad; - Gfx* gfx; - Gfx* gfxRef; + Gfx* tempGfxDisp; + Gfx* lockedGfxDisp; OPEN_DISPS(play->state.gfxCtx, "../z_en_mag.c", 1151); - gfxRef = POLY_OPA_DISP; - gfx = Gfx_Open(gfxRef); - gSPDisplayList(OVERLAY_DISP++, gfx); + tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); + gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); - EnMag_DrawInner(thisx, play, &gfx); + EnMag_DrawInner(thisx, play, &tempGfxDisp); - gSPEndDisplayList(gfx++); - Gfx_Close(gfxRef, gfx); - POLY_OPA_DISP = gfx; + gSPEndDisplayList(tempGfxDisp++); + Gfx_Close(lockedGfxDisp, tempGfxDisp); + POLY_OPA_DISP = tempGfxDisp; CLOSE_DISPS(play->state.gfxCtx, "../z_en_mag.c", 1161); } diff --git a/src/overlays/misc/ovl_kaleido_scope/z_kaleido_debug.c b/src/overlays/misc/ovl_kaleido_scope/z_kaleido_debug.c index 6cf75817a52..02d40a4c430 100644 --- a/src/overlays/misc/ovl_kaleido_scope/z_kaleido_debug.c +++ b/src/overlays/misc/ovl_kaleido_scope/z_kaleido_debug.c @@ -98,8 +98,8 @@ void KaleidoScope_DrawDebugEditor(PlayState* play) { static s32 heldDBtnTimer = 0; PauseContext* pauseCtx = &play->pauseCtx; Input* input = &play->state.input[0]; - Gfx* gfx; - Gfx* gfxRef; + Gfx* tempGfxDisp; + Gfx* lockedGfxDisp; s16 spD8[4]; s16 slot; s16 i; @@ -123,15 +123,14 @@ void KaleidoScope_DrawDebugEditor(PlayState* play) { gDPSetCombineLERP(POLY_OPA_DISP++, 0, 0, 0, PRIMITIVE, TEXEL0, 0, PRIMITIVE, 0, 0, 0, 0, PRIMITIVE, TEXEL0, 0, PRIMITIVE, 0); - gfxRef = POLY_OPA_DISP; - gfx = Gfx_Open(gfxRef); - gSPDisplayList(OVERLAY_DISP++, gfx); + tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); + gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); - KaleidoScope_DrawDebugEditorText(&gfx); + KaleidoScope_DrawDebugEditorText(&tempGfxDisp); - gSPEndDisplayList(gfx++); - Gfx_Close(gfxRef, gfx); - POLY_OPA_DISP = gfx; + gSPEndDisplayList(tempGfxDisp++); + Gfx_Close(lockedGfxDisp, tempGfxDisp); + POLY_OPA_DISP = tempGfxDisp; gDPPipeSync(POLY_OPA_DISP++); gDPSetPrimColor(POLY_OPA_DISP++, 0, 0, 255, 0, 0, 255); diff --git a/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope.c b/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope.c index 936400e1c0a..e5397bd816c 100644 --- a/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope.c +++ b/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope.c @@ -832,25 +832,24 @@ static PreRender sPlayerPreRender; static void* sPreRenderCvg; void KaleidoScope_SetupPlayerPreRender(PlayState* play) { - Gfx* gfx; - Gfx* gfxRef; + Gfx* tempGfxDisp; + Gfx* lockedGfxDisp; void* fbuf; fbuf = play->state.gfxCtx->curFrameBuffer; OPEN_DISPS(play->state.gfxCtx, "../z_kaleido_scope_PAL.c", 496); - gfxRef = POLY_OPA_DISP; - gfx = Gfx_Open(gfxRef); - gSPDisplayList(WORK_DISP++, gfx); + tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); + gSPDisplayList(WORK_DISP++, tempGfxDisp); PreRender_SetValues(&sPlayerPreRender, PAUSE_EQUIP_PLAYER_WIDTH, PAUSE_EQUIP_PLAYER_HEIGHT, fbuf, NULL); - PreRender_SaveFramebuffer(&sPlayerPreRender, &gfx); - PreRender_DrawCoverage(&sPlayerPreRender, &gfx); + PreRender_SaveFramebuffer(&sPlayerPreRender, &tempGfxDisp); + PreRender_DrawCoverage(&sPlayerPreRender, &tempGfxDisp); - gSPEndDisplayList(gfx++); - Gfx_Close(gfxRef, gfx); - POLY_OPA_DISP = gfx; + gSPEndDisplayList(tempGfxDisp++); + Gfx_Close(lockedGfxDisp, tempGfxDisp); + POLY_OPA_DISP = tempGfxDisp; R_GRAPH_TASKSET00_FLAGS |= 1; From 3e5585cbec7d29d8a019e56af22e3b08d4552aed Mon Sep 17 00:00:00 2001 From: mzxrules Date: Fri, 6 Sep 2024 15:05:14 -0400 Subject: [PATCH 2/4] format.py --- include/functions.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/functions.h b/include/functions.h index a0156d314d1..016d21f8d45 100644 --- a/include/functions.h +++ b/include/functions.h @@ -1258,8 +1258,8 @@ void* Graph_Alloc2(GraphicsContext* gfxCtx, size_t size); void Graph_OpenDisps(Gfx** dispRefs, GraphicsContext* gfxCtx, const char* file, int line); void Graph_CloseDisps(Gfx** dispRefs, GraphicsContext* gfxCtx, const char* file, int line); #endif -Gfx* Gfx_Open(Gfx* gfx); -Gfx* Gfx_Close(Gfx* gfx, Gfx* dst); +Gfx* Gfx_Open(Gfx* gfxDisp); +Gfx* Gfx_Close(Gfx* gfxDisp, Gfx* gfx); void* Gfx_Alloc(Gfx** gfxP, u32 size); ListAlloc* ListAlloc_Init(ListAlloc* this); void* ListAlloc_Alloc(ListAlloc* this, u32 size); From 74ebde21e8eab9c0f49a0e749b392e03ce4db5a9 Mon Sep 17 00:00:00 2001 From: mzxrules Date: Sun, 8 Sep 2024 21:21:47 -0400 Subject: [PATCH 3/4] rename missed temp variables --- src/code/game.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/code/game.c b/src/code/game.c index 6cf7a875d41..a204ffc77d2 100644 --- a/src/code/game.c +++ b/src/code/game.c @@ -163,22 +163,22 @@ void GameState_DrawInputDisplay(u16 input, Gfx** gfxP) { #endif void GameState_Draw(GameState* gameState, GraphicsContext* gfxCtx) { - Gfx* newDList; - Gfx* polyOpaP; + Gfx* tempGfxDisp; + Gfx* lockedGfxDisp; OPEN_DISPS(gfxCtx, "../game.c", 746); - newDList = Gfx_Open(polyOpaP = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, newDList); + tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); + gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); if (R_ENABLE_FB_FILTER == 1) { - GameState_SetFBFilter(&newDList); + GameState_SetFBFilter(&tempGfxDisp); } #if OOT_DEBUG sLastButtonPressed = gameState->input[0].press.button | gameState->input[0].cur.button; if (R_DISABLE_INPUT_DISPLAY == 0) { - GameState_DrawInputDisplay(sLastButtonPressed, &newDList); + GameState_DrawInputDisplay(sLastButtonPressed, &tempGfxDisp); } if (R_ENABLE_AUDIO_DBG & 1) { @@ -186,9 +186,9 @@ void GameState_Draw(GameState* gameState, GraphicsContext* gfxCtx) { GfxPrint printer; GfxPrint_Init(&printer); - GfxPrint_Open(&printer, newDList); + GfxPrint_Open(&printer, tempGfxDisp); AudioDebug_Draw(&printer); - newDList = GfxPrint_Close(&printer); + tempGfxDisp = GfxPrint_Close(&printer); GfxPrint_Destroy(&printer); } #endif @@ -205,9 +205,9 @@ void GameState_Draw(GameState* gameState, GraphicsContext* gfxCtx) { R_ENABLE_ARENA_DBG = 0; } - gSPEndDisplayList(newDList++); - Gfx_Close(polyOpaP, newDList); - POLY_OPA_DISP = newDList; + gSPEndDisplayList(tempGfxDisp++); + Gfx_Close(lockedGfxDisp, tempGfxDisp); + POLY_OPA_DISP = tempGfxDisp; CLOSE_DISPS(gfxCtx, "../game.c", 800); @@ -236,23 +236,23 @@ void GameState_SetFrameBuffer(GraphicsContext* gfxCtx) { } void func_800C49F4(GraphicsContext* gfxCtx) { - Gfx* newDlist; - Gfx* polyOpaP; + Gfx* tempGfxDisp; + Gfx* lockedGfxDisp; OPEN_DISPS(gfxCtx, "../game.c", 846); - newDlist = Gfx_Open(polyOpaP = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, newDlist); + tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); + gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); #if PLATFORM_N64 if (D_80121212 != 0) { - func_801C6EA0(&newDlist); + func_801C6EA0(&tempGfxDisp); } #endif - gSPEndDisplayList(newDlist++); - Gfx_Close(polyOpaP, newDlist); - POLY_OPA_DISP = newDlist; + gSPEndDisplayList(tempGfxDisp++); + Gfx_Close(lockedGfxDisp, tempGfxDisp); + POLY_OPA_DISP = tempGfxDisp; CLOSE_DISPS(gfxCtx, "../game.c", 865); } From 4c40183da4fb930ab9e8561edebf49732ba36504 Mon Sep 17 00:00:00 2001 From: mzxrules Date: Tue, 1 Oct 2024 14:05:02 -0400 Subject: [PATCH 4/4] implement Open/Close using macros --- include/gfxalloc.h | 28 ++++++++++++++-- src/code/flg_set.c | 15 ++++----- src/code/game.c | 32 ++++++++----------- src/code/gfxalloc.c | 23 +++++++------ src/code/z_debug.c | 15 ++++----- src/code/z_demo.c | 14 +++----- src/code/z_kankyo.c | 14 +++----- src/code/z_message.c | 22 +++++-------- src/code/z_play.c | 19 +++++------ src/overlays/actors/ovl_En_Mag/z_en_mag.c | 15 +++------ .../misc/ovl_kaleido_scope/z_kaleido_debug.c | 15 +++------ .../misc/ovl_kaleido_scope/z_kaleido_scope.c | 15 ++++----- 12 files changed, 106 insertions(+), 121 deletions(-) diff --git a/include/gfxalloc.h b/include/gfxalloc.h index 158a2a9f589..692d2bf3b11 100644 --- a/include/gfxalloc.h +++ b/include/gfxalloc.h @@ -2,9 +2,33 @@ #define GFXALLOC_H #include "ultra64.h" +#include "gfx.h" -Gfx* Gfx_Open(Gfx* gfx); -Gfx* Gfx_Close(Gfx* gfx, Gfx* dst); +Gfx* Gfx_Open(Gfx* gfxDisp); +Gfx* Gfx_Close(Gfx* gfxDisp, Gfx* gfxAllocDisp); void* Gfx_Alloc(Gfx** gfxP, u32 size); +/** + * Creates a new temporary graphics display list pointer, using the memory reserved by POLY_OPA_DISP + * POLY_OPA_DISP cannot be written to until GFX_ALLOC_CLOSE is called. + * + * @param gfxAllocDisp is the new temporary graphics display list pointer. + * @param tempGfx is an intermediate Gfx* variable that should not be touched. + * @param disp is the top level display list that is being given more memory. + */ +#define GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, disp) \ + gfxAllocDisp = Gfx_Open(tempGfx = POLY_OPA_DISP); \ + gSPDisplayList(disp++, gfxAllocDisp) + +/** + * Closes the graphics display list created by GFX_ALLOC_OPEN. + * + * @param gfxAllocDisp is the graphics display list pointer that was created with GFX_ALLOC_OPEN + * @param tempGfx is the same Gfx* variable that was passed into GFX_ALLOC_OPEN + */ +#define GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx) \ + gSPEndDisplayList(gfxAllocDisp++); \ + Gfx_Close(tempGfx, gfxAllocDisp); \ + POLY_OPA_DISP = gfxAllocDisp + #endif diff --git a/src/code/flg_set.c b/src/code/flg_set.c index d160f3008b1..6dc96e6e717 100644 --- a/src/code/flg_set.c +++ b/src/code/flg_set.c @@ -68,8 +68,8 @@ void FlagSet_Update(PlayState* play) { GraphicsContext* gfxCtx = play->state.gfxCtx; Input* input = &play->state.input[0]; - Gfx* tempGfxDisp; - Gfx* lockedGfxDisp; + Gfx* gfxAllocDisp; + Gfx* tempGfx; OPEN_DISPS(gfxCtx, "../flg_set.c", 131); @@ -77,11 +77,10 @@ void FlagSet_Update(PlayState* play) { GfxPrint printer; s32 pad; - tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, OVERLAY_DISP); GfxPrint_Init(&printer); - GfxPrint_Open(&printer, tempGfxDisp); + GfxPrint_Open(&printer, gfxAllocDisp); GfxPrint_SetColor(&printer, 250, 50, 50, 255); GfxPrint_SetPos(&printer, 4, 13); GfxPrint_Printf(&printer, entries[entryIdx].name); @@ -166,12 +165,10 @@ void FlagSet_Update(PlayState* play) { timer--; } - tempGfxDisp = GfxPrint_Close(&printer); + gfxAllocDisp = GfxPrint_Close(&printer); GfxPrint_Destroy(&printer); - gSPEndDisplayList(tempGfxDisp++); - Gfx_Close(lockedGfxDisp, tempGfxDisp); - POLY_OPA_DISP = tempGfxDisp; + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); } if (CHECK_BTN_ALL(input->press.button, BTN_L)) { diff --git a/src/code/game.c b/src/code/game.c index f6ffff5eb32..b6bb800f3a2 100644 --- a/src/code/game.c +++ b/src/code/game.c @@ -164,22 +164,21 @@ void GameState_DrawInputDisplay(u16 input, Gfx** gfxP) { #endif void GameState_Draw(GameState* gameState, GraphicsContext* gfxCtx) { - Gfx* tempGfxDisp; - Gfx* lockedGfxDisp; + Gfx* gfxAllocDisp; + Gfx* tempGfx; OPEN_DISPS(gfxCtx, "../game.c", 746); - tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, OVERLAY_DISP); if (R_ENABLE_FB_FILTER == 1) { - GameState_SetFBFilter(&tempGfxDisp); + GameState_SetFBFilter(&gfxAllocDisp); } #if OOT_DEBUG sLastButtonPressed = gameState->input[0].press.button | gameState->input[0].cur.button; if (R_DISABLE_INPUT_DISPLAY == 0) { - GameState_DrawInputDisplay(sLastButtonPressed, &tempGfxDisp); + GameState_DrawInputDisplay(sLastButtonPressed, &gfxAllocDisp); } if (R_ENABLE_AUDIO_DBG & 1) { @@ -187,9 +186,9 @@ void GameState_Draw(GameState* gameState, GraphicsContext* gfxCtx) { GfxPrint printer; GfxPrint_Init(&printer); - GfxPrint_Open(&printer, tempGfxDisp); + GfxPrint_Open(&printer, gfxAllocDisp); AudioDebug_Draw(&printer); - tempGfxDisp = GfxPrint_Close(&printer); + gfxAllocDisp = GfxPrint_Close(&printer); GfxPrint_Destroy(&printer); } #endif @@ -206,9 +205,7 @@ void GameState_Draw(GameState* gameState, GraphicsContext* gfxCtx) { R_ENABLE_ARENA_DBG = 0; } - gSPEndDisplayList(tempGfxDisp++); - Gfx_Close(lockedGfxDisp, tempGfxDisp); - POLY_OPA_DISP = tempGfxDisp; + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); CLOSE_DISPS(gfxCtx, "../game.c", 800); @@ -237,23 +234,20 @@ void GameState_SetFrameBuffer(GraphicsContext* gfxCtx) { } void func_800C49F4(GraphicsContext* gfxCtx) { - Gfx* tempGfxDisp; - Gfx* lockedGfxDisp; + Gfx* gfxAllocDisp; + Gfx* tempGfx; OPEN_DISPS(gfxCtx, "../game.c", 846); - tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, OVERLAY_DISP); #if PLATFORM_N64 if (D_80121212 != 0) { - func_801C6EA0(&tempGfxDisp); + func_801C6EA0(&gfxAllocDisp); } #endif - gSPEndDisplayList(tempGfxDisp++); - Gfx_Close(lockedGfxDisp, tempGfxDisp); - POLY_OPA_DISP = tempGfxDisp; + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); CLOSE_DISPS(gfxCtx, "../game.c", 865); } diff --git a/src/code/gfxalloc.c b/src/code/gfxalloc.c index e9273d8abf3..48b20865a94 100644 --- a/src/code/gfxalloc.c +++ b/src/code/gfxalloc.c @@ -1,12 +1,14 @@ -#include "global.h" +#include "ultra64.h" +#include "gfxalloc.h" /** * Creates a new temporary graphics display list pointer, using the memory reserved by gfxDisp * * @param gfxDisp is the display list yielding memory. It cannot be written to until Gfx_Close is called. - * @returns a new graphics display list pointer. + * @return a new graphics display list pointer. * - * @note This is used to give WORK_DISP more memory to write instructions without increasing the WORK_DISP buffer size. + * @note This is used to take memory allocated to a larger display buffer and use it for a smaller display buffer. + * For example, space in POLY_OPA_DISP can be reserved for WORK_DISP and OVERLAY_DISP task data. */ Gfx* Gfx_Open(Gfx* gfxDisp) { // reserve space for a gSPBranchList command when Gfx_Close is called @@ -18,11 +20,13 @@ Gfx* Gfx_Open(Gfx* gfxDisp) { * * @param gfxDisp is the display list yielding memory. * @param gfx is the graphics display list pointer that was created with Gfx_Open - * @returns gfxDisp's new position + * @return gfxDisp's new position. + * + * @note gfxDisp must be updated after the call with the return value of this function to complete the operation. */ -Gfx* Gfx_Close(Gfx* gfxDisp, Gfx* gfx) { - gSPBranchList(gfxDisp, gfx); - return gfx; +Gfx* Gfx_Close(Gfx* gfxDisp, Gfx* gfxAllocDisp) { + gSPBranchList(gfxDisp, gfxAllocDisp); + return gfxAllocDisp; } /** @@ -30,17 +34,16 @@ Gfx* Gfx_Close(Gfx* gfxDisp, Gfx* gfx) { * * @param gfxP is a pointer to a graphics display list pointer * @param size is the number of bytes to reserve - * @returns start pointer to the allocated memory + * @return the start pointer to the allocated memory */ void* Gfx_Alloc(Gfx** gfxP, u32 size) { u8* ptr; Gfx* dst; size = ALIGN8(size); - ptr = (u8*)(*gfxP + 1); - dst = (Gfx*)(ptr + size); + gSPBranchList(*gfxP, dst); *gfxP = dst; diff --git a/src/code/z_debug.c b/src/code/z_debug.c index 536f4dd8377..44cbc8c9af4 100644 --- a/src/code/z_debug.c +++ b/src/code/z_debug.c @@ -281,17 +281,16 @@ void Regs_DrawEditor(GfxPrint* printer) { * Draws the Reg Editor and Debug Camera text on screen */ void Debug_DrawText(GraphicsContext* gfxCtx) { - Gfx* tempGfxDisp; - Gfx* lockedGfxDisp; + Gfx* gfxAllocDisp; + Gfx* tempGfx; GfxPrint printer; s32 pad; OPEN_DISPS(gfxCtx, "../z_debug.c", 628); GfxPrint_Init(&printer); - tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); - GfxPrint_Open(&printer, tempGfxDisp); + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, OVERLAY_DISP); + GfxPrint_Open(&printer, gfxAllocDisp); if ((OREG(0) == 1) || (OREG(0) == 8)) { DebugCamera_DrawScreenText(&printer); @@ -305,10 +304,8 @@ void Debug_DrawText(GraphicsContext* gfxCtx) { sDebugCamTextEntryCount = 0; - tempGfxDisp = GfxPrint_Close(&printer); - gSPEndDisplayList(tempGfxDisp++); - Gfx_Close(lockedGfxDisp, tempGfxDisp); - POLY_OPA_DISP = tempGfxDisp; + gfxAllocDisp = GfxPrint_Close(&printer); + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); CLOSE_DISPS(gfxCtx, "../z_debug.c", 664); diff --git a/src/code/z_demo.c b/src/code/z_demo.c index cada27694b3..d925427e34b 100644 --- a/src/code/z_demo.c +++ b/src/code/z_demo.c @@ -2217,18 +2217,14 @@ void CutsceneHandler_RunScript(PlayState* play, CutsceneContext* csCtx) { if (gSaveContext.save.cutsceneIndex >= 0xFFF0) { #if OOT_DEBUG if (BREG(0) != 0) { - Gfx* displayList; - Gfx* prevDisplayList; + Gfx* gfxAllocDisp; + Gfx* tempGfx; OPEN_DISPS(play->state.gfxCtx, "../z_demo.c", 4101); - prevDisplayList = POLY_OPA_DISP; - displayList = Gfx_Open(POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, displayList); - Cutscene_DrawDebugInfo(play, &displayList, csCtx); - gSPEndDisplayList(displayList++); - Gfx_Close(prevDisplayList, displayList); - POLY_OPA_DISP = displayList; + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, OVERLAY_DISP); + Cutscene_DrawDebugInfo(play, &gfxAllocDisp, csCtx); + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); CLOSE_DISPS(play->state.gfxCtx, "../z_demo.c", 4108); } diff --git a/src/code/z_kankyo.c b/src/code/z_kankyo.c index cc04636e346..7372c37e52f 100644 --- a/src/code/z_kankyo.c +++ b/src/code/z_kankyo.c @@ -981,18 +981,14 @@ void Environment_Update(PlayState* play, EnvironmentContext* envCtx, LightContex #if OOT_DEBUG if (R_ENABLE_ARENA_DBG != 0 || CREG(2) != 0) { - Gfx* tempGfxDisp; - Gfx* lockedGfxDisp; + Gfx* gfxAllocDisp; + Gfx* tempGfx; OPEN_DISPS(play->state.gfxCtx, "../z_kankyo.c", 1682); - tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); - Environment_PrintDebugInfo(play, &tempGfxDisp); - - gSPEndDisplayList(tempGfxDisp++); - Gfx_Close(lockedGfxDisp, tempGfxDisp); - POLY_OPA_DISP = tempGfxDisp; + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, OVERLAY_DISP); + Environment_PrintDebugInfo(play, &gfxAllocDisp); + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); CLOSE_DISPS(play->state.gfxCtx, "../z_kankyo.c", 1690); } diff --git a/src/code/z_message.c b/src/code/z_message.c index 10343234c75..669ea1a03ca 100644 --- a/src/code/z_message.c +++ b/src/code/z_message.c @@ -3973,8 +3973,8 @@ void Message_DrawDebugText(PlayState* play, Gfx** p) { #endif void Message_Draw(PlayState* play) { - Gfx* tempGfxDisp; - Gfx* lockedGfxDisp; + Gfx* gfxAllocDisp; + Gfx* tempGfx; #if OOT_VERSION < GC_US s32 pad; #endif @@ -3988,21 +3988,15 @@ void Message_Draw(PlayState* play) { watchVar = gSaveContext.save.info.scarecrowLongSongSet; Message_DrawDebugVariableChanged(&watchVar, play->state.gfxCtx); if (BREG(0) != 0 && play->msgCtx.textId != 0) { - tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); - Message_DrawDebugText(play, &tempGfxDisp); - gSPEndDisplayList(tempGfxDisp++); - Gfx_Close(lockedGfxDisp, tempGfxDisp); - POLY_OPA_DISP = tempGfxDisp; + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, OVERLAY_DISP); + Message_DrawDebugText(play, &gfxAllocDisp); + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); } #endif - tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); - Message_DrawMain(play, &tempGfxDisp); - gSPEndDisplayList(tempGfxDisp++); - Gfx_Close(lockedGfxDisp, tempGfxDisp); - POLY_OPA_DISP = tempGfxDisp; + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, OVERLAY_DISP); + Message_DrawMain(play, &gfxAllocDisp); + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); CLOSE_DISPS(play->state.gfxCtx, "../z_message_PAL.c", 3582); } diff --git a/src/code/z_play.c b/src/code/z_play.c index 7def3f9b64a..1362920cea4 100644 --- a/src/code/z_play.c +++ b/src/code/z_play.c @@ -1159,11 +1159,10 @@ void Play_Draw(PlayState* this) { gSPSegment(POLY_OPA_DISP++, 0x01, this->billboardMtx); if (!OOT_DEBUG || (R_HREG_MODE != HREG_MODE_PLAY) || R_PLAY_DRAW_COVER_ELEMENTS) { - Gfx* tempGfxDisp; - Gfx* lockedGfxDisp; + Gfx* gfxAllocDisp; + Gfx* tempGfx; - tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, OVERLAY_DISP); if ((this->transitionMode == TRANS_MODE_INSTANCE_RUNNING) || (this->transitionMode == TRANS_MODE_INSTANCE_WAIT) || (this->transitionCtx.transitionType >= 56)) { @@ -1174,11 +1173,11 @@ void Play_Draw(PlayState* this) { SET_FULLSCREEN_VIEWPORT(&view); - View_ApplyTo(&view, VIEW_ALL, &tempGfxDisp); - this->transitionCtx.draw(&this->transitionCtx.instanceData, &tempGfxDisp); + View_ApplyTo(&view, VIEW_ALL, &gfxAllocDisp); + this->transitionCtx.draw(&this->transitionCtx.instanceData, &gfxAllocDisp); } - TransitionFade_Draw(&this->transitionFadeFlash, &tempGfxDisp); + TransitionFade_Draw(&this->transitionFadeFlash, &gfxAllocDisp); #if PLATFORM_N64 if (gVisMonoColor.a != 0) @@ -1187,12 +1186,10 @@ void Play_Draw(PlayState* this) { #endif { gPlayVisMono.vis.primColor.rgba = gVisMonoColor.rgba; - VisMono_Draw(&gPlayVisMono, &tempGfxDisp); + VisMono_Draw(&gPlayVisMono, &gfxAllocDisp); } - gSPEndDisplayList(tempGfxDisp++); - Gfx_Close(lockedGfxDisp, tempGfxDisp); - POLY_OPA_DISP = tempGfxDisp; + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); } if (gTransitionTileState == TRANS_TILE_READY) { diff --git a/src/overlays/actors/ovl_En_Mag/z_en_mag.c b/src/overlays/actors/ovl_En_Mag/z_en_mag.c index afd57b2fb35..77138434021 100644 --- a/src/overlays/actors/ovl_En_Mag/z_en_mag.c +++ b/src/overlays/actors/ovl_En_Mag/z_en_mag.c @@ -803,19 +803,14 @@ void EnMag_DrawInner(Actor* thisx, PlayState* play, Gfx** gfxP) { void EnMag_Draw(Actor* thisx, PlayState* play) { s32 pad; - Gfx* tempGfxDisp; - Gfx* lockedGfxDisp; + Gfx* gfxAllocDisp; + Gfx* tempGfx; OPEN_DISPS(play->state.gfxCtx, "../z_en_mag.c", 1151); - tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); - - EnMag_DrawInner(thisx, play, &tempGfxDisp); - - gSPEndDisplayList(tempGfxDisp++); - Gfx_Close(lockedGfxDisp, tempGfxDisp); - POLY_OPA_DISP = tempGfxDisp; + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, OVERLAY_DISP); + EnMag_DrawInner(thisx, play, &gfxAllocDisp); + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); CLOSE_DISPS(play->state.gfxCtx, "../z_en_mag.c", 1161); } diff --git a/src/overlays/misc/ovl_kaleido_scope/z_kaleido_debug.c b/src/overlays/misc/ovl_kaleido_scope/z_kaleido_debug.c index 02d40a4c430..728ebb7e66b 100644 --- a/src/overlays/misc/ovl_kaleido_scope/z_kaleido_debug.c +++ b/src/overlays/misc/ovl_kaleido_scope/z_kaleido_debug.c @@ -98,8 +98,8 @@ void KaleidoScope_DrawDebugEditor(PlayState* play) { static s32 heldDBtnTimer = 0; PauseContext* pauseCtx = &play->pauseCtx; Input* input = &play->state.input[0]; - Gfx* tempGfxDisp; - Gfx* lockedGfxDisp; + Gfx* gfxAllocDisp; + Gfx* tempGfx; s16 spD8[4]; s16 slot; s16 i; @@ -123,14 +123,9 @@ void KaleidoScope_DrawDebugEditor(PlayState* play) { gDPSetCombineLERP(POLY_OPA_DISP++, 0, 0, 0, PRIMITIVE, TEXEL0, 0, PRIMITIVE, 0, 0, 0, 0, PRIMITIVE, TEXEL0, 0, PRIMITIVE, 0); - tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); - gSPDisplayList(OVERLAY_DISP++, tempGfxDisp); - - KaleidoScope_DrawDebugEditorText(&tempGfxDisp); - - gSPEndDisplayList(tempGfxDisp++); - Gfx_Close(lockedGfxDisp, tempGfxDisp); - POLY_OPA_DISP = tempGfxDisp; + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, OVERLAY_DISP); + KaleidoScope_DrawDebugEditorText(&gfxAllocDisp); + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); gDPPipeSync(POLY_OPA_DISP++); gDPSetPrimColor(POLY_OPA_DISP++, 0, 0, 255, 0, 0, 255); diff --git a/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope.c b/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope.c index 3afb6e81689..6c38cede752 100644 --- a/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope.c +++ b/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope.c @@ -907,24 +907,21 @@ static PreRender sPlayerPreRender; static void* sPreRenderCvg; void KaleidoScope_SetupPlayerPreRender(PlayState* play) { - Gfx* tempGfxDisp; - Gfx* lockedGfxDisp; + Gfx* gfxAllocDisp; + Gfx* tempGfx; void* fbuf; fbuf = play->state.gfxCtx->curFrameBuffer; OPEN_DISPS(play->state.gfxCtx, "../z_kaleido_scope_PAL.c", 496); - tempGfxDisp = Gfx_Open(lockedGfxDisp = POLY_OPA_DISP); - gSPDisplayList(WORK_DISP++, tempGfxDisp); + GFX_ALLOC_OPEN(gfxAllocDisp, tempGfx, WORK_DISP); PreRender_SetValues(&sPlayerPreRender, PAUSE_EQUIP_PLAYER_WIDTH, PAUSE_EQUIP_PLAYER_HEIGHT, fbuf, NULL); - PreRender_SaveFramebuffer(&sPlayerPreRender, &tempGfxDisp); - PreRender_DrawCoverage(&sPlayerPreRender, &tempGfxDisp); + PreRender_SaveFramebuffer(&sPlayerPreRender, &gfxAllocDisp); + PreRender_DrawCoverage(&sPlayerPreRender, &gfxAllocDisp); - gSPEndDisplayList(tempGfxDisp++); - Gfx_Close(lockedGfxDisp, tempGfxDisp); - POLY_OPA_DISP = tempGfxDisp; + GFX_ALLOC_CLOSE(gfxAllocDisp, tempGfx); R_GRAPH_TASKSET00_FLAGS |= 1;