From a0701c170b5861572eab29bc9f0aed92aa0e11d9 Mon Sep 17 00:00:00 2001 From: Anghelo Carvajal Date: Tue, 6 Jun 2023 08:54:18 -0400 Subject: [PATCH] `z64game.h` (#1250) * z64game.h * grrrrr * fix missing stuff * Update include/z64game.h Co-authored-by: engineer124 <47598039+engineer124@users.noreply.github.com> * Update include/z64game.h Co-authored-by: engineer124 <47598039+engineer124@users.noreply.github.com> * review * global.h to the top * reiew * fix --------- Co-authored-by: engineer124 <47598039+engineer124@users.noreply.github.com> --- include/functions.h | 19 ---- include/macros.h | 15 --- include/variables.h | 5 +- include/z64.h | 49 +-------- include/z64game.h | 104 ++++++++++++++++++ include/z_prenmi.h | 10 +- include/z_title_setup.h | 8 +- src/code/game.c | 6 +- src/code/z_prenmi.c | 8 +- .../gamestates/ovl_daytelop/z_daytelop.c | 3 +- .../gamestates/ovl_daytelop/z_daytelop.h | 10 +- .../ovl_file_choose/z_file_select.h | 5 + .../gamestates/ovl_opening/z_opening.c | 3 + .../gamestates/ovl_opening/z_opening.h | 18 +-- src/overlays/gamestates/ovl_select/z_select.h | 7 +- src/overlays/gamestates/ovl_title/z_title.h | 8 +- 16 files changed, 155 insertions(+), 123 deletions(-) create mode 100644 include/z64game.h diff --git a/include/functions.h b/include/functions.h index 71aa147299..626bfc261f 100644 --- a/include/functions.h +++ b/include/functions.h @@ -1874,25 +1874,6 @@ void AudioMgr_ThreadEntry(void* arg); void AudioMgr_Unlock(AudioMgr* audioMgr); void AudioMgr_Init(AudioMgr* audioMgr, void* stack, OSPri pri, OSId id, SchedContext* sched, IrqMgr* irqMgr); -void Game_UpdateFramerateVariables(s32 divisor); -void Game_SetFramerateDivisor(GameState* gameState, s32 divisor); -void GameState_SetFBFilter(Gfx** gfx, void* zbuffer); -void Game_Nop80173534(GameState* gameState); -void GameState_Draw(GameState* gameState, GraphicsContext* gfxCtx); -void GameState_SetFrameBuffer(GraphicsContext* gfxCtx); -void func_801736DC(GraphicsContext* gfxCtx); -void Game_UpdateInput(GameState* gameState); -void Game_Update(GameState* gameState); -void Game_IncrementFrameCount(GameState* gameState); -void GameState_InitArena(GameState* gameState, size_t size); -void GameState_Realloc(GameState* gameState, size_t size); -void GameState_Init(GameState* gameState, GameStateFunc init, GraphicsContext* gfxCtx); -void GameState_Destroy(GameState* gameState); -GameStateFunc GameState_GetInit(GameState* gameState); -size_t GameState_GetSize(GameState* gameState); -u32 GameState_IsRunning(GameState* gameState); -s32 GameState_GetArenaSize(GameState* gameState); -s32 func_80173B48(GameState* gameState); void GameAlloc_Log(GameAlloc* this); void* GameAlloc_Malloc(GameAlloc* this, size_t size); void GameAlloc_Free(GameAlloc* this, void* data); diff --git a/include/macros.h b/include/macros.h index 25ccb3d371..9f2305e012 100644 --- a/include/macros.h +++ b/include/macros.h @@ -28,21 +28,6 @@ #define GET_ACTIVE_CAM(play) ((play)->cameraPtrs[(play)->activeCamId]) -#define STOP_GAMESTATE(curState) \ - do { \ - GameState* state = curState; \ - \ - state->running = false; \ - } while(0) - -#define SET_NEXT_GAMESTATE(curState, nextInit, nextSize) \ - do { \ - GameState* state = curState; \ - \ - (state)->init = nextInit; \ - (state)->size = nextSize; \ - } while (0) - #define GET_PLAYER(play) ((Player*)(play)->actorCtx.actorLists[ACTORCAT_PLAYER].first) #define GET_FIRST_ENEMY(play) ((Actor*)(play)->actorCtx.actorLists[ACTORCAT_ENEMY].first) diff --git a/include/variables.h b/include/variables.h index fef772123b..ba1548f0fa 100644 --- a/include/variables.h +++ b/include/variables.h @@ -745,10 +745,7 @@ extern Input* D_801D0D60; // extern UNK_TYPE2 D_801D14E4; // extern UNK_TYPE2 D_801D14E8; // extern UNK_TYPE4 D_801D14F0; -extern s32 gFramerateDivisor; -extern f32 gFramerateDivisorF; -extern f32 gFramerateDivisorHalf; -extern f32 gFramerateDivisorThird; + extern u32 retryCount; extern u32 cfbIdx[3]; extern s32 gScreenWidth; diff --git a/include/z64.h b/include/z64.h index 637f28dc4f..1b9cabdc96 100644 --- a/include/z64.h +++ b/include/z64.h @@ -44,6 +44,7 @@ #include "z64effect.h" #include "z64frameadvance.h" #include "z64game_over.h" +#include "z64game.h" #include "z64interface.h" #include "z64item.h" #include "z64light.h" @@ -407,54 +408,6 @@ typedef struct { /* 0x844 */ void* fb; } FaultThreadStruct; // size = 0x848 -struct GameState; - -typedef void (*GameStateFunc)(struct GameState* gameState); - -typedef struct { - /* 0x00 */ void* loadedRamAddr; - /* 0x04 */ uintptr_t vromStart; // if applicable - /* 0x08 */ uintptr_t vromEnd; // if applicable - /* 0x0C */ void* vramStart; // if applicable - /* 0x10 */ void* vramEnd; // if applicable - /* 0x14 */ UNK_PTR unk_14; - /* 0x18 */ GameStateFunc init; // initializes and executes the given context - /* 0x1C */ GameStateFunc destroy; // deconstructs the context, and sets the next context to load - /* 0x20 */ UNK_PTR unk_20; - /* 0x24 */ UNK_PTR unk_24; - /* 0x28 */ UNK_TYPE unk_28; - /* 0x2C */ size_t instanceSize; -} GameStateOverlay; // size = 0x30 - -typedef struct GameAllocEntry { - /* 0x0 */ struct GameAllocEntry* next; - /* 0x4 */ struct GameAllocEntry* prev; - /* 0x8 */ size_t size; - /* 0xC */ u32 unk_0C; -} GameAllocEntry; // size = 0x10 - -typedef struct GameAlloc { - /* 0x00 */ GameAllocEntry base; - /* 0x10 */ GameAllocEntry* head; -} GameAlloc; // size = 0x14 - -typedef struct GameState { - /* 0x00 */ GraphicsContext* gfxCtx; - /* 0x04 */ GameStateFunc main; - /* 0x08 */ GameStateFunc destroy; - /* 0x0C */ GameStateFunc init; // Usually the current game state's init, though after stopping, the graph thread will look here to determine the next game state to load. - /* 0x10 */ size_t size; - /* 0x14 */ Input input[MAXCONTROLLERS]; - /* 0x74 */ TwoHeadArena heap; - /* 0x84 */ GameAlloc alloc; - /* 0x98 */ UNK_TYPE1 pad98[0x3]; - /* 0x9B */ u8 running; // If 0, switch to next game state - /* 0x9C */ u32 frames; - /* 0xA0 */ u8 padA0[0x2]; - /* 0xA2 */ u8 framerateDivisor; // game speed? - /* 0xA3 */ u8 unk_A3; -} GameState; // size = 0xA4 - struct PlayState; typedef s32 (*ColChkResetFunc)(struct PlayState*, Collider*); diff --git a/include/z64game.h b/include/z64game.h new file mode 100644 index 0000000000..c5551edc7b --- /dev/null +++ b/include/z64game.h @@ -0,0 +1,104 @@ +#ifndef Z64_GAME_H +#define Z64_GAME_H + +#include "ultra64.h" +#include "libc/stdbool.h" +#include "libc/stdint.h" +#include "io/controller.h" +#include "tha.h" +#include "padmgr.h" + +struct GraphicsContext; +struct GameState; + + +typedef void (*GameStateFunc)(struct GameState* gameState); + +typedef struct { + /* 0x00 */ void* loadedRamAddr; + /* 0x04 */ uintptr_t vromStart; // if applicable + /* 0x08 */ uintptr_t vromEnd; // if applicable + /* 0x0C */ void* vramStart; // if applicable + /* 0x10 */ void* vramEnd; // if applicable + /* 0x14 */ UNK_PTR unk_14; + /* 0x18 */ GameStateFunc init; // initializes and executes the given context + /* 0x1C */ GameStateFunc destroy; // deconstructs the context, and sets the next context to load + /* 0x20 */ UNK_PTR unk_20; + /* 0x24 */ UNK_PTR unk_24; + /* 0x28 */ UNK_TYPE unk_28; + /* 0x2C */ size_t instanceSize; +} GameStateOverlay; // size = 0x30 + +typedef struct GameAllocEntry { + /* 0x0 */ struct GameAllocEntry* next; + /* 0x4 */ struct GameAllocEntry* prev; + /* 0x8 */ size_t size; + /* 0xC */ u32 unk_0C; +} GameAllocEntry; // size = 0x10 + +typedef struct GameAlloc { + /* 0x00 */ GameAllocEntry base; + /* 0x10 */ GameAllocEntry* head; +} GameAlloc; // size = 0x14 + +typedef struct GameState { + /* 0x00 */ struct GraphicsContext* gfxCtx; + /* 0x04 */ GameStateFunc main; + /* 0x08 */ GameStateFunc destroy; + /* 0x0C */ GameStateFunc init; // Usually the current game state's init, though after stopping, the graph thread will look here to determine the next game state to load. + /* 0x10 */ size_t size; + /* 0x14 */ Input input[MAXCONTROLLERS]; + /* 0x74 */ TwoHeadArena heap; + /* 0x84 */ GameAlloc alloc; + /* 0x98 */ UNK_TYPE1 pad98[0x3]; + /* 0x9B */ u8 running; // If 0, switch to next game state + /* 0x9C */ u32 frames; + /* 0xA0 */ u8 padA0[0x2]; + /* 0xA2 */ u8 framerateDivisor; + /* 0xA3 */ u8 unk_A3; +} GameState; // size = 0xA4 + + +void Game_UpdateFramerateVariables(s32 divisor); +void Game_SetFramerateDivisor(GameState* gameState, s32 divisor); +void GameState_SetFBFilter(Gfx** gfx, void* zbuffer); +void Game_Nop80173534(GameState* gameState); +void GameState_Draw(GameState* gameState, struct GraphicsContext* gfxCtx); +void GameState_SetFrameBuffer(struct GraphicsContext* gfxCtx); +void func_801736DC(struct GraphicsContext* gfxCtx); +void Game_UpdateInput(GameState* gameState); +void Game_Update(GameState* gameState); +void Game_IncrementFrameCount(GameState* gameState); +void GameState_InitArena(GameState* gameState, size_t size); +void GameState_Realloc(GameState* gameState, size_t size); +void GameState_Init(GameState* gameState, GameStateFunc init, struct GraphicsContext* gfxCtx); +void GameState_Destroy(GameState* gameState); +GameStateFunc GameState_GetInit(GameState* gameState); +size_t GameState_GetSize(GameState* gameState); +u32 GameState_IsRunning(GameState* gameState); +s32 GameState_GetArenaSize(GameState* gameState); +s32 func_80173B48(GameState* gameState); + +extern s32 gFramerateDivisor; +extern f32 gFramerateDivisorF; +extern f32 gFramerateDivisorHalf; +extern f32 gFramerateDivisorThird; + + +#define STOP_GAMESTATE(curState) \ + do { \ + GameState* state = curState; \ + \ + state->running = false; \ + } while(0) + +#define SET_NEXT_GAMESTATE(curState, nextInit, nextSize) \ + do { \ + GameState* state = curState; \ + \ + (state)->init = nextInit; \ + (state)->size = nextSize; \ + } while (0) + + +#endif diff --git a/include/z_prenmi.h b/include/z_prenmi.h index 04c3f5f16c..7be3eb16f9 100644 --- a/include/z_prenmi.h +++ b/include/z_prenmi.h @@ -1,15 +1,15 @@ #ifndef Z64PRENMI_H #define Z64PRENMI_H -#include "global.h" - -void PreNMI_Destroy(GameState* thisx); -void PreNMI_Init(GameState* thisx); +#include "z64game.h" typedef struct { /* 0x00 */ GameState state; /* 0xA4 */ u32 timer; - /* 0xA8 */ UNK_TYPE4 unkA8; + /* 0xA8 */ s32 unk_A8; } PreNMIState; // size = 0xAC +void PreNMI_Destroy(GameState* thisx); +void PreNMI_Init(GameState* thisx); + #endif diff --git a/include/z_title_setup.h b/include/z_title_setup.h index 3b299af975..1bd7c7d7d9 100644 --- a/include/z_title_setup.h +++ b/include/z_title_setup.h @@ -1,13 +1,13 @@ #ifndef Z64TITLE_SETUP_H #define Z64TITLE_SETUP_H -#include "global.h" - -void Setup_Destroy(GameState* gameState); -void Setup_Init(GameState* thisx); +#include "z64game.h" typedef struct { /* 0x00 */ GameState state; } SetupState; // size = 0xA4 +void Setup_Destroy(GameState* gameState); +void Setup_Init(GameState* thisx); + #endif diff --git a/src/code/game.c b/src/code/game.c index c982411778..8f4e0f44d9 100644 --- a/src/code/game.c +++ b/src/code/game.c @@ -11,9 +11,9 @@ f32 gFramerateDivisorThird = 1.0f / 3.0f; void Game_UpdateFramerateVariables(s32 divisor) { gFramerateDivisor = divisor; - gFramerateDivisorF = (f32)divisor; - gFramerateDivisorHalf = (f32)(divisor * 0.5f); - gFramerateDivisorThird = (f32)(divisor / 3.0f); + gFramerateDivisorF = divisor; + gFramerateDivisorHalf = divisor / 2.0f; + gFramerateDivisorThird = divisor / 3.0f; } void Game_SetFramerateDivisor(GameState* gameState, s32 divisor) { diff --git a/src/code/z_prenmi.c b/src/code/z_prenmi.c index 94d235a378..097d4d9461 100644 --- a/src/code/z_prenmi.c +++ b/src/code/z_prenmi.c @@ -1,3 +1,4 @@ +#include "global.h" #include "z_prenmi.h" void PreNMI_Stop(PreNMIState* this) { @@ -9,10 +10,9 @@ void PreNMI_Update(PreNMIState* this) { if (this->timer == 0) { ViConfig_UpdateVi(1); PreNMI_Stop(this); - return; + } else { + this->timer--; } - - this->timer--; } void PreNMI_Draw(PreNMIState* this) { @@ -48,7 +48,7 @@ void PreNMI_Init(GameState* thisx) { this->state.main = PreNMI_Main; this->state.destroy = PreNMI_Destroy; this->timer = 30; - this->unkA8 = 10; + this->unk_A8 = 10; Game_SetFramerateDivisor(&this->state, 1); } diff --git a/src/overlays/gamestates/ovl_daytelop/z_daytelop.c b/src/overlays/gamestates/ovl_daytelop/z_daytelop.c index 108ccedcd7..134c6457b8 100644 --- a/src/overlays/gamestates/ovl_daytelop/z_daytelop.c +++ b/src/overlays/gamestates/ovl_daytelop/z_daytelop.c @@ -4,9 +4,10 @@ * Description: "Dawn of ... day" screen */ +#include "global.h" #include "z_daytelop.h" +#include "z64save.h" #include "z64shrink_window.h" -#include "z64view.h" #include "misc/daytelop_static/daytelop_static.h" #include "interface/icon_item_gameover_static/icon_item_gameover_static.h" diff --git a/src/overlays/gamestates/ovl_daytelop/z_daytelop.h b/src/overlays/gamestates/ovl_daytelop/z_daytelop.h index a7350e8984..da49827c36 100644 --- a/src/overlays/gamestates/ovl_daytelop/z_daytelop.h +++ b/src/overlays/gamestates/ovl_daytelop/z_daytelop.h @@ -1,16 +1,13 @@ #ifndef Z64_DAYTELOP_H #define Z64_DAYTELOP_H -#include "global.h" - -void DayTelop_Init(GameState* thisx); -void DayTelop_Destroy(GameState* thisx); +#include "z64game.h" +#include "z64view.h" typedef struct { /* 0x000 */ GameState state; /* 0x0A4 */ void* daytelopStaticFile; /* 0x0A8 */ void* gameoverStaticFile; - /* 0x0AC */ UNK_TYPE1 unk_AC[0x4]; /* 0x0B0 */ View view; /* 0x218 */ UNK_TYPE1 unk_218[0x28]; /* 0x240 */ s16 transitionCountdown; @@ -24,4 +21,7 @@ typedef enum { /* 2 */ DAYTELOP_HOURSTEXT_ON } DaytelopFadeState; +void DayTelop_Init(GameState* thisx); +void DayTelop_Destroy(GameState* thisx); + #endif diff --git a/src/overlays/gamestates/ovl_file_choose/z_file_select.h b/src/overlays/gamestates/ovl_file_choose/z_file_select.h index 7dcc72a9da..88236e63e8 100644 --- a/src/overlays/gamestates/ovl_file_choose/z_file_select.h +++ b/src/overlays/gamestates/ovl_file_choose/z_file_select.h @@ -2,6 +2,11 @@ #define FILE_SELECT_H #include "global.h" +#include "z64game.h" +#include "z64message.h" +#include "z64skybox.h" +#include "z64view.h" + // `sramCtx->noFlashSaveBuf` is never allocated space, so should never be used // Slot offsets are also based on OoT SaveContext sizes, and contains incorrect sizes from MM diff --git a/src/overlays/gamestates/ovl_opening/z_opening.c b/src/overlays/gamestates/ovl_opening/z_opening.c index 9697b111ff..84425cc732 100644 --- a/src/overlays/gamestates/ovl_opening/z_opening.c +++ b/src/overlays/gamestates/ovl_opening/z_opening.c @@ -4,9 +4,12 @@ * Description: Initializes the game into the title screen */ +#include "global.h" #include "z_opening.h" +#include "z64save.h" #include "z64shrink_window.h" #include "z64view.h" +#include "regs.h" void TitleSetup_SetupTitleScreen(TitleSetupState* this) { static s32 sOpeningEntrances[] = { ENTRANCE(CUTSCENE, 0), ENTRANCE(CUTSCENE, 1) }; diff --git a/src/overlays/gamestates/ovl_opening/z_opening.h b/src/overlays/gamestates/ovl_opening/z_opening.h index 36a01ffe93..530b069e1a 100644 --- a/src/overlays/gamestates/ovl_opening/z_opening.h +++ b/src/overlays/gamestates/ovl_opening/z_opening.h @@ -1,15 +1,15 @@ -#ifndef _Z64_OPENING_H_ -#define _Z64_OPENING_H_ +#ifndef Z64_OPENING_H +#define Z64_OPENING_H -#include "global.h" +#include "z64game.h" +#include "z64view.h" + +typedef struct { + /* 0x000 */ GameState state; + /* 0x0A8 */ View view; +} TitleSetupState; // size = 0x210 void TitleSetup_Init(GameState* thisx); void TitleSetup_Destroy(GameState* thisx); -typedef struct { - /* 0x000 */ GameState state; - /* 0x0A4 */ UNK_TYPE1 unk_A4[0x4]; - /* 0x0A8 */ View view; -} TitleSetupState; // size = 0x210 - #endif diff --git a/src/overlays/gamestates/ovl_select/z_select.h b/src/overlays/gamestates/ovl_select/z_select.h index 8b5974103f..876c9e3ff7 100644 --- a/src/overlays/gamestates/ovl_select/z_select.h +++ b/src/overlays/gamestates/ovl_select/z_select.h @@ -1,7 +1,8 @@ -#ifndef _Z64_SELECT_H_ -#define _Z64_SELECT_H_ +#ifndef Z64_SELECT_H +#define Z64_SELECT_H -#include "global.h" +#include "z64game.h" +#include "z64view.h" struct MapSelectState; diff --git a/src/overlays/gamestates/ovl_title/z_title.h b/src/overlays/gamestates/ovl_title/z_title.h index 0cb19da1a6..ee5d5c841b 100644 --- a/src/overlays/gamestates/ovl_title/z_title.h +++ b/src/overlays/gamestates/ovl_title/z_title.h @@ -2,9 +2,8 @@ #define Z64_TITLE_H #include "global.h" - -void ConsoleLogo_Init(GameState* thisx); -void ConsoleLogo_Destroy(GameState* thisx); +#include "z64game.h" +#include "z64view.h" typedef struct { /* 0x000 */ GameState state; @@ -20,4 +19,7 @@ typedef struct { /* 0x244 */ u8 exit; } ConsoleLogoState; // size = 0x248 +void ConsoleLogo_Init(GameState* thisx); +void ConsoleLogo_Destroy(GameState* thisx); + #endif