diff --git a/soh/include/variables.h b/soh/include/variables.h index f5486f761..e3408eef6 100644 --- a/soh/include/variables.h +++ b/soh/include/variables.h @@ -247,6 +247,7 @@ extern "C" extern GfxPool gGfxPools[2]; // 0x24820 bytes extern u8* gAudioHeap; extern u8* gSystemHeap; + extern GameState* gGameState; #ifdef __cplusplus }; diff --git a/soh/soh/Enhancements/ResetGameplayFrames.cpp b/soh/soh/Enhancements/ResetGameplayFrames.cpp new file mode 100644 index 000000000..3b234dbe5 --- /dev/null +++ b/soh/soh/Enhancements/ResetGameplayFrames.cpp @@ -0,0 +1,30 @@ +#include "soh/Enhancements/game-interactor/GameInteractor_Hooks.h" +#include "soh/ShipInit.hpp" + +extern "C" { +#include "functions.h" +#include "variables.h" +} + +void ResetGameplayFramesOnSoftReset() { + PlayState* play = (PlayState*)gGameState; + + if (gGameState->init == TitleSetup_Init) { + play->gameplayFrames = 0; + } +} + +void ResetGameplayFramesOnTitleScreenExit() { + PlayState* play = (PlayState*)gGameState; + + if (gSaveContext.fileNum == 0xFF) { + play->gameplayFrames = 0; + } +} + +void RegisterResetGameplayFrames() { + COND_HOOK(OnPlayDestroy, true, ResetGameplayFramesOnSoftReset); + COND_HOOK(OnPlayDestroy, true, ResetGameplayFramesOnTitleScreenExit); +} + +static RegisterShipInitFunc initFunc(RegisterResetGameplayFrames); diff --git a/soh/soh/Enhancements/debugconsole.cpp b/soh/soh/Enhancements/debugconsole.cpp index 9ef0fe2ab..7037f9b0a 100644 --- a/soh/soh/Enhancements/debugconsole.cpp +++ b/soh/soh/Enhancements/debugconsole.cpp @@ -204,14 +204,13 @@ static bool SetPosHandler(std::shared_ptr Console, const std::vec } static bool ResetHandler(std::shared_ptr Console, std::vector args, std::string* output) { - if (gPlayState == nullptr) { - ERROR_MESSAGE("PlayState == nullptr"); + if (gGameState == nullptr) { + ERROR_MESSAGE("gGameState == nullptr"); return 1; } - gPlayState->gameplayFrames = 0; - SET_NEXT_GAMESTATE(&gPlayState->state, TitleSetup_Init, GameState); - gPlayState->state.running = false; + SET_NEXT_GAMESTATE(gGameState, TitleSetup_Init, GameState); + gGameState->running = false; GameInteractor::Instance->ExecuteHooks(gSaveContext.fileNum); return 0; } diff --git a/soh/src/code/game.c b/soh/src/code/game.c index d7524009b..7e1db9c17 100644 --- a/soh/src/code/game.c +++ b/soh/src/code/game.c @@ -14,6 +14,10 @@ ViMode sViMode; FaultClient sGameFaultClient; u16 sLastButtonPressed; +// #region SOH [General] Making gGameState available +GameState* gGameState; +// #endregion + // Forward declared, because this in a C++ header. int gfx_create_framebuffer(uint32_t width, uint32_t height, uint32_t native_width, uint32_t native_height, uint8_t resize); void gfx_texture_cache_clear(); diff --git a/soh/src/code/graph.c b/soh/src/code/graph.c index f691c2619..c55417d93 100644 --- a/soh/src/code/graph.c +++ b/soh/src/code/graph.c @@ -18,7 +18,6 @@ // SOH [Port] Game State management for our render loop static struct RunFrameContext { GraphicsContext gfxCtx; - GameState* gameState; GameStateOverlay* nextOvl; GameStateOverlay* ovl; int state; @@ -487,28 +486,28 @@ static void RunFrame() size = runFrameContext.ovl->instanceSize; osSyncPrintf("クラスサイズ=%dバイト\n", size); // "Class size = %d bytes" - runFrameContext.gameState = SYSTEM_ARENA_MALLOC_DEBUG(size); + gGameState = SYSTEM_ARENA_MALLOC_DEBUG(size); - if (!runFrameContext.gameState) + if (!gGameState) { osSyncPrintf("確保失敗\n"); // "Failure to secure" snprintf(faultMsg, sizeof(faultMsg), "CLASS SIZE= %d bytes", size); Fault_AddHungupAndCrashImpl("GAME CLASS MALLOC FAILED", faultMsg); } - GameState_Init(runFrameContext.gameState, runFrameContext.ovl->init, &runFrameContext.gfxCtx); + GameState_Init(gGameState, runFrameContext.ovl->init, &runFrameContext.gfxCtx); // Setup the normal skybox once before entering any game states to avoid the 0xabababab crash. // The crash is due to certain skyboxes not loading all the data they need from Skybox_Setup. if (!hasSetupSkybox) { - PlayState* play = (PlayState*)runFrameContext.gameState; + PlayState* play = (PlayState*)gGameState; Skybox_Setup(play, &play->skyboxCtx, SKYBOX_NORMAL_SKY); hasSetupSkybox = true; } uint64_t freq = GetFrequency(); - while (GameState_IsRunning(runFrameContext.gameState)) + while (GameState_IsRunning(gGameState)) { //uint64_t ticksA, ticksB; //ticksA = GetPerfCounter(); @@ -517,7 +516,7 @@ static void RunFrame() PadMgr_ThreadEntry(&gPadMgr); - Graph_Update(&runFrameContext.gfxCtx, runFrameContext.gameState); + Graph_Update(&runFrameContext.gfxCtx, gGameState); //ticksB = GetPerfCounter(); if (GfxDebuggerIsDebuggingRequested()) { @@ -535,9 +534,9 @@ static void RunFrame() nextFrame:; } - runFrameContext.nextOvl = Graph_GetNextGameState(runFrameContext.gameState); - GameState_Destroy(runFrameContext.gameState); - SYSTEM_ARENA_FREE_DEBUG(runFrameContext.gameState); + runFrameContext.nextOvl = Graph_GetNextGameState(gGameState); + GameState_Destroy(gGameState); + SYSTEM_ARENA_FREE_DEBUG(gGameState); Overlay_FreeGameState(runFrameContext.ovl); } Graph_Destroy(&runFrameContext.gfxCtx); diff --git a/soh/src/code/z_play.c b/soh/src/code/z_play.c index 2b4743417..8d35a33c4 100644 --- a/soh/src/code/z_play.c +++ b/soh/src/code/z_play.c @@ -205,11 +205,6 @@ void Play_Destroy(GameState* thisx) { GameInteractor_ExecuteOnPlayDestroy(); - // Only initialize the frame counter when exiting the title screen - if (gSaveContext.fileNum == 0xFF) { - play->gameplayFrames = 0; - } - play->state.gfxCtx->callback = NULL; play->state.gfxCtx->callbackParam = 0;