From 7b1cb9af9c8a7266e0ad64a3150e8a93ac5ad3ce Mon Sep 17 00:00:00 2001 From: briaguya Date: Sun, 29 May 2022 00:19:51 -0400 Subject: [PATCH] got the hookshots rendering --- .../libultraship/Lib/Fast3D/gfx_sdl2.cpp | 10 +++ soh/soh/Enhancements/randomizer.cpp | 71 ++++++++++++------- soh/soh/Enhancements/randomizer.h | 4 +- soh/soh/OTRGlobals.cpp | 8 +-- soh/soh/OTRGlobals.h | 1 + .../ovl_file_choose/z_file_choose.c | 67 ++++++++++++++++- .../ovl_file_choose/z_file_nameset_PAL.c | 2 +- 7 files changed, 130 insertions(+), 33 deletions(-) diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_sdl2.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_sdl2.cpp index e80097c81..8d3c529cb 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_sdl2.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_sdl2.cpp @@ -132,6 +132,8 @@ static int frameDivisor = 1; static void gfx_sdl_init(const char *game_name, bool start_in_fullscreen) { SDL_Init(SDL_INIT_VIDEO); + SDL_EventState(SDL_DROPFILE, SDL_ENABLE); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); @@ -227,8 +229,11 @@ static void gfx_sdl_onkeyup(int scancode) { } } +extern "C" void LoadItemLocations(const char* spoilerFileName); + static void gfx_sdl_handle_events(void) { SDL_Event event; + char* dropped_filedir; while (SDL_PollEvent(&event)) { SohImGui::EventImpl event_impl; event_impl.sdl = { &event }; @@ -249,6 +254,11 @@ static void gfx_sdl_handle_events(void) { window_height = event.window.data2; } break; + case SDL_DROPFILE: + { + LoadItemLocations(event.drop.file); + break; + } case SDL_QUIT: exit(0); } diff --git a/soh/soh/Enhancements/randomizer.cpp b/soh/soh/Enhancements/randomizer.cpp index c9c44d203..6b0a934b0 100644 --- a/soh/soh/Enhancements/randomizer.cpp +++ b/soh/soh/Enhancements/randomizer.cpp @@ -4,8 +4,8 @@ #include #include #include -#include -#include +#include +#include using json = nlohmann::json; @@ -643,41 +643,64 @@ s16 Randomizer::GetItemModelFromId(s16 itemId) { return itemIdToModel[itemId]; } -void Randomizer::LoadItemLocations() { +void Randomizer::LoadItemLocations(const char* spoilerFileName) { // bandaid until new save stuff happens - ParseItemLocations(""); + ParseItemLocations(spoilerFileName); for(auto itemLocation : gSaveContext.itemLocations) { this->itemLocations[itemLocation.check] = itemLocation.get; } } -void Randomizer::ParseItemLocations(std::string spoilerFileName) { +void Randomizer::ParseItemLocations(const char* spoilerFileName) { // todo pull this in from cvar or something - std::ifstream spoilerFileStream("spoiler.json"); + std::ifstream spoilerFileStream(spoilerFileName); if (!spoilerFileStream) return; - json spoilerFileJson; - spoilerFileStream >> spoilerFileJson; - json locationsJson = spoilerFileJson["locations"]; - int index = 0; - for (auto it = locationsJson.begin(); it != locationsJson.end(); ++it) { - if (it->is_structured()) { - json itemJson = *it; - for (auto itemit = itemJson.begin(); itemit != itemJson.end(); ++itemit) { - // todo handle prices - if (itemit.key() == "item") { - gSaveContext.itemLocations[index].check = SpoilerfileCheckNameToEnum[it.key()]; - gSaveContext.itemLocations[index].get = SpoilerfileGetNameToEnum[itemit.value()]; - } - } - } else { - gSaveContext.itemLocations[index].check = SpoilerfileCheckNameToEnum[it.key()]; - gSaveContext.itemLocations[index].get = SpoilerfileGetNameToEnum[it.value()]; + bool success = false; + + try { + json spoilerFileJson; + spoilerFileStream >> spoilerFileJson; + json locationsJson = spoilerFileJson["locations"]; + json hashJson = spoilerFileJson["file_hash"]; + + int index = 0; + for (auto it = hashJson.begin(); it != hashJson.end(); ++it) { + //gSaveContext.seedIcons[index] = gSeedTextures[it.value()]; + index++; } - index++; + index = 0; + for (auto it = locationsJson.begin(); it != locationsJson.end(); ++it) { + if (it->is_structured()) { + json itemJson = *it; + for (auto itemit = itemJson.begin(); itemit != itemJson.end(); ++itemit) { + // todo handle prices + if (itemit.key() == "item") { + + gSaveContext.itemLocations[index].check = SpoilerfileCheckNameToEnum[it.key()]; + gSaveContext.itemLocations[index].get = SpoilerfileGetNameToEnum[itemit.value()]; + } + } + } else { + gSaveContext.itemLocations[index].check = SpoilerfileCheckNameToEnum[it.key()]; + gSaveContext.itemLocations[index].get = SpoilerfileGetNameToEnum[it.value()]; + } + + index++; + } + + Audio_PlaySoundGeneral(NA_SE_SY_CORRECT_CHIME, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); + success = true; + } catch (const std::exception& e) { + Audio_PlaySoundGeneral(NA_SE_SY_ERROR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); + return; + } + + if (success) { + CVar_SetS32("gRandomizer", 1); } } diff --git a/soh/soh/Enhancements/randomizer.h b/soh/soh/Enhancements/randomizer.h index 50da6b21e..3a8550a1a 100644 --- a/soh/soh/Enhancements/randomizer.h +++ b/soh/soh/Enhancements/randomizer.h @@ -21,8 +21,8 @@ class Randomizer { ~Randomizer(); s16 GetItemModelFromId(s16 itemId); - void LoadItemLocations(); - void ParseItemLocations(std::string spoilerfilename); + void LoadItemLocations(const char* spoilerFileName); + void ParseItemLocations(const char* spoilerFileName); s32 GetRandomizedItemId(GetItemID ogId, s16 actorId = -1, s16 sceneNum = -1, s16 actorParams = -1, s32 homePosX = 0, s32 homePosY = 0, s32 homePosZ = 0); }; diff --git a/soh/soh/OTRGlobals.cpp b/soh/soh/OTRGlobals.cpp index 791c1eb49..4799478bc 100644 --- a/soh/soh/OTRGlobals.cpp +++ b/soh/soh/OTRGlobals.cpp @@ -1000,12 +1000,12 @@ extern "C" s16 GetItemModelFromId(s16 itemId) { return OTRGlobals::Instance->gRandomizer->GetItemModelFromId(itemId); } -extern "C" void LoadItemLocations() { - OTRGlobals::Instance->gRandomizer->LoadItemLocations(); +extern "C" void LoadItemLocations(const char* spoilerFileName) { + OTRGlobals::Instance->gRandomizer->LoadItemLocations(spoilerFileName); } -extern "C" void ParseItemLocations(const char* spoilerfilename) { - OTRGlobals::Instance->gRandomizer->ParseItemLocations(spoilerfilename); +extern "C" void ParseItemLocations(const char* spoilerFileName) { + OTRGlobals::Instance->gRandomizer->ParseItemLocations(spoilerFileName); } extern "C" s32 GetRandomizedItemId(GetItemID ogId, s16 sceneNum, s16 actorParams) { diff --git a/soh/soh/OTRGlobals.h b/soh/soh/OTRGlobals.h index 77b4a92d2..c31e3e9af 100644 --- a/soh/soh/OTRGlobals.h +++ b/soh/soh/OTRGlobals.h @@ -74,6 +74,7 @@ int AudioPlayer_GetDesiredBuffered(void); void AudioPlayer_Play(const uint8_t* buf, uint32_t len); void AudioMgr_CreateNextAudioBuffer(s16* samples, u32 num_samples); int Controller_ShouldRumble(size_t i); +void LoadItemLocations(const char* spoilerFileName); void ParseItemLocations(const char* spoilerfilename); s32 GetRandomizedItemId(GetItemID ogId, s16 sceneNum, s16 actorParams); s32 GetRandomizedItemIdFromActor(GetItemID ogId, s16 actorId); diff --git a/soh/src/overlays/gamestates/ovl_file_choose/z_file_choose.c b/soh/src/overlays/gamestates/ovl_file_choose/z_file_choose.c index 2ef733324..13da4f664 100644 --- a/soh/src/overlays/gamestates/ovl_file_choose/z_file_choose.c +++ b/soh/src/overlays/gamestates/ovl_file_choose/z_file_choose.c @@ -4,6 +4,7 @@ #include "textures/title_static/title_static.h" #include "textures/parameter_static/parameter_static.h" +#include static s16 sUnused = 106; @@ -63,6 +64,7 @@ void FileChoose_InitModeUpdate(GameState* thisx) { this->configMode = CM_FADE_IN_START; this->nextTitleLabel = FS_TITLE_OPEN_FILE; osSyncPrintf("Sram Start─Load 》》》》》 "); + CVar_SetS32("gRandomizer", 0); Sram_VerifyAndLoadAllSaves(this, &this->sramCtx); osSyncPrintf("終了!!!\n"); } @@ -163,6 +165,65 @@ void FileChoose_FinishFadeIn(GameState* thisx) { } } +typedef struct { + char tex[512]; + uint16_t width; + uint16_t height; + uint8_t im_fmt; + uint8_t im_siz; +} Sprite; + +Sprite sprDPad = { gHookshotIconTex, 32, 32, G_IM_FMT_RGBA, G_IM_SIZ_32b }; + +void SpriteLoad(FileChooseContext* this, Sprite* sprite) { + OPEN_DISPS(this->state.gfxCtx, "gfx.c", 12); + + if (sprite->im_siz == G_IM_SIZ_16b) { + gDPLoadTextureBlock(POLY_OPA_DISP++, sprite->tex, sprite->im_fmt, + G_IM_SIZ_16b, // @TEMP until I figure out how to use sprite->im_siz + sprite->width, sprite->height, 0, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMIRROR | G_TX_WRAP, + G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, G_TX_NOLOD); + } else { + gDPLoadTextureBlock(POLY_OPA_DISP++, sprite->tex, sprite->im_fmt, + G_IM_SIZ_32b, // @TEMP until I figure out how to use sprite->im_siz + sprite->width, sprite->height, 0, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMIRROR | G_TX_WRAP, + G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, G_TX_NOLOD); + } + + CLOSE_DISPS(this->state.gfxCtx, "gfx.c", 40); +} + +void SpriteDraw(FileChooseContext* this, Sprite* sprite, int left, int top, int width, int height) { + int width_factor = (1 << 10) * sprite->width / width; + int height_factor = (1 << 10) * sprite->height / height; + + OPEN_DISPS(this->state.gfxCtx, "gfx.c", 51); + + gSPWideTextureRectangle(POLY_OPA_DISP++, left << 2, top << 2, (left + width) << 2, (top + height) << 2, + G_TX_RENDERTILE, + 0, 0, width_factor, height_factor); + + CLOSE_DISPS(this->state.gfxCtx, "gfx.c", 62); +} + +void DrawSeedHashSprites(FileChooseContext* this) { + OPEN_DISPS(this->state.gfxCtx, "dpad.c", 60); + gDPPipeSync(POLY_OPA_DISP++); + gDPSetCombineMode(POLY_OPA_DISP++, G_CC_MODULATEIA_PRIM, G_CC_MODULATEIA_PRIM); + gDPSetPrimColor(POLY_OPA_DISP++, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF); + + // Draw Seed Icons + u16 xStart = 64; + for (u8 i = 0; i < 5; i++) { + SpriteLoad(this, &sprDPad); + SpriteDraw(this, &sprDPad, xStart + (40 * i), 10, 24, 24); + } + + gDPPipeSync(POLY_OPA_DISP++); + + CLOSE_DISPS(this->state.gfxCtx, "dpad.c", 113); +} + /** * Update the cursor and wait for the player to select a button to change menus accordingly. * If an empty file is selected, enter the name entry config mode. @@ -814,6 +875,8 @@ void FileChoose_DrawFileInfo(GameState* thisx, s16 fileIndex, s16 isActive) { s16 j; s16 deathCountSplit[3]; + DrawSeedHashSprites(this); + if (1) {} OPEN_DISPS(this->state.gfxCtx, "../z_file_choose.c", 1709); @@ -1458,7 +1521,7 @@ void FileChoose_LoadGame(GameState* thisx) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_DECIDE_L, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); gSaveContext.fileNum = this->buttonIndex; Sram_OpenSave(&this->sramCtx); - LoadItemLocations(); + // LoadItemLocations(); gSaveContext.gameMode = 0; SET_NEXT_GAMESTATE(&this->state, Select_Init, SelectContext); this->state.running = false; @@ -1466,7 +1529,7 @@ void FileChoose_LoadGame(GameState* thisx) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_DECIDE_L, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); gSaveContext.fileNum = this->buttonIndex; Sram_OpenSave(&this->sramCtx); - LoadItemLocations(); + // LoadItemLocations(); gSaveContext.gameMode = 0; SET_NEXT_GAMESTATE(&this->state, Gameplay_Init, GlobalContext); this->state.running = false; diff --git a/soh/src/overlays/gamestates/ovl_file_choose/z_file_nameset_PAL.c b/soh/src/overlays/gamestates/ovl_file_choose/z_file_nameset_PAL.c index 82f5c4b17..32a2c1f83 100644 --- a/soh/src/overlays/gamestates/ovl_file_choose/z_file_nameset_PAL.c +++ b/soh/src/overlays/gamestates/ovl_file_choose/z_file_nameset_PAL.c @@ -449,7 +449,7 @@ void FileChoose_DrawNameEntry(GameState* thisx) { dayTime = ((void)0, gSaveContext.dayTime); - ParseItemLocations("blarg"); + // ParseItemLocations("blarg"); Sram_InitSave(this, &this->sramCtx);