From f20ab2c2600d9e07b342a226f07fc98721dbc8fc Mon Sep 17 00:00:00 2001 From: MaikelChan Date: Fri, 8 Apr 2022 23:23:24 +0200 Subject: [PATCH 01/13] Fixed transparent texture making framebuffers also transparent in D3D11. (#84) This happened with the Mirror Shield in the inventory screen preview. --- libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp index c989290c6..3481a4db2 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp @@ -531,8 +531,8 @@ static struct ShaderProgram *gfx_d3d11_create_and_load_new_shader(uint64_t shade blend_desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; blend_desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; blend_desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; - blend_desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; - blend_desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; + blend_desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; + blend_desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; blend_desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; blend_desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; } else { From 3e8c48c1162b004f160e727ea8704885dc0d1dd5 Mon Sep 17 00:00:00 2001 From: rozlette Date: Sun, 3 Apr 2022 14:47:24 -0500 Subject: [PATCH 02/13] Add save editor --- .../Lib/Fast3D/gfx_direct3d11.cpp | 4 +- libultraship/libultraship/SohImGuiImpl.cpp | 67 ++- libultraship/libultraship/SohImGuiImpl.h | 14 +- soh/soh.vcxproj | 4 + soh/soh.vcxproj.filters | 18 + .../Enhancements/debugger/debugSaveEditor.cpp | 527 ++++++++++++++++++ .../Enhancements/debugger/debugSaveEditor.h | 3 + soh/soh/Enhancements/debugger/debugger.cpp | 6 + soh/soh/Enhancements/debugger/debugger.h | 3 + soh/soh/OTRGlobals.cpp | 2 + 10 files changed, 641 insertions(+), 7 deletions(-) create mode 100644 soh/soh/Enhancements/debugger/debugSaveEditor.cpp create mode 100644 soh/soh/Enhancements/debugger/debugSaveEditor.h create mode 100644 soh/soh/Enhancements/debugger/debugger.cpp create mode 100644 soh/soh/Enhancements/debugger/debugger.h diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp index 3481a4db2..9c2f46422 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp @@ -977,8 +977,8 @@ uint16_t gfx_d3d11_get_pixel_depth_old(float x, float y) { } // namespace -void* SohImGui::GetTextureByID(int id) { - return d3d.textures[id].resource_view.Get(); +ImTextureID SohImGui::GetTextureByID(int id) { + return impl.backend == Backend::DX11 ? d3d.textures[id].resource_view.Get() : reinterpret_cast(id); } struct GfxRenderingAPI gfx_direct3d11_api = { diff --git a/libultraship/libultraship/SohImGuiImpl.cpp b/libultraship/libultraship/SohImGuiImpl.cpp index bdf9c267b..7489d14a2 100644 --- a/libultraship/libultraship/SohImGuiImpl.cpp +++ b/libultraship/libultraship/SohImGuiImpl.cpp @@ -1,6 +1,7 @@ #include "SohImGuiImpl.h" #include +#include #include #include "Archive.h" @@ -14,9 +15,11 @@ #include "TextureMod.h" #include "Window.h" #include "Cvar.h" +#include "Texture.h" #include "../Fast3D/gfx_pc.h" #include "Lib/stb/stb_image.h" #include "Lib/Fast3D/gfx_rendering_api.h" +#include "Lib/spdlog/include/spdlog/common.h" #include "Utils/StringHelper.h" #ifdef ENABLE_OPENGL @@ -51,6 +54,9 @@ namespace SohImGui { bool p_open = false; bool needs_save = false; + std::map> windowCategories; + std::map customWindows; + void ImGuiWMInit() { switch (impl.backend) { case Backend::SDL: @@ -153,7 +159,7 @@ namespace SohImGui { } } - void LoadTexture(std::string name, std::string path) { + void LoadTexture(const std::string& name, const std::string& path) { GfxRenderingAPI* api = gfx_get_current_rendering_api(); const auto res = GlobalCtx2::GetInstance()->GetResourceManager()->LoadFile(normalize(path)); @@ -173,6 +179,25 @@ namespace SohImGui { stbi_image_free(img_data); } + void LoadResource(const std::string& name, const std::string& path) { + GfxRenderingAPI* api = gfx_get_current_rendering_api(); + const auto res = static_cast(GlobalCtx2::GetInstance()->GetResourceManager()->LoadResource(normalize(path)).get()); + + if (res->texType != Ship::TextureType::RGBA32bpp) { + // TODO convert other image types + SPDLOG_WARN("SohImGui::LoadResource: Attempting to load unsupporting image type %s", path.c_str()); + return; + } + + const auto asset = new GameAsset{ api->new_texture() }; + + api->select_texture(0, asset->textureId); + api->set_sampler_parameters(0, false, 0, 0); + api->upload_texture(res->imageData, res->width, res->height); + + DefaultAssets[name] = asset; + } + void Init(WindowImpl window_impl) { impl = window_impl; Game::LoadSettings(); @@ -219,7 +244,7 @@ namespace SohImGui { ImGuiProcessEvent(event); } -#define BindButton(btn, status) ImGui::Image(impl.backend == Backend::DX11 ? GetTextureByID(DefaultAssets[btn]->textureId) : (ImTextureID)(DefaultAssets[btn]->textureId), ImVec2(16.0f * scale, 16.0f * scale), ImVec2(0, 0), ImVec2(1.0f, 1.0f), ImVec4(255, 255, 255, (status) ? 255 : 0)); +#define BindButton(btn, status) ImGui::Image(GetTextureByID(DefaultAssets[btn]->textureId), ImVec2(16.0f * scale, 16.0f * scale), ImVec2(0, 0), ImVec2(1.0f, 1.0f), ImVec4(255, 255, 255, (status) ? 255 : 0)); void BindAudioSlider(const char* name, const char* key, float* value, SeqPlayers playerId) { ImGui::Text(name, static_cast(100 * *(value))); @@ -278,7 +303,7 @@ namespace SohImGui { if (ImGui::BeginMenuBar()) { if (DefaultAssets.contains("Game_Icon")) { ImGui::SetCursorPos(ImVec2(5, 2.5f)); - ImGui::Image(impl.backend == Backend::DX11 ? GetTextureByID(DefaultAssets["Game_Icon"]->textureId) : reinterpret_cast(DefaultAssets["Game_Icon"]->textureId), ImVec2(16.0f, 16.0f)); + ImGui::Image(GetTextureByID(DefaultAssets["Game_Icon"]->textureId), ImVec2(16.0f, 16.0f)); ImGui::SameLine(); ImGui::SetCursorPos(ImVec2(25, 0)); } @@ -425,6 +450,15 @@ namespace SohImGui { ImGui::EndMenu(); } + for (const auto& category : windowCategories) { + if (ImGui::BeginMenu(category.first.c_str())) { + for (const std::string& name : category.second) { + HOOK(ImGui::MenuItem(name.c_str(), nullptr, &customWindows[name].enabled)); + } + ImGui::EndMenu(); + } + } + ImGui::EndMenuBar(); } @@ -527,6 +561,13 @@ namespace SohImGui { console->Draw(); + for (auto& windowIter : customWindows) { + CustomWindow& window = windowIter.second; + if (window.drawFunc != nullptr) { + window.drawFunc(window.enabled); + } + } + ImGui::Render(); ImGuiRenderDrawData(ImGui::GetDrawData()); if (UseViewports()) { @@ -538,4 +579,22 @@ namespace SohImGui { void BindCmd(const std::string& cmd, CommandEntry entry) { console->Commands[cmd] = std::move(entry); } -} + + void AddWindow(const std::string& category, const std::string& name, WindowDrawFunc drawFunc) { + if (customWindows.contains(name)) { + SPDLOG_ERROR("SohImGui::AddWindow: Attempting to add duplicate window name %s", name.c_str()); + return; + } + + customWindows[name] = { + .enabled = false, + .drawFunc = drawFunc + }; + + windowCategories[category].emplace_back(name); + } + + ImTextureID GetTextureByName(const std::string& name) { + return GetTextureByID(DefaultAssets[name]->textureId); + } +} \ No newline at end of file diff --git a/libultraship/libultraship/SohImGuiImpl.h b/libultraship/libultraship/SohImGuiImpl.h index b7c946eff..8275e4735 100644 --- a/libultraship/libultraship/SohImGuiImpl.h +++ b/libultraship/libultraship/SohImGuiImpl.h @@ -47,11 +47,23 @@ namespace SohImGui { } sdl; } EventImpl; + extern WindowImpl impl; + + using WindowDrawFunc = void(*)(bool& enabled); + + typedef struct { + bool enabled; + WindowDrawFunc drawFunc; + } CustomWindow; + extern Console* console; void Init(WindowImpl window_impl); void Update(EventImpl event); void Draw(void); void ShowCursor(bool hide, Dialogues w); void BindCmd(const std::string& cmd, CommandEntry entry); - void* GetTextureByID(int id); + void AddWindow(const std::string& category, const std::string& name, WindowDrawFunc drawFunc); + void LoadResource(const std::string& name, const std::string& path); + ImTextureID GetTextureByID(int id); + ImTextureID GetTextureByName(const std::string& name); } diff --git a/soh/soh.vcxproj b/soh/soh.vcxproj index aa6dee304..5560bde76 100644 --- a/soh/soh.vcxproj +++ b/soh/soh.vcxproj @@ -178,6 +178,8 @@ + + @@ -922,6 +924,8 @@ + + diff --git a/soh/soh.vcxproj.filters b/soh/soh.vcxproj.filters index 30ca9d4ed..de9ad6f96 100644 --- a/soh/soh.vcxproj.filters +++ b/soh/soh.vcxproj.filters @@ -76,6 +76,12 @@ {18b9727f-30de-4ab8-a317-916090d4a110} + + {9a4378ec-e30f-47b6-9ad6-5ce738b4cf99} + + + {04fc1c52-49ff-48e2-ae23-2c00867374f8} + @@ -2169,6 +2175,12 @@ Source Files\src\overlays\actors + + Source Files\soh\Enhancements\debugger + + + Source Files\soh\Enhancements\debugger + @@ -3710,6 +3722,12 @@ Header Files + + Header Files\soh\Enhancements\debugger + + + Header Files\soh\Enhancements\debugger + diff --git a/soh/soh/Enhancements/debugger/debugSaveEditor.cpp b/soh/soh/Enhancements/debugger/debugSaveEditor.cpp new file mode 100644 index 000000000..95e2f912c --- /dev/null +++ b/soh/soh/Enhancements/debugger/debugSaveEditor.cpp @@ -0,0 +1,527 @@ +#include "debugSaveEditor.h" +#include "../libultraship/SohImGuiImpl.h" + +#include +#include +#include + +extern "C" { +#include +#include "variables.h" +#include "functions.h" +#include "macros.h" +extern GlobalContext* gGlobalCtx; + +#include "textures/icon_item_static/icon_item_static.h" +} + +typedef struct { + uint32_t id; + std::string name; + std::string texturePath; +} ItemMapEntry; + +#define ITEM_MAP_ENTRY(id) \ + { \ + id, { \ + id, #id, static_cast(gItemIcons[id]) \ + } \ + } + +// Maps items ids to info for use in ImGui +std::map itemMapping = { + ITEM_MAP_ENTRY(ITEM_STICK), + ITEM_MAP_ENTRY(ITEM_NUT), + ITEM_MAP_ENTRY(ITEM_BOMB), + ITEM_MAP_ENTRY(ITEM_BOW), + ITEM_MAP_ENTRY(ITEM_ARROW_FIRE), + ITEM_MAP_ENTRY(ITEM_DINS_FIRE), + ITEM_MAP_ENTRY(ITEM_SLINGSHOT), + ITEM_MAP_ENTRY(ITEM_OCARINA_FAIRY), + ITEM_MAP_ENTRY(ITEM_OCARINA_TIME), + ITEM_MAP_ENTRY(ITEM_BOMBCHU), + ITEM_MAP_ENTRY(ITEM_HOOKSHOT), + ITEM_MAP_ENTRY(ITEM_LONGSHOT), + ITEM_MAP_ENTRY(ITEM_ARROW_ICE), + ITEM_MAP_ENTRY(ITEM_FARORES_WIND), + ITEM_MAP_ENTRY(ITEM_BOOMERANG), + ITEM_MAP_ENTRY(ITEM_LENS), + ITEM_MAP_ENTRY(ITEM_BEAN), + ITEM_MAP_ENTRY(ITEM_HAMMER), + ITEM_MAP_ENTRY(ITEM_ARROW_LIGHT), + ITEM_MAP_ENTRY(ITEM_NAYRUS_LOVE), + ITEM_MAP_ENTRY(ITEM_BOTTLE), + ITEM_MAP_ENTRY(ITEM_POTION_RED), + ITEM_MAP_ENTRY(ITEM_POTION_GREEN), + ITEM_MAP_ENTRY(ITEM_POTION_BLUE), + ITEM_MAP_ENTRY(ITEM_FAIRY), + ITEM_MAP_ENTRY(ITEM_FISH), + ITEM_MAP_ENTRY(ITEM_MILK_BOTTLE), + ITEM_MAP_ENTRY(ITEM_LETTER_RUTO), + ITEM_MAP_ENTRY(ITEM_BLUE_FIRE), + ITEM_MAP_ENTRY(ITEM_BUG), + ITEM_MAP_ENTRY(ITEM_BIG_POE), + ITEM_MAP_ENTRY(ITEM_MILK_HALF), + ITEM_MAP_ENTRY(ITEM_POE), + ITEM_MAP_ENTRY(ITEM_WEIRD_EGG), + ITEM_MAP_ENTRY(ITEM_CHICKEN), + ITEM_MAP_ENTRY(ITEM_LETTER_ZELDA), + ITEM_MAP_ENTRY(ITEM_MASK_KEATON), + ITEM_MAP_ENTRY(ITEM_MASK_SKULL), + ITEM_MAP_ENTRY(ITEM_MASK_SPOOKY), + ITEM_MAP_ENTRY(ITEM_MASK_BUNNY), + ITEM_MAP_ENTRY(ITEM_MASK_GORON), + ITEM_MAP_ENTRY(ITEM_MASK_ZORA), + ITEM_MAP_ENTRY(ITEM_MASK_GERUDO), + ITEM_MAP_ENTRY(ITEM_MASK_TRUTH), + ITEM_MAP_ENTRY(ITEM_SOLD_OUT), + ITEM_MAP_ENTRY(ITEM_POCKET_EGG), + ITEM_MAP_ENTRY(ITEM_POCKET_CUCCO), + ITEM_MAP_ENTRY(ITEM_COJIRO), + ITEM_MAP_ENTRY(ITEM_ODD_MUSHROOM), + ITEM_MAP_ENTRY(ITEM_ODD_POTION), + ITEM_MAP_ENTRY(ITEM_SAW), + ITEM_MAP_ENTRY(ITEM_SWORD_BROKEN), + ITEM_MAP_ENTRY(ITEM_PRESCRIPTION), + ITEM_MAP_ENTRY(ITEM_FROG), + ITEM_MAP_ENTRY(ITEM_EYEDROPS), + ITEM_MAP_ENTRY(ITEM_CLAIM_CHECK), + ITEM_MAP_ENTRY(ITEM_BOW_ARROW_FIRE), + ITEM_MAP_ENTRY(ITEM_BOW_ARROW_ICE), + ITEM_MAP_ENTRY(ITEM_BOW_ARROW_LIGHT), + ITEM_MAP_ENTRY(ITEM_SWORD_KOKIRI), + ITEM_MAP_ENTRY(ITEM_SWORD_MASTER), + ITEM_MAP_ENTRY(ITEM_SWORD_BGS), + ITEM_MAP_ENTRY(ITEM_SHIELD_DEKU), + ITEM_MAP_ENTRY(ITEM_SHIELD_HYLIAN), + ITEM_MAP_ENTRY(ITEM_SHIELD_MIRROR), + ITEM_MAP_ENTRY(ITEM_TUNIC_KOKIRI), + ITEM_MAP_ENTRY(ITEM_TUNIC_GORON), + ITEM_MAP_ENTRY(ITEM_TUNIC_ZORA), + ITEM_MAP_ENTRY(ITEM_BOOTS_KOKIRI), + ITEM_MAP_ENTRY(ITEM_BOOTS_IRON), + ITEM_MAP_ENTRY(ITEM_BOOTS_HOVER), + ITEM_MAP_ENTRY(ITEM_BULLET_BAG_30), + ITEM_MAP_ENTRY(ITEM_BULLET_BAG_40), + ITEM_MAP_ENTRY(ITEM_BULLET_BAG_50), + ITEM_MAP_ENTRY(ITEM_QUIVER_30), + ITEM_MAP_ENTRY(ITEM_QUIVER_40), + ITEM_MAP_ENTRY(ITEM_QUIVER_50), + ITEM_MAP_ENTRY(ITEM_BOMB_BAG_20), + ITEM_MAP_ENTRY(ITEM_BOMB_BAG_30), + ITEM_MAP_ENTRY(ITEM_BOMB_BAG_40), + ITEM_MAP_ENTRY(ITEM_BRACELET), + ITEM_MAP_ENTRY(ITEM_GAUNTLETS_SILVER), + ITEM_MAP_ENTRY(ITEM_GAUNTLETS_GOLD), + ITEM_MAP_ENTRY(ITEM_SCALE_SILVER), + ITEM_MAP_ENTRY(ITEM_SCALE_GOLDEN), + ITEM_MAP_ENTRY(ITEM_SWORD_KNIFE), + ITEM_MAP_ENTRY(ITEM_WALLET_ADULT), + ITEM_MAP_ENTRY(ITEM_WALLET_GIANT), + ITEM_MAP_ENTRY(ITEM_SEEDS), + ITEM_MAP_ENTRY(ITEM_FISHING_POLE), +}; + +// Maps entries in the GS flag array to the area name it represents +std::vector gsMapping = { + "Deku Tree", + "Dodongo's Cavern", + "Inside Jabu-Jabu's Belly", + "Forest Temple", + "Fire Temple", + "Water Temple", + "Spirit Temple", + "Shadow Temple", + "Bottom of the Well", + "Ice Cavern", + "Hyrule Field", + "Lon Lon Ranch", + "Kokiri Forest", + "Lost Woods, Sacred Forest Meadow", + "Castle Town and Ganon's Castle", + "Death Mountain Trail, Goron City", + "Kakariko Village", + "Zora Fountain, River", + "Lake Hylia", + "Gerudo Valley", + "Gerudo Fortress", + "Desert Colossus, Haunted Wasteland", +}; +extern "C" u8 gAreaGsFlags[]; + +extern "C" u8 gAmmoItems[]; + +// Modification of gAmmoItems that replaces ITEM_NONE with the item in inventory slot it represents +u8 gAllAmmoItems[] = { + ITEM_STICK, ITEM_NUT, ITEM_BOMB, ITEM_BOW, ITEM_ARROW_FIRE, ITEM_DINS_FIRE, + ITEM_SLINGSHOT, ITEM_OCARINA_TIME, ITEM_BOMBCHU, ITEM_LONGSHOT, ITEM_ARROW_ICE, ITEM_FARORES_WIND, + ITEM_BOOMERANG, ITEM_LENS, ITEM_BEAN, ITEM_HAMMER, +}; + +// Adds a text tooltip for the previous ImGui item +void SetLastItemHoverText(const std::string& text) { + if (ImGui::IsItemHovered()) { + ImGui::BeginTooltip(); + ImGui::Text(text.c_str()); + ImGui::EndTooltip(); + } +} + +// Adds a "?" next to the previous ImGui item with a custom tooltip +void InsertHelpHoverText(const std::string& text) { + ImGui::SameLine(); + ImGui::TextColored(ImVec4(0.7f, 0.7f, 0.7f, 1.0f), "?"); + if (ImGui::IsItemHovered()) { + ImGui::BeginTooltip(); + ImGui::Text(text.c_str()); + ImGui::EndTooltip(); + } +} + +void DrawSaveEditor(bool& open) { + if (!open) { + return; + } + + ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver); + if (!ImGui::Begin("Save Editor", &open)) { + ImGui::End(); + return; + } + + // TODO This is the bare minimum to get the player name showing + // There will need to be more effort to get it robust and editable + std::string name; + for (int i = 0; i < 8; i++) { + char letter = gSaveContext.playerName[i] + 0x3D; + if (letter == '{') { + letter = '\0'; + } + name += letter; + } + name += '\0'; + + if (ImGui::BeginTabBar("SaveContextTabBar", ImGuiTabBarFlags_NoCloseWithMiddleMouseButton)) { + if (ImGui::BeginTabItem("Info")) { + ImGui::PushItemWidth(ImGui::GetFontSize() * 6); + + ImGui::Text("Name: %s", name.c_str()); + InsertHelpHoverText("Player Name"); + + // Use an intermediary to keep the health from updating (and potentially killing the player) + // until it is done being edited + int16_t healthIntermediary = gSaveContext.healthCapacity; + ImGui::InputScalar("Max Health", ImGuiDataType_S16, &healthIntermediary); + if (ImGui::IsItemDeactivated()) { + gSaveContext.healthCapacity = healthIntermediary; + } + InsertHelpHoverText("Maximum health. 16 units per full heart"); + if (gSaveContext.health > gSaveContext.healthCapacity) { + gSaveContext.health = gSaveContext.healthCapacity; // Clamp health to new max + } + + const uint16_t healthMin = 0; + const uint16_t healthMax = gSaveContext.healthCapacity; + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15); + ImGui::SliderScalar("Health", ImGuiDataType_S16, &gSaveContext.health, &healthMin, &healthMax); + InsertHelpHoverText("Current health. 16 units per full heart"); + + bool doubleDefense = gSaveContext.doubleDefense != 0; + if (ImGui::Checkbox("Double Defense", &doubleDefense)) { + gSaveContext.doubleDefense = doubleDefense; + gSaveContext.inventory.defenseHearts = + gSaveContext.doubleDefense ? 20 : 0; // Set to get the border drawn in the UI + } + InsertHelpHoverText("Is double defense unlocked?"); + + std::string magicName; + if (gSaveContext.magicLevel == 2) { + magicName = "Double"; + } else if (gSaveContext.magicLevel == 1) { + magicName = "Single"; + } else { + magicName = "None"; + } + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 6); + if (ImGui::BeginCombo("Magic Level", magicName.c_str())) { + if (ImGui::Selectable("Double")) { + gSaveContext.magicLevel = 2; + gSaveContext.magicAcquired = true; + gSaveContext.doubleMagic = true; + } + if (ImGui::Selectable("Single")) { + gSaveContext.magicLevel = 1; + gSaveContext.magicAcquired = true; + gSaveContext.doubleMagic = false; + } + if (ImGui::Selectable("None")) { + gSaveContext.magicLevel = 0; + gSaveContext.magicAcquired = false; + gSaveContext.doubleMagic = false; + } + + ImGui::EndCombo(); + } + InsertHelpHoverText("Current magic level"); + gSaveContext.unk_13F4 = gSaveContext.magicLevel * 0x30; // Set to get the bar drawn in the UI + if (gSaveContext.magic > gSaveContext.unk_13F4) { + gSaveContext.magic = gSaveContext.unk_13F4; // Clamp magic to new max + } + + const uint8_t magicMin = 0; + const uint8_t magicMax = gSaveContext.unk_13F4; + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15); + ImGui::SliderScalar("Magic", ImGuiDataType_S8, &gSaveContext.magic, &magicMin, &magicMax); + InsertHelpHoverText("Current magic. 48 units per magic level"); + + ImGui::InputScalar("Rupees", ImGuiDataType_S16, &gSaveContext.rupees); + InsertHelpHoverText("Current rupees"); + + const uint16_t dayTimeMin = 0; + const uint16_t dayTimeMax = 0xFFFF; + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15); + ImGui::SliderScalar("Time", ImGuiDataType_U16, &gSaveContext.dayTime, &dayTimeMin, &dayTimeMax); + InsertHelpHoverText("Time of day"); + if (ImGui::Button("Dawn")) { + gSaveContext.dayTime = 0x4000; + } + ImGui::SameLine(); + if (ImGui::Button("Noon")) { + gSaveContext.dayTime = 0x8000; + } + ImGui::SameLine(); + if (ImGui::Button("Sunset")) { + gSaveContext.dayTime = 0xC000; + } + ImGui::SameLine(); + if (ImGui::Button("Midnight")) { + gSaveContext.dayTime = 0; + } + + ImGui::InputScalar("Total Days", ImGuiDataType_S32, &gSaveContext.totalDays); + InsertHelpHoverText("Total number of days elapsed since the start of the game"); + + ImGui::InputScalar("Deaths", ImGuiDataType_U16, &gSaveContext.deaths); + InsertHelpHoverText("Total number of deaths"); + + // TODO Move to quest status screen once the page is created + ImGui::InputScalar("GS Count", ImGuiDataType_S16, &gSaveContext.inventory.gsTokens); + InsertHelpHoverText("Number of gold skulltula tokens aquired"); + + bool bgsFlag = gSaveContext.bgsFlag != 0; + if (ImGui::Checkbox("Has BGS", &bgsFlag)) { + gSaveContext.bgsFlag = bgsFlag; + } + InsertHelpHoverText("Is Biggoron sword unlocked? Replaces Giant's knife"); + + ImGui::InputScalar("Sword Health", ImGuiDataType_U16, &gSaveContext.swordHealth); + InsertHelpHoverText("Giant's knife health. Default is 8. Must be >0 for Biggoron sword to work"); + + ImGui::InputScalar("Bgs Day Count", ImGuiDataType_S32, &gSaveContext.bgsDayCount); + InsertHelpHoverText("Total number of days elapsed since giving Biggoron the claim check"); + + // TODO Changing Link's age is more involved than just setting gSaveContext.linkAge + // It might not fit here and instead should be only changable when changing scenes + /* + if (ImGui::BeginCombo("Link Age", LINK_IS_ADULT ? "Adult" : "Child")) { + if (ImGui::Selectable("Adult")) { + gSaveContext.linkAge = 0; + } + if (ImGui::Selectable("Child")) { + gSaveContext.linkAge = 1; + } + + ImGui::EndCombo(); + } + */ + + ImGui::PopItemWidth(); + ImGui::EndTabItem(); + } + + if (ImGui::BeginTabItem("Inventory")) { + static bool restrictToValid = true; + + ImGui::Checkbox("Restrict to valid items", &restrictToValid); + InsertHelpHoverText("Restricts items and ammo to only what is possible to legally acquire in-game"); + + for (int32_t y = 0; y < 4; y++) { + for (int32_t x = 0; x < 6; x++) { + int32_t index = x + y * 6; + static int32_t selectedIndex = -1; + static const char* itemPopupPicker = "itemPopupPicker"; + + ImGui::PushID(index); + + if (x != 0) { + ImGui::SameLine(); + } + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1, 1, 1, 0)); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); + uint8_t item = gSaveContext.inventory.items[index]; + if (item != ITEM_NONE) { + const ItemMapEntry& slotEntry = itemMapping.find(item)->second; + if (ImGui::ImageButton(SohImGui::GetTextureByName(slotEntry.name), ImVec2(32.0f, 32.0f), + ImVec2(0, 0), ImVec2(1, 1), 0)) { + selectedIndex = index; + ImGui::OpenPopup(itemPopupPicker); + } + } else { + if (ImGui::Button("##itemNone", ImVec2(32.0f, 32.0f))) { + selectedIndex = index; + ImGui::OpenPopup(itemPopupPicker); + } + } + ImGui::PopStyleVar(); + ImGui::PopStyleColor(); + + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); + if (ImGui::BeginPopup(itemPopupPicker)) { + if (ImGui::Button("##itemNonePicker", ImVec2(32.0f, 32.0f))) { + gSaveContext.inventory.items[selectedIndex] = ITEM_NONE; + ImGui::CloseCurrentPopup(); + } + SetLastItemHoverText("ITEM_NONE"); + + std::vector possibleItems; + if (restrictToValid) { + // Scan gItemSlots to find legal items for this slot. Bottles are a special case + for (int slotIndex = 0; slotIndex < 56; slotIndex++) { + int testIndex = (selectedIndex == SLOT_BOTTLE_1 || selectedIndex == SLOT_BOTTLE_2 || + selectedIndex == SLOT_BOTTLE_3 || selectedIndex == SLOT_BOTTLE_4) + ? SLOT_BOTTLE_1 + : selectedIndex; + if (gItemSlots[slotIndex] == testIndex) { + possibleItems.push_back(itemMapping[slotIndex]); + } + } + } else { + for (const auto& entry : itemMapping) { + possibleItems.push_back(entry.second); + } + } + + for (int32_t pickerIndex = 0; pickerIndex < possibleItems.size(); pickerIndex++) { + if (((pickerIndex + 1) % 8) != 0) { + ImGui::SameLine(); + } + const ItemMapEntry& slotEntry = possibleItems[pickerIndex]; + if (ImGui::ImageButton(SohImGui::GetTextureByName(slotEntry.name), ImVec2(32.0f, 32.0f), + ImVec2(0, 0), ImVec2(1, 1), 0)) { + gSaveContext.inventory.items[selectedIndex] = slotEntry.id; + ImGui::CloseCurrentPopup(); + } + SetLastItemHoverText(slotEntry.name); + } + + ImGui::EndPopup(); + } + ImGui::PopStyleVar(); + + ImGui::PopID(); + } + } + + ImGui::Text("Ammo"); + for (uint32_t ammoIndex = 0, drawnAmmoItems = 0; ammoIndex < 16; ammoIndex++) { + uint8_t item = (restrictToValid) ? gAmmoItems[ammoIndex] : gAllAmmoItems[ammoIndex]; + if (item != ITEM_NONE) { + // For legal items, display as 1 row of 7. For unrestricted items, display rows of 6 to match + // inventory + if ((restrictToValid && (drawnAmmoItems != 0)) || ((drawnAmmoItems % 6) != 0)) { + ImGui::SameLine(); + } + drawnAmmoItems++; + + ImGui::PushID(ammoIndex); + ImGui::PushItemWidth(32.0f); + ImGui::BeginGroup(); + + ImGui::Image(SohImGui::GetTextureByName(itemMapping[item].name), ImVec2(32.0f, 32.0f)); + ImGui::InputScalar("##ammoInput", ImGuiDataType_S8, &AMMO(item)); + + ImGui::EndGroup(); + ImGui::PopItemWidth(); + ImGui::PopID(); + } + } + + ImGui::EndTabItem(); + } + + if (ImGui::BeginTabItem("Flags")) { + static uint32_t selectedGsMap = 0; + ImGui::Text("Gold Skulltulas"); + ImGui::Text("Map"); + ImGui::SameLine(); + if (ImGui::BeginCombo("##Gold Skulltula Map", gsMapping[selectedGsMap].c_str())) { + for (int32_t gsIndex = 0; gsIndex < gsMapping.size(); gsIndex++) { + if (ImGui::Selectable(gsMapping[gsIndex].c_str())) { + selectedGsMap = gsIndex; + } + } + + ImGui::EndCombo(); + } + + // TODO We should write out descriptions for each one... ugh + ImGui::Text("Flags"); + uint32_t currentFlags = GET_GS_FLAGS(selectedGsMap); + uint32_t allFlags = gAreaGsFlags[selectedGsMap]; + uint32_t setMask = 1; + // Iterate over bitfield and create a checkbox for each skulltula + while (allFlags != 0) { + bool isThisSet = (currentFlags & 0x1) == 0x1; + + ImGui::SameLine(); + ImGui::PushID(allFlags); + if (ImGui::Checkbox("##gs", &isThisSet)) { + if (isThisSet) { + SET_GS_FLAGS(selectedGsMap, setMask); + } else { + // Have to do this roundabout method as the macro does not support clearing flags + uint32_t currentFlagsBase = GET_GS_FLAGS(selectedGsMap); + gSaveContext.gsFlags[selectedGsMap >> 2] &= ~gGsFlagsMasks[selectedGsMap & 3]; + SET_GS_FLAGS(selectedGsMap, currentFlagsBase & ~setMask); + } + } + + ImGui::PopID(); + + allFlags >>= 1; + currentFlags >>= 1; + setMask <<= 1; + } + + static bool keepGsCountUpdated = true; + ImGui::Checkbox("Keep GS Count Updated", &keepGsCountUpdated); + InsertHelpHoverText("Automatically adjust the number of gold skulltula tokens acquired based on set flags"); + int32_t gsCount = 0; + if (keepGsCountUpdated) { + for (int32_t gsFlagIndex = 0; gsFlagIndex < 6; gsFlagIndex++) { + gsCount += std::popcount(static_cast(gSaveContext.gsFlags[gsFlagIndex])); + } + gSaveContext.inventory.gsTokens = gsCount; + } + + // TODO other flag types, like switch, clear, etc. + // These flags interact with the actor context, so it's a bit more complicated + + ImGui::EndTabItem(); + } + + ImGui::EndTabBar(); + } + + ImGui::End(); +} + +void InitSaveEditor() { + SohImGui::AddWindow("Debug", "Save Editor", DrawSaveEditor); + + // Load item icons into ImGui + for (const auto& entry : itemMapping) { + SohImGui::LoadResource(entry.second.name, entry.second.texturePath); + } +} diff --git a/soh/soh/Enhancements/debugger/debugSaveEditor.h b/soh/soh/Enhancements/debugger/debugSaveEditor.h new file mode 100644 index 000000000..084f7cadc --- /dev/null +++ b/soh/soh/Enhancements/debugger/debugSaveEditor.h @@ -0,0 +1,3 @@ +#pragma once + +void InitSaveEditor(); diff --git a/soh/soh/Enhancements/debugger/debugger.cpp b/soh/soh/Enhancements/debugger/debugger.cpp new file mode 100644 index 000000000..e493375cd --- /dev/null +++ b/soh/soh/Enhancements/debugger/debugger.cpp @@ -0,0 +1,6 @@ +#include "debugger.h" +#include "debugSaveEditor.h" + +void Debug_Init(void) { + InitSaveEditor(); +} diff --git a/soh/soh/Enhancements/debugger/debugger.h b/soh/soh/Enhancements/debugger/debugger.h new file mode 100644 index 000000000..4bc0f985b --- /dev/null +++ b/soh/soh/Enhancements/debugger/debugger.h @@ -0,0 +1,3 @@ +#pragma once + +void Debug_Init(void); diff --git a/soh/soh/OTRGlobals.cpp b/soh/soh/OTRGlobals.cpp index d10600bce..007afd702 100644 --- a/soh/soh/OTRGlobals.cpp +++ b/soh/soh/OTRGlobals.cpp @@ -22,6 +22,7 @@ #include "Lib/stb/stb_image.h" #include "AudioPlayer.h" #include "../soh/Enhancements/debugconsole.h" +#include "../soh/Enhancements/debugger/debugger.h" #include "Utils/BitConverter.h" OTRGlobals* OTRGlobals::Instance; @@ -54,6 +55,7 @@ extern "C" void InitOTR() { clearMtx = (uintptr_t)&gMtxClear; OTRMessage_Init(); DebugConsole_Init(); + Debug_Init(); } extern "C" uint64_t GetFrequency() { From 06fd7f662af4358e9a6111270ce0cd311a9af9ea Mon Sep 17 00:00:00 2001 From: Josh Bodner <30329717+jbodner09@users.noreply.github.com> Date: Sun, 10 Apr 2022 15:19:26 -0700 Subject: [PATCH 03/13] Added DPad support to file selection and pause screens (#124) * Added DPad support to file selection and pause screens * Wrap changes behind CVar * Fix merge conflict for real * Remove unnecessary const_cast * Fixed transparent texture making framebuffers also transparent in D3D11. (#84) This happened with the Mirror Shield in the inventory screen preview. * Add save editor * Added DPad support to file selection and pause screens * Fixing rebase conflict * Remove unnecessary const_cast Co-authored-by: MaikelChan Co-authored-by: rozlette --- libultraship/libultraship/GameSettings.cpp | 6 +- libultraship/libultraship/GameSettings.h | 1 + libultraship/libultraship/SohImGuiImpl.cpp | 9 +- .../ovl_file_choose/z_file_choose.c | 8 +- .../ovl_file_choose/z_file_copy_erase.c | 21 ++- .../ovl_file_choose/z_file_nameset_PAL.c | 21 ++- .../ovl_kaleido_scope/z_kaleido_collect.c | 163 +++++++++--------- .../ovl_kaleido_scope/z_kaleido_equipment.c | 13 +- .../misc/ovl_kaleido_scope/z_kaleido_item.c | 17 +- .../ovl_kaleido_scope/z_kaleido_map_PAL.c | 30 ++-- .../misc/ovl_kaleido_scope/z_kaleido_prompt.c | 5 +- .../ovl_kaleido_scope/z_kaleido_scope_PAL.c | 5 +- 12 files changed, 163 insertions(+), 136 deletions(-) diff --git a/libultraship/libultraship/GameSettings.cpp b/libultraship/libultraship/GameSettings.cpp index f12042bc0..28f4b23cb 100644 --- a/libultraship/libultraship/GameSettings.cpp +++ b/libultraship/libultraship/GameSettings.cpp @@ -59,7 +59,7 @@ namespace Game { CVar_SetS32("gPauseLiveLink", Settings.enhancements.animated_pause_menu); Settings.enhancements.minimal_ui = stob(Conf[EnhancementSection]["minimal_ui"]); - CVar_SetS32(const_cast("gMinimalUI"), Settings.enhancements.minimal_ui); + CVar_SetS32("gMinimalUI", Settings.enhancements.minimal_ui); // Audio Settings.audio.master = Ship::stof(Conf[AudioSection]["master"]); @@ -89,6 +89,9 @@ namespace Game { Settings.controller.input_enabled = stob(Conf[ControllerSection]["input_enabled"]); CVar_SetS32("gInputEnabled", Settings.controller.input_enabled); + + Settings.controller.dpad_pause_name = stob(Conf[ControllerSection]["dpad_pause_name"]); + CVar_SetS32("gDpadPauseName", Settings.controller.dpad_pause_name); // Cheats Settings.cheats.debug_mode = stob(Conf[CheatSection]["debug_mode"]); @@ -149,6 +152,7 @@ namespace Game { Conf[ControllerSection]["rumble_strength"] = std::to_string(Settings.controller.rumble_strength); Conf[ControllerSection]["input_scale"] = std::to_string(Settings.controller.input_scale); Conf[ControllerSection]["input_enabled"] = std::to_string(Settings.controller.input_enabled); + Conf[ControllerSection]["dpad_pause_name"] = std::to_string(Settings.controller.dpad_pause_name); // Cheats Conf[CheatSection]["debug_mode"] = std::to_string(Settings.cheats.debug_mode); diff --git a/libultraship/libultraship/GameSettings.h b/libultraship/libultraship/GameSettings.h index 4f9bb4c99..15ebdad16 100644 --- a/libultraship/libultraship/GameSettings.h +++ b/libultraship/libultraship/GameSettings.h @@ -34,6 +34,7 @@ struct SoHConfigType { float gyroDriftX = 0.0f; float gyroDriftY = 0.0f; bool input_enabled = false; + bool dpad_pause_name = false; } controller; // Cheats diff --git a/libultraship/libultraship/SohImGuiImpl.cpp b/libultraship/libultraship/SohImGuiImpl.cpp index 7489d14a2..cef156246 100644 --- a/libultraship/libultraship/SohImGuiImpl.cpp +++ b/libultraship/libultraship/SohImGuiImpl.cpp @@ -353,6 +353,13 @@ namespace SohImGui { needs_save = true; } + ImGui::Separator(); + + if (ImGui::Checkbox("Dpad Support on Pause and File Select", &Game::Settings.controller.dpad_pause_name)) { + CVar_SetS32("gDpadPauseName", Game::Settings.controller.dpad_pause_name); + needs_save = true; + } + ImGui::EndMenu(); } @@ -367,7 +374,7 @@ namespace SohImGui { } if (ImGui::Checkbox("Minimal UI", &Game::Settings.enhancements.minimal_ui)) { - CVar_SetS32(const_cast("gMinimalUI"), Game::Settings.enhancements.minimal_ui); + CVar_SetS32("gMinimalUI", Game::Settings.enhancements.minimal_ui); needs_save = true; } 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 ca5154567..84543fa26 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 @@ -179,6 +179,7 @@ void FileChoose_UpdateMainMenu(GameState* thisx) { FileChooseContext* this = (FileChooseContext*)thisx; SramContext* sramCtx = &this->sramCtx; Input* input = &this->state.input[0]; + bool dpad = CVar_GetS32("gDpadPauseName", 0); if (CHECK_BTN_ALL(input->press.button, BTN_START) || CHECK_BTN_ALL(input->press.button, BTN_A)) { if (this->buttonIndex <= FS_BTN_MAIN_FILE_3) { @@ -237,10 +238,10 @@ void FileChoose_UpdateMainMenu(GameState* thisx) { } } } else { - if (ABS(this->stickRelY) > 30) { + if ((ABS(this->stickRelY) > 30) || (dpad && CHECK_BTN_ANY(input->press.button, BTN_DDOWN | BTN_DUP))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); - if (this->stickRelY > 30) { + if ((this->stickRelY > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { this->buttonIndex--; if (this->buttonIndex < FS_BTN_MAIN_FILE_1) { this->buttonIndex = FS_BTN_MAIN_OPTIONS; @@ -1318,6 +1319,7 @@ void FileChoose_FadeInFileInfo(GameState* thisx) { void FileChoose_ConfirmFile(GameState* thisx) { FileChooseContext* this = (FileChooseContext*)thisx; Input* input = &this->state.input[0]; + bool dpad = CVar_GetS32("gDpadPauseName", 0); if (CHECK_BTN_ALL(input->press.button, BTN_START) || (CHECK_BTN_ALL(input->press.button, BTN_A))) { if (this->confirmButtonIndex == FS_BTN_CONFIRM_YES) { @@ -1332,7 +1334,7 @@ void FileChoose_ConfirmFile(GameState* thisx) { } else if (CHECK_BTN_ALL(input->press.button, BTN_B)) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CLOSE, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); this->selectMode++; - } else if (ABS(this->stickRelY) >= 30) { + } else if ((ABS(this->stickRelY) >= 30) || (dpad && CHECK_BTN_ANY(input->press.button, BTN_DDOWN | BTN_DUP))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); this->confirmButtonIndex ^= 1; } diff --git a/soh/src/overlays/gamestates/ovl_file_choose/z_file_copy_erase.c b/soh/src/overlays/gamestates/ovl_file_choose/z_file_copy_erase.c index 5d13c408d..1f44abe95 100644 --- a/soh/src/overlays/gamestates/ovl_file_choose/z_file_copy_erase.c +++ b/soh/src/overlays/gamestates/ovl_file_choose/z_file_copy_erase.c @@ -62,6 +62,7 @@ void FileChoose_SelectCopySource(GameState* thisx) { FileChooseContext* this = (FileChooseContext*)thisx; SramContext* sramCtx = &this->sramCtx; Input* input = &this->state.input[0]; + bool dpad = CVar_GetS32("gDpadPauseName", 0); if (((this->buttonIndex == FS_BTN_COPY_QUIT) && CHECK_BTN_ANY(input->press.button, BTN_A | BTN_START)) || CHECK_BTN_ALL(input->press.button, BTN_B)) { @@ -82,10 +83,10 @@ void FileChoose_SelectCopySource(GameState* thisx) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_ERROR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); } } else { - if (ABS(this->stickRelY) >= 30) { + if ((ABS(this->stickRelY) >= 30) || (dpad && CHECK_BTN_ANY(input->press.button, BTN_DDOWN | BTN_DUP))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); - if (this->stickRelY >= 30) { + if ((this->stickRelY >= 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { this->buttonIndex--; if (this->buttonIndex < FS_BTN_COPY_FILE_1) { @@ -174,6 +175,7 @@ void FileChoose_SelectCopyDest(GameState* thisx) { FileChooseContext* this = (FileChooseContext*)thisx; SramContext* sramCtx = &this->sramCtx; Input* input = &this->state.input[0]; + bool dpad = CVar_GetS32("gDpadPauseName", 0); if (((this->buttonIndex == FS_BTN_COPY_QUIT) && CHECK_BTN_ANY(input->press.button, BTN_A | BTN_START)) || CHECK_BTN_ALL(input->press.button, BTN_B)) { @@ -194,10 +196,10 @@ void FileChoose_SelectCopyDest(GameState* thisx) { } } else { - if (ABS(this->stickRelY) >= 30) { + if ((ABS(this->stickRelY) >= 30) || (dpad && CHECK_BTN_ANY(input->press.button, BTN_DDOWN | BTN_DUP))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); - if (this->stickRelY >= 30) { + if ((this->stickRelY >= 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { this->buttonIndex--; if ((this->buttonIndex == this->selectedFileIndex)) { @@ -360,6 +362,7 @@ void FileChoose_CopyConfirm(GameState* thisx) { SramContext* sramCtx = &this->sramCtx; Input* input = &this->state.input[0]; u16 dayTime; + bool dpad = CVar_GetS32("gDpadPauseName", 0); if (((this->buttonIndex != FS_BTN_CONFIRM_YES) && CHECK_BTN_ANY(input->press.button, BTN_A | BTN_START)) || CHECK_BTN_ALL(input->press.button, BTN_B)) { @@ -377,7 +380,7 @@ void FileChoose_CopyConfirm(GameState* thisx) { this->configMode = CM_COPY_ANIM_1; func_800AA000(300.0f, 0xB4, 0x14, 0x64); Audio_PlaySoundGeneral(NA_SE_SY_FSEL_DECIDE_L, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); - } else if (ABS(this->stickRelY) >= 30) { + } else if ((ABS(this->stickRelY) >= 30) || (dpad && CHECK_BTN_ANY(input->press.button, BTN_DDOWN | BTN_DUP))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); this->buttonIndex ^= 1; } @@ -680,6 +683,7 @@ void FileChoose_EraseSelect(GameState* thisx) { FileChooseContext* this = (FileChooseContext*)thisx; SramContext* sramCtx = &this->sramCtx; Input* input = &this->state.input[0]; + bool dpad = CVar_GetS32("gDpadPauseName", 0); if (((this->buttonIndex == FS_BTN_COPY_QUIT) && CHECK_BTN_ANY(input->press.button, BTN_A | BTN_START)) || CHECK_BTN_ALL(input->press.button, BTN_B)) { @@ -700,10 +704,10 @@ void FileChoose_EraseSelect(GameState* thisx) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_ERROR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); } } else { - if (ABS(this->stickRelY) >= 30) { + if ((ABS(this->stickRelY) >= 30) || (dpad && CHECK_BTN_ANY(input->press.button, BTN_DDOWN | BTN_DUP))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); - if (this->stickRelY >= 30) { + if ((this->stickRelY >= 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { this->buttonIndex--; if (this->buttonIndex < FS_BTN_ERASE_FILE_1) { this->buttonIndex = FS_BTN_ERASE_QUIT; @@ -817,6 +821,7 @@ void FileChoose_SetupEraseConfirm2(GameState* thisx) { void FileChoose_EraseConfirm(GameState* thisx) { FileChooseContext* this = (FileChooseContext*)thisx; Input* input = &this->state.input[0]; + bool dpad = CVar_GetS32("gDpadPauseName", 0); if (((this->buttonIndex != FS_BTN_CONFIRM_YES) && CHECK_BTN_ANY(input->press.button, BTN_A | BTN_START)) || CHECK_BTN_ALL(input->press.button, BTN_B)) { @@ -833,7 +838,7 @@ void FileChoose_EraseConfirm(GameState* thisx) { this->nextTitleLabel = FS_TITLE_ERASE_COMPLETE; func_800AA000(200.0f, 0xFF, 0x14, 0x96); sEraseDelayTimer = 15; - } else if (ABS(this->stickRelY) >= 30) { + } else if ((ABS(this->stickRelY) >= 30) || (dpad && CHECK_BTN_ANY(input->press.button, BTN_DDOWN | BTN_DUP))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); this->buttonIndex ^= 1; } 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 73aa5e6d1..501986d2b 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 @@ -509,12 +509,14 @@ void FileChoose_StartNameEntry(GameState* thisx) { */ void FileChoose_UpdateKeyboardCursor(GameState* thisx) { FileChooseContext* this = (FileChooseContext*)thisx; + Input* input = &this->state.input[0]; s16 prevKbdX; + bool dpad = CVar_GetS32("gDpadPauseName", 0); this->kbdButton = 99; if (this->kbdY != 5) { - if (this->stickRelX < -30) { + if ((this->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); this->charIndex--; this->kbdX--; @@ -522,7 +524,7 @@ void FileChoose_UpdateKeyboardCursor(GameState* thisx) { this->kbdX = 12; this->charIndex = (this->kbdY * 13) + this->kbdX; } - } else if (this->stickRelX > 30) { + } else if ((this->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); this->charIndex++; this->kbdX++; @@ -532,13 +534,13 @@ void FileChoose_UpdateKeyboardCursor(GameState* thisx) { } } } else { - if (this->stickRelX < -30) { + if ((this->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); this->kbdX--; if (this->kbdX < 3) { this->kbdX = 4; } - } else if (this->stickRelX > 30) { + } else if ((this->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); this->kbdX++; if (this->kbdX > 4) { @@ -547,7 +549,7 @@ void FileChoose_UpdateKeyboardCursor(GameState* thisx) { } } - if (this->stickRelY > 30) { + if ((this->stickRelY > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); this->kbdY--; @@ -578,7 +580,7 @@ void FileChoose_UpdateKeyboardCursor(GameState* thisx) { this->charIndex += this->kbdX; } } - } else if (this->stickRelY < -30) { + } else if ((this->stickRelY < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DDOWN))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); this->kbdY++; @@ -655,6 +657,7 @@ void FileChoose_UpdateOptionsMenu(GameState* thisx) { FileChooseContext* this = (FileChooseContext*)thisx; SramContext* sramCtx = &this->sramCtx; Input* input = &this->state.input[0]; + bool dpad = CVar_GetS32("gDpadPauseName", 0); if (CHECK_BTN_ALL(input->press.button, BTN_B)) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_DECIDE_L, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); @@ -675,7 +678,7 @@ void FileChoose_UpdateOptionsMenu(GameState* thisx) { return; } - if (this->stickRelX < -30) { + if ((this->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); if (sSelectedSetting == FS_SETTING_AUDIO) { @@ -688,7 +691,7 @@ void FileChoose_UpdateOptionsMenu(GameState* thisx) { } else { gSaveContext.zTargetSetting ^= 1; } - } else if (this->stickRelX > 30) { + } else if ((this->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); if (sSelectedSetting == FS_SETTING_AUDIO) { @@ -702,7 +705,7 @@ void FileChoose_UpdateOptionsMenu(GameState* thisx) { } } - if ((this->stickRelY < -30) || (this->stickRelY > 30)) { + if ((ABS(this->stickRelY) > 30) || (dpad && CHECK_BTN_ANY(input->press.button, BTN_DDOWN | BTN_DUP))) { Audio_PlaySoundGeneral(NA_SE_SY_FSEL_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); sSelectedSetting ^= 1; } else if (CHECK_BTN_ALL(input->press.button, BTN_A)) { diff --git a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_collect.c b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_collect.c index 46be1154f..80ffe8d4a 100644 --- a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_collect.c +++ b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_collect.c @@ -74,6 +74,7 @@ void KaleidoScope_DrawQuestStatus(GlobalContext* globalCtx, GraphicsContext* gfx s16 pad2; s16 phi_s0_2; s16 sp208[3]; + bool dpad = CVar_GetS32("gDpadPauseName", 0); OPEN_DISPS(gfxCtx, "../z_kaleido_collect.c", 248); @@ -83,96 +84,92 @@ void KaleidoScope_DrawQuestStatus(GlobalContext* globalCtx, GraphicsContext* gfx if (pauseCtx->cursorSpecialPos == 0) { pauseCtx->nameColorSet = 0; + sp216 = pauseCtx->cursorSlot[PAUSE_QUEST]; + phi_s3 = pauseCtx->cursorPoint[PAUSE_QUEST]; - if ((pauseCtx->state != 6) || ((pauseCtx->stickRelX == 0) && (pauseCtx->stickRelY == 0))) { - sp216 = pauseCtx->cursorSlot[PAUSE_QUEST]; - } else { - phi_s3 = pauseCtx->cursorPoint[PAUSE_QUEST]; - - if (pauseCtx->stickRelX < -30) { - phi_s0 = D_8082A1AC[phi_s3][2]; - if (phi_s0 == -3) { - KaleidoScope_MoveCursorToSpecialPos(globalCtx, PAUSE_CURSOR_PAGE_LEFT); - pauseCtx->unk_1E4 = 0; - } else { - while (phi_s0 >= 0) { - if ((s16)KaleidoScope_UpdateQuestStatusPoint(pauseCtx, phi_s0) != 0) { - break; - } - phi_s0 = D_8082A1AC[phi_s0][2]; - } - } - } else if (pauseCtx->stickRelX > 30) { - phi_s0 = D_8082A1AC[phi_s3][3]; - if (phi_s0 == -2) { - KaleidoScope_MoveCursorToSpecialPos(globalCtx, PAUSE_CURSOR_PAGE_RIGHT); - pauseCtx->unk_1E4 = 0; - } else { - while (phi_s0 >= 0) { - if ((s16)KaleidoScope_UpdateQuestStatusPoint(pauseCtx, phi_s0) != 0) { - break; - } - phi_s0 = D_8082A1AC[phi_s0][3]; - } - } - } - - if (pauseCtx->stickRelY < -30) { - phi_s0 = D_8082A1AC[phi_s3][1]; - while (phi_s0 >= 0) { - if ((s16)KaleidoScope_UpdateQuestStatusPoint(pauseCtx, phi_s0) != 0) { - break; - } - phi_s0 = D_8082A1AC[phi_s0][1]; - } - } else if (pauseCtx->stickRelY > 30) { - phi_s0 = D_8082A1AC[phi_s3][0]; - while (phi_s0 >= 0) { - if ((s16)KaleidoScope_UpdateQuestStatusPoint(pauseCtx, phi_s0) != 0) { - break; - } - phi_s0 = D_8082A1AC[phi_s0][0]; - } - } - - if (phi_s3 != pauseCtx->cursorPoint[PAUSE_QUEST]) { + if ((pauseCtx->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { + phi_s0 = D_8082A1AC[phi_s3][2]; + if (phi_s0 == -3) { + KaleidoScope_MoveCursorToSpecialPos(globalCtx, PAUSE_CURSOR_PAGE_LEFT); pauseCtx->unk_1E4 = 0; - Audio_PlaySoundGeneral(NA_SE_SY_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); - } - - if (pauseCtx->cursorPoint[PAUSE_QUEST] != 0x18) { - if (CHECK_QUEST_ITEM(pauseCtx->cursorPoint[PAUSE_QUEST])) { - if (pauseCtx->cursorPoint[PAUSE_QUEST] < 6) { - phi_s0_2 = pauseCtx->cursorPoint[PAUSE_QUEST] + 0x66; - osSyncPrintf("000 ccc=%d\n", phi_s0_2); - } else if (pauseCtx->cursorPoint[PAUSE_QUEST] < 0x12) { - phi_s0_2 = pauseCtx->cursorPoint[PAUSE_QUEST] + 0x54; - osSyncPrintf("111 ccc=%d\n", phi_s0_2); - } else { - phi_s0_2 = pauseCtx->cursorPoint[PAUSE_QUEST] + 0x5A; - osSyncPrintf("222 ccc=%d (%d, %d, %d)\n", phi_s0_2, pauseCtx->cursorPoint[PAUSE_QUEST], - 0x12, 0x6C); + } else { + while (phi_s0 >= 0) { + if ((s16)KaleidoScope_UpdateQuestStatusPoint(pauseCtx, phi_s0) != 0) { + break; } + phi_s0 = D_8082A1AC[phi_s0][2]; + } + } + } else if ((pauseCtx->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { + phi_s0 = D_8082A1AC[phi_s3][3]; + if (phi_s0 == -2) { + KaleidoScope_MoveCursorToSpecialPos(globalCtx, PAUSE_CURSOR_PAGE_RIGHT); + pauseCtx->unk_1E4 = 0; + } else { + while (phi_s0 >= 0) { + if ((s16)KaleidoScope_UpdateQuestStatusPoint(pauseCtx, phi_s0) != 0) { + break; + } + phi_s0 = D_8082A1AC[phi_s0][3]; + } + } + } + + if ((pauseCtx->stickRelY < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DDOWN))) { + phi_s0 = D_8082A1AC[phi_s3][1]; + while (phi_s0 >= 0) { + if ((s16)KaleidoScope_UpdateQuestStatusPoint(pauseCtx, phi_s0) != 0) { + break; + } + phi_s0 = D_8082A1AC[phi_s0][1]; + } + } else if ((pauseCtx->stickRelY > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { + phi_s0 = D_8082A1AC[phi_s3][0]; + while (phi_s0 >= 0) { + if ((s16)KaleidoScope_UpdateQuestStatusPoint(pauseCtx, phi_s0) != 0) { + break; + } + phi_s0 = D_8082A1AC[phi_s0][0]; + } + } + + if (phi_s3 != pauseCtx->cursorPoint[PAUSE_QUEST]) { + pauseCtx->unk_1E4 = 0; + Audio_PlaySoundGeneral(NA_SE_SY_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); + } + + if (pauseCtx->cursorPoint[PAUSE_QUEST] != 0x18) { + if (CHECK_QUEST_ITEM(pauseCtx->cursorPoint[PAUSE_QUEST])) { + if (pauseCtx->cursorPoint[PAUSE_QUEST] < 6) { + phi_s0_2 = pauseCtx->cursorPoint[PAUSE_QUEST] + 0x66; + osSyncPrintf("000 ccc=%d\n", phi_s0_2); + } else if (pauseCtx->cursorPoint[PAUSE_QUEST] < 0x12) { + phi_s0_2 = pauseCtx->cursorPoint[PAUSE_QUEST] + 0x54; + osSyncPrintf("111 ccc=%d\n", phi_s0_2); } else { - phi_s0_2 = PAUSE_ITEM_NONE; - osSyncPrintf("999 ccc=%d (%d, %d)\n", PAUSE_ITEM_NONE, pauseCtx->cursorPoint[PAUSE_QUEST], - 0x18); + phi_s0_2 = pauseCtx->cursorPoint[PAUSE_QUEST] + 0x5A; + osSyncPrintf("222 ccc=%d (%d, %d, %d)\n", phi_s0_2, pauseCtx->cursorPoint[PAUSE_QUEST], + 0x12, 0x6C); } } else { - if ((gSaveContext.inventory.questItems & 0xF0000000) != 0) { - phi_s0_2 = 0x72; - } else { - phi_s0_2 = PAUSE_ITEM_NONE; - } - osSyncPrintf("888 ccc=%d (%d, %d, %x)\n", phi_s0_2, pauseCtx->cursorPoint[PAUSE_QUEST], 0x72, - gSaveContext.inventory.questItems & 0xF0000000); + phi_s0_2 = PAUSE_ITEM_NONE; + osSyncPrintf("999 ccc=%d (%d, %d)\n", PAUSE_ITEM_NONE, pauseCtx->cursorPoint[PAUSE_QUEST], + 0x18); } - - sp216 = pauseCtx->cursorPoint[PAUSE_QUEST]; - pauseCtx->cursorItem[pauseCtx->pageIndex] = phi_s0_2; - pauseCtx->cursorSlot[pauseCtx->pageIndex] = sp216; + } else { + if ((gSaveContext.inventory.questItems & 0xF0000000) != 0) { + phi_s0_2 = 0x72; + } else { + phi_s0_2 = PAUSE_ITEM_NONE; + } + osSyncPrintf("888 ccc=%d (%d, %d, %x)\n", phi_s0_2, pauseCtx->cursorPoint[PAUSE_QUEST], 0x72, + gSaveContext.inventory.questItems & 0xF0000000); } + sp216 = pauseCtx->cursorPoint[PAUSE_QUEST]; + pauseCtx->cursorItem[pauseCtx->pageIndex] = phi_s0_2; + pauseCtx->cursorSlot[pauseCtx->pageIndex] = sp216; + KaleidoScope_SetCursorVtx(pauseCtx, sp216 * 4, pauseCtx->questVtx); if ((pauseCtx->state == 6) && (pauseCtx->unk_1E4 == 0) && (pauseCtx->cursorSpecialPos == 0)) { @@ -215,7 +212,7 @@ void KaleidoScope_DrawQuestStatus(GlobalContext* globalCtx, GraphicsContext* gfx } } } else if (pauseCtx->cursorSpecialPos == PAUSE_CURSOR_PAGE_LEFT) { - if (pauseCtx->stickRelX > 30) { + if ((pauseCtx->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { pauseCtx->cursorPoint[PAUSE_QUEST] = 0x15; pauseCtx->nameDisplayTimer = 0; pauseCtx->cursorSpecialPos = 0; @@ -232,7 +229,7 @@ void KaleidoScope_DrawQuestStatus(GlobalContext* globalCtx, GraphicsContext* gfx pauseCtx->cursorSlot[pauseCtx->pageIndex] = sp216; } } else { - if (pauseCtx->stickRelX < -30) { + if ((pauseCtx->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { pauseCtx->cursorPoint[PAUSE_QUEST] = 0; pauseCtx->nameDisplayTimer = 0; pauseCtx->cursorSpecialPos = 0; diff --git a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_equipment.c b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_equipment.c index 3c236637b..52019dcb2 100644 --- a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_equipment.c +++ b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_equipment.c @@ -140,6 +140,7 @@ void KaleidoScope_DrawEquipment(GlobalContext* globalCtx) { s16 cursorX; s16 cursorY; volatile s16 oldCursorPoint; + bool dpad = CVar_GetS32("gDpadPauseName", 0); OPEN_DISPS(globalCtx->state.gfxCtx, "../z_kaleido_equipment.c", 219); @@ -174,7 +175,7 @@ void KaleidoScope_DrawEquipment(GlobalContext* globalCtx) { cursorMoveResult = 0; while (cursorMoveResult == 0) { - if (pauseCtx->stickRelX < -30) { + if ((pauseCtx->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { if (pauseCtx->cursorX[PAUSE_EQUIP] != 0) { pauseCtx->cursorX[PAUSE_EQUIP] -= 1; pauseCtx->cursorPoint[PAUSE_EQUIP] -= 1; @@ -216,7 +217,7 @@ void KaleidoScope_DrawEquipment(GlobalContext* globalCtx) { cursorMoveResult = 3; } } - } else if (pauseCtx->stickRelX > 30) { + } else if ((pauseCtx->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { if (pauseCtx->cursorX[PAUSE_EQUIP] < 3) { pauseCtx->cursorX[PAUSE_EQUIP] += 1; pauseCtx->cursorPoint[PAUSE_EQUIP] += 1; @@ -264,7 +265,7 @@ void KaleidoScope_DrawEquipment(GlobalContext* globalCtx) { cursorMoveResult = 0; while (cursorMoveResult == 0) { - if (pauseCtx->stickRelY > 30) { + if ((pauseCtx->stickRelY > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { if (pauseCtx->cursorY[PAUSE_EQUIP] != 0) { pauseCtx->cursorY[PAUSE_EQUIP] -= 1; pauseCtx->cursorPoint[PAUSE_EQUIP] -= 4; @@ -286,7 +287,7 @@ void KaleidoScope_DrawEquipment(GlobalContext* globalCtx) { pauseCtx->cursorPoint[PAUSE_EQUIP] = cursorPoint; cursorMoveResult = 3; } - } else if (pauseCtx->stickRelY < -30) { + } else if ((pauseCtx->stickRelY < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DDOWN))) { if (pauseCtx->cursorY[PAUSE_EQUIP] < 3) { pauseCtx->cursorY[PAUSE_EQUIP] += 1; pauseCtx->cursorPoint[PAUSE_EQUIP] += 4; @@ -309,7 +310,7 @@ void KaleidoScope_DrawEquipment(GlobalContext* globalCtx) { } } } else if (pauseCtx->cursorSpecialPos == PAUSE_CURSOR_PAGE_LEFT) { - if (pauseCtx->stickRelX > 30) { + if ((pauseCtx->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { pauseCtx->nameDisplayTimer = 0; pauseCtx->cursorSpecialPos = 0; @@ -356,7 +357,7 @@ void KaleidoScope_DrawEquipment(GlobalContext* globalCtx) { } } } else { - if (pauseCtx->stickRelX < -30) { + if ((pauseCtx->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { pauseCtx->nameDisplayTimer = 0; pauseCtx->cursorSpecialPos = 0; Audio_PlaySoundGeneral(NA_SE_SY_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); diff --git a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_item.c b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_item.c index bb42ff711..d39a90e16 100644 --- a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_item.c +++ b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_item.c @@ -96,6 +96,7 @@ void KaleidoScope_DrawItemSelect(GlobalContext* globalCtx) { s16 cursorY; s16 oldCursorPoint; s16 moveCursorResult; + bool dpad = CVar_GetS32("gDpadPauseName", 0); OPEN_DISPS(globalCtx->state.gfxCtx, "../z_kaleido_item.c", 234); @@ -120,7 +121,7 @@ void KaleidoScope_DrawItemSelect(GlobalContext* globalCtx) { pauseCtx->stickRelX = 40; } - if (ABS(pauseCtx->stickRelX) > 30) { + if ((ABS(pauseCtx->stickRelX) > 30) || (dpad && CHECK_BTN_ANY(input->press.button, BTN_DLEFT | BTN_DRIGHT))) { cursorPoint = pauseCtx->cursorPoint[PAUSE_ITEM]; cursorX = pauseCtx->cursorX[PAUSE_ITEM]; cursorY = pauseCtx->cursorY[PAUSE_ITEM]; @@ -132,7 +133,7 @@ void KaleidoScope_DrawItemSelect(GlobalContext* globalCtx) { if (gSaveContext.inventory.items[pauseCtx->cursorPoint[PAUSE_ITEM]]) {} while (moveCursorResult == 0) { - if (pauseCtx->stickRelX < -30) { + if ((pauseCtx->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { if (pauseCtx->cursorX[PAUSE_ITEM] != 0) { pauseCtx->cursorX[PAUSE_ITEM] -= 1; pauseCtx->cursorPoint[PAUSE_ITEM] -= 1; @@ -164,7 +165,7 @@ void KaleidoScope_DrawItemSelect(GlobalContext* globalCtx) { moveCursorResult = 2; } } - } else if (pauseCtx->stickRelX > 30) { + } else if ((pauseCtx->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { if (pauseCtx->cursorX[PAUSE_ITEM] < 5) { pauseCtx->cursorX[PAUSE_ITEM] += 1; pauseCtx->cursorPoint[PAUSE_ITEM] += 1; @@ -208,7 +209,7 @@ void KaleidoScope_DrawItemSelect(GlobalContext* globalCtx) { cursorItem, pauseCtx->cursorSpecialPos); } } else if (pauseCtx->cursorSpecialPos == PAUSE_CURSOR_PAGE_LEFT) { - if (pauseCtx->stickRelX > 30) { + if ((pauseCtx->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { pauseCtx->nameDisplayTimer = 0; pauseCtx->cursorSpecialPos = 0; @@ -242,7 +243,7 @@ void KaleidoScope_DrawItemSelect(GlobalContext* globalCtx) { } } } else { - if (pauseCtx->stickRelX < -30) { + if ((pauseCtx->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { pauseCtx->nameDisplayTimer = 0; pauseCtx->cursorSpecialPos = 0; @@ -280,13 +281,13 @@ void KaleidoScope_DrawItemSelect(GlobalContext* globalCtx) { if (pauseCtx->cursorSpecialPos == 0) { if (cursorItem != PAUSE_ITEM_NONE) { - if (ABS(pauseCtx->stickRelY) > 30) { + if ((ABS(pauseCtx->stickRelY) > 30) || (dpad && CHECK_BTN_ANY(input->press.button, BTN_DDOWN | BTN_DUP))) { moveCursorResult = 0; cursorPoint = pauseCtx->cursorPoint[PAUSE_ITEM]; cursorY = pauseCtx->cursorY[PAUSE_ITEM]; while (moveCursorResult == 0) { - if (pauseCtx->stickRelY > 30) { + if ((pauseCtx->stickRelY > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { if (pauseCtx->cursorY[PAUSE_ITEM] != 0) { pauseCtx->cursorY[PAUSE_ITEM] -= 1; pauseCtx->cursorPoint[PAUSE_ITEM] -= 6; @@ -300,7 +301,7 @@ void KaleidoScope_DrawItemSelect(GlobalContext* globalCtx) { moveCursorResult = 2; } - } else if (pauseCtx->stickRelY < -30) { + } else if ((pauseCtx->stickRelY < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DDOWN))) { if (pauseCtx->cursorY[PAUSE_ITEM] < 3) { pauseCtx->cursorY[PAUSE_ITEM] += 1; pauseCtx->cursorPoint[PAUSE_ITEM] += 6; diff --git a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_map_PAL.c b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_map_PAL.c index af5c2b424..022fdd41d 100644 --- a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_map_PAL.c +++ b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_map_PAL.c @@ -36,6 +36,7 @@ void KaleidoScope_DrawDungeonMap(GlobalContext* globalCtx, GraphicsContext* gfxC static u16 mapBgPulseStage = 0; InterfaceContext* interfaceCtx = &globalCtx->interfaceCtx; PauseContext* pauseCtx = &globalCtx->pauseCtx; + Input* input = &globalCtx->state.input[0]; s16 i; s16 j; s16 oldCursorPoint; @@ -43,6 +44,7 @@ void KaleidoScope_DrawDungeonMap(GlobalContext* globalCtx, GraphicsContext* gfxC s16 stepG; s16 stepB; u16 rgba16; + bool dpad = CVar_GetS32("gDpadPauseName", 0); OPEN_DISPS(gfxCtx, "../z_kaleido_map_PAL.c", 123); @@ -51,7 +53,7 @@ void KaleidoScope_DrawDungeonMap(GlobalContext* globalCtx, GraphicsContext* gfxC oldCursorPoint = pauseCtx->cursorPoint[PAUSE_MAP]; if (pauseCtx->cursorSpecialPos == 0) { - if (pauseCtx->stickRelX > 30) { + if ((pauseCtx->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { if (pauseCtx->cursorX[PAUSE_MAP] != 0) { KaleidoScope_MoveCursorToSpecialPos(globalCtx, PAUSE_CURSOR_PAGE_RIGHT); } else { @@ -67,7 +69,7 @@ void KaleidoScope_DrawDungeonMap(GlobalContext* globalCtx, GraphicsContext* gfxC } } } - } else if (pauseCtx->stickRelX < -30) { + } else if ((pauseCtx->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { if (pauseCtx->cursorX[PAUSE_MAP] == 0) { KaleidoScope_MoveCursorToSpecialPos(globalCtx, PAUSE_CURSOR_PAGE_LEFT); } else { @@ -82,7 +84,7 @@ void KaleidoScope_DrawDungeonMap(GlobalContext* globalCtx, GraphicsContext* gfxC } if (pauseCtx->cursorPoint[PAUSE_MAP] < 3) { - if (pauseCtx->stickRelY > 30) { + if ((pauseCtx->stickRelY > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { if (pauseCtx->cursorPoint[PAUSE_MAP] != 0) { for (i = pauseCtx->cursorPoint[PAUSE_MAP] - 1; i >= 0; i--) { if (CHECK_DUNGEON_ITEM(i, gSaveContext.mapIndex)) { @@ -92,7 +94,7 @@ void KaleidoScope_DrawDungeonMap(GlobalContext* globalCtx, GraphicsContext* gfxC } } } else { - if (pauseCtx->stickRelY < -30) { + if ((pauseCtx->stickRelY < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DDOWN))) { if (pauseCtx->cursorPoint[PAUSE_MAP] != 2) { for (i = pauseCtx->cursorPoint[PAUSE_MAP] + 1; i < 3; i++) { if (CHECK_DUNGEON_ITEM(i, gSaveContext.mapIndex)) { @@ -104,7 +106,7 @@ void KaleidoScope_DrawDungeonMap(GlobalContext* globalCtx, GraphicsContext* gfxC } } } else { - if (pauseCtx->stickRelY > 30) { + if ((pauseCtx->stickRelY > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { if (pauseCtx->cursorPoint[PAUSE_MAP] >= 4) { for (i = pauseCtx->cursorPoint[PAUSE_MAP] - 3 - 1; i >= 0; i--) { if ((gSaveContext.sceneFlags[gSaveContext.mapIndex].floors & gBitFlags[i]) || @@ -115,7 +117,7 @@ void KaleidoScope_DrawDungeonMap(GlobalContext* globalCtx, GraphicsContext* gfxC } } } - } else if (pauseCtx->stickRelY < -30) { + } else if ((pauseCtx->stickRelY < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DDOWN))) { if (pauseCtx->cursorPoint[PAUSE_MAP] != 10) { for (i = pauseCtx->cursorPoint[PAUSE_MAP] - 3 + 1; i < 11; i++) { if ((gSaveContext.sceneFlags[gSaveContext.mapIndex].floors & gBitFlags[i]) || @@ -138,7 +140,7 @@ void KaleidoScope_DrawDungeonMap(GlobalContext* globalCtx, GraphicsContext* gfxC } } } else if (pauseCtx->cursorSpecialPos == PAUSE_CURSOR_PAGE_LEFT) { - if (pauseCtx->stickRelX > 30) { + if ((pauseCtx->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { pauseCtx->nameDisplayTimer = 0; pauseCtx->cursorSpecialPos = 0; pauseCtx->cursorSlot[PAUSE_MAP] = pauseCtx->cursorPoint[PAUSE_MAP] = pauseCtx->dungeonMapSlot; @@ -148,7 +150,7 @@ void KaleidoScope_DrawDungeonMap(GlobalContext* globalCtx, GraphicsContext* gfxC Audio_PlaySoundGeneral(NA_SE_SY_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); } } else { - if (pauseCtx->stickRelX < -30) { + if ((pauseCtx->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { pauseCtx->nameDisplayTimer = 0; pauseCtx->cursorSpecialPos = 0; pauseCtx->cursorX[PAUSE_MAP] = 1; @@ -396,6 +398,7 @@ void KaleidoScope_DrawWorldMap(GlobalContext* globalCtx, GraphicsContext* gfxCtx }; static u16 D_8082A6D4 = 0; PauseContext* pauseCtx = &globalCtx->pauseCtx; + Input* input = &globalCtx->state.input[0]; s16 i; s16 j; s16 t; @@ -404,6 +407,7 @@ void KaleidoScope_DrawWorldMap(GlobalContext* globalCtx, GraphicsContext* gfxCtx s16 stepR; s16 stepG; s16 stepB; + bool dpad = CVar_GetS32("gDpadPauseName", 0); OPEN_DISPS(gfxCtx, "../z_kaleido_map_PAL.c", 556); @@ -412,7 +416,7 @@ void KaleidoScope_DrawWorldMap(GlobalContext* globalCtx, GraphicsContext* gfxCtx oldCursorPoint = pauseCtx->cursorPoint[PAUSE_WORLD_MAP]; if (pauseCtx->cursorSpecialPos == 0) { - if (pauseCtx->stickRelX > 30) { + if ((pauseCtx->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { D_8082A6D4 = 0; do { @@ -423,7 +427,7 @@ void KaleidoScope_DrawWorldMap(GlobalContext* globalCtx, GraphicsContext* gfxCtx break; } } while (pauseCtx->worldMapPoints[pauseCtx->cursorPoint[PAUSE_WORLD_MAP]] == 0); - } else if (pauseCtx->stickRelX < -30) { + } else if ((pauseCtx->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { D_8082A6D4 = 0; do { @@ -444,7 +448,7 @@ void KaleidoScope_DrawWorldMap(GlobalContext* globalCtx, GraphicsContext* gfxCtx } else { pauseCtx->cursorItem[PAUSE_MAP] = gSaveContext.worldMapArea + 0x18; if (pauseCtx->cursorSpecialPos == PAUSE_CURSOR_PAGE_LEFT) { - if (pauseCtx->stickRelX > 30) { + if ((pauseCtx->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { pauseCtx->cursorPoint[PAUSE_WORLD_MAP] = 0; pauseCtx->cursorSpecialPos = 0; @@ -459,7 +463,7 @@ void KaleidoScope_DrawWorldMap(GlobalContext* globalCtx, GraphicsContext* gfxCtx D_8082A6D4 = 0; } } else { - if (pauseCtx->stickRelX < -30) { + if ((pauseCtx->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { pauseCtx->cursorPoint[PAUSE_WORLD_MAP] = 11; pauseCtx->cursorSpecialPos = 0; @@ -663,7 +667,7 @@ void KaleidoScope_DrawWorldMap(GlobalContext* globalCtx, GraphicsContext* gfxCtx gDPLoadTextureBlock(POLY_KAL_DISP++, gWorldMapDotTex, G_IM_FMT_IA, G_IM_SIZ_8b, 8, 8, 0, G_TX_WRAP | G_TX_NOMIRROR, G_TX_WRAP | G_TX_NOMIRROR, G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, G_TX_NOLOD); - for (j = i = 0; i < 12; i++, t++, j += 4) { + for (j = t = i = 0; i < 12; i++, t++, j += 4) { if (pauseCtx->worldMapPoints[i] != 0) { gDPPipeSync(POLY_KAL_DISP++); diff --git a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_prompt.c b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_prompt.c index c16404fb2..bfb4897d8 100644 --- a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_prompt.c +++ b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_prompt.c @@ -7,12 +7,13 @@ void KaleidoScope_UpdatePrompt(GlobalContext* globalCtx) { Input* input = &globalCtx->state.input[0]; s8 relStickX = input->rel.stick_x; s16 step; + bool dpad = CVar_GetS32("gDpadPauseName", 0); if (((pauseCtx->state == 7) && (pauseCtx->unk_1EC == 1)) || (pauseCtx->state == 0xE) || (pauseCtx->state == 0x10)) { - if ((pauseCtx->promptChoice == 0) && (relStickX >= 30)) { + if ((pauseCtx->promptChoice == 0) && ((relStickX >= 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT)))) { Audio_PlaySoundGeneral(NA_SE_SY_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); pauseCtx->promptChoice = 4; - } else if ((pauseCtx->promptChoice != 0) && (relStickX <= -30)) { + } else if ((pauseCtx->promptChoice != 0) && ((relStickX <= -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT)))) { Audio_PlaySoundGeneral(NA_SE_SY_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); pauseCtx->promptChoice = 0; } diff --git a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope_PAL.c b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope_PAL.c index 140aa7734..9db5399a4 100644 --- a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope_PAL.c +++ b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope_PAL.c @@ -936,8 +936,9 @@ void KaleidoScope_HandlePageToggles(PauseContext* pauseCtx, Input* input) { return; } + bool dpad = CVar_GetS32("gDpadPauseName", 0); if (pauseCtx->cursorSpecialPos == PAUSE_CURSOR_PAGE_LEFT) { - if (pauseCtx->stickRelX < -30) { + if ((pauseCtx->stickRelX < -30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { pauseCtx->pageSwitchTimer++; if ((pauseCtx->pageSwitchTimer >= 10) || (pauseCtx->pageSwitchTimer == 0)) { KaleidoScope_SwitchPage(pauseCtx, 0); @@ -946,7 +947,7 @@ void KaleidoScope_HandlePageToggles(PauseContext* pauseCtx, Input* input) { pauseCtx->pageSwitchTimer = -1; } } else if (pauseCtx->cursorSpecialPos == PAUSE_CURSOR_PAGE_RIGHT) { - if (pauseCtx->stickRelX > 30) { + if ((pauseCtx->stickRelX > 30) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { pauseCtx->pageSwitchTimer++; if ((pauseCtx->pageSwitchTimer >= 10) || (pauseCtx->pageSwitchTimer == 0)) { KaleidoScope_SwitchPage(pauseCtx, 2); From 9a7c63c46cfcb3531c8ae1c0a555b4978556537f Mon Sep 17 00:00:00 2001 From: GaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com> Date: Sat, 2 Apr 2022 13:47:22 +0200 Subject: [PATCH 04/13] Allocate aligned heaps --- soh/include/functions.h | 3 +++ soh/include/variables.h | 4 ++-- soh/src/buffers/heaps.c | 45 +++++++++++++++++++++++++++++++++++++---- soh/src/code/main.c | 3 +++ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/soh/include/functions.h b/soh/include/functions.h index 060022ac9..a4b1a9a99 100644 --- a/soh/include/functions.h +++ b/soh/include/functions.h @@ -2397,6 +2397,9 @@ void FileChoose_Destroy(GameState* thisx); char* SetQuote(); +void Heaps_Alloc(void); +void Heaps_Free(void); + #ifdef __cplusplus }; #endif diff --git a/soh/include/variables.h b/soh/include/variables.h index 80e3b792d..b58166b60 100644 --- a/soh/include/variables.h +++ b/soh/include/variables.h @@ -237,8 +237,8 @@ extern void(*D_801755D0)(void); extern u8 gGfxSPTaskYieldBuffer[OS_YIELD_DATA_SIZE]; // 0xC00 bytes extern u8 gGfxSPTaskStack[0x400]; // 0x400 bytes extern GfxPool gGfxPools[2]; // 0x24820 bytes - extern u8 gAudioHeap[0x38000]; // 0x38000 bytes - extern u8 gSystemHeap[]; + extern u8* gAudioHeap; + extern u8* gSystemHeap; #ifdef __cplusplus }; diff --git a/soh/src/buffers/heaps.c b/soh/src/buffers/heaps.c index 4309acd18..6d162bc9e 100644 --- a/soh/src/buffers/heaps.c +++ b/soh/src/buffers/heaps.c @@ -1,7 +1,44 @@ #include "z64.h" +#include +#include -// 0x38000 bytes -u8 gAudioHeap[0x38000]; +#ifndef _MSC_VER +#include +#endif -//u8 gSystemHeap[UNK_SIZE]; -u8 gSystemHeap[1024 * 1024 * 128]; +#define AUDIO_HEAP_SIZE 0x38000 +#define SYSTEM_HEAP_SIZE (1024 * 1024 * 128) + +u8* gAudioHeap; + +u8* gSystemHeap; + +void Heaps_Alloc(void) +{ +#ifdef _MSC_VER + gAudioHeap = (u8*)_aligned_malloc(AUDIO_HEAP_SIZE, 16); + gSystemHeap = (u8*)_aligned_malloc(SYSTEM_HEAP_SIZE, 16); +#elif defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L) + if (posix_memalign((void**)&gAudioHeap, 16, AUDIO_HEAP_SIZE) != 0) + gAudioHeap = NULL; + if (posix_memalign((void**)&gSystemHeap, 16, SYSTEM_HEAP_SIZE) != 0) + gSystemHeap = NULL; +#else + gAudioHeap = (u8*)memalign(16, AUDIO_HEAP_SIZE); + gSystemHeap = (u8*)memalign(16, SYSTEM_HEAP_SIZE); +#endif + + assert(gAudioHeap != NULL); + assert(gSystemHeap != NULL); +} + +void Heaps_Free(void) +{ +#ifdef _MSC_VER + _aligned_free(gAudioHeap); + _aligned_free(gSystemHeap); +#else + free(gAudioHeap); + free(gSystemHeap); +#endif +} diff --git a/soh/src/code/main.c b/soh/src/code/main.c index eaaa7b388..0680aad3e 100644 --- a/soh/src/code/main.c +++ b/soh/src/code/main.c @@ -63,6 +63,7 @@ void Main(void* arg) { PreNmiBuff_Init(gAppNmiBufferPtr); Fault_Init(); SysCfb_Init(0); + Heaps_Alloc(); sysHeap = gSystemHeap; fb = SysCfb_GetFbPtr(0); gSystemHeapSize = 1024 * 1024 * 4; @@ -131,4 +132,6 @@ void Main(void* arg) { osDestroyThread(&sGraphThread); func_800FBFD8(); osSyncPrintf("mainproc 実行終了\n"); // "End of execution" + + Heaps_Free(); } From beb454d0002c9e6ef5c7f708ae7f088c15aa59eb Mon Sep 17 00:00:00 2001 From: GaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com> Date: Mon, 11 Apr 2022 19:46:42 +0200 Subject: [PATCH 05/13] Formatting fixes --- soh/src/buffers/heaps.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/soh/src/buffers/heaps.c b/soh/src/buffers/heaps.c index 6d162bc9e..d09f86e72 100644 --- a/soh/src/buffers/heaps.c +++ b/soh/src/buffers/heaps.c @@ -10,22 +10,21 @@ #define SYSTEM_HEAP_SIZE (1024 * 1024 * 128) u8* gAudioHeap; - u8* gSystemHeap; void Heaps_Alloc(void) { #ifdef _MSC_VER - gAudioHeap = (u8*)_aligned_malloc(AUDIO_HEAP_SIZE, 16); - gSystemHeap = (u8*)_aligned_malloc(SYSTEM_HEAP_SIZE, 16); + gAudioHeap = (u8*)_aligned_malloc(AUDIO_HEAP_SIZE, 0x10); + gSystemHeap = (u8*)_aligned_malloc(SYSTEM_HEAP_SIZE, 0x10); #elif defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L) - if (posix_memalign((void**)&gAudioHeap, 16, AUDIO_HEAP_SIZE) != 0) + if (posix_memalign((void**)&gAudioHeap, 0x10, AUDIO_HEAP_SIZE) != 0) gAudioHeap = NULL; - if (posix_memalign((void**)&gSystemHeap, 16, SYSTEM_HEAP_SIZE) != 0) + if (posix_memalign((void**)&gSystemHeap, 0x10, SYSTEM_HEAP_SIZE) != 0) gSystemHeap = NULL; #else - gAudioHeap = (u8*)memalign(16, AUDIO_HEAP_SIZE); - gSystemHeap = (u8*)memalign(16, SYSTEM_HEAP_SIZE); + gAudioHeap = (u8*)memalign(0x10, AUDIO_HEAP_SIZE); + gSystemHeap = (u8*)memalign(0x10, SYSTEM_HEAP_SIZE); #endif assert(gAudioHeap != NULL); From 03a5c7ed29db889344a8275c4c0a8c7ae1480779 Mon Sep 17 00:00:00 2001 From: Josh Bodner <30329717+jbodner09@users.noreply.github.com> Date: Mon, 11 Apr 2022 14:06:55 -0700 Subject: [PATCH 06/13] Added DPad support to ocarina and text prompts (#137) * Added DPad support to ocarina playing and text choice selection. * Wrap changes in CVar * Fix mapping not updating if CVar is changed in-game * Remove unnecessary const_cast * Fixed transparent texture making framebuffers also transparent in D3D11. (#84) This happened with the Mirror Shield in the inventory screen preview. * Add save editor * Added DPad support to ocarina playing and text choice selection. * Fixing rebase conflict * Fix mapping not updating if CVar is changed in-game * Remove unnecessary const_cast * Added DPad support to file selection and pause screens (#124) * Added DPad support to file selection and pause screens * Wrap changes behind CVar * Fix merge conflict for real * Remove unnecessary const_cast * Fixed transparent texture making framebuffers also transparent in D3D11. (#84) This happened with the Mirror Shield in the inventory screen preview. * Add save editor * Added DPad support to file selection and pause screens * Fixing rebase conflict * Remove unnecessary const_cast Co-authored-by: MaikelChan Co-authored-by: rozlette * Added DPad support to ocarina playing and text choice selection. * Fixing rebase conflict again * Fix mapping not updating if CVar is changed in-game Co-authored-by: MaikelChan Co-authored-by: rozlette --- libultraship/libultraship/GameSettings.cpp | 4 ++ libultraship/libultraship/GameSettings.h | 1 + libultraship/libultraship/SohImGuiImpl.cpp | 5 +++ soh/src/code/code_800EC960.c | 46 +++++++++++++--------- soh/src/code/z_message_PAL.c | 7 ++-- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/libultraship/libultraship/GameSettings.cpp b/libultraship/libultraship/GameSettings.cpp index 28f4b23cb..c3294d9e3 100644 --- a/libultraship/libultraship/GameSettings.cpp +++ b/libultraship/libultraship/GameSettings.cpp @@ -92,6 +92,9 @@ namespace Game { Settings.controller.dpad_pause_name = stob(Conf[ControllerSection]["dpad_pause_name"]); CVar_SetS32("gDpadPauseName", Settings.controller.dpad_pause_name); + + Settings.controller.dpad_ocarina_text = stob(Conf[ControllerSection]["dpad_ocarina_text"]); + CVar_SetS32("gDpadOcarinaText", Settings.controller.dpad_ocarina_text); // Cheats Settings.cheats.debug_mode = stob(Conf[CheatSection]["debug_mode"]); @@ -153,6 +156,7 @@ namespace Game { Conf[ControllerSection]["input_scale"] = std::to_string(Settings.controller.input_scale); Conf[ControllerSection]["input_enabled"] = std::to_string(Settings.controller.input_enabled); Conf[ControllerSection]["dpad_pause_name"] = std::to_string(Settings.controller.dpad_pause_name); + Conf[ControllerSection]["dpad_ocarina_text"] = std::to_string(Settings.controller.dpad_ocarina_text); // Cheats Conf[CheatSection]["debug_mode"] = std::to_string(Settings.cheats.debug_mode); diff --git a/libultraship/libultraship/GameSettings.h b/libultraship/libultraship/GameSettings.h index 15ebdad16..8e1293c65 100644 --- a/libultraship/libultraship/GameSettings.h +++ b/libultraship/libultraship/GameSettings.h @@ -35,6 +35,7 @@ struct SoHConfigType { float gyroDriftY = 0.0f; bool input_enabled = false; bool dpad_pause_name = false; + bool dpad_ocarina_text = false; } controller; // Cheats diff --git a/libultraship/libultraship/SohImGuiImpl.cpp b/libultraship/libultraship/SohImGuiImpl.cpp index cef156246..7bdf4859a 100644 --- a/libultraship/libultraship/SohImGuiImpl.cpp +++ b/libultraship/libultraship/SohImGuiImpl.cpp @@ -360,6 +360,11 @@ namespace SohImGui { needs_save = true; } + if (ImGui::Checkbox("DPad Support in Ocarina and Text Choice", &Game::Settings.controller.dpad_ocarina_text)) { + CVar_SetS32("gDpadOcarinaText", Game::Settings.controller.dpad_ocarina_text); + needs_save = true; + } + ImGui::EndMenu(); } diff --git a/soh/src/code/code_800EC960.c b/soh/src/code/code_800EC960.c index 5efa93167..23d1ba375 100644 --- a/soh/src/code/code_800EC960.c +++ b/soh/src/code/code_800EC960.c @@ -824,10 +824,12 @@ NatureAmbienceDataIO sNatureAmbienceDataIO[20] = { }, }; -u32 sOcarinaAllowedBtnMask = 0x800F; -s32 sOcarinaABtnMap = 0x8000; -s32 sOcarinaCUPBtnMap = 8; -s32 sOcarinaCDownBtnMap = 4; +u32 sOcarinaAllowedBtnMask = (BTN_A | BTN_CUP | BTN_CDOWN | BTN_CLEFT | BTN_CRIGHT); +s32 sOcarinaABtnMap = BTN_A; +s32 sOcarinaCUPBtnMap = BTN_CUP; +s32 sOcarinaCDownBtnMap = BTN_CDOWN; +s32 sOcarinaCLeftBtnMap = BTN_CLEFT; +s32 sOcarinaCRightBtnMap = BTN_CRIGHT; u8 sOcarinaInpEnabled = 0; s8 D_80130F10 = 0; // "OCA", ocarina active? u8 sCurOcarinaBtnVal = 0xFF; @@ -1245,19 +1247,23 @@ void func_800F56A8(void); void Audio_PlayNatureAmbienceSequence(u8 natureAmbienceId); s32 Audio_SetGanonDistVol(u8 targetVol); -void func_800EC960(u8 custom) { - if (!custom) { - osSyncPrintf("AUDIO : Ocarina Control Assign Normal\n"); +// Function originally not called, so repurposing for DPad input +void func_800EC960(u8 dpad) { + if (dpad) { + sOcarinaAllowedBtnMask = + (BTN_A | BTN_CUP | BTN_CDOWN | BTN_CLEFT | BTN_CRIGHT | BTN_DUP | BTN_DDOWN | BTN_DLEFT | BTN_DRIGHT); + sOcarinaABtnMap = BTN_A; + sOcarinaCUPBtnMap = BTN_CUP | BTN_DUP; + sOcarinaCDownBtnMap = BTN_CDOWN | BTN_DDOWN; + sOcarinaCLeftBtnMap = BTN_CLEFT | BTN_DLEFT; + sOcarinaCRightBtnMap = BTN_CRIGHT | BTN_DRIGHT; + } else { sOcarinaAllowedBtnMask = (BTN_A | BTN_CUP | BTN_CDOWN | BTN_CLEFT | BTN_CRIGHT); sOcarinaABtnMap = BTN_A; sOcarinaCUPBtnMap = BTN_CUP; sOcarinaCDownBtnMap = BTN_CDOWN; - } else { - osSyncPrintf("AUDIO : Ocarina Control Assign Custom\n"); - sOcarinaAllowedBtnMask = (BTN_A | BTN_B | BTN_CDOWN | BTN_CLEFT | BTN_CRIGHT); - sOcarinaABtnMap = BTN_B; - sOcarinaCUPBtnMap = BTN_CDOWN; - sOcarinaCDownBtnMap = BTN_A; + sOcarinaCLeftBtnMap = BTN_CLEFT; + sOcarinaCRightBtnMap = BTN_CRIGHT; } } @@ -1542,6 +1548,7 @@ void func_800ED200(void) { void func_800ED458(s32 arg0) { u32 phi_v1_2; + bool dpad = CVar_GetS32("gDpadOcarinaText", 0); if (D_80130F3C != 0 && D_80131880 != 0) { D_80131880--; @@ -1561,6 +1568,7 @@ void func_800ED458(s32 arg0) { D_8016BA18 &= phi_v1_2; } + func_800EC960(dpad); if (D_8016BA18 & sOcarinaABtnMap) { osSyncPrintf("Presss NA_KEY_D4 %08x\n", sOcarinaABtnMap); sCurOcarinaBtnVal = 2; @@ -1569,12 +1577,12 @@ void func_800ED458(s32 arg0) { osSyncPrintf("Presss NA_KEY_F4 %08x\n", sOcarinaCDownBtnMap); sCurOcarinaBtnVal = 5; sCurOcarinaBtnIdx = 1; - } else if (D_8016BA18 & 1) { - osSyncPrintf("Presss NA_KEY_A4 %08x\n", 1); + } else if (D_8016BA18 & sOcarinaCRightBtnMap) { + osSyncPrintf("Presss NA_KEY_A4 %08x\n", sOcarinaCRightBtnMap); sCurOcarinaBtnVal = 9; sCurOcarinaBtnIdx = 2; - } else if (D_8016BA18 & 2) { - osSyncPrintf("Presss NA_KEY_B4 %08x\n", 2); + } else if (D_8016BA18 & sOcarinaCLeftBtnMap) { + osSyncPrintf("Presss NA_KEY_B4 %08x\n", sOcarinaCRightBtnMap); sCurOcarinaBtnVal = 0xB; sCurOcarinaBtnIdx = 3; } else if (D_8016BA18 & sOcarinaCUPBtnMap) { @@ -1671,7 +1679,7 @@ void Audio_OcaSetSongPlayback(s8 songIdxPlusOne, s8 playbackState) { void Audio_OcaPlayback(void) { u32 noteTimerStep; - u32 nextNoteTimerStep; + u32 nextNoteTimerStep = 0; if (sPlaybackState != 0) { if (sStaffPlaybackPos == 0) { @@ -4772,7 +4780,7 @@ void Audio_SetCodeReverb(s8 reverb) { } void func_800F6700(s8 arg0) { - s8 sp1F; + s8 sp1F = 0; switch (arg0) { case 0: diff --git a/soh/src/code/z_message_PAL.c b/soh/src/code/z_message_PAL.c index 4979f047c..43b0fcc12 100644 --- a/soh/src/code/z_message_PAL.c +++ b/soh/src/code/z_message_PAL.c @@ -203,8 +203,9 @@ void Message_HandleChoiceSelection(GlobalContext* globalCtx, u8 numChoices) { static s16 sAnalogStickHeld = false; MessageContext* msgCtx = &globalCtx->msgCtx; Input* input = &globalCtx->state.input[0]; + bool dpad = CVar_GetS32("gDpadOcarinaText", 0); - if (input->rel.stick_y >= 30 && !sAnalogStickHeld) { + if ((input->rel.stick_y >= 30 && !sAnalogStickHeld) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { sAnalogStickHeld = true; msgCtx->choiceIndex--; if (msgCtx->choiceIndex > 128) { @@ -212,7 +213,7 @@ void Message_HandleChoiceSelection(GlobalContext* globalCtx, u8 numChoices) { } else { Audio_PlaySoundGeneral(NA_SE_SY_CURSOR, &D_801333D4, 4, &D_801333E0, &D_801333E0, &D_801333E8); } - } else if (input->rel.stick_y <= -30 && !sAnalogStickHeld) { + } else if ((input->rel.stick_y <= -30 && !sAnalogStickHeld) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DDOWN))) { sAnalogStickHeld = true; msgCtx->choiceIndex++; if (msgCtx->choiceIndex > numChoices) { @@ -3032,7 +3033,7 @@ void Message_Update(GlobalContext* globalCtx) { Input* input = &globalCtx->state.input[0]; s16 var; s16 focusScreenPosX; - s16 averageY; + s16 averageY = 0; s16 playerFocusScreenPosY; s16 actorFocusScreenPosY; From e16481933965a46dd2f2a9394394193cab27a5bf Mon Sep 17 00:00:00 2001 From: Josh Bodner <30329717+jbodner09@users.noreply.github.com> Date: Tue, 12 Apr 2022 18:38:08 -0700 Subject: [PATCH 07/13] Added DPad support to shops (#139) * Added DPad support to shops * Wrap changes in CVar * Remove unnesseccary const_cast * Fixed transparent texture making framebuffers also transparent in D3D11. (#84) This happened with the Mirror Shield in the inventory screen preview. * Add save editor * Added DPad support to shops * Fixing rebase conflict * Remove unnesseccary const_cast * Added DPad support to file selection and pause screens (#124) * Added DPad support to file selection and pause screens * Wrap changes behind CVar * Fix merge conflict for real * Remove unnecessary const_cast * Fixed transparent texture making framebuffers also transparent in D3D11. (#84) This happened with the Mirror Shield in the inventory screen preview. * Add save editor * Added DPad support to file selection and pause screens * Fixing rebase conflict * Remove unnecessary const_cast Co-authored-by: MaikelChan Co-authored-by: rozlette * Added DPad support to shops * Fixing rebase conflict again * Allocate aligned heaps * Formatting fixes * Added DPad support to ocarina and text prompts (#137) * Added DPad support to ocarina playing and text choice selection. * Wrap changes in CVar * Fix mapping not updating if CVar is changed in-game * Remove unnecessary const_cast * Fixed transparent texture making framebuffers also transparent in D3D11. (#84) This happened with the Mirror Shield in the inventory screen preview. * Add save editor * Added DPad support to ocarina playing and text choice selection. * Fixing rebase conflict * Fix mapping not updating if CVar is changed in-game * Remove unnecessary const_cast * Added DPad support to file selection and pause screens (#124) * Added DPad support to file selection and pause screens * Wrap changes behind CVar * Fix merge conflict for real * Remove unnecessary const_cast * Fixed transparent texture making framebuffers also transparent in D3D11. (#84) This happened with the Mirror Shield in the inventory screen preview. * Add save editor * Added DPad support to file selection and pause screens * Fixing rebase conflict * Remove unnecessary const_cast Co-authored-by: MaikelChan Co-authored-by: rozlette * Added DPad support to ocarina playing and text choice selection. * Fixing rebase conflict again * Fix mapping not updating if CVar is changed in-game Co-authored-by: MaikelChan Co-authored-by: rozlette * Added DPad support to shops * Fixing rebase conflict for the last time * Totally Fixing rebase conflict again I promise * This has to be the last time I fix this rebase conflict Co-authored-by: MaikelChan Co-authored-by: rozlette Co-authored-by: GaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com> --- libultraship/libultraship/GameSettings.cpp | 4 ++ libultraship/libultraship/GameSettings.h | 1 + libultraship/libultraship/SohImGuiImpl.cpp | 5 +++ .../overlays/actors/ovl_En_Ossan/z_en_ossan.c | 38 +++++++++++-------- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/libultraship/libultraship/GameSettings.cpp b/libultraship/libultraship/GameSettings.cpp index c3294d9e3..678555c7d 100644 --- a/libultraship/libultraship/GameSettings.cpp +++ b/libultraship/libultraship/GameSettings.cpp @@ -95,6 +95,9 @@ namespace Game { Settings.controller.dpad_ocarina_text = stob(Conf[ControllerSection]["dpad_ocarina_text"]); CVar_SetS32("gDpadOcarinaText", Settings.controller.dpad_ocarina_text); + + Settings.controller.dpad_shop = stob(Conf[ControllerSection]["dpad_shop"]); + CVar_SetS32("gDpadShop", Settings.controller.dpad_shop); // Cheats Settings.cheats.debug_mode = stob(Conf[CheatSection]["debug_mode"]); @@ -157,6 +160,7 @@ namespace Game { Conf[ControllerSection]["input_enabled"] = std::to_string(Settings.controller.input_enabled); Conf[ControllerSection]["dpad_pause_name"] = std::to_string(Settings.controller.dpad_pause_name); Conf[ControllerSection]["dpad_ocarina_text"] = std::to_string(Settings.controller.dpad_ocarina_text); + Conf[ControllerSection]["dpad_shop"] = std::to_string(Settings.controller.dpad_shop); // Cheats Conf[CheatSection]["debug_mode"] = std::to_string(Settings.cheats.debug_mode); diff --git a/libultraship/libultraship/GameSettings.h b/libultraship/libultraship/GameSettings.h index 8e1293c65..17079c9bf 100644 --- a/libultraship/libultraship/GameSettings.h +++ b/libultraship/libultraship/GameSettings.h @@ -36,6 +36,7 @@ struct SoHConfigType { bool input_enabled = false; bool dpad_pause_name = false; bool dpad_ocarina_text = false; + bool dpad_shop = false; } controller; // Cheats diff --git a/libultraship/libultraship/SohImGuiImpl.cpp b/libultraship/libultraship/SohImGuiImpl.cpp index 7bdf4859a..9ba3184d8 100644 --- a/libultraship/libultraship/SohImGuiImpl.cpp +++ b/libultraship/libultraship/SohImGuiImpl.cpp @@ -365,6 +365,11 @@ namespace SohImGui { needs_save = true; } + if (ImGui::Checkbox("DPad Support for Browsing Shop Items", &Game::Settings.controller.dpad_shop)) { + CVar_SetS32("gDpadShop", Game::Settings.controller.dpad_shop); + needs_save = true; + } + ImGui::EndMenu(); } diff --git a/soh/src/overlays/actors/ovl_En_Ossan/z_en_ossan.c b/soh/src/overlays/actors/ovl_En_Ossan/z_en_ossan.c index 04975d910..4d5fefec6 100644 --- a/soh/src/overlays/actors/ovl_En_Ossan/z_en_ossan.c +++ b/soh/src/overlays/actors/ovl_En_Ossan/z_en_ossan.c @@ -948,7 +948,9 @@ s32 EnOssan_FacingShopkeeperDialogResult(EnOssan* this, GlobalContext* globalCtx } void EnOssan_State_FacingShopkeeper(EnOssan* this, GlobalContext* globalCtx, Player* player) { + Input* input = &globalCtx->state.input[0]; u8 nextIndex; + bool dpad = CVar_GetS32("gDpadShop", 0); if ((Message_GetState(&globalCtx->msgCtx) == TEXT_STATE_CHOICE) && !EnOssan_TestEndInteraction(this, globalCtx, &globalCtx->state.input[0])) { @@ -957,7 +959,7 @@ void EnOssan_State_FacingShopkeeper(EnOssan* this, GlobalContext* globalCtx, Pla return; } // Stick Left - if (this->stickAccumX < 0) { + if ((this->stickAccumX < 0) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { nextIndex = EnOssan_SetCursorIndexFromNeutral(this, 4); if (nextIndex != CURSOR_INVALID) { this->cursorIndex = nextIndex; @@ -966,7 +968,7 @@ void EnOssan_State_FacingShopkeeper(EnOssan* this, GlobalContext* globalCtx, Pla this->stickLeftPrompt.isEnabled = false; func_80078884(NA_SE_SY_CURSOR); } - } else if (this->stickAccumX > 0) { + } else if ((this->stickAccumX > 0) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { nextIndex = EnOssan_SetCursorIndexFromNeutral(this, 0); if (nextIndex != CURSOR_INVALID) { this->cursorIndex = nextIndex; @@ -1023,11 +1025,13 @@ void EnOssan_State_LookToRightShelf(EnOssan* this, GlobalContext* globalCtx, Pla } } -void EnOssan_CursorUpDown(EnOssan* this) { +void EnOssan_CursorUpDown(EnOssan* this, GlobalContext* globalCtx) { + Input* input = &globalCtx->state.input[0]; u8 curTemp = this->cursorIndex; u8 curScanTemp; + bool dpad = CVar_GetS32("gDpadShop", 0); - if (this->stickAccumY < 0) { + if ((this->stickAccumY < 0) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DDOWN))) { curTemp &= 0xFE; if (this->shelfSlots[curTemp] != NULL) { this->cursorIndex = curTemp; @@ -1066,7 +1070,7 @@ void EnOssan_CursorUpDown(EnOssan* this) { } } } - } else if (this->stickAccumY > 0) { + } else if ((this->stickAccumY > 0) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DUP))) { curTemp |= 1; if (this->shelfSlots[curTemp] != NULL) { this->cursorIndex = curTemp; @@ -1172,11 +1176,13 @@ s32 EnOssan_HasPlayerSelectedItem(GlobalContext* globalCtx, EnOssan* this, Input } void EnOssan_State_BrowseLeftShelf(EnOssan* this, GlobalContext* globalCtx, Player* player) { + Input* input = &globalCtx->state.input[0]; s32 a; s32 b; u8 prevIndex = this->cursorIndex; s32 c; s32 d; + bool dpad = CVar_GetS32("gDpadShop", 0); if (!EnOssan_ReturnItemToShelf(this)) { osSyncPrintf("%s[%d]:" VT_FGCOL(GREEN) "ズーム中ï¼ï¼" VT_RST "\n", "../z_en_oB1.c", 2152); @@ -1193,7 +1199,7 @@ void EnOssan_State_BrowseLeftShelf(EnOssan* this, GlobalContext* globalCtx, Play if ((Message_GetState(&globalCtx->msgCtx) == TEXT_STATE_EVENT) && !EnOssan_HasPlayerSelectedItem(globalCtx, this, &globalCtx->state.input[0])) { if (this->moveHorizontal) { - if (this->stickAccumX > 0) { + if ((this->stickAccumX > 0) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { a = EnOssan_CursorRight(this, this->cursorIndex, 4); if (a != CURSOR_INVALID) { this->cursorIndex = a; @@ -1201,14 +1207,14 @@ void EnOssan_State_BrowseLeftShelf(EnOssan* this, GlobalContext* globalCtx, Play EnOssan_SetLookToShopkeeperFromShelf(globalCtx, this); return; } - } else if (this->stickAccumX < 0) { + } else if ((this->stickAccumX < 0) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { b = EnOssan_CursorLeft(this, this->cursorIndex, 8); if (b != CURSOR_INVALID) { this->cursorIndex = b; } } } else { - if (this->stickAccumX > 0 && this->stickAccumX > 500) { + if ((this->stickAccumX > 0 && this->stickAccumX > 500) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { c = EnOssan_CursorRight(this, this->cursorIndex, 4); if (c != CURSOR_INVALID) { this->cursorIndex = c; @@ -1216,14 +1222,14 @@ void EnOssan_State_BrowseLeftShelf(EnOssan* this, GlobalContext* globalCtx, Play EnOssan_SetLookToShopkeeperFromShelf(globalCtx, this); return; } - } else if (this->stickAccumX < 0 && this->stickAccumX < -500) { + } else if ((this->stickAccumX < 0 && this->stickAccumX < -500) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { d = EnOssan_CursorLeft(this, this->cursorIndex, 8); if (d != CURSOR_INVALID) { this->cursorIndex = d; } } } - EnOssan_CursorUpDown(this); + EnOssan_CursorUpDown(this, globalCtx); if (this->cursorIndex != prevIndex) { Message_ContinueTextbox(globalCtx, this->shelfSlots[this->cursorIndex]->actor.textId); func_80078884(NA_SE_SY_CURSOR); @@ -1232,9 +1238,11 @@ void EnOssan_State_BrowseLeftShelf(EnOssan* this, GlobalContext* globalCtx, Play } void EnOssan_State_BrowseRightShelf(EnOssan* this, GlobalContext* globalCtx, Player* player) { + Input* input = &globalCtx->state.input[0]; s32 pad[2]; u8 prevIndex; u8 nextIndex; + bool dpad = CVar_GetS32("gDpadShop", 0); prevIndex = this->cursorIndex; if (!EnOssan_ReturnItemToShelf(this)) { @@ -1252,7 +1260,7 @@ void EnOssan_State_BrowseRightShelf(EnOssan* this, GlobalContext* globalCtx, Pla if ((Message_GetState(&globalCtx->msgCtx) == TEXT_STATE_EVENT) && !EnOssan_HasPlayerSelectedItem(globalCtx, this, &globalCtx->state.input[0])) { if (this->moveHorizontal) { - if (this->stickAccumX < 0) { + if ((this->stickAccumX < 0) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { nextIndex = EnOssan_CursorRight(this, this->cursorIndex, 0); if (nextIndex != CURSOR_INVALID) { this->cursorIndex = nextIndex; @@ -1260,14 +1268,14 @@ void EnOssan_State_BrowseRightShelf(EnOssan* this, GlobalContext* globalCtx, Pla EnOssan_SetLookToShopkeeperFromShelf(globalCtx, this); return; } - } else if (this->stickAccumX > 0) { + } else if ((this->stickAccumX > 0) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { nextIndex = EnOssan_CursorLeft(this, this->cursorIndex, 4); if (nextIndex != CURSOR_INVALID) { this->cursorIndex = nextIndex; } } } else { - if (this->stickAccumX < 0 && this->stickAccumX < -500) { + if ((this->stickAccumX < 0 && this->stickAccumX < -500) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DLEFT))) { nextIndex = EnOssan_CursorRight(this, this->cursorIndex, 0); if (nextIndex != CURSOR_INVALID) { this->cursorIndex = nextIndex; @@ -1275,14 +1283,14 @@ void EnOssan_State_BrowseRightShelf(EnOssan* this, GlobalContext* globalCtx, Pla EnOssan_SetLookToShopkeeperFromShelf(globalCtx, this); return; } - } else if (this->stickAccumX > 0 && this->stickAccumX > 500) { + } else if ((this->stickAccumX > 0 && this->stickAccumX > 500) || (dpad && CHECK_BTN_ALL(input->press.button, BTN_DRIGHT))) { nextIndex = EnOssan_CursorLeft(this, this->cursorIndex, 4); if (nextIndex != CURSOR_INVALID) { this->cursorIndex = nextIndex; } } } - EnOssan_CursorUpDown(this); + EnOssan_CursorUpDown(this, globalCtx); if (this->cursorIndex != prevIndex) { Message_ContinueTextbox(globalCtx, this->shelfSlots[this->cursorIndex]->actor.textId); func_80078884(NA_SE_SY_CURSOR); From 0ef2d0c750fec74716d9113a68a4c0181640028d Mon Sep 17 00:00:00 2001 From: rozlette Date: Wed, 13 Apr 2022 23:53:04 -0500 Subject: [PATCH 08/13] Remove leftover debug code (fixes #60) --- soh/src/overlays/actors/ovl_Boss_Ganondrof/z_boss_ganondrof.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/soh/src/overlays/actors/ovl_Boss_Ganondrof/z_boss_ganondrof.c b/soh/src/overlays/actors/ovl_Boss_Ganondrof/z_boss_ganondrof.c index 6f0c3dc30..94ea16422 100644 --- a/soh/src/overlays/actors/ovl_Boss_Ganondrof/z_boss_ganondrof.c +++ b/soh/src/overlays/actors/ovl_Boss_Ganondrof/z_boss_ganondrof.c @@ -1274,7 +1274,7 @@ void BossGanondrof_CollisionCheck(BossGanondrof* this, GlobalContext* globalCtx) this->actor.colChkInfo.health -= dmg; } - if ((s8)this->actor.colChkInfo.health <= 0 || 1) { + if ((s8)this->actor.colChkInfo.health <= 0) { BossGanondrof_SetupDeath(this, globalCtx); Enemy_StartFinishingBlow(globalCtx, &this->actor); return; From 1aa08caade5bcb8a840eced6a71a74abddbc81cf Mon Sep 17 00:00:00 2001 From: Sparkling Shampoo Date: Wed, 6 Apr 2022 23:33:32 -0400 Subject: [PATCH 09/13] Prevent cached resource from being acquired while it is being destroyed Fixes #158 --- libultraship/libultraship/ResourceMgr.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libultraship/libultraship/ResourceMgr.cpp b/libultraship/libultraship/ResourceMgr.cpp index e6f3c8a0a..b4d7b76a8 100644 --- a/libultraship/libultraship/ResourceMgr.cpp +++ b/libultraship/libultraship/ResourceMgr.cpp @@ -217,9 +217,12 @@ namespace Ship { std::shared_ptr ResourceMgr::GetCachedFile(std::string FilePath) { auto resCacheFind = ResourceCache.find(FilePath); - - if (resCacheFind != ResourceCache.end()) + + if (resCacheFind != ResourceCache.end() && + resCacheFind->second.use_count() > 0) + { return resCacheFind->second; + } else return nullptr; } From 1dcd24e7e2aad2b22d2bfdb398e3a05e8a808eef Mon Sep 17 00:00:00 2001 From: "Sparkling.Shampoo" <102923452+sparklingshampoo@users.noreply.github.com> Date: Sun, 17 Apr 2022 11:19:45 -0400 Subject: [PATCH 10/13] Fix cylinder-tri intersection for GCC due to FLT_EVAL_METHOD (#157) --- soh/src/code/sys_math3d.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/soh/src/code/sys_math3d.c b/soh/src/code/sys_math3d.c index d442b7c4a..39ffea881 100644 --- a/soh/src/code/sys_math3d.c +++ b/soh/src/code/sys_math3d.c @@ -1857,7 +1857,7 @@ s32 Math3D_CylTriVsIntersect(Cylinder16* cyl, TriNorm* tri, Vec3f* intersect) { } } - if (minDistSq != 1.e38f) { + if (minDistSq != (f32)1.e38f) { return true; } From a11038f51555aa57e82ae241fc52e3164e937512 Mon Sep 17 00:00:00 2001 From: Rozelette Date: Sun, 17 Apr 2022 10:24:43 -0500 Subject: [PATCH 11/13] Add flag, equipment, and quest status editors (#164) --- libultraship/libultraship/SohImGuiImpl.cpp | 31 +- libultraship/libultraship/SohImGuiImpl.h | 3 +- soh/soh.vcxproj | 2 + soh/soh.vcxproj.filters | 6 + .../Enhancements/debugger/debugSaveEditor.cpp | 1223 +++++++++++++---- soh/soh/util.cpp | 314 +++++ soh/soh/util.h | 11 + 7 files changed, 1281 insertions(+), 309 deletions(-) create mode 100644 soh/soh/util.cpp create mode 100644 soh/soh/util.h diff --git a/libultraship/libultraship/SohImGuiImpl.cpp b/libultraship/libultraship/SohImGuiImpl.cpp index 9ba3184d8..b7e6e01ab 100644 --- a/libultraship/libultraship/SohImGuiImpl.cpp +++ b/libultraship/libultraship/SohImGuiImpl.cpp @@ -179,21 +179,46 @@ namespace SohImGui { stbi_image_free(img_data); } - void LoadResource(const std::string& name, const std::string& path) { + void LoadResource(const std::string& name, const std::string& path, const ImVec4& tint) { GfxRenderingAPI* api = gfx_get_current_rendering_api(); const auto res = static_cast(GlobalCtx2::GetInstance()->GetResourceManager()->LoadResource(normalize(path)).get()); - if (res->texType != Ship::TextureType::RGBA32bpp) { + std::vector texBuffer; + texBuffer.reserve(res->width * res->height * 4); + + switch (res->texType) { + case Ship::TextureType::RGBA32bpp: + texBuffer.assign(res->imageData, res->imageData + (res->width * res->height * 4)); + break; + case Ship::TextureType::GrayscaleAlpha8bpp: + for (int32_t i = 0; i < res->width * res->height; i++) { + uint8_t ia = res->imageData[i]; + uint8_t color = ((ia >> 4) & 0xF) * 255 / 15; + uint8_t alpha = (ia & 0xF) * 255 / 15; + texBuffer.push_back(color); + texBuffer.push_back(color); + texBuffer.push_back(color); + texBuffer.push_back(alpha); + } + break; + default: // TODO convert other image types SPDLOG_WARN("SohImGui::LoadResource: Attempting to load unsupporting image type %s", path.c_str()); return; } + for (size_t pixel = 0; pixel < texBuffer.size() / 4; pixel++) { + texBuffer[pixel * 4 + 0] *= tint.x; + texBuffer[pixel * 4 + 1] *= tint.y; + texBuffer[pixel * 4 + 2] *= tint.z; + texBuffer[pixel * 4 + 3] *= tint.w; + } + const auto asset = new GameAsset{ api->new_texture() }; api->select_texture(0, asset->textureId); api->set_sampler_parameters(0, false, 0, 0); - api->upload_texture(res->imageData, res->width, res->height); + api->upload_texture(texBuffer.data(), res->width, res->height); DefaultAssets[name] = asset; } diff --git a/libultraship/libultraship/SohImGuiImpl.h b/libultraship/libultraship/SohImGuiImpl.h index 8275e4735..58dac7a1b 100644 --- a/libultraship/libultraship/SohImGuiImpl.h +++ b/libultraship/libultraship/SohImGuiImpl.h @@ -1,5 +1,6 @@ #pragma once +#include "Lib/ImGui/imgui.h" #include "SohConsole.h" struct GameAsset { @@ -63,7 +64,7 @@ namespace SohImGui { void ShowCursor(bool hide, Dialogues w); void BindCmd(const std::string& cmd, CommandEntry entry); void AddWindow(const std::string& category, const std::string& name, WindowDrawFunc drawFunc); - void LoadResource(const std::string& name, const std::string& path); + void LoadResource(const std::string& name, const std::string& path, const ImVec4& tint = ImVec4(1, 1, 1, 1)); ImTextureID GetTextureByID(int id); ImTextureID GetTextureByName(const std::string& name); } diff --git a/soh/soh.vcxproj b/soh/soh.vcxproj index 5560bde76..adf205f14 100644 --- a/soh/soh.vcxproj +++ b/soh/soh.vcxproj @@ -185,6 +185,7 @@ + @@ -928,6 +929,7 @@ + diff --git a/soh/soh.vcxproj.filters b/soh/soh.vcxproj.filters index de9ad6f96..c31a78b19 100644 --- a/soh/soh.vcxproj.filters +++ b/soh/soh.vcxproj.filters @@ -2181,6 +2181,9 @@ Source Files\soh\Enhancements\debugger + + Source Files\soh + @@ -3728,6 +3731,9 @@ Header Files\soh\Enhancements\debugger + + Header Files\soh + diff --git a/soh/soh/Enhancements/debugger/debugSaveEditor.cpp b/soh/soh/Enhancements/debugger/debugSaveEditor.cpp index 95e2f912c..2aaac5a1b 100644 --- a/soh/soh/Enhancements/debugger/debugSaveEditor.cpp +++ b/soh/soh/Enhancements/debugger/debugSaveEditor.cpp @@ -1,6 +1,8 @@ #include "debugSaveEditor.h" +#include "../../util.h" #include "../libultraship/SohImGuiImpl.h" +#include #include #include #include @@ -13,18 +15,20 @@ extern "C" { extern GlobalContext* gGlobalCtx; #include "textures/icon_item_static/icon_item_static.h" +#include "textures/icon_item_24_static/icon_item_24_static.h" } typedef struct { uint32_t id; std::string name; + std::string nameFaded; std::string texturePath; } ItemMapEntry; #define ITEM_MAP_ENTRY(id) \ { \ id, { \ - id, #id, static_cast(gItemIcons[id]) \ + id, #id, #id "_Faded", static_cast(gItemIcons[id]) \ } \ } @@ -120,6 +124,10 @@ std::map itemMapping = { ITEM_MAP_ENTRY(ITEM_WALLET_GIANT), ITEM_MAP_ENTRY(ITEM_SEEDS), ITEM_MAP_ENTRY(ITEM_FISHING_POLE), + ITEM_MAP_ENTRY(ITEM_KEY_BOSS), + ITEM_MAP_ENTRY(ITEM_COMPASS), + ITEM_MAP_ENTRY(ITEM_DUNGEON_MAP), + ITEM_MAP_ENTRY(ITEM_KEY_SMALL), }; // Maps entries in the GS flag array to the area name it represents @@ -147,6 +155,7 @@ std::vector gsMapping = { "Gerudo Fortress", "Desert Colossus, Haunted Wasteland", }; + extern "C" u8 gAreaGsFlags[]; extern "C" u8 gAmmoItems[]; @@ -158,6 +167,63 @@ u8 gAllAmmoItems[] = { ITEM_BOOMERANG, ITEM_LENS, ITEM_BEAN, ITEM_HAMMER, }; +typedef struct { + uint32_t id; + std::string name; + std::string nameFaded; + std::string texturePath; +} QuestMapEntry; + +#define QUEST_MAP_ENTRY(id, tex) \ + { \ + id, { \ + id, #id, #id "_Faded", tex \ + } \ + } + +// Maps quest items ids to info for use in ImGui +std::map questMapping = { + QUEST_MAP_ENTRY(QUEST_MEDALLION_FOREST, gForestMedallionIconTex), + QUEST_MAP_ENTRY(QUEST_MEDALLION_FIRE, gFireMedallionIconTex), + QUEST_MAP_ENTRY(QUEST_MEDALLION_WATER, gWaterMedallionIconTex), + QUEST_MAP_ENTRY(QUEST_MEDALLION_SPIRIT, gSpiritMedallionIconTex), + QUEST_MAP_ENTRY(QUEST_MEDALLION_SHADOW, gShadowMedallionIconTex), + QUEST_MAP_ENTRY(QUEST_MEDALLION_LIGHT, gLightMedallionIconTex), + QUEST_MAP_ENTRY(QUEST_KOKIRI_EMERALD, gKokiriEmeraldIconTex), + QUEST_MAP_ENTRY(QUEST_GORON_RUBY, gGoronRubyIconTex), + QUEST_MAP_ENTRY(QUEST_ZORA_SAPPHIRE, gZoraSapphireIconTex), + QUEST_MAP_ENTRY(QUEST_STONE_OF_AGONY, gStoneOfAgonyIconTex), + QUEST_MAP_ENTRY(QUEST_GERUDO_CARD, gGerudosCardIconTex), +}; + +typedef struct { + uint32_t id; + std::string name; + std::string nameFaded; + ImVec4 color; +} SongMapEntry; + +#define SONG_MAP_ENTRY(id, r, g, b) \ + { \ + id, #id, #id "_Faded", ImVec4(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f) \ + } + +// Maps song ids to info for use in ImGui +std::array songMapping = { { + SONG_MAP_ENTRY(QUEST_SONG_LULLABY, 255, 255, 255), + SONG_MAP_ENTRY(QUEST_SONG_EPONA, 255, 255, 255), + SONG_MAP_ENTRY(QUEST_SONG_SARIA, 255, 255, 255), + SONG_MAP_ENTRY(QUEST_SONG_SUN, 255, 255, 255), + SONG_MAP_ENTRY(QUEST_SONG_TIME, 255, 255, 255), + SONG_MAP_ENTRY(QUEST_SONG_STORMS, 255, 255, 255), + SONG_MAP_ENTRY(QUEST_SONG_MINUET, 150, 255, 100), + SONG_MAP_ENTRY(QUEST_SONG_BOLERO, 255, 80, 40), + SONG_MAP_ENTRY(QUEST_SONG_SERENADE, 100, 150, 255), + SONG_MAP_ENTRY(QUEST_SONG_REQUIEM, 255, 160, 0), + SONG_MAP_ENTRY(QUEST_SONG_NOCTURNE, 255, 100, 255), + SONG_MAP_ENTRY(QUEST_SONG_PRELUDE, 255, 240, 100), +} }; + // Adds a text tooltip for the previous ImGui item void SetLastItemHoverText(const std::string& text) { if (ImGui::IsItemHovered()) { @@ -178,17 +244,33 @@ void InsertHelpHoverText(const std::string& text) { } } -void DrawSaveEditor(bool& open) { - if (!open) { - return; - } +// Encapsulates what is drawn by the passed-in function within a border +template +void DrawGroupWithBorder(T&& drawFunc) { + // First group encapsulates the inner portion and border + ImGui::BeginGroup(); - ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver); - if (!ImGui::Begin("Save Editor", &open)) { - ImGui::End(); - return; - } + ImVec2 padding = ImGui::GetStyle().FramePadding; + ImVec2 p0 = ImGui::GetCursorScreenPos(); + ImGui::SetCursorScreenPos(ImVec2(p0.x + padding.x, p0.y + padding.y)); + // Second group encapsulates just the inner portion + ImGui::BeginGroup(); + + drawFunc(); + + ImGui::Dummy(padding); + ImGui::EndGroup(); + + ImVec2 p1 = ImGui::GetItemRectMax(); + p1.x += padding.x; + ImVec4 borderCol = ImGui::GetStyle().Colors[ImGuiCol_Border]; + ImGui::GetWindowDrawList()->AddRect(p0, p1, IM_COL32(borderCol.x * 255, borderCol.y * 255, borderCol.z * 255, borderCol.w * 255)); + + ImGui::EndGroup(); +} + +void DrawInfoTab() { // TODO This is the bare minimum to get the player name showing // There will need to be more effort to get it robust and editable std::string name; @@ -201,313 +283,833 @@ void DrawSaveEditor(bool& open) { } name += '\0'; + ImGui::PushItemWidth(ImGui::GetFontSize() * 6); + + ImGui::Text("Name: %s", name.c_str()); + InsertHelpHoverText("Player Name"); + + // Use an intermediary to keep the health from updating (and potentially killing the player) + // until it is done being edited + int16_t healthIntermediary = gSaveContext.healthCapacity; + ImGui::InputScalar("Max Health", ImGuiDataType_S16, &healthIntermediary); + if (ImGui::IsItemDeactivated()) { + gSaveContext.healthCapacity = healthIntermediary; + } + InsertHelpHoverText("Maximum health. 16 units per full heart"); + if (gSaveContext.health > gSaveContext.healthCapacity) { + gSaveContext.health = gSaveContext.healthCapacity; // Clamp health to new max + } + + const uint16_t healthMin = 0; + const uint16_t healthMax = gSaveContext.healthCapacity; + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15); + ImGui::SliderScalar("Health", ImGuiDataType_S16, &gSaveContext.health, &healthMin, &healthMax); + InsertHelpHoverText("Current health. 16 units per full heart"); + + bool doubleDefense = gSaveContext.doubleDefense != 0; + if (ImGui::Checkbox("Double Defense", &doubleDefense)) { + gSaveContext.doubleDefense = doubleDefense; + gSaveContext.inventory.defenseHearts = + gSaveContext.doubleDefense ? 20 : 0; // Set to get the border drawn in the UI + } + InsertHelpHoverText("Is double defense unlocked?"); + + std::string magicName; + if (gSaveContext.magicLevel == 2) { + magicName = "Double"; + } else if (gSaveContext.magicLevel == 1) { + magicName = "Single"; + } else { + magicName = "None"; + } + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 6); + if (ImGui::BeginCombo("Magic Level", magicName.c_str())) { + if (ImGui::Selectable("Double")) { + gSaveContext.magicLevel = 2; + gSaveContext.magicAcquired = true; + gSaveContext.doubleMagic = true; + } + if (ImGui::Selectable("Single")) { + gSaveContext.magicLevel = 1; + gSaveContext.magicAcquired = true; + gSaveContext.doubleMagic = false; + } + if (ImGui::Selectable("None")) { + gSaveContext.magicLevel = 0; + gSaveContext.magicAcquired = false; + gSaveContext.doubleMagic = false; + } + + ImGui::EndCombo(); + } + InsertHelpHoverText("Current magic level"); + gSaveContext.unk_13F4 = gSaveContext.magicLevel * 0x30; // Set to get the bar drawn in the UI + if (gSaveContext.magic > gSaveContext.unk_13F4) { + gSaveContext.magic = gSaveContext.unk_13F4; // Clamp magic to new max + } + + const uint8_t magicMin = 0; + const uint8_t magicMax = gSaveContext.unk_13F4; + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15); + ImGui::SliderScalar("Magic", ImGuiDataType_S8, &gSaveContext.magic, &magicMin, &magicMax); + InsertHelpHoverText("Current magic. 48 units per magic level"); + + ImGui::InputScalar("Rupees", ImGuiDataType_S16, &gSaveContext.rupees); + InsertHelpHoverText("Current rupees"); + + const uint16_t dayTimeMin = 0; + const uint16_t dayTimeMax = 0xFFFF; + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15); + ImGui::SliderScalar("Time", ImGuiDataType_U16, &gSaveContext.dayTime, &dayTimeMin, &dayTimeMax); + InsertHelpHoverText("Time of day"); + if (ImGui::Button("Dawn")) { + gSaveContext.dayTime = 0x4000; + } + ImGui::SameLine(); + if (ImGui::Button("Noon")) { + gSaveContext.dayTime = 0x8000; + } + ImGui::SameLine(); + if (ImGui::Button("Sunset")) { + gSaveContext.dayTime = 0xC000; + } + ImGui::SameLine(); + if (ImGui::Button("Midnight")) { + gSaveContext.dayTime = 0; + } + + ImGui::InputScalar("Total Days", ImGuiDataType_S32, &gSaveContext.totalDays); + InsertHelpHoverText("Total number of days elapsed since the start of the game"); + + ImGui::InputScalar("Deaths", ImGuiDataType_U16, &gSaveContext.deaths); + InsertHelpHoverText("Total number of deaths"); + + bool bgsFlag = gSaveContext.bgsFlag != 0; + if (ImGui::Checkbox("Has BGS", &bgsFlag)) { + gSaveContext.bgsFlag = bgsFlag; + } + InsertHelpHoverText("Is Biggoron sword unlocked? Replaces Giant's knife"); + + ImGui::InputScalar("Sword Health", ImGuiDataType_U16, &gSaveContext.swordHealth); + InsertHelpHoverText("Giant's knife health. Default is 8. Must be >0 for Biggoron sword to work"); + + ImGui::InputScalar("Bgs Day Count", ImGuiDataType_S32, &gSaveContext.bgsDayCount); + InsertHelpHoverText("Total number of days elapsed since giving Biggoron the claim check"); + + // TODO Changing Link's age is more involved than just setting gSaveContext.linkAge + // It might not fit here and instead should be only changable when changing scenes + /* + if (ImGui::BeginCombo("Link Age", LINK_IS_ADULT ? "Adult" : "Child")) { + if (ImGui::Selectable("Adult")) { + gSaveContext.linkAge = 0; + } + if (ImGui::Selectable("Child")) { + gSaveContext.linkAge = 1; + } + + ImGui::EndCombo(); + } + */ + + ImGui::PopItemWidth(); +} + +void DrawInventoryTab() { + static bool restrictToValid = true; + + ImGui::Checkbox("Restrict to valid items", &restrictToValid); + InsertHelpHoverText("Restricts items and ammo to only what is possible to legally acquire in-game"); + + for (int32_t y = 0; y < 4; y++) { + for (int32_t x = 0; x < 6; x++) { + int32_t index = x + y * 6; + static int32_t selectedIndex = -1; + static const char* itemPopupPicker = "itemPopupPicker"; + + ImGui::PushID(index); + + if (x != 0) { + ImGui::SameLine(); + } + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1, 1, 1, 0)); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); + uint8_t item = gSaveContext.inventory.items[index]; + if (item != ITEM_NONE) { + const ItemMapEntry& slotEntry = itemMapping.find(item)->second; + if (ImGui::ImageButton(SohImGui::GetTextureByName(slotEntry.name), ImVec2(32.0f, 32.0f), ImVec2(0, 0), + ImVec2(1, 1), 0)) { + selectedIndex = index; + ImGui::OpenPopup(itemPopupPicker); + } + } else { + if (ImGui::Button("##itemNone", ImVec2(32.0f, 32.0f))) { + selectedIndex = index; + ImGui::OpenPopup(itemPopupPicker); + } + } + ImGui::PopStyleVar(); + ImGui::PopStyleColor(); + + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); + if (ImGui::BeginPopup(itemPopupPicker)) { + if (ImGui::Button("##itemNonePicker", ImVec2(32.0f, 32.0f))) { + gSaveContext.inventory.items[selectedIndex] = ITEM_NONE; + ImGui::CloseCurrentPopup(); + } + SetLastItemHoverText("None"); + + std::vector possibleItems; + if (restrictToValid) { + // Scan gItemSlots to find legal items for this slot. Bottles are a special case + for (int slotIndex = 0; slotIndex < 56; slotIndex++) { + int testIndex = (selectedIndex == SLOT_BOTTLE_1 || selectedIndex == SLOT_BOTTLE_2 || + selectedIndex == SLOT_BOTTLE_3 || selectedIndex == SLOT_BOTTLE_4) + ? SLOT_BOTTLE_1 + : selectedIndex; + if (gItemSlots[slotIndex] == testIndex) { + possibleItems.push_back(itemMapping[slotIndex]); + } + } + } else { + for (const auto& entry : itemMapping) { + possibleItems.push_back(entry.second); + } + } + + for (int32_t pickerIndex = 0; pickerIndex < possibleItems.size(); pickerIndex++) { + if (((pickerIndex + 1) % 8) != 0) { + ImGui::SameLine(); + } + const ItemMapEntry& slotEntry = possibleItems[pickerIndex]; + if (ImGui::ImageButton(SohImGui::GetTextureByName(slotEntry.name), ImVec2(32.0f, 32.0f), + ImVec2(0, 0), ImVec2(1, 1), 0)) { + gSaveContext.inventory.items[selectedIndex] = slotEntry.id; + ImGui::CloseCurrentPopup(); + } + SetLastItemHoverText(SohUtils::GetItemName(slotEntry.id)); + } + + ImGui::EndPopup(); + } + ImGui::PopStyleVar(); + + ImGui::PopID(); + } + } + + ImGui::Text("Ammo"); + for (uint32_t ammoIndex = 0, drawnAmmoItems = 0; ammoIndex < 16; ammoIndex++) { + uint8_t item = (restrictToValid) ? gAmmoItems[ammoIndex] : gAllAmmoItems[ammoIndex]; + if (item != ITEM_NONE) { + // For legal items, display as 1 row of 7. For unrestricted items, display rows of 6 to match + // inventory + if ((restrictToValid && (drawnAmmoItems != 0)) || ((drawnAmmoItems % 6) != 0)) { + ImGui::SameLine(); + } + drawnAmmoItems++; + + ImGui::PushID(ammoIndex); + ImGui::PushItemWidth(32.0f); + ImGui::BeginGroup(); + + ImGui::Image(SohImGui::GetTextureByName(itemMapping[item].name), ImVec2(32.0f, 32.0f)); + ImGui::InputScalar("##ammoInput", ImGuiDataType_S8, &AMMO(item)); + + ImGui::EndGroup(); + ImGui::PopItemWidth(); + ImGui::PopID(); + } + } +} + +// Draw a flag bitfield as an grid of checkboxes +void DrawFlagArray(const std::string& name, uint32_t& flags) { + ImGui::PushID(name.c_str()); + for (int32_t flagIndex = 0; flagIndex < 32; flagIndex++) { + if ((flagIndex % 8) != 0) { + ImGui::SameLine(); + } + ImGui::PushID(flagIndex); + uint32_t bitMask = 1 << flagIndex; + bool flag = (flags & bitMask) != 0; + if (ImGui::Checkbox("##check", &flag)) { + if (flag) { + flags |= bitMask; + } else { + flags &= ~bitMask; + } + } + ImGui::PopID(); + } + ImGui::PopID(); +} + +void DrawFlagsTab() { + if (ImGui::TreeNode("Current Scene")) { + if (gGlobalCtx != nullptr) { + ActorContext* act = &gGlobalCtx->actorCtx; + + DrawGroupWithBorder([&]() { + ImGui::Text("Switch"); + InsertHelpHoverText("Permanently-saved switch flags"); + DrawFlagArray("Switch", act->flags.swch); + }); + + ImGui::SameLine(); + + DrawGroupWithBorder([&]() { + ImGui::Text("Temp Switch"); + InsertHelpHoverText("Temporary switch flags. Unset on scene transitions"); + DrawFlagArray("Temp Switch", act->flags.tempSwch); + }); + + DrawGroupWithBorder([&]() { + ImGui::Text("Clear"); + InsertHelpHoverText("Permanently-saved room-clear flags"); + DrawFlagArray("Clear", act->flags.clear); + }); + + ImGui::SameLine(); + + DrawGroupWithBorder([&]() { + ImGui::Text("Temp Clear"); + InsertHelpHoverText("Temporary room-clear flags. Unset on scene transitions"); + DrawFlagArray("Temp Clear", act->flags.tempClear); + }); + + DrawGroupWithBorder([&]() { + ImGui::Text("Collect"); + InsertHelpHoverText("Permanently-saved collect flags"); + DrawFlagArray("Collect", act->flags.collect); + }); + + ImGui::SameLine(); + + DrawGroupWithBorder([&]() { + ImGui::Text("Temp Collect"); + InsertHelpHoverText("Temporary collect flags. Unset on scene transitions"); + DrawFlagArray("Temp Collect", act->flags.tempCollect); + }); + + DrawGroupWithBorder([&]() { + ImGui::Text("Chest"); + InsertHelpHoverText("Permanently-saved chest flags"); + DrawFlagArray("Chest", act->flags.chest); + }); + + ImGui::SameLine(); + + ImGui::BeginGroup(); + + if (ImGui::Button("Reload Flags")) { + act->flags.swch = gSaveContext.sceneFlags[gGlobalCtx->sceneNum].swch; + act->flags.clear = gSaveContext.sceneFlags[gGlobalCtx->sceneNum].clear; + act->flags.collect = gSaveContext.sceneFlags[gGlobalCtx->sceneNum].collect; + act->flags.chest = gSaveContext.sceneFlags[gGlobalCtx->sceneNum].chest; + } + SetLastItemHoverText("Load flags from saved scene flags. Normally happens on scene load"); + + if (ImGui::Button("Save Flags")) { + gSaveContext.sceneFlags[gGlobalCtx->sceneNum].swch = act->flags.swch; + gSaveContext.sceneFlags[gGlobalCtx->sceneNum].clear = act->flags.clear; + gSaveContext.sceneFlags[gGlobalCtx->sceneNum].collect = act->flags.collect; + gSaveContext.sceneFlags[gGlobalCtx->sceneNum].chest = act->flags.chest; + } + SetLastItemHoverText("Save current scene flags. Normally happens on scene exit"); + + ImGui::EndGroup(); + } else { + ImGui::Text("Current game state does not have an active scene"); + } + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Saved Scene Flags")) { + static uint32_t selectedSceneFlagMap = 0; + ImGui::Text("Map"); + ImGui::SameLine(); + if (ImGui::BeginCombo("##Map", SohUtils::GetSceneName(selectedSceneFlagMap).c_str())) { + for (int32_t sceneIndex = 0; sceneIndex < SCENE_ID_MAX; sceneIndex++) { + if (ImGui::Selectable(SohUtils::GetSceneName(sceneIndex).c_str())) { + selectedSceneFlagMap = sceneIndex; + } + } + + ImGui::EndCombo(); + } + + // Don't show current scene button if there is no current scene + if (gGlobalCtx != nullptr) { + ImGui::SameLine(); + if (ImGui::Button("Current")) { + selectedSceneFlagMap = gGlobalCtx->sceneNum; + } + SetLastItemHoverText("Open flags for current scene"); + } + + DrawGroupWithBorder([&]() { + ImGui::Text("Switch"); + InsertHelpHoverText("Switch flags"); + DrawFlagArray("Switch", gSaveContext.sceneFlags[selectedSceneFlagMap].swch); + }); + + ImGui::SameLine(); + + DrawGroupWithBorder([&]() { + ImGui::Text("Clear"); + InsertHelpHoverText("Room-clear flags"); + DrawFlagArray("Clear", gSaveContext.sceneFlags[selectedSceneFlagMap].clear); + }); + + DrawGroupWithBorder([&]() { + ImGui::Text("Collect"); + InsertHelpHoverText("Collect flags"); + DrawFlagArray("Collect", gSaveContext.sceneFlags[selectedSceneFlagMap].collect); + }); + + ImGui::SameLine(); + + DrawGroupWithBorder([&]() { + ImGui::Text("Chest"); + InsertHelpHoverText("Chest flags"); + DrawFlagArray("Chest", gSaveContext.sceneFlags[selectedSceneFlagMap].chest); + }); + + DrawGroupWithBorder([&]() { + ImGui::Text("Rooms"); + InsertHelpHoverText("Flags for visted rooms"); + DrawFlagArray("Rooms", gSaveContext.sceneFlags[selectedSceneFlagMap].rooms); + }); + + ImGui::SameLine(); + + DrawGroupWithBorder([&]() { + ImGui::Text("Floors"); + InsertHelpHoverText("Flags for visted floors"); + DrawFlagArray("Floors", gSaveContext.sceneFlags[selectedSceneFlagMap].floors); + }); + + ImGui::TreePop(); + } + + DrawGroupWithBorder([&]() { + static uint32_t selectedGsMap = 0; + ImGui::Text("Gold Skulltulas"); + ImGui::Text("Map"); + ImGui::SameLine(); + if (ImGui::BeginCombo("##Gold Skulltula Map", gsMapping[selectedGsMap].c_str())) { + for (int32_t gsIndex = 0; gsIndex < gsMapping.size(); gsIndex++) { + if (ImGui::Selectable(gsMapping[gsIndex].c_str())) { + selectedGsMap = gsIndex; + } + } + + ImGui::EndCombo(); + } + + // TODO We should write out descriptions for each one... ugh + ImGui::Text("Flags"); + uint32_t currentFlags = GET_GS_FLAGS(selectedGsMap); + uint32_t allFlags = gAreaGsFlags[selectedGsMap]; + uint32_t setMask = 1; + // Iterate over bitfield and create a checkbox for each skulltula + while (allFlags != 0) { + bool isThisSet = (currentFlags & 0x1) == 0x1; + + ImGui::SameLine(); + ImGui::PushID(allFlags); + if (ImGui::Checkbox("##gs", &isThisSet)) { + if (isThisSet) { + SET_GS_FLAGS(selectedGsMap, setMask); + } else { + // Have to do this roundabout method as the macro does not support clearing flags + uint32_t currentFlagsBase = GET_GS_FLAGS(selectedGsMap); + gSaveContext.gsFlags[selectedGsMap >> 2] &= ~gGsFlagsMasks[selectedGsMap & 3]; + SET_GS_FLAGS(selectedGsMap, currentFlagsBase & ~setMask); + } + } + + ImGui::PopID(); + + allFlags >>= 1; + currentFlags >>= 1; + setMask <<= 1; + } + + static bool keepGsCountUpdated = true; + ImGui::Checkbox("Keep GS Count Updated", &keepGsCountUpdated); + InsertHelpHoverText("Automatically adjust the number of gold skulltula tokens acquired based on set flags"); + int32_t gsCount = 0; + if (keepGsCountUpdated) { + for (int32_t gsFlagIndex = 0; gsFlagIndex < 6; gsFlagIndex++) { + gsCount += std::popcount(static_cast(gSaveContext.gsFlags[gsFlagIndex])); + } + gSaveContext.inventory.gsTokens = gsCount; + } + }); +} + +// Draws a combo that lets you choose and upgrade value from a drop-down of text values +void DrawUpgrade(const std::string& categoryName, int32_t categoryId, const std::vector& names) { + ImGui::Text(categoryName.c_str()); + ImGui::SameLine(); + ImGui::PushID(categoryName.c_str()); + if (ImGui::BeginCombo("##upgrade", names[CUR_UPG_VALUE(categoryId)].c_str())) { + for (int32_t i = 0; i < names.size(); i++) { + if (ImGui::Selectable(names[i].c_str())) { + Inventory_ChangeUpgrade(categoryId, i); + } + } + + ImGui::EndCombo(); + } + ImGui::PopID(); + SetLastItemHoverText(categoryName.c_str()); +} + +// Draws a combo that lets you choose and upgrade value from a popup grid of icons +void DrawUpgradeIcon(const std::string& categoryName, int32_t categoryId, const std::vector& items) { + static const char* upgradePopupPicker = "upgradePopupPicker"; + + ImGui::PushID(categoryName.c_str()); + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1, 1, 1, 0)); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); + uint8_t item = items[CUR_UPG_VALUE(categoryId)]; + if (item != ITEM_NONE) { + const ItemMapEntry& slotEntry = itemMapping[item]; + if (ImGui::ImageButton(SohImGui::GetTextureByName(slotEntry.name), ImVec2(32.0f, 32.0f), ImVec2(0, 0), + ImVec2(1, 1), 0)) { + ImGui::OpenPopup(upgradePopupPicker); + } + } else { + if (ImGui::Button("##itemNone", ImVec2(32.0f, 32.0f))) { + ImGui::OpenPopup(upgradePopupPicker); + } + } + ImGui::PopStyleVar(); + ImGui::PopStyleColor(); + SetLastItemHoverText(categoryName.c_str()); + + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); + if (ImGui::BeginPopup(upgradePopupPicker)) { + for (int32_t pickerIndex = 0; pickerIndex < items.size(); pickerIndex++) { + if ((pickerIndex % 8) != 0) { + ImGui::SameLine(); + } + + if (items[pickerIndex] == ITEM_NONE) { + if (ImGui::Button("##upgradePopupPicker", ImVec2(32.0f, 32.0f))) { + Inventory_ChangeUpgrade(categoryId, pickerIndex); + ImGui::CloseCurrentPopup(); + } + SetLastItemHoverText("None"); + } else { + const ItemMapEntry& slotEntry = itemMapping[items[pickerIndex]]; + if (ImGui::ImageButton(SohImGui::GetTextureByName(slotEntry.name), ImVec2(32.0f, 32.0f), ImVec2(0, 0), + ImVec2(1, 1), 0)) { + Inventory_ChangeUpgrade(categoryId, pickerIndex); + ImGui::CloseCurrentPopup(); + } + SetLastItemHoverText(SohUtils::GetItemName(slotEntry.id)); + } + } + + ImGui::EndPopup(); + } + ImGui::PopStyleVar(); + + ImGui::PopID(); +} + +void DrawEquipmentTab() { + const std::vector equipmentValues = { + ITEM_SWORD_KOKIRI, ITEM_SWORD_MASTER, ITEM_SWORD_BGS, ITEM_SWORD_BROKEN, + ITEM_SHIELD_DEKU, ITEM_SHIELD_HYLIAN, ITEM_SHIELD_MIRROR, ITEM_NONE, + ITEM_TUNIC_KOKIRI, ITEM_TUNIC_GORON, ITEM_TUNIC_ZORA, ITEM_NONE, + ITEM_BOOTS_KOKIRI, ITEM_BOOTS_IRON, ITEM_BOOTS_HOVER, ITEM_NONE, + }; + for (int32_t i = 0; i < equipmentValues.size(); i++) { + // Skip over unused 4th slots for shields, boots, and tunics + if (equipmentValues[i] == ITEM_NONE) { + continue; + } + if ((i % 4) != 0) { + ImGui::SameLine(); + } + + ImGui::PushID(i); + uint32_t bitMask = 1 << i; + bool hasEquip = (bitMask & gSaveContext.inventory.equipment) != 0; + const ItemMapEntry& entry = itemMapping[equipmentValues[i]]; + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); + if (ImGui::ImageButton(SohImGui::GetTextureByName(hasEquip ? entry.name : entry.nameFaded), + ImVec2(32.0f, 32.0f), ImVec2(0, 0), ImVec2(1, 1), 0)) { + if (hasEquip) { + gSaveContext.inventory.equipment &= ~bitMask; + } else { + gSaveContext.inventory.equipment |= bitMask; + } + } + ImGui::PopStyleColor(); + ImGui::PopID(); + SetLastItemHoverText(SohUtils::GetItemName(entry.id)); + } + + const std::vector bulletBagValues = { + ITEM_NONE, + ITEM_BULLET_BAG_30, + ITEM_BULLET_BAG_40, + ITEM_BULLET_BAG_50, + }; + DrawUpgradeIcon("Bullet Bag", UPG_BULLET_BAG, bulletBagValues); + + ImGui::SameLine(); + + const std::vector quiverValues = { + ITEM_NONE, + ITEM_QUIVER_30, + ITEM_QUIVER_40, + ITEM_QUIVER_50, + }; + DrawUpgradeIcon("Quiver", UPG_QUIVER, quiverValues); + + ImGui::SameLine(); + + const std::vector bombBagValues = { + ITEM_NONE, + ITEM_BOMB_BAG_20, + ITEM_BOMB_BAG_30, + ITEM_BOMB_BAG_40, + }; + DrawUpgradeIcon("Bomb Bag", UPG_BOMB_BAG, bombBagValues); + + ImGui::SameLine(); + + const std::vector scaleValues = { + ITEM_NONE, + ITEM_SCALE_SILVER, + ITEM_SCALE_GOLDEN, + }; + DrawUpgradeIcon("Scale", UPG_SCALE, scaleValues); + + ImGui::SameLine(); + + const std::vector strengthValues = { + ITEM_NONE, + ITEM_BRACELET, + ITEM_GAUNTLETS_SILVER, + ITEM_GAUNTLETS_GOLD, + }; + DrawUpgradeIcon("Strength", UPG_STRENGTH, strengthValues); + + // There is no icon for child wallet, so default to a text list + const std::vector walletNames = { + "Child (99)", + "Adult (200)", + "Giant (500)", + }; + DrawUpgrade("Wallet", UPG_WALLET, walletNames); + + const std::vector stickNames = { + "None", + "10", + "20", + "30", + }; + DrawUpgrade("Sticks", UPG_STICKS, stickNames); + + const std::vector nutNames = { + "None", + "20", + "30", + "40", + }; + DrawUpgrade("Deku Nuts", UPG_NUTS, nutNames); +} + +// Draws a toggleable icon for a quest item that is faded when disabled +void DrawQuestItemButton(uint32_t item) { + const QuestMapEntry& entry = questMapping[item]; + uint32_t bitMask = 1 << entry.id; + bool hasQuestItem = (bitMask & gSaveContext.inventory.questItems) != 0; + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); + if (ImGui::ImageButton(SohImGui::GetTextureByName(hasQuestItem ? entry.name : entry.nameFaded), + ImVec2(32.0f, 32.0f), ImVec2(0, 0), ImVec2(1, 1), 0)) { + if (hasQuestItem) { + gSaveContext.inventory.questItems &= ~bitMask; + } else { + gSaveContext.inventory.questItems |= bitMask; + } + } + ImGui::PopStyleColor(); + SetLastItemHoverText(SohUtils::GetQuestItemName(entry.id)); +} + +// Draws a toggleable icon for a dungeon item that is faded when disabled +void DrawDungeonItemButton(uint32_t item, uint32_t scene) { + const ItemMapEntry& entry = itemMapping[item]; + uint32_t bitMask = 1 << (entry.id - ITEM_KEY_BOSS); // Bitset starts at ITEM_KEY_BOSS == 0. the rest are sequential + bool hasItem = (bitMask & gSaveContext.inventory.dungeonItems[scene]) != 0; + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); + if (ImGui::ImageButton(SohImGui::GetTextureByName(hasItem ? entry.name : entry.nameFaded), + ImVec2(32.0f, 32.0f), ImVec2(0, 0), ImVec2(1, 1), 0)) { + if (hasItem) { + gSaveContext.inventory.dungeonItems[scene] &= ~bitMask; + } else { + gSaveContext.inventory.dungeonItems[scene] |= bitMask; + } + } + ImGui::PopStyleColor(); + SetLastItemHoverText(SohUtils::GetItemName(entry.id)); +} + +void DrawQuestStatusTab() { + ImGui::PushItemWidth(ImGui::GetFontSize() * 6); + + for (int32_t i = QUEST_MEDALLION_FOREST; i < QUEST_MEDALLION_LIGHT + 1; i++) { + if (i != QUEST_MEDALLION_FOREST) { + ImGui::SameLine(); + } + DrawQuestItemButton(i); + } + + for (int32_t i = QUEST_KOKIRI_EMERALD; i < QUEST_ZORA_SAPPHIRE + 1; i++) { + if (i != QUEST_KOKIRI_EMERALD) { + ImGui::SameLine(); + } + DrawQuestItemButton(i); + } + + // Put Stone of Agony and Gerudo Card on the same line with a little space between them + ImGui::SameLine(); + ImGui::Dummy(ImVec2(20, 0)); + + ImGui::SameLine(); + DrawQuestItemButton(QUEST_STONE_OF_AGONY); + + ImGui::SameLine(); + DrawQuestItemButton(QUEST_GERUDO_CARD); + + for (const SongMapEntry& entry : songMapping) { + if ((entry.id != QUEST_SONG_MINUET) && (entry.id != QUEST_SONG_LULLABY)) { + ImGui::SameLine(); + } + + uint32_t bitMask = 1 << entry.id; + bool hasQuestItem = (bitMask & gSaveContext.inventory.questItems) != 0; + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); + if (ImGui::ImageButton(SohImGui::GetTextureByName(hasQuestItem ? entry.name : entry.nameFaded), + ImVec2(16.0f, 24.0f), ImVec2(0, 0), ImVec2(1, 1), 0)) { + if (hasQuestItem) { + gSaveContext.inventory.questItems &= ~bitMask; + } else { + gSaveContext.inventory.questItems |= bitMask; + } + } + ImGui::PopStyleColor(); + SetLastItemHoverText(SohUtils::GetQuestItemName(entry.id)); + } + + ImGui::InputScalar("GS Count", ImGuiDataType_S16, &gSaveContext.inventory.gsTokens); + InsertHelpHoverText("Number of gold skulltula tokens aquired"); + + uint32_t bitMask = 1 << QUEST_SKULL_TOKEN; + bool gsUnlocked = (bitMask & gSaveContext.inventory.questItems) != 0; + if (ImGui::Checkbox("GS unlocked", &gsUnlocked)) { + if (gsUnlocked) { + gSaveContext.inventory.questItems |= bitMask; + } else { + gSaveContext.inventory.questItems &= ~bitMask; + } + } + InsertHelpHoverText("If unlocked, enables showing the gold skulltula count in the quest status menu"); + + int32_t pohCount = (gSaveContext.inventory.questItems & 0xF0000000) >> 28; + if (ImGui::BeginCombo("PoH count", std::to_string(pohCount).c_str())) { + for (int32_t i = 0; i < 4; i++) { + if (ImGui::Selectable(std::to_string(i).c_str(), pohCount == i)) { + gSaveContext.inventory.questItems &= ~0xF0000000; + gSaveContext.inventory.questItems |= (i << 28); + } + } + ImGui::EndCombo(); + } + InsertHelpHoverText("The number of pieces of heart acquired towards the next heart container"); + + DrawGroupWithBorder([&]() { + ImGui::Text("Dungeon Items"); + + static int32_t dungeonItemsScene = SCENE_YDAN; + ImGui::PushItemWidth(-ImGui::GetWindowWidth() * 0.35f); + if (ImGui::BeginCombo("##DungeonSelect", SohUtils::GetSceneName(dungeonItemsScene).c_str())) { + for (int32_t dungeonIndex = SCENE_YDAN; dungeonIndex < SCENE_BDAN_BOSS + 1; dungeonIndex++) { + if (ImGui::Selectable(SohUtils::GetSceneName(dungeonIndex).c_str(), + dungeonIndex == dungeonItemsScene)) { + dungeonItemsScene = dungeonIndex; + } + } + + ImGui::EndCombo(); + } + ImGui::PopItemWidth(); + + DrawDungeonItemButton(ITEM_KEY_BOSS, dungeonItemsScene); + ImGui::SameLine(); + DrawDungeonItemButton(ITEM_COMPASS, dungeonItemsScene); + ImGui::SameLine(); + DrawDungeonItemButton(ITEM_DUNGEON_MAP, dungeonItemsScene); + + if (dungeonItemsScene != SCENE_BDAN_BOSS) { + float lineHeight = ImGui::GetTextLineHeightWithSpacing(); + ImGui::Image(SohImGui::GetTextureByName(itemMapping[ITEM_KEY_SMALL].name), ImVec2(lineHeight, lineHeight)); + ImGui::SameLine(); + ImGui::InputScalar("##Keys", ImGuiDataType_S8, gSaveContext.inventory.dungeonKeys + dungeonItemsScene); + } else { + // dungeonItems is size 20 but dungeonKeys is size 19, so there are no keys for the last scene (Barinade's Lair) + ImGui::Text("Barinade's Lair does not have small keys"); + } + }); + + ImGui::PopItemWidth(); +} + +void DrawSaveEditor(bool& open) { + if (!open) { + return; + } + + ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver); + if (!ImGui::Begin("Save Editor", &open)) { + ImGui::End(); + return; + } + if (ImGui::BeginTabBar("SaveContextTabBar", ImGuiTabBarFlags_NoCloseWithMiddleMouseButton)) { if (ImGui::BeginTabItem("Info")) { - ImGui::PushItemWidth(ImGui::GetFontSize() * 6); - - ImGui::Text("Name: %s", name.c_str()); - InsertHelpHoverText("Player Name"); - - // Use an intermediary to keep the health from updating (and potentially killing the player) - // until it is done being edited - int16_t healthIntermediary = gSaveContext.healthCapacity; - ImGui::InputScalar("Max Health", ImGuiDataType_S16, &healthIntermediary); - if (ImGui::IsItemDeactivated()) { - gSaveContext.healthCapacity = healthIntermediary; - } - InsertHelpHoverText("Maximum health. 16 units per full heart"); - if (gSaveContext.health > gSaveContext.healthCapacity) { - gSaveContext.health = gSaveContext.healthCapacity; // Clamp health to new max - } - - const uint16_t healthMin = 0; - const uint16_t healthMax = gSaveContext.healthCapacity; - ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15); - ImGui::SliderScalar("Health", ImGuiDataType_S16, &gSaveContext.health, &healthMin, &healthMax); - InsertHelpHoverText("Current health. 16 units per full heart"); - - bool doubleDefense = gSaveContext.doubleDefense != 0; - if (ImGui::Checkbox("Double Defense", &doubleDefense)) { - gSaveContext.doubleDefense = doubleDefense; - gSaveContext.inventory.defenseHearts = - gSaveContext.doubleDefense ? 20 : 0; // Set to get the border drawn in the UI - } - InsertHelpHoverText("Is double defense unlocked?"); - - std::string magicName; - if (gSaveContext.magicLevel == 2) { - magicName = "Double"; - } else if (gSaveContext.magicLevel == 1) { - magicName = "Single"; - } else { - magicName = "None"; - } - ImGui::SetNextItemWidth(ImGui::GetFontSize() * 6); - if (ImGui::BeginCombo("Magic Level", magicName.c_str())) { - if (ImGui::Selectable("Double")) { - gSaveContext.magicLevel = 2; - gSaveContext.magicAcquired = true; - gSaveContext.doubleMagic = true; - } - if (ImGui::Selectable("Single")) { - gSaveContext.magicLevel = 1; - gSaveContext.magicAcquired = true; - gSaveContext.doubleMagic = false; - } - if (ImGui::Selectable("None")) { - gSaveContext.magicLevel = 0; - gSaveContext.magicAcquired = false; - gSaveContext.doubleMagic = false; - } - - ImGui::EndCombo(); - } - InsertHelpHoverText("Current magic level"); - gSaveContext.unk_13F4 = gSaveContext.magicLevel * 0x30; // Set to get the bar drawn in the UI - if (gSaveContext.magic > gSaveContext.unk_13F4) { - gSaveContext.magic = gSaveContext.unk_13F4; // Clamp magic to new max - } - - const uint8_t magicMin = 0; - const uint8_t magicMax = gSaveContext.unk_13F4; - ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15); - ImGui::SliderScalar("Magic", ImGuiDataType_S8, &gSaveContext.magic, &magicMin, &magicMax); - InsertHelpHoverText("Current magic. 48 units per magic level"); - - ImGui::InputScalar("Rupees", ImGuiDataType_S16, &gSaveContext.rupees); - InsertHelpHoverText("Current rupees"); - - const uint16_t dayTimeMin = 0; - const uint16_t dayTimeMax = 0xFFFF; - ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15); - ImGui::SliderScalar("Time", ImGuiDataType_U16, &gSaveContext.dayTime, &dayTimeMin, &dayTimeMax); - InsertHelpHoverText("Time of day"); - if (ImGui::Button("Dawn")) { - gSaveContext.dayTime = 0x4000; - } - ImGui::SameLine(); - if (ImGui::Button("Noon")) { - gSaveContext.dayTime = 0x8000; - } - ImGui::SameLine(); - if (ImGui::Button("Sunset")) { - gSaveContext.dayTime = 0xC000; - } - ImGui::SameLine(); - if (ImGui::Button("Midnight")) { - gSaveContext.dayTime = 0; - } - - ImGui::InputScalar("Total Days", ImGuiDataType_S32, &gSaveContext.totalDays); - InsertHelpHoverText("Total number of days elapsed since the start of the game"); - - ImGui::InputScalar("Deaths", ImGuiDataType_U16, &gSaveContext.deaths); - InsertHelpHoverText("Total number of deaths"); - - // TODO Move to quest status screen once the page is created - ImGui::InputScalar("GS Count", ImGuiDataType_S16, &gSaveContext.inventory.gsTokens); - InsertHelpHoverText("Number of gold skulltula tokens aquired"); - - bool bgsFlag = gSaveContext.bgsFlag != 0; - if (ImGui::Checkbox("Has BGS", &bgsFlag)) { - gSaveContext.bgsFlag = bgsFlag; - } - InsertHelpHoverText("Is Biggoron sword unlocked? Replaces Giant's knife"); - - ImGui::InputScalar("Sword Health", ImGuiDataType_U16, &gSaveContext.swordHealth); - InsertHelpHoverText("Giant's knife health. Default is 8. Must be >0 for Biggoron sword to work"); - - ImGui::InputScalar("Bgs Day Count", ImGuiDataType_S32, &gSaveContext.bgsDayCount); - InsertHelpHoverText("Total number of days elapsed since giving Biggoron the claim check"); - - // TODO Changing Link's age is more involved than just setting gSaveContext.linkAge - // It might not fit here and instead should be only changable when changing scenes - /* - if (ImGui::BeginCombo("Link Age", LINK_IS_ADULT ? "Adult" : "Child")) { - if (ImGui::Selectable("Adult")) { - gSaveContext.linkAge = 0; - } - if (ImGui::Selectable("Child")) { - gSaveContext.linkAge = 1; - } - - ImGui::EndCombo(); - } - */ - - ImGui::PopItemWidth(); + DrawInfoTab(); ImGui::EndTabItem(); } if (ImGui::BeginTabItem("Inventory")) { - static bool restrictToValid = true; - - ImGui::Checkbox("Restrict to valid items", &restrictToValid); - InsertHelpHoverText("Restricts items and ammo to only what is possible to legally acquire in-game"); - - for (int32_t y = 0; y < 4; y++) { - for (int32_t x = 0; x < 6; x++) { - int32_t index = x + y * 6; - static int32_t selectedIndex = -1; - static const char* itemPopupPicker = "itemPopupPicker"; - - ImGui::PushID(index); - - if (x != 0) { - ImGui::SameLine(); - } - - ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1, 1, 1, 0)); - ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); - uint8_t item = gSaveContext.inventory.items[index]; - if (item != ITEM_NONE) { - const ItemMapEntry& slotEntry = itemMapping.find(item)->second; - if (ImGui::ImageButton(SohImGui::GetTextureByName(slotEntry.name), ImVec2(32.0f, 32.0f), - ImVec2(0, 0), ImVec2(1, 1), 0)) { - selectedIndex = index; - ImGui::OpenPopup(itemPopupPicker); - } - } else { - if (ImGui::Button("##itemNone", ImVec2(32.0f, 32.0f))) { - selectedIndex = index; - ImGui::OpenPopup(itemPopupPicker); - } - } - ImGui::PopStyleVar(); - ImGui::PopStyleColor(); - - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); - if (ImGui::BeginPopup(itemPopupPicker)) { - if (ImGui::Button("##itemNonePicker", ImVec2(32.0f, 32.0f))) { - gSaveContext.inventory.items[selectedIndex] = ITEM_NONE; - ImGui::CloseCurrentPopup(); - } - SetLastItemHoverText("ITEM_NONE"); - - std::vector possibleItems; - if (restrictToValid) { - // Scan gItemSlots to find legal items for this slot. Bottles are a special case - for (int slotIndex = 0; slotIndex < 56; slotIndex++) { - int testIndex = (selectedIndex == SLOT_BOTTLE_1 || selectedIndex == SLOT_BOTTLE_2 || - selectedIndex == SLOT_BOTTLE_3 || selectedIndex == SLOT_BOTTLE_4) - ? SLOT_BOTTLE_1 - : selectedIndex; - if (gItemSlots[slotIndex] == testIndex) { - possibleItems.push_back(itemMapping[slotIndex]); - } - } - } else { - for (const auto& entry : itemMapping) { - possibleItems.push_back(entry.second); - } - } - - for (int32_t pickerIndex = 0; pickerIndex < possibleItems.size(); pickerIndex++) { - if (((pickerIndex + 1) % 8) != 0) { - ImGui::SameLine(); - } - const ItemMapEntry& slotEntry = possibleItems[pickerIndex]; - if (ImGui::ImageButton(SohImGui::GetTextureByName(slotEntry.name), ImVec2(32.0f, 32.0f), - ImVec2(0, 0), ImVec2(1, 1), 0)) { - gSaveContext.inventory.items[selectedIndex] = slotEntry.id; - ImGui::CloseCurrentPopup(); - } - SetLastItemHoverText(slotEntry.name); - } - - ImGui::EndPopup(); - } - ImGui::PopStyleVar(); - - ImGui::PopID(); - } - } - - ImGui::Text("Ammo"); - for (uint32_t ammoIndex = 0, drawnAmmoItems = 0; ammoIndex < 16; ammoIndex++) { - uint8_t item = (restrictToValid) ? gAmmoItems[ammoIndex] : gAllAmmoItems[ammoIndex]; - if (item != ITEM_NONE) { - // For legal items, display as 1 row of 7. For unrestricted items, display rows of 6 to match - // inventory - if ((restrictToValid && (drawnAmmoItems != 0)) || ((drawnAmmoItems % 6) != 0)) { - ImGui::SameLine(); - } - drawnAmmoItems++; - - ImGui::PushID(ammoIndex); - ImGui::PushItemWidth(32.0f); - ImGui::BeginGroup(); - - ImGui::Image(SohImGui::GetTextureByName(itemMapping[item].name), ImVec2(32.0f, 32.0f)); - ImGui::InputScalar("##ammoInput", ImGuiDataType_S8, &AMMO(item)); - - ImGui::EndGroup(); - ImGui::PopItemWidth(); - ImGui::PopID(); - } - } - + DrawInventoryTab(); ImGui::EndTabItem(); } if (ImGui::BeginTabItem("Flags")) { - static uint32_t selectedGsMap = 0; - ImGui::Text("Gold Skulltulas"); - ImGui::Text("Map"); - ImGui::SameLine(); - if (ImGui::BeginCombo("##Gold Skulltula Map", gsMapping[selectedGsMap].c_str())) { - for (int32_t gsIndex = 0; gsIndex < gsMapping.size(); gsIndex++) { - if (ImGui::Selectable(gsMapping[gsIndex].c_str())) { - selectedGsMap = gsIndex; - } - } + DrawFlagsTab(); + ImGui::EndTabItem(); + } - ImGui::EndCombo(); - } - - // TODO We should write out descriptions for each one... ugh - ImGui::Text("Flags"); - uint32_t currentFlags = GET_GS_FLAGS(selectedGsMap); - uint32_t allFlags = gAreaGsFlags[selectedGsMap]; - uint32_t setMask = 1; - // Iterate over bitfield and create a checkbox for each skulltula - while (allFlags != 0) { - bool isThisSet = (currentFlags & 0x1) == 0x1; - - ImGui::SameLine(); - ImGui::PushID(allFlags); - if (ImGui::Checkbox("##gs", &isThisSet)) { - if (isThisSet) { - SET_GS_FLAGS(selectedGsMap, setMask); - } else { - // Have to do this roundabout method as the macro does not support clearing flags - uint32_t currentFlagsBase = GET_GS_FLAGS(selectedGsMap); - gSaveContext.gsFlags[selectedGsMap >> 2] &= ~gGsFlagsMasks[selectedGsMap & 3]; - SET_GS_FLAGS(selectedGsMap, currentFlagsBase & ~setMask); - } - } - - ImGui::PopID(); - - allFlags >>= 1; - currentFlags >>= 1; - setMask <<= 1; - } - - static bool keepGsCountUpdated = true; - ImGui::Checkbox("Keep GS Count Updated", &keepGsCountUpdated); - InsertHelpHoverText("Automatically adjust the number of gold skulltula tokens acquired based on set flags"); - int32_t gsCount = 0; - if (keepGsCountUpdated) { - for (int32_t gsFlagIndex = 0; gsFlagIndex < 6; gsFlagIndex++) { - gsCount += std::popcount(static_cast(gSaveContext.gsFlags[gsFlagIndex])); - } - gSaveContext.inventory.gsTokens = gsCount; - } - - // TODO other flag types, like switch, clear, etc. - // These flags interact with the actor context, so it's a bit more complicated + if (ImGui::BeginTabItem("Equipment")) { + DrawEquipmentTab(); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Quest Status")) { + DrawQuestStatusTab(); ImGui::EndTabItem(); } @@ -523,5 +1125,16 @@ void InitSaveEditor() { // Load item icons into ImGui for (const auto& entry : itemMapping) { SohImGui::LoadResource(entry.second.name, entry.second.texturePath); + SohImGui::LoadResource(entry.second.nameFaded, entry.second.texturePath, ImVec4(1, 1, 1, 0.3f)); + } + for (const auto& entry : questMapping) { + SohImGui::LoadResource(entry.second.name, entry.second.texturePath); + SohImGui::LoadResource(entry.second.nameFaded, entry.second.texturePath, ImVec4(1, 1, 1, 0.3f)); + } + for (const auto& entry : songMapping) { + SohImGui::LoadResource(entry.name, gSongNoteTex, entry.color); + ImVec4 fadedCol = entry.color; + fadedCol.w = 0.3f; + SohImGui::LoadResource(entry.nameFaded, gSongNoteTex, fadedCol); } } diff --git a/soh/soh/util.cpp b/soh/soh/util.cpp new file mode 100644 index 000000000..7a5d0205d --- /dev/null +++ b/soh/soh/util.cpp @@ -0,0 +1,314 @@ +#include "util.h" + +#include + +std::vector sceneNames = { + "Inside the Deku Tree", + "Dodongo's Cavern", + "Inside Jabu-Jabu's Belly", + "Forest Temple", + "Fire Temple", + "Water Temple", + "Spirit Temple", + "Shadow Temple", + "Bottom of the Well", + "Ice Cavern", + "Ganon's Tower", + "Gerudo Training Ground", + "Thieves' Hideout", + "Inside Ganon's Castle", + "Ganon's Tower (Collapsing)", + "Inside Ganon's Castle (Collapsing)", + "Treasure Box Shop", + "Gohma's Lair", + "King Dodongo's Lair", + "Barinade's Lair", + "Phantom Ganon's Lair", + "Volvagia's Lair", + "Morpha's Lair", + "Twinrova's Lair & Nabooru's Mini-Boss Room", + "Bongo Bongo's Lair", + "Ganondorf's Lair", + "Tower Collapse Exterior", + "Market Entrance (Child - Day)", + "Market Entrance (Child - Night)", + "Market Entrance (Ruins)", + "Back Alley (Child - Day)", + "Back Alley (Child - Night)", + "Market (Child - Day)", + "Market (Child - Night)", + "Market (Ruins)", + "Temple of Time Exterior (Child - Day)", + "Temple of Time Exterior (Child - Night)", + "Temple of Time Exterior (Ruins)", + "Know-It-All Brothers' House", + "House of Twins", + "Mido's House", + "Saria's House", + "Carpenter Boss's House", + "Back Alley House (Man in Green)", + "Bazaar", + "Kokiri Shop", + "Goron Shop", + "Zora Shop", + "Kakariko Potion Shop", + "Market Potion Shop", + "Bombchu Shop", + "Happy Mask Shop", + "Link's House", + "Back Alley House (Dog Lady)", + "Stable", + "Impa's House", + "Lakeside Laboratory", + "Carpenters' Tent", + "Gravekeeper's Hut", + "Great Fairy's Fountain (Upgrades)", + "Fairy's Fountain", + "Great Fairy's Fountain (Spells)", + "Grottos", + "Grave (Redead)", + "Grave (Fairy's Fountain)", + "Royal Family's Tomb", + "Shooting Gallery", + "Temple of Time", + "Chamber of the Sages", + "Castle Hedge Maze (Day)", + "Castle Hedge Maze (Night)", + "Cutscene Map", + "Dampé's Grave & Windmill", + "Fishing Pond", + "Castle Courtyard", + "Bombchu Bowling Alley", + "Ranch House & Silo", + "Guard House", + "Granny's Potion Shop", + "Ganon's Tower Collapse & Battle Arena", + "House of Skulltula", + "Spot 00 - Hyrule Field", + "Spot 01 - Kakariko Village", + "Spot 02 - Graveyard", + "Spot 03 - Zora's River", + "Spot 04 - Kokiri Forest", + "Spot 05 - Sacred Forest Meadow", + "Spot 06 - Lake Hylia", + "Spot 07 - Zora's Domain", + "Spot 08 - Zora's Fountain", + "Spot 09 - Gerudo Valley", + "Spot 10 - Lost Woods", + "Spot 11 - Desert Colossus", + "Spot 12 - Gerudo's Fortress", + "Spot 13 - Haunted Wasteland", + "Spot 15 - Hyrule Castle", + "Spot 16 - Death Mountain Trail", + "Spot 17 - Death Mountain Crater", + "Spot 18 - Goron City", + "Spot 20 - Lon Lon Ranch", + "Ganon's Castle Exterior", + "Jungle Gym", + "Ganondorf Test Room", + "Depth Test", + "Stalfos Mini-Boss Room", + "Stalfos Boss Room", + "Sutaru", + "Castle Hedge Maze (Early)", + "Sasa Test", + "Treasure Chest Room", +}; + +std::vector itemNames = { + "Deku Stick", + "Deku Nut", + "Bomb", + "Fairy Bow", + "Fire Arrow", + "Din's Fire", + "Fairy Slingshot", + "Fairy Ocarina", + "Ocarina of Time", + "Bombchu", + "Hookshot", + "Longshot", + "Ice Arrow", + "Farore's Wind", + "Boomerang", + "Lens of Truth", + "Magic Bean", + "Megaton Hammer", + "Light Arrow", + "Nayru's Love", + "Empty Bottle", + "Red Potion", + "Green Potion", + "Blue Potion", + "Bottled Fairy", + "Fish", + "Lon Lon Milk & Bottle", + "Ruto's Letter", + "Blue Fire", + "Bug", + "Big Poe", + "Lon Lon Milk (Half)", + "Poe", + "Weird Egg", + "Chicken", + "Zelda's Letter", + "Keaton Mask", + "Skull Mask", + "Spooky Mask", + "Bunny Hood", + "Goron Mask", + "Zora Mask", + "Gerudo Mask", + "Mask of Truth", + "SOLD OUT", + "Pocket Egg", + "Pocket Cucco", + "Cojiro", + "Odd Mushroom", + "Odd Potion", + "Poacher's Saw", + "Goron's Sword (Broken)", + "Prescription", + "Eyeball Frog", + "Eye Drops", + "Claim Check", + "Fairy Bow & Fire Arrow", + "Fairy Bow & Ice Arrow", + "Fairy Bow & Light Arrow", + "Kokiri Sword", + "Master Sword", + "Giant's Knife & Biggoron's Sword", + "Deku Shield", + "Hylian Shield", + "Mirror Shield", + "Kokiri Tunic", + "Goron Tunic", + "Zora Tunic", + "Kokiri Boots", + "Iron Boots", + "Hover Boots", + "Bullet Bag (30)", + "Bullet Bag (40)", + "Bullet Bag (50)", + "Quiver (30)", + "Big Quiver (40)", + "Biggest Quiver (50)", + "Bomb Bag (20)", + "Big Bomb Bag (30)", + "Biggest Bomb Bag (40)", + "Goron's Bracelet", + "Silver Gauntlets", + "Golden Gauntlets", + "Silver Scale", + "Golden Scale", + "Giant's Knife (Broken)", + "Adult's Wallet", + "Giant's Wallet", + "Deku Seeds (5)", + "Fishing Pole", + "Minuet of Forest", + "Bolero of Fire", + "Serenade of Water", + "Requiem of Spirit", + "Nocturne of Shadow", + "Prelude of Light", + "Zelda's Lullaby", + "Epona's Song", + "Saria's Song", + "Sun's Song", + "Song of Time", + "Song of Storms", + "Forest Medallion", + "Fire Medallion", + "Water Medallion", + "Spirit Medallion", + "Shadow Medallion", + "Light Medallion", + "Kokiri's Emerald", + "Goron's Ruby", + "Zora's Sapphire", + "Stone of Agony", + "Gerudo's Card", + "Gold Skulltula Token", + "Heart Container", + "Piece of Heart [?]", + "Big Key", + "Compass", + "Dungeon Map", + "Small Key", + "Small Magic Jar", + "Large Magic Jar", + "Piece of Heart", + "[Removed]", + "[Removed]", + "[Removed]", + "[Removed]", + "[Removed]", + "[Removed]", + "[Removed]", + "Lon Lon Milk", + "Recovery Heart", + "Green Rupee", + "Blue Rupee", + "Red Rupee", + "Purple Rupee", + "Huge Rupee", + "[Removed]", + "Deku Sticks (5)", + "Deku Sticks (10)", + "Deku Nuts (5)", + "Deku Nuts (10)", + "Bombs (5)", + "Bombs (10)", + "Bombs (20)", + "Bombs (30)", + "Arrows (Small)", + "Arrows (Medium)", + "Arrows (Large)", + "Deku Seeds (30)", + "Bombchu (5)", + "Bombchu (20)", + "Deku Stick Upgrade (20)", + "Deku Stick Upgrade (30)", + "Deku Nut Upgrade (30)", + "Deku Nut Upgrade (40)", +}; + +std::vector questItemNames = { + "Forest Medallion", + "Fire Medallion", + "Water Medallion", + "Spirit Medallion", + "Shadow Medallion", + "Light Medallion", + "Minuet of Forest", + "Bolero of Fire", + "Serenade of Water", + "Requiem of Spirit", + "Nocturne of Shadow", + "Prelude of Light", + "Zelda's Lullaby", + "Epona's Song", + "Saria's Song", + "Sun's Song", + "Song of Time", + "Song of Storms", + "Kokiri's Emerald", + "Goron's Ruby", + "Zora's Sapphire", + "Stone of Agony", + "Gerudo's Card", + "Gold Skulltula Token", +}; + +const std::string& SohUtils::GetSceneName(int32_t scene) { + return sceneNames[scene]; +} + +const std::string& SohUtils::GetItemName(int32_t item) { + return itemNames[item]; +} + +const std::string& SohUtils::GetQuestItemName(int32_t item) { + return questItemNames[item]; +} diff --git a/soh/soh/util.h b/soh/soh/util.h new file mode 100644 index 000000000..9fd806f18 --- /dev/null +++ b/soh/soh/util.h @@ -0,0 +1,11 @@ +#pragma once +#include +#include + +namespace SohUtils { + const std::string& GetSceneName(int32_t scene); + + const std::string& GetItemName(int32_t item); + + const std::string& GetQuestItemName(int32_t item); +} // namespace SohUtils From fe6dbd2a5b38e63db5fde84091a68803892cc0f1 Mon Sep 17 00:00:00 2001 From: Sirius902 <10891979+Sirius902@users.noreply.github.com> Date: Sun, 17 Apr 2022 08:26:49 -0700 Subject: [PATCH 12/13] MM Bunny Hood enhancement (#181) Allow bunny hood in boss rooms Use math instead of array Allow other masks with enhancement because why not --- libultraship/libultraship/GameSettings.cpp | 4 +++ libultraship/libultraship/GameSettings.h | 1 + libultraship/libultraship/SohImGuiImpl.cpp | 7 ++++- soh/src/code/z_parameter.c | 6 ++++- .../actors/ovl_player_actor/z_player.c | 27 +++++++++++++++---- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/libultraship/libultraship/GameSettings.cpp b/libultraship/libultraship/GameSettings.cpp index 678555c7d..1b8d473b2 100644 --- a/libultraship/libultraship/GameSettings.cpp +++ b/libultraship/libultraship/GameSettings.cpp @@ -61,6 +61,9 @@ namespace Game { Settings.enhancements.minimal_ui = stob(Conf[EnhancementSection]["minimal_ui"]); CVar_SetS32("gMinimalUI", Settings.enhancements.minimal_ui); + Settings.enhancements.mm_bunny_hood = stob(Conf[EnhancementSection]["mm_bunny_hood"]); + CVar_SetS32("gMMBunnyHood", Settings.enhancements.mm_bunny_hood); + // Audio Settings.audio.master = Ship::stof(Conf[AudioSection]["master"]); CVar_SetFloat("gGameMasterVolume", Settings.audio.master); @@ -152,6 +155,7 @@ namespace Game { Conf[EnhancementSection]["disable_lod"] = std::to_string(Settings.enhancements.disable_lod); Conf[EnhancementSection]["animated_pause_menu"] = std::to_string(Settings.enhancements.animated_pause_menu); Conf[EnhancementSection]["minimal_ui"] = std::to_string(Settings.enhancements.minimal_ui); + Conf[EnhancementSection]["mm_bunny_hood"] = std::to_string(Settings.enhancements.mm_bunny_hood); // Controllers Conf[ControllerSection]["gyro_sensitivity"] = std::to_string(Settings.controller.gyro_sensitivity); diff --git a/libultraship/libultraship/GameSettings.h b/libultraship/libultraship/GameSettings.h index 17079c9bf..340e93d26 100644 --- a/libultraship/libultraship/GameSettings.h +++ b/libultraship/libultraship/GameSettings.h @@ -24,6 +24,7 @@ struct SoHConfigType { bool disable_lod = false; bool animated_pause_menu = false; bool minimal_ui = false; + bool mm_bunny_hood = false; } enhancements; // Controller diff --git a/libultraship/libultraship/SohImGuiImpl.cpp b/libultraship/libultraship/SohImGuiImpl.cpp index b7e6e01ab..93dcd2492 100644 --- a/libultraship/libultraship/SohImGuiImpl.cpp +++ b/libultraship/libultraship/SohImGuiImpl.cpp @@ -413,6 +413,11 @@ namespace SohImGui { needs_save = true; } + if (ImGui::Checkbox("MM Bunny Hood", &Game::Settings.enhancements.mm_bunny_hood)) { + CVar_SetS32("gMMBunnyHood", Game::Settings.enhancements.mm_bunny_hood); + needs_save = true; + } + ImGui::Text("Graphics"); ImGui::Separator(); @@ -639,4 +644,4 @@ namespace SohImGui { ImTextureID GetTextureByName(const std::string& name) { return GetTextureByID(DefaultAssets[name]->textureId); } -} \ No newline at end of file +} diff --git a/soh/src/code/z_parameter.c b/soh/src/code/z_parameter.c index 1653cdaaf..dc96ba133 100644 --- a/soh/src/code/z_parameter.c +++ b/soh/src/code/z_parameter.c @@ -919,7 +919,11 @@ void func_80083108(GlobalContext* globalCtx) { if (interfaceCtx->restrictions.tradeItems != 0) { for (i = 1; i < 4; i++) { - if ((gSaveContext.equips.buttonItems[i] >= ITEM_WEIRD_EGG) && + if ((CVar_GetS32("gMMBunnyHood", 0) != 0) + && (gSaveContext.equips.buttonItems[i] >= ITEM_MASK_KEATON) + && (gSaveContext.equips.buttonItems[i] <= ITEM_MASK_TRUTH)) { + gSaveContext.buttonStatus[i] = BTN_ENABLED; + } else if ((gSaveContext.equips.buttonItems[i] >= ITEM_WEIRD_EGG) && (gSaveContext.equips.buttonItems[i] <= ITEM_CLAIM_CHECK)) { if (gSaveContext.buttonStatus[i] == BTN_ENABLED) { sp28 = 1; diff --git a/soh/src/overlays/actors/ovl_player_actor/z_player.c b/soh/src/overlays/actors/ovl_player_actor/z_player.c index a2a810175..734ba015e 100644 --- a/soh/src/overlays/actors/ovl_player_actor/z_player.c +++ b/soh/src/overlays/actors/ovl_player_actor/z_player.c @@ -1875,10 +1875,20 @@ void func_80833DF8(Player* this, GlobalContext* globalCtx) { s32 i; if (this->currentMask != PLAYER_MASK_NONE) { - maskActionParam = this->currentMask - 1 + PLAYER_AP_MASK_KEATON; - if (!func_80833C98(C_BTN_ITEM(0), maskActionParam) && !func_80833C98(C_BTN_ITEM(1), maskActionParam) && - !func_80833C98(C_BTN_ITEM(2), maskActionParam)) { - this->currentMask = PLAYER_MASK_NONE; + if (CVar_GetS32("gMMBunnyHood", 0) != 0) { + s32 maskItem = this->currentMask - PLAYER_MASK_KEATON + ITEM_MASK_KEATON; + + if (gSaveContext.equips.buttonItems[0] != maskItem && gSaveContext.equips.buttonItems[1] != maskItem && + gSaveContext.equips.buttonItems[2] != maskItem && gSaveContext.equips.buttonItems[3] != maskItem) { + this->currentMask = PLAYER_MASK_NONE; + func_808328EC(this, NA_SE_PL_CHANGE_ARMS); + } + } else { + maskActionParam = this->currentMask - 1 + PLAYER_AP_MASK_KEATON; + if (!func_80833C98(C_BTN_ITEM(0), maskActionParam) && !func_80833C98(C_BTN_ITEM(1), maskActionParam) && + !func_80833C98(C_BTN_ITEM(2), maskActionParam)) { + this->currentMask = PLAYER_MASK_NONE; + } } } @@ -5942,7 +5952,11 @@ void func_8083DFE0(Player* this, f32* arg1, s16* arg2) { s16 yawDiff = this->currentYaw - *arg2; if (this->swordState == 0) { - this->linearVelocity = CLAMP(this->linearVelocity, -(R_RUN_SPEED_LIMIT / 100.0f), (R_RUN_SPEED_LIMIT / 100.0f)); + float maxSpeed = R_RUN_SPEED_LIMIT / 100.0f; + if (CVar_GetS32("gMMBunnyHood", 0) != 0 && this->currentMask == PLAYER_MASK_BUNNY) { + maxSpeed *= 1.5f; + } + this->linearVelocity = CLAMP(this->linearVelocity, -maxSpeed, maxSpeed); } if (ABS(yawDiff) > 0x6000) { @@ -7523,6 +7537,9 @@ void func_80842180(Player* this, GlobalContext* globalCtx) { func_80837268(this, &sp2C, &sp2A, 0.018f, globalCtx); if (!func_8083C484(this, &sp2C, &sp2A)) { + if (CVar_GetS32("gMMBunnyHood", 0) != 0 && this->currentMask == PLAYER_MASK_BUNNY) { + sp2C *= 1.5f; + } func_8083DF68(this, sp2C, sp2A); func_8083DDC8(this, globalCtx); From ceef4a94537a9b6fd14b5ff1b58f4605fd4c64fd Mon Sep 17 00:00:00 2001 From: Emill Date: Mon, 18 Apr 2022 11:37:47 +0200 Subject: [PATCH 13/13] Graphics backend enhancements etc. (#163) --- libultraship/libultraship/GameSettings.h | 5 + .../Lib/Fast3D/gfx_direct3d11.cpp | 541 +++++++++--------- .../Lib/Fast3D/gfx_direct3d_common.cpp | 16 +- .../libultraship/Lib/Fast3D/gfx_dxgi.cpp | 27 +- .../libultraship/Lib/Fast3D/gfx_opengl.cpp | 292 ++++++---- .../libultraship/Lib/Fast3D/gfx_pc.cpp | 186 ++++-- libultraship/libultraship/Lib/Fast3D/gfx_pc.h | 13 +- .../Lib/Fast3D/gfx_rendering_api.h | 24 +- libultraship/libultraship/Lib/ImGui/imgui.h | 1 - .../libultraship/Lib/ImGui/imgui_widgets.cpp | 27 - libultraship/libultraship/SohImGuiImpl.cpp | 169 +++--- libultraship/libultraship/SohImGuiImpl.h | 5 +- libultraship/libultraship/Window.cpp | 4 + libultraship/libultraship/Window.h | 1 + soh/include/functions.h | 1 + soh/soh/GbiWrap.cpp | 1 + soh/soh/OTRGlobals.cpp | 75 ++- soh/soh/OTRGlobals.h | 2 +- soh/src/code/graph.c | 29 - soh/src/code/z_kankyo.c | 3 + soh/src/code/z_lights.c | 38 ++ 21 files changed, 872 insertions(+), 588 deletions(-) diff --git a/libultraship/libultraship/GameSettings.h b/libultraship/libultraship/GameSettings.h index 340e93d26..fe6087494 100644 --- a/libultraship/libultraship/GameSettings.h +++ b/libultraship/libultraship/GameSettings.h @@ -52,6 +52,11 @@ struct SoHConfigType { bool moon_jump_on_l = false; bool super_tunic = false; } cheats; + + // Graphics + struct { + bool show = false; + } graphics; }; enum SeqPlayers { diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp index 9c2f46422..04d777e7e 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_direct3d11.cpp @@ -37,9 +37,8 @@ namespace { struct PerFrameCB { uint32_t noise_frame; - float noise_scale_x; - float noise_scale_y; - uint32_t padding; + float noise_scale; + uint32_t padding[2]; // constant buffers must be multiples of 16 bytes in size }; struct PerDrawCB { @@ -51,12 +50,12 @@ struct PerDrawCB { } textures[2]; }; -struct CoordCB { - float x, y; - float padding[2]; // structure size must be multiple of 16 +struct Coord { + int x, y; }; struct TextureData { + ComPtr texture; ComPtr resource_view; ComPtr sampler_state; uint32_t width; @@ -64,10 +63,13 @@ struct TextureData { bool linear_filtering; }; -struct FramebufferData { +struct Framebuffer { ComPtr render_target_view; ComPtr depth_stencil_view; + ComPtr depth_stencil_srv; uint32_t texture_id; + bool has_depth_buffer; + uint32_t msaa_level; }; struct ShaderProgramD3D11 { @@ -91,34 +93,30 @@ static struct { pD3DCompile D3DCompile; D3D_FEATURE_LEVEL feature_level; + uint32_t msaa_num_quality_levels[D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT]; ComPtr device; ComPtr swap_chain; ComPtr context; - ComPtr backbuffer_view; - ComPtr depth_stencil_view; - ComPtr depth_stencil_srv; ComPtr rasterizer_state; ComPtr depth_stencil_state; ComPtr vertex_buffer; ComPtr per_frame_cb; ComPtr per_draw_cb; - ComPtr depth_stencil_texture; - ComPtr depth_stencil_copy_texture; ComPtr coord_buffer; + ComPtr coord_buffer_srv; ComPtr depth_value_output_buffer; ComPtr depth_value_output_buffer_copy; ComPtr depth_value_output_uav; - ComPtr depth_value_sampler; ComPtr compute_shader; - bool copied_depth_buffer; + ComPtr compute_shader_msaa; + ComPtr compute_shader_msaa_blob; + size_t coord_buffer_size; #if DEBUG_D3D ComPtr debug; #endif - DXGI_SAMPLE_DESC sample_description; - PerFrameCB per_frame_cb_data; PerDrawCB per_draw_cb_data; @@ -128,14 +126,15 @@ static struct { int current_tile; uint32_t current_texture_ids[2]; - std::vector framebuffers; + std::vector framebuffers; // Current state struct ShaderProgramD3D11 *shader_program; - uint32_t current_width, current_height; + //uint32_t current_width, current_height; uint32_t render_target_height; + int current_framebuffer; int8_t depth_test; int8_t depth_mask; @@ -156,7 +155,9 @@ static struct { static LARGE_INTEGER last_time, accumulated_time, frequency; -void create_depth_stencil_objects(uint32_t width, uint32_t height, ID3D11Texture2D **texture, ID3D11DepthStencilView **view, ID3D11ShaderResourceView **srv) { +int gfx_d3d11_create_framebuffer(void); + +void create_depth_stencil_objects(uint32_t width, uint32_t height, uint32_t msaa_count, ID3D11DepthStencilView **view, ID3D11ShaderResourceView **srv) { D3D11_TEXTURE2D_DESC texture_desc; texture_desc.Width = width; texture_desc.Height = height; @@ -164,93 +165,42 @@ void create_depth_stencil_objects(uint32_t width, uint32_t height, ID3D11Texture texture_desc.ArraySize = 1; texture_desc.Format = d3d.feature_level >= D3D_FEATURE_LEVEL_10_0 ? DXGI_FORMAT_R32_TYPELESS : DXGI_FORMAT_R24G8_TYPELESS; - texture_desc.SampleDesc.Count = 1; + texture_desc.SampleDesc.Count = msaa_count; texture_desc.SampleDesc.Quality = 0; texture_desc.Usage = D3D11_USAGE_DEFAULT; texture_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | (srv != nullptr ? D3D11_BIND_SHADER_RESOURCE : 0); texture_desc.CPUAccessFlags = 0; texture_desc.MiscFlags = 0; - ThrowIfFailed(d3d.device->CreateTexture2D(&texture_desc, nullptr, texture)); + ComPtr texture; + ThrowIfFailed(d3d.device->CreateTexture2D(&texture_desc, nullptr, texture.GetAddressOf())); D3D11_DEPTH_STENCIL_VIEW_DESC view_desc; view_desc.Format = d3d.feature_level >= D3D_FEATURE_LEVEL_10_0 ? DXGI_FORMAT_D32_FLOAT : DXGI_FORMAT_D24_UNORM_S8_UINT; - view_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; view_desc.Flags = 0; - view_desc.Texture2D.MipSlice = 0; + if (msaa_count > 1) { + view_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS; + view_desc.Texture2DMS.UnusedField_NothingToDefine = 0; + } else { + view_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; + view_desc.Texture2D.MipSlice = 0; + } - ThrowIfFailed(d3d.device->CreateDepthStencilView(*texture, &view_desc, view)); + ThrowIfFailed(d3d.device->CreateDepthStencilView(texture.Get(), &view_desc, view)); if (srv != nullptr) { D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc; srv_desc.Format = d3d.feature_level >= D3D_FEATURE_LEVEL_10_0 ? DXGI_FORMAT_R32_FLOAT : DXGI_FORMAT_R24_UNORM_X8_TYPELESS; - srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + srv_desc.ViewDimension = msaa_count > 1 ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D; srv_desc.Texture2D.MostDetailedMip = 0; srv_desc.Texture2D.MipLevels = -1; - ThrowIfFailed(d3d.device->CreateShaderResourceView(*texture, &srv_desc, srv)); + ThrowIfFailed(d3d.device->CreateShaderResourceView(texture.Get(), &srv_desc, srv)); } } -static void create_render_target_views(bool is_resize) { - DXGI_SWAP_CHAIN_DESC1 desc1; - - if (is_resize) { - // Release previous stuff (if any) - - d3d.backbuffer_view.Reset(); - d3d.depth_stencil_texture.Reset(); - d3d.depth_stencil_view.Reset(); - d3d.depth_stencil_srv.Reset(); - d3d.depth_stencil_copy_texture.Reset(); - - // Resize swap chain buffers - - ThrowIfFailed(d3d.swap_chain->GetDesc1(&desc1)); - ThrowIfFailed(d3d.swap_chain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, desc1.Flags), - gfx_dxgi_get_h_wnd(), "Failed to resize IDXGISwapChain buffers."); - } - - // Get new size - - ThrowIfFailed(d3d.swap_chain->GetDesc1(&desc1)); - - // Create back buffer - - ComPtr backbuffer_texture; - ThrowIfFailed(d3d.swap_chain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID *) backbuffer_texture.GetAddressOf()), - gfx_dxgi_get_h_wnd(), "Failed to get backbuffer from IDXGISwapChain."); - - ThrowIfFailed(d3d.device->CreateRenderTargetView(backbuffer_texture.Get(), nullptr, d3d.backbuffer_view.GetAddressOf()), - gfx_dxgi_get_h_wnd(), "Failed to create render target view."); - - // Create depth buffer - create_depth_stencil_objects(desc1.Width, desc1.Height, d3d.depth_stencil_texture.GetAddressOf(), d3d.depth_stencil_view.GetAddressOf(), d3d.depth_stencil_srv.GetAddressOf()); - - // Create texture that can be used to retrieve depth value - - D3D11_TEXTURE2D_DESC depth_texture = {}; - depth_texture.Width = desc1.Width; - depth_texture.Height = desc1.Height; - depth_texture.MipLevels = 1; - depth_texture.ArraySize = 1; - depth_texture.Format = DXGI_FORMAT_D32_FLOAT; - depth_texture.SampleDesc.Count = 1; - depth_texture.SampleDesc.Quality = 0; - depth_texture.Usage = D3D11_USAGE_STAGING; - depth_texture.BindFlags = 0; - depth_texture.CPUAccessFlags = D3D11_CPU_ACCESS_READ; - depth_texture.MiscFlags = 0; - ThrowIfFailed(d3d.device->CreateTexture2D(&depth_texture, nullptr, d3d.depth_stencil_copy_texture.GetAddressOf())); - - // Save resolution - - d3d.current_width = desc1.Width; - d3d.current_height = desc1.Height; -} - static void gfx_d3d11_init(void) { // Load d3d11.dll d3d.d3d11_module = LoadLibraryW(L"d3d11.dll"); @@ -300,11 +250,6 @@ static void gfx_d3d11_init(void) { } }); - // Sample description to be used in back buffer and depth buffer - - d3d.sample_description.Count = 1; - d3d.sample_description.Quality = 0; - // Create the swap chain d3d.swap_chain = gfx_dxgi_create_swap_chain(d3d.device.Get()); @@ -315,9 +260,19 @@ static void gfx_d3d11_init(void) { gfx_dxgi_get_h_wnd(), "Failed to get ID3D11Debug device."); #endif - // Create views + // Create the default framebuffer which represents the window + Framebuffer& fb = d3d.framebuffers[gfx_d3d11_create_framebuffer()]; - create_render_target_views(false); + // Check the size of the window + DXGI_SWAP_CHAIN_DESC1 swap_chain_desc; + ThrowIfFailed(d3d.swap_chain->GetDesc1(&swap_chain_desc)); + d3d.textures[fb.texture_id].width = swap_chain_desc.Width; + d3d.textures[fb.texture_id].height = swap_chain_desc.Height; + fb.msaa_level = 1; + + for (uint32_t sample_count = 1; sample_count <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; sample_count++) { + ThrowIfFailed(d3d.device->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM, sample_count, &d3d.msaa_num_quality_levels[sample_count - 1])); + } // Create main vertex buffer @@ -364,61 +319,30 @@ static void gfx_d3d11_init(void) { // Create compute shader that can be used to retrieve depth buffer values - D3D11_BUFFER_DESC coord_cb_desc; - coord_cb_desc.Usage = D3D11_USAGE_DYNAMIC; - coord_cb_desc.ByteWidth = sizeof(CoordCB); - coord_cb_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; - coord_cb_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; - coord_cb_desc.MiscFlags = 0; - coord_cb_desc.StructureByteStride = 0; - - ThrowIfFailed(d3d.device->CreateBuffer(&coord_cb_desc, nullptr, d3d.coord_buffer.GetAddressOf())); - - D3D11_SAMPLER_DESC sampler_desc = {}; - sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; - sampler_desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; - sampler_desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; - sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; - sampler_desc.MinLOD = 0; - sampler_desc.MaxLOD = D3D11_FLOAT32_MAX; - ThrowIfFailed(d3d.device->CreateSamplerState(&sampler_desc, d3d.depth_value_sampler.GetAddressOf())); - - D3D11_BUFFER_DESC output_buffer_desc; - output_buffer_desc.Usage = D3D11_USAGE_DEFAULT; - output_buffer_desc.ByteWidth = sizeof(float); - output_buffer_desc.BindFlags = D3D11_BIND_UNORDERED_ACCESS; - output_buffer_desc.CPUAccessFlags = 0; - output_buffer_desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; - output_buffer_desc.StructureByteStride = sizeof(float); - ThrowIfFailed(d3d.device->CreateBuffer(&output_buffer_desc, nullptr, d3d.depth_value_output_buffer.GetAddressOf())); - - D3D11_UNORDERED_ACCESS_VIEW_DESC output_buffer_uav_desc; - output_buffer_uav_desc.Format = DXGI_FORMAT_UNKNOWN; - output_buffer_uav_desc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; - output_buffer_uav_desc.Buffer.FirstElement = 0; - output_buffer_uav_desc.Buffer.NumElements = 1; - output_buffer_uav_desc.Buffer.Flags = 0; - ThrowIfFailed(d3d.device->CreateUnorderedAccessView(d3d.depth_value_output_buffer.Get(), &output_buffer_uav_desc, d3d.depth_value_output_uav.GetAddressOf())); - - output_buffer_desc.Usage = D3D11_USAGE_STAGING; - output_buffer_desc.BindFlags = 0; - output_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; - ThrowIfFailed(d3d.device->CreateBuffer(&output_buffer_desc, nullptr, d3d.depth_value_output_buffer_copy.GetAddressOf())); - const char* shader_source = R"( sampler my_sampler : register(s0); Texture2D tex : register(t0); -cbuffer coordCB : register(b0) { - float2 coord; -} - +StructuredBuffer coord : register(t1); RWStructuredBuffer output : register(u0); [numthreads(1, 1, 1)] void CSMain(uint3 DTid : SV_DispatchThreadID) { - output[0] = tex.SampleLevel(my_sampler, coord, 0); + output[DTid.x] = tex.Load(int3(coord[DTid.x], 0)); } )"; + + const char* shader_source_msaa = R"( +sampler my_sampler : register(s0); +Texture2DMS tex : register(t0); +StructuredBuffer coord : register(t1); +RWStructuredBuffer output : register(u0); + +[numthreads(1, 1, 1)] +void CSMain(uint3 DTid : SV_DispatchThreadID) { + output[DTid.x] = tex.Load(coord[DTid.x], 0); +} +)"; + #if DEBUG_D3D UINT compile_flags = D3DCOMPILE_DEBUG; #else @@ -426,7 +350,9 @@ void CSMain(uint3 DTid : SV_DispatchThreadID) { #endif ComPtr cs, error_blob; - HRESULT hr = d3d.D3DCompile(shader_source, strlen(shader_source), nullptr, nullptr, nullptr, "CSMain", "cs_4_0", compile_flags, 0, cs.GetAddressOf(), error_blob.GetAddressOf()); + HRESULT hr; + + hr = d3d.D3DCompile(shader_source, strlen(shader_source), nullptr, nullptr, nullptr, "CSMain", "cs_4_0", compile_flags, 0, cs.GetAddressOf(), error_blob.GetAddressOf()); if (FAILED(hr)) { char* err = (char*)error_blob->GetBufferPointer(); @@ -436,6 +362,14 @@ void CSMain(uint3 DTid : SV_DispatchThreadID) { ThrowIfFailed(d3d.device->CreateComputeShader(cs->GetBufferPointer(), cs->GetBufferSize(), nullptr, d3d.compute_shader.GetAddressOf())); + hr = d3d.D3DCompile(shader_source_msaa, strlen(shader_source_msaa), nullptr, nullptr, nullptr, "CSMain", "cs_4_1", compile_flags, 0, d3d.compute_shader_msaa_blob.GetAddressOf(), error_blob.ReleaseAndGetAddressOf()); + + if (FAILED(hr)) { + char* err = (char*)error_blob->GetBufferPointer(); + MessageBoxA(gfx_dxgi_get_h_wnd(), err, "Error", MB_OK | MB_ICONERROR); + throw hr; + } + // Create ImGui SohImGui::WindowImpl window_impl; @@ -445,8 +379,8 @@ void CSMain(uint3 DTid : SV_DispatchThreadID) { } -static bool gfx_d3d11_z_is_from_0_to_1(void) { - return true; +static struct GfxClipParameters gfx_d3d11_get_clip_parameters(void) { + return { true, false }; } static void gfx_d3d11_unload_shader(struct ShaderProgram *old_prg) { @@ -531,8 +465,8 @@ static struct ShaderProgram *gfx_d3d11_create_and_load_new_shader(uint64_t shade blend_desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; blend_desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; blend_desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; - blend_desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; - blend_desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; + blend_desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO; + blend_desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE; // We initially clear alpha to 1.0f and want to keep it at 1.0f blend_desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; blend_desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; } else { @@ -592,6 +526,10 @@ static D3D11_TEXTURE_ADDRESS_MODE gfx_cm_to_d3d11(uint32_t val) { static void gfx_d3d11_upload_texture(const uint8_t *rgba32_buf, uint32_t width, uint32_t height) { // Create texture + TextureData *texture_data = &d3d.textures[d3d.current_texture_ids[d3d.current_tile]]; + texture_data->width = width; + texture_data->height = height; + D3D11_TEXTURE2D_DESC texture_desc; ZeroMemory(&texture_desc, sizeof(D3D11_TEXTURE2D_DESC)); @@ -612,21 +550,11 @@ static void gfx_d3d11_upload_texture(const uint8_t *rgba32_buf, uint32_t width, resource_data.SysMemPitch = width * 4; resource_data.SysMemSlicePitch = resource_data.SysMemPitch * height; - ComPtr texture; - ThrowIfFailed(d3d.device->CreateTexture2D(&texture_desc, &resource_data, texture.GetAddressOf())); - - TextureData *texture_data = &d3d.textures[d3d.current_texture_ids[d3d.current_tile]]; - texture_data->width = width; - texture_data->height = height; + ThrowIfFailed(d3d.device->CreateTexture2D(&texture_desc, &resource_data, texture_data->texture.ReleaseAndGetAddressOf())); // Create shader resource view from texture - if (texture_data->resource_view.Get() != nullptr) { - // Free the previous texture in this slot - texture_data->resource_view.Reset(); - } - - ThrowIfFailed(d3d.device->CreateShaderResourceView(texture.Get(), nullptr, texture_data->resource_view.GetAddressOf())); + ThrowIfFailed(d3d.device->CreateShaderResourceView(texture_data->texture.Get(), nullptr, texture_data->resource_view.ReleaseAndGetAddressOf())); } static void gfx_d3d11_set_sampler_parameters(int tile, bool linear_filter, uint32_t cms, uint32_t cmt) { @@ -803,20 +731,10 @@ static void gfx_d3d11_draw_triangles(float buf_vbo[], size_t buf_vbo_len, size_t } static void gfx_d3d11_on_resize(void) { - create_render_target_views(true); + //create_render_target_views(true); } static void gfx_d3d11_start_frame(void) { - // Set render targets - - d3d.context->OMSetRenderTargets(1, d3d.backbuffer_view.GetAddressOf(), d3d.depth_stencil_view.Get()); - - // Clear render targets - - const float clearColor[] = { 0.0f, 0.0f, 0.0f, 1.0f }; - d3d.context->ClearRenderTargetView(d3d.backbuffer_view.Get(), clearColor); - d3d.context->ClearDepthStencilView(d3d.depth_stencil_view.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0); - // Set per-frame constant buffer d3d.per_frame_cb_data.noise_frame++; @@ -824,22 +742,9 @@ static void gfx_d3d11_start_frame(void) { // No high values, as noise starts to look ugly d3d.per_frame_cb_data.noise_frame = 0; } - float aspect_ratio = (float) d3d.current_width / (float) d3d.current_height; - d3d.render_target_height = d3d.current_height; - d3d.per_frame_cb_data.noise_scale_x = 120 * aspect_ratio; // 120 = N64 height resolution (240) / 2 - d3d.per_frame_cb_data.noise_scale_y = 120; - - D3D11_MAPPED_SUBRESOURCE ms; - ZeroMemory(&ms, sizeof(D3D11_MAPPED_SUBRESOURCE)); - d3d.context->Map(d3d.per_frame_cb.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &ms); - memcpy(ms.pData, &d3d.per_frame_cb_data, sizeof(PerFrameCB)); - d3d.context->Unmap(d3d.per_frame_cb.Get(), 0); - - d3d.copied_depth_buffer = false; } static void gfx_d3d11_end_frame(void) { - SohImGui::Draw(); d3d.context->Flush(); } @@ -847,43 +752,13 @@ static void gfx_d3d11_finish_render(void) { d3d.context->Flush(); } -void gfx_d3d11_resize_framebuffer(int fb, uint32_t width, uint32_t height) { - FramebufferData& fd = d3d.framebuffers[fb]; - TextureData& td = d3d.textures[fd.texture_id]; - - ComPtr texture, depth_stencil_texture; - - D3D11_TEXTURE2D_DESC texture_desc; - texture_desc.Width = width; - texture_desc.Height = height; - texture_desc.Usage = D3D11_USAGE_DEFAULT; - texture_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET; - texture_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - texture_desc.CPUAccessFlags = 0; - texture_desc.MiscFlags = 0; - texture_desc.ArraySize = 1; - texture_desc.MipLevels = 1; - texture_desc.SampleDesc.Count = 1; - texture_desc.SampleDesc.Quality = 0; - - ThrowIfFailed(d3d.device->CreateTexture2D(&texture_desc, nullptr, texture.GetAddressOf())); - create_depth_stencil_objects(width, height, depth_stencil_texture.GetAddressOf(), fd.depth_stencil_view.ReleaseAndGetAddressOf(), nullptr); - ThrowIfFailed(d3d.device->CreateRenderTargetView(texture.Get(), nullptr, fd.render_target_view.ReleaseAndGetAddressOf())); - ThrowIfFailed(d3d.device->CreateShaderResourceView(texture.Get(), nullptr, td.resource_view.ReleaseAndGetAddressOf())); - - td.width = width; - td.height = height; -} - -int gfx_d3d11_create_framebuffer(uint32_t width, uint32_t height) { +int gfx_d3d11_create_framebuffer(void) { uint32_t texture_id = gfx_d3d11_new_texture(); TextureData& t = d3d.textures[texture_id]; - t.width = width; - t.height = height; size_t index = d3d.framebuffers.size(); d3d.framebuffers.resize(d3d.framebuffers.size() + 1); - FramebufferData& data = d3d.framebuffers.back(); + Framebuffer& data = d3d.framebuffers.back(); data.texture_id = texture_id; uint32_t tile = 0; @@ -892,24 +767,109 @@ int gfx_d3d11_create_framebuffer(uint32_t width, uint32_t height) { gfx_d3d11_set_sampler_parameters(0, true, G_TX_WRAP, G_TX_WRAP); d3d.current_texture_ids[tile] = saved; - gfx_d3d11_resize_framebuffer(index, width, height); - return (int)index; } -void gfx_d3d11_set_framebuffer(int fb) { - d3d.render_target_height = d3d.textures[d3d.framebuffers[fb].texture_id].height; +static void gfx_d3d11_update_framebuffer_parameters(int fb_id, uint32_t width, uint32_t height, uint32_t msaa_level, bool opengl_invert_y, bool render_target, bool has_depth_buffer, bool can_extract_depth) { + Framebuffer& fb = d3d.framebuffers[fb_id]; + TextureData& tex = d3d.textures[fb.texture_id]; - d3d.context->OMSetRenderTargets(1, d3d.framebuffers[fb].render_target_view.GetAddressOf(), d3d.framebuffers[fb].depth_stencil_view.Get()); + width = max(width, 1U); + height = max(height, 1U); + while (msaa_level > 1 && d3d.msaa_num_quality_levels[msaa_level - 1] == 0) { + --msaa_level; + } - const float clearColor[] = { 0.0f, 0.0f, 0.0f, 1.0f }; - d3d.context->ClearRenderTargetView(d3d.framebuffers[fb].render_target_view.Get(), clearColor); - d3d.context->ClearDepthStencilView(d3d.framebuffers[fb].depth_stencil_view.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0); + bool diff = tex.width != width || tex.height != height || fb.msaa_level != msaa_level; + + if (diff || (fb.render_target_view.Get() != nullptr) != render_target) { + if (fb_id != 0) { + D3D11_TEXTURE2D_DESC texture_desc; + texture_desc.Width = width; + texture_desc.Height = height; + texture_desc.Usage = D3D11_USAGE_DEFAULT; + texture_desc.BindFlags = (msaa_level <= 1 ? D3D11_BIND_SHADER_RESOURCE : 0) | (render_target ? D3D11_BIND_RENDER_TARGET : 0); + texture_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + texture_desc.CPUAccessFlags = 0; + texture_desc.MiscFlags = 0; + texture_desc.ArraySize = 1; + texture_desc.MipLevels = 1; + texture_desc.SampleDesc.Count = msaa_level; + texture_desc.SampleDesc.Quality = 0; + + ThrowIfFailed(d3d.device->CreateTexture2D(&texture_desc, nullptr, tex.texture.ReleaseAndGetAddressOf())); + + if (msaa_level <= 1) { + ThrowIfFailed(d3d.device->CreateShaderResourceView(tex.texture.Get(), nullptr, tex.resource_view.ReleaseAndGetAddressOf())); + } + } else if (diff) { + DXGI_SWAP_CHAIN_DESC1 desc1; + ThrowIfFailed(d3d.swap_chain->GetDesc1(&desc1)); + if (desc1.Width != width || desc1.Height != height) { + fb.render_target_view.Reset(); + tex.texture.Reset(); + ThrowIfFailed(d3d.swap_chain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, desc1.Flags)); + } + ThrowIfFailed(d3d.swap_chain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID *)tex.texture.ReleaseAndGetAddressOf())); + } + if (render_target) { + ThrowIfFailed(d3d.device->CreateRenderTargetView(tex.texture.Get(), nullptr, fb.render_target_view.ReleaseAndGetAddressOf())); + } + + tex.width = width; + tex.height = height; + } + + if (has_depth_buffer && (diff || !fb.has_depth_buffer || (fb.depth_stencil_srv.Get() != nullptr) != can_extract_depth)) { + fb.depth_stencil_srv.Reset(); + create_depth_stencil_objects(width, height, msaa_level, fb.depth_stencil_view.ReleaseAndGetAddressOf(), can_extract_depth ? fb.depth_stencil_srv.GetAddressOf() : nullptr); + } + if (!has_depth_buffer) { + fb.depth_stencil_view.Reset(); + fb.depth_stencil_srv.Reset(); + } + + fb.has_depth_buffer = has_depth_buffer; + fb.msaa_level = msaa_level; } -void gfx_d3d11_reset_framebuffer(void) { - d3d.render_target_height = d3d.current_height; - d3d.context->OMSetRenderTargets(1, d3d.backbuffer_view.GetAddressOf(), d3d.depth_stencil_view.Get()); +void gfx_d3d11_start_draw_to_framebuffer(int fb_id, float noise_scale) { + Framebuffer& fb = d3d.framebuffers[fb_id]; + d3d.render_target_height = d3d.textures[fb.texture_id].height; + + d3d.context->OMSetRenderTargets(1, fb.render_target_view.GetAddressOf(), fb.has_depth_buffer ? fb.depth_stencil_view.Get() : nullptr); + + d3d.current_framebuffer = fb_id; + + if (noise_scale != 0.0f) { + d3d.per_frame_cb_data.noise_scale = 1.0f / noise_scale; + } + + D3D11_MAPPED_SUBRESOURCE ms; + ZeroMemory(&ms, sizeof(D3D11_MAPPED_SUBRESOURCE)); + d3d.context->Map(d3d.per_frame_cb.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &ms); + memcpy(ms.pData, &d3d.per_frame_cb_data, sizeof(PerFrameCB)); + d3d.context->Unmap(d3d.per_frame_cb.Get(), 0); +} + +void gfx_d3d11_clear_framebuffer(void) { + Framebuffer& fb = d3d.framebuffers[d3d.current_framebuffer]; + const float clearColor[] = { 0.0f, 0.0f, 0.0f, 1.0f }; + d3d.context->ClearRenderTargetView(fb.render_target_view.Get(), clearColor); + if (fb.has_depth_buffer) { + d3d.context->ClearDepthStencilView(fb.depth_stencil_view.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0); + } +} + +void gfx_d3d11_resolve_msaa_color_buffer(int fb_id_target, int fb_id_source) { + Framebuffer& fb_dst = d3d.framebuffers[fb_id_target]; + Framebuffer& fb_src = d3d.framebuffers[fb_id_source]; + + d3d.context->ResolveSubresource(d3d.textures[fb_dst.texture_id].texture.Get(), 0, d3d.textures[fb_src.texture_id].texture.Get(), 0, DXGI_FORMAT_R8G8B8A8_UNORM); +} + +void *gfx_d3d11_get_framebuffer_texture_id(int fb_id) { + return (void *)d3d.textures[d3d.framebuffers[fb_id].texture_id].resource_view.Get(); } void gfx_d3d11_select_texture_fb(int fbID) { @@ -917,62 +877,107 @@ void gfx_d3d11_select_texture_fb(int fbID) { gfx_d3d11_select_texture(tile, d3d.framebuffers[fbID].texture_id); } -uint16_t gfx_d3d11_get_pixel_depth(float x, float y) { +std::map, uint16_t> gfx_d3d11_get_pixel_depth(int fb_id, const std::set>& coordinates) { + Framebuffer& fb = d3d.framebuffers[fb_id]; + TextureData& td = d3d.textures[fb.texture_id]; + + if (coordinates.size() > d3d.coord_buffer_size) { + d3d.coord_buffer.Reset(); + d3d.coord_buffer_srv.Reset(); + d3d.depth_value_output_buffer.Reset(); + d3d.depth_value_output_uav.Reset(); + d3d.depth_value_output_buffer_copy.Reset(); + + D3D11_BUFFER_DESC coord_buf_desc; + coord_buf_desc.Usage = D3D11_USAGE_DYNAMIC; + coord_buf_desc.ByteWidth = sizeof(Coord) * coordinates.size(); + coord_buf_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + coord_buf_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + coord_buf_desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; + coord_buf_desc.StructureByteStride = sizeof(Coord); + + ThrowIfFailed(d3d.device->CreateBuffer(&coord_buf_desc, nullptr, d3d.coord_buffer.GetAddressOf())); + + D3D11_SHADER_RESOURCE_VIEW_DESC coord_buf_srv_desc; + coord_buf_srv_desc.Format = DXGI_FORMAT_UNKNOWN; + coord_buf_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; + coord_buf_srv_desc.Buffer.FirstElement = 0; + coord_buf_srv_desc.Buffer.NumElements = coordinates.size(); + + ThrowIfFailed(d3d.device->CreateShaderResourceView(d3d.coord_buffer.Get(), &coord_buf_srv_desc, d3d.coord_buffer_srv.GetAddressOf())); + + D3D11_BUFFER_DESC output_buffer_desc; + output_buffer_desc.Usage = D3D11_USAGE_DEFAULT; + output_buffer_desc.ByteWidth = sizeof(float) * coordinates.size(); + output_buffer_desc.BindFlags = D3D11_BIND_UNORDERED_ACCESS; + output_buffer_desc.CPUAccessFlags = 0; + output_buffer_desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; + output_buffer_desc.StructureByteStride = sizeof(float); + ThrowIfFailed(d3d.device->CreateBuffer(&output_buffer_desc, nullptr, d3d.depth_value_output_buffer.GetAddressOf())); + + D3D11_UNORDERED_ACCESS_VIEW_DESC output_buffer_uav_desc; + output_buffer_uav_desc.Format = DXGI_FORMAT_UNKNOWN; + output_buffer_uav_desc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; + output_buffer_uav_desc.Buffer.FirstElement = 0; + output_buffer_uav_desc.Buffer.NumElements = coordinates.size(); + output_buffer_uav_desc.Buffer.Flags = 0; + ThrowIfFailed(d3d.device->CreateUnorderedAccessView(d3d.depth_value_output_buffer.Get(), &output_buffer_uav_desc, d3d.depth_value_output_uav.GetAddressOf())); + + output_buffer_desc.Usage = D3D11_USAGE_STAGING; + output_buffer_desc.BindFlags = 0; + output_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + ThrowIfFailed(d3d.device->CreateBuffer(&output_buffer_desc, nullptr, d3d.depth_value_output_buffer_copy.GetAddressOf())); + + d3d.coord_buffer_size = coordinates.size(); + } + D3D11_MAPPED_SUBRESOURCE ms; + if (fb.msaa_level > 1 && d3d.compute_shader_msaa.Get() == nullptr) { + ThrowIfFailed(d3d.device->CreateComputeShader(d3d.compute_shader_msaa_blob->GetBufferPointer(), d3d.compute_shader_msaa_blob->GetBufferSize(), nullptr, d3d.compute_shader_msaa.GetAddressOf())); + } + // ImGui overwrites these values, so we cannot set them once at init - d3d.context->CSSetShader(d3d.compute_shader.Get(), nullptr, 0); - d3d.context->CSSetConstantBuffers(0, 1, d3d.coord_buffer.GetAddressOf()); - d3d.context->CSSetSamplers(0, 1, d3d.depth_value_sampler.GetAddressOf()); + d3d.context->CSSetShader(fb.msaa_level > 1 ? d3d.compute_shader_msaa.Get() : d3d.compute_shader.Get(), nullptr, 0); d3d.context->CSSetUnorderedAccessViews(0, 1, d3d.depth_value_output_uav.GetAddressOf(), nullptr); ThrowIfFailed(d3d.context->Map(d3d.coord_buffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &ms)); - CoordCB* coord_cb = (CoordCB*)ms.pData; - coord_cb->x = x / d3d.current_width; - // We invert y because the game assumes OpenGL coordinates (bottom-left corner is origin), while DX's origin is top-left corner - coord_cb->y = 1 - y / d3d.current_height; + Coord *coord_cb = (Coord *)ms.pData; + { + size_t i = 0; + for (const auto& coord : coordinates) { + coord_cb[i].x = coord.first; + // We invert y because the gfx_pc assumes OpenGL coordinates (bottom-left corner is origin), while DX's origin is top-left corner + coord_cb[i].y = td.height - 1 - coord.second; + ++i; + } + } d3d.context->Unmap(d3d.coord_buffer.Get(), 0); - // The depth stencil texture can only have one mapping at a time, so temporarily unbind from the OM - d3d.context->OMSetRenderTargets(1, d3d.backbuffer_view.GetAddressOf(), nullptr); - d3d.context->CSSetShaderResources(0, 1, d3d.depth_stencil_srv.GetAddressOf()); + // The depth stencil texture can only have one mapping at a time, so unbind from the OM + ID3D11RenderTargetView* null_arr1[1] = { nullptr }; + d3d.context->OMSetRenderTargets(1, null_arr1, nullptr); - d3d.context->Dispatch(1, 1, 1); + ID3D11ShaderResourceView *srvs[] = { fb.depth_stencil_srv.Get(), d3d.coord_buffer_srv.Get() }; + d3d.context->CSSetShaderResources(0, 2, srvs); + + d3d.context->Dispatch(coordinates.size(), 1, 1); d3d.context->CopyResource(d3d.depth_value_output_buffer_copy.Get(), d3d.depth_value_output_buffer.Get()); ThrowIfFailed(d3d.context->Map(d3d.depth_value_output_buffer_copy.Get(), 0, D3D11_MAP_READ, 0, &ms)); - float res = *(float *)ms.pData; - d3d.context->Unmap(d3d.depth_value_output_buffer_copy.Get(), 0); - - ID3D11ShaderResourceView *null_arr[1] = { nullptr }; - d3d.context->CSSetShaderResources(0, 1, null_arr); - d3d.context->OMSetRenderTargets(1, d3d.backbuffer_view.GetAddressOf(), d3d.depth_stencil_view.Get()); - - return res * 65532.0f; -} - -uint16_t gfx_d3d11_get_pixel_depth_old(float x, float y) { - // This approach, compared to using a compute shader, might have better performance on nvidia cards - - if (!d3d.copied_depth_buffer) { - d3d.context->CopyResource(d3d.depth_stencil_copy_texture.Get(), d3d.depth_stencil_texture.Get()); - d3d.copied_depth_buffer = true; - } - - D3D11_MAPPED_SUBRESOURCE mapping_desc; - d3d.context->Map(d3d.depth_stencil_copy_texture.Get(), 0, D3D11_MAP_READ, 0, &mapping_desc); - float res = 0; - if (mapping_desc.pData != nullptr) { - float *addr = (float *)mapping_desc.pData; - uint32_t num_pixels = mapping_desc.DepthPitch / sizeof(float); - uint32_t width = mapping_desc.RowPitch / sizeof(float); - uint32_t height = width == 0 ? 0 : num_pixels / width; - if (x >= 0 && x < width && y >= 0 && y < height) { - res = addr[width * (height - 1 - (int)y) + (int)x]; + std::map, uint16_t> res; + { + size_t i = 0; + for (const auto& coord : coordinates) { + res.emplace(coord, ((float *)ms.pData)[i++] * 65532.0f); } } - d3d.context->Unmap(d3d.depth_stencil_copy_texture.Get(), 0); - return res * 65532.0f; + d3d.context->Unmap(d3d.depth_value_output_buffer_copy.Get(), 0); + + ID3D11ShaderResourceView *null_arr[2] = { nullptr, nullptr }; + d3d.context->CSSetShaderResources(0, 2, null_arr); + + return res; } } // namespace @@ -982,7 +987,7 @@ ImTextureID SohImGui::GetTextureByID(int id) { } struct GfxRenderingAPI gfx_direct3d11_api = { - gfx_d3d11_z_is_from_0_to_1, + gfx_d3d11_get_clip_parameters, gfx_d3d11_unload_shader, gfx_d3d11_load_shader, gfx_d3d11_create_and_load_new_shader, @@ -993,7 +998,6 @@ struct GfxRenderingAPI gfx_direct3d11_api = { gfx_d3d11_upload_texture, gfx_d3d11_set_sampler_parameters, gfx_d3d11_set_depth_test_and_mask, - gfx_d3d11_get_pixel_depth, gfx_d3d11_set_zmode_decal, gfx_d3d11_set_viewport, gfx_d3d11_set_scissor, @@ -1005,9 +1009,12 @@ struct GfxRenderingAPI gfx_direct3d11_api = { gfx_d3d11_end_frame, gfx_d3d11_finish_render, gfx_d3d11_create_framebuffer, - gfx_d3d11_resize_framebuffer, - gfx_d3d11_set_framebuffer, - gfx_d3d11_reset_framebuffer, + gfx_d3d11_update_framebuffer_parameters, + gfx_d3d11_start_draw_to_framebuffer, + gfx_d3d11_clear_framebuffer, + gfx_d3d11_resolve_msaa_color_buffer, + gfx_d3d11_get_pixel_depth, + gfx_d3d11_get_framebuffer_texture_id, gfx_d3d11_select_texture_fb, gfx_d3d11_delete_texture }; diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_direct3d_common.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_direct3d_common.cpp index ca60aef8b..f778812d1 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_direct3d_common.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_direct3d_common.cpp @@ -134,9 +134,6 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f } } } - if (cc_features.opt_alpha && cc_features.opt_noise) { - append_line(buf, &len, " float4 screenPos : TEXCOORD2;"); - } if (cc_features.opt_fog) { append_line(buf, &len, " float4 fog : FOG;"); num_floats += 4; @@ -163,7 +160,7 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f if (cc_features.opt_alpha && cc_features.opt_noise) { append_line(buf, &len, "cbuffer PerFrameCB : register(b0) {"); append_line(buf, &len, " uint noise_frame;"); - append_line(buf, &len, " float2 noise_scale;"); + append_line(buf, &len, " float noise_scale;"); append_line(buf, &len, "}"); append_line(buf, &len, "float random(in float3 value) {"); @@ -217,9 +214,6 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f append_line(buf, &len, ") {"); append_line(buf, &len, " PSInput result;"); append_line(buf, &len, " result.position = position;"); - if (cc_features.opt_alpha && cc_features.opt_noise) { - append_line(buf, &len, " result.screenPos = position;"); - } for (int i = 0; i < 2; i++) { if (cc_features.used_textures[i]) { len += sprintf(buf + len, " result.uv%d = uv%d;\r\n", i, i); @@ -244,7 +238,11 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f if (include_root_signature) { append_line(buf, &len, "[RootSignature(RS)]"); } - append_line(buf, &len, "float4 PSMain(PSInput input) : SV_TARGET {"); + if (cc_features.opt_alpha && cc_features.opt_noise) { + append_line(buf, &len, "float4 PSMain(PSInput input, float4 screenSpace : SV_Position) : SV_TARGET {"); + } else { + append_line(buf, &len, "float4 PSMain(PSInput input) : SV_TARGET {"); + } for (int i = 0; i < 2; i++) { if (cc_features.used_textures[i]) { len += sprintf(buf + len, " float2 tc%d = input.uv%d;\r\n", i, i); @@ -301,7 +299,7 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f } if (cc_features.opt_alpha && cc_features.opt_noise) { - append_line(buf, &len, " float2 coords = (input.screenPos.xy / input.screenPos.w) * noise_scale;"); + append_line(buf, &len, " float2 coords = screenSpace.xy * noise_scale;"); append_line(buf, &len, " texel.a *= round(saturate(random(float3(floor(coords), noise_frame)) + texel.a - 0.5));"); } diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_dxgi.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_dxgi.cpp index 6cfc3cf32..49abe0b28 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_dxgi.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_dxgi.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -61,6 +62,7 @@ static struct { RECT last_window_rect; bool is_full_screen, last_maximized_state; + bool dxgi1_4; ComPtr factory; ComPtr swap_chain; HANDLE waitable_object; @@ -197,17 +199,6 @@ static void toggle_borderless_window_full_screen(bool enable, bool call_callback } } -static void gfx_dxgi_on_resize(void) { - if (dxgi.swap_chain.Get() != nullptr) { - gfx_get_current_rendering_api()->on_resize(); - - DXGI_SWAP_CHAIN_DESC1 desc1; - ThrowIfFailed(dxgi.swap_chain->GetDesc1(&desc1)); - dxgi.current_width = desc1.Width; - dxgi.current_height = desc1.Height; - } -} - static void onkeydown(WPARAM w_param, LPARAM l_param) { int key = ((l_param >> 16) & 0x1ff); if (dxgi.on_key_down != nullptr) { @@ -227,7 +218,8 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par SohImGui::Update(event_impl); switch (message) { case WM_SIZE: - gfx_dxgi_on_resize(); + dxgi.current_width = (uint32_t)(l_param & 0xffff); + dxgi.current_height = (uint32_t)(l_param >> 16); break; case WM_DESTROY: exit(0); @@ -573,6 +565,13 @@ void gfx_dxgi_create_factory_and_device(bool debug, int d3d_version, bool (*crea ThrowIfFailed(dxgi.CreateDXGIFactory1(__uuidof(IDXGIFactory2), &dxgi.factory)); } + { + ComPtr factory4; + if (dxgi.factory->QueryInterface(__uuidof(IDXGIFactory4), &factory4) == S_OK) { + dxgi.dxgi1_4 = true; + } + } + ComPtr adapter; for (UINT i = 0; dxgi.factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND; i++) { DXGI_ADAPTER_DESC1 desc; @@ -604,7 +603,9 @@ ComPtr gfx_dxgi_create_swap_chain(IUnknown *device) { swap_chain_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swap_chain_desc.Scaling = win8 ? DXGI_SCALING_NONE : DXGI_SCALING_STRETCH; - swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // Apparently this was backported to Win 7 Platform Update + swap_chain_desc.SwapEffect = dxgi.dxgi1_4 ? + DXGI_SWAP_EFFECT_FLIP_DISCARD : // Introduced in DXGI 1.4 and Windows 10 + DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // Apparently flip sequential was also backported to Win 7 Platform Update swap_chain_desc.Flags = dxgi_13 ? DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT : 0; swap_chain_desc.SampleDesc.Count = 1; diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_opengl.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_opengl.cpp index 16e750d14..5373f8799 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_opengl.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_opengl.cpp @@ -52,19 +52,33 @@ struct ShaderProgram { uint8_t num_attribs; bool used_noise; GLint frame_count_location; - GLint window_height_location; + GLint noise_scale_location; +}; + +struct Framebuffer { + uint32_t width, height; + bool has_depth_buffer; + uint32_t msaa_level; + bool invert_y; + + GLuint fbo, clrbuf, clrbuf_msaa, rbo; }; static map, struct ShaderProgram> shader_program_pool; static GLuint opengl_vbo; - -static uint32_t frame_count; -static uint32_t current_height; -static map> fb2tex; static bool current_depth_mask; -static bool gfx_opengl_z_is_from_0_to_1(void) { - return false; +static uint32_t frame_count; + +static vector framebuffers; +static size_t current_framebuffer; +static float current_noise_scale; + +GLuint pixel_depth_rb, pixel_depth_fb; +size_t pixel_depth_rb_size; + +static struct GfxClipParameters gfx_opengl_get_clip_parameters(void) { + return { false, framebuffers[current_framebuffer].invert_y }; } static void gfx_opengl_vertex_array_set_attribs(struct ShaderProgram *prg) { @@ -81,7 +95,7 @@ static void gfx_opengl_vertex_array_set_attribs(struct ShaderProgram *prg) { static void gfx_opengl_set_uniforms(struct ShaderProgram *prg) { if (prg->used_noise) { glUniform1i(prg->frame_count_location, frame_count); - glUniform1i(prg->window_height_location, current_height); + glUniform1f(prg->noise_scale_location, current_noise_scale); } } @@ -277,7 +291,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad if (cc_features.opt_alpha && cc_features.opt_noise) { append_line(fs_buf, &fs_len, "uniform int frame_count;"); - append_line(fs_buf, &fs_len, "uniform int window_height;"); + append_line(fs_buf, &fs_len, "uniform float noise_scale;"); append_line(fs_buf, &fs_len, "float random(in vec3 value) {"); append_line(fs_buf, &fs_len, " float random = dot(sin(value), vec3(12.9898, 78.233, 37.719));"); @@ -338,7 +352,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad } if (cc_features.opt_alpha && cc_features.opt_noise) { - append_line(fs_buf, &fs_len, "texel.a *= floor(clamp(random(vec3(floor(gl_FragCoord.xy * (240.0 / float(window_height))), float(frame_count))) + texel.a, 0.0, 1.0));"); + append_line(fs_buf, &fs_len, "texel.a *= floor(clamp(random(vec3(floor(gl_FragCoord.xy * noise_scale), float(frame_count))) + texel.a, 0.0, 1.0));"); } if (cc_features.opt_alpha) { @@ -460,7 +474,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad if (cc_features.opt_alpha && cc_features.opt_noise) { prg->frame_count_location = glGetUniformLocation(shader_program, "frame_count"); - prg->window_height_location = glGetUniformLocation(shader_program, "window_height"); + prg->noise_scale_location = glGetUniformLocation(shader_program, "noise_scale"); prg->used_noise = true; } else { prg->used_noise = false; @@ -496,7 +510,7 @@ static void gfx_opengl_select_texture(int tile, GLuint texture_id) { } static void gfx_opengl_upload_texture(const uint8_t *rgba32_buf, uint32_t width, uint32_t height) { - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba32_buf); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba32_buf); } static uint32_t gfx_cm_to_opengl(uint32_t val) { @@ -543,7 +557,6 @@ static void gfx_opengl_set_zmode_decal(bool zmode_decal) { static void gfx_opengl_set_viewport(int x, int y, int width, int height) { glViewport(x, y, width, height); - current_height = height; } static void gfx_opengl_set_scissor(int x, int y, int width, int height) { @@ -564,10 +577,6 @@ static void gfx_opengl_draw_triangles(float buf_vbo[], size_t buf_vbo_len, size_ glDrawArrays(GL_TRIANGLES, 0, 3 * buf_vbo_num_tris); } -static unsigned int framebuffer; -static unsigned int textureColorbuffer; -static unsigned int rbo; - static void gfx_opengl_init(void) { //#if FOR_WINDOWS glewInit(); @@ -576,143 +585,214 @@ static void gfx_opengl_init(void) { glGenBuffers(1, &opengl_vbo); glBindBuffer(GL_ARRAY_BUFFER, opengl_vbo); - glGenFramebuffers(1, &framebuffer); - glGenTextures(1, &textureColorbuffer); - glGenRenderbuffers(1, &rbo); - - SohUtils::saveEnvironmentVar("framebuffer", std::to_string(textureColorbuffer)); glDepthFunc(GL_LEQUAL); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + framebuffers.resize(1); // for the default screen buffer + + glGenRenderbuffers(1, &pixel_depth_rb); + glBindRenderbuffer(GL_RENDERBUFFER, pixel_depth_rb); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 1, 1); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + + glGenFramebuffers(1, &pixel_depth_fb); + glBindFramebuffer(GL_FRAMEBUFFER, pixel_depth_fb); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, pixel_depth_rb); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + pixel_depth_rb_size = 1; } static void gfx_opengl_on_resize(void) { } static void gfx_opengl_start_frame(void) { - GLsizei framebuffer_width = gfx_current_dimensions.width; - GLsizei framebuffer_height = gfx_current_dimensions.height; - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); - std::shared_ptr wnd = Ship::GlobalCtx2::GetInstance()->GetWindow(); - glBindTexture(GL_TEXTURE_2D, textureColorbuffer); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, framebuffer_width, framebuffer_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureColorbuffer, 0); - glBindRenderbuffer(GL_RENDERBUFFER, rbo); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, framebuffer_width, framebuffer_height); // use a single renderbuffer object for both a depth AND stencil buffer. - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); // now actually attach it - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); - frame_count++; - - glDisable(GL_SCISSOR_TEST); - glDepthMask(GL_TRUE); - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glEnable(GL_SCISSOR_TEST); - glEnable(GL_DEPTH_CLAMP); - current_depth_mask = true; } static void gfx_opengl_end_frame(void) { - - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glClearColor(1.0f, 1.0f, 1.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - GLint last_program; - glGetIntegerv(GL_CURRENT_PROGRAM, &last_program); - glUseProgram(0); - SohImGui::Draw(); - glUseProgram(last_program); + glFlush(); } static void gfx_opengl_finish_render(void) { } -static int gfx_opengl_create_framebuffer(uint32_t width, uint32_t height) { - GLuint textureColorbuffer; - - glGenTextures(1, &textureColorbuffer); - glBindTexture(GL_TEXTURE_2D, textureColorbuffer); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); +static int gfx_opengl_create_framebuffer() { + GLuint clrbuf; + glGenTextures(1, &clrbuf); + glBindTexture(GL_TEXTURE_2D, clrbuf); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glBindTexture(GL_TEXTURE_2D, 0); + GLuint clrbuf_msaa; + glGenRenderbuffers(1, &clrbuf_msaa); + GLuint rbo; glGenRenderbuffers(1, &rbo); glBindRenderbuffer(GL_RENDERBUFFER, rbo); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 1, 1); glBindRenderbuffer(GL_RENDERBUFFER, 0); GLuint fbo; glGenFramebuffers(1, &fbo); - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureColorbuffer, 0); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); - glBindFramebuffer(GL_FRAMEBUFFER, fbo); + size_t i = framebuffers.size(); + framebuffers.resize(i + 1); - fb2tex[fbo] = make_pair(textureColorbuffer, rbo); - - //glBindFramebuffer(GL_FRAMEBUFFER, 0); + framebuffers[i].fbo = fbo; + framebuffers[i].clrbuf = clrbuf; + framebuffers[i].clrbuf_msaa = clrbuf_msaa; + framebuffers[i].rbo = rbo; return fbo; } -static void gfx_opengl_resize_framebuffer(int fb, uint32_t width, uint32_t height) { - glBindTexture(GL_TEXTURE_2D, fb2tex[fb].first); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); - glBindTexture(GL_TEXTURE_2D, 0); +static void gfx_opengl_update_framebuffer_parameters(int fb_id, uint32_t width, uint32_t height, uint32_t msaa_level, bool opengl_invert_y, bool render_target, bool has_depth_buffer, bool can_extract_depth) { + Framebuffer& fb = framebuffers[fb_id]; - glBindRenderbuffer(GL_RENDERBUFFER, fb2tex[fb].second); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); - glBindRenderbuffer(GL_RENDERBUFFER, 0); + width = max(width, 1U); + height = max(height, 1U); + + glBindFramebuffer(GL_FRAMEBUFFER, fb.fbo); + + if (fb_id != 0) { + if (fb.width != width || fb.height != height || fb.msaa_level != msaa_level) { + if (msaa_level <= 1) { + glBindTexture(GL_TEXTURE_2D, fb.clrbuf); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glBindTexture(GL_TEXTURE_2D, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb.clrbuf, 0); + } else { + glBindRenderbuffer(GL_RENDERBUFFER, fb.clrbuf_msaa); + glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa_level, GL_RGB8, width, height); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, fb.clrbuf_msaa); + } + } + + if (has_depth_buffer && (fb.width != width || fb.height != height || fb.msaa_level != msaa_level || !fb.has_depth_buffer)) { + glBindRenderbuffer(GL_RENDERBUFFER, fb.rbo); + if (msaa_level <= 1) { + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); + } else { + glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa_level, GL_DEPTH24_STENCIL8, width, height); + } + glBindRenderbuffer(GL_RENDERBUFFER, 0); + } + + if (!fb.has_depth_buffer && has_depth_buffer) { + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb.rbo); + } else if (fb.has_depth_buffer && !has_depth_buffer) { + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0); + } + } + + fb.width = width; + fb.height = height; + fb.has_depth_buffer = has_depth_buffer; + fb.msaa_level = msaa_level; + fb.invert_y = opengl_invert_y; } -void gfx_opengl_set_framebuffer(int fb) -{ - if (GLEW_ARB_clip_control || GLEW_VERSION_4_5) { - // Set origin to upper left corner, to match N64 and DX11 - // If this function is not supported, the texture will be upside down :( - glClipControl(GL_UPPER_LEFT, GL_NEGATIVE_ONE_TO_ONE); - } - glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb); +void gfx_opengl_start_draw_to_framebuffer(int fb_id, float noise_scale) { + Framebuffer& fb = framebuffers[fb_id]; + if (noise_scale != 0.0f) { + current_noise_scale = 1.0f / noise_scale; + } + + glBindFramebuffer(GL_FRAMEBUFFER, fb.fbo); + + current_framebuffer = fb_id; +} + +void gfx_opengl_clear_framebuffer() { + glDisable(GL_SCISSOR_TEST); glDepthMask(GL_TRUE); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDepthMask(current_depth_mask ? GL_TRUE : GL_FALSE); + glEnable(GL_SCISSOR_TEST); } -void gfx_opengl_reset_framebuffer(void) -{ - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - glBindFramebuffer(GL_FRAMEBUFFER_EXT, framebuffer); - if (GLEW_ARB_clip_control || GLEW_VERSION_4_5) { - glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE); - } +void gfx_opengl_resolve_msaa_color_buffer(int fb_id_target, int fb_id_source) { + Framebuffer& fb_dst = framebuffers[fb_id_target]; + Framebuffer& fb_src = framebuffers[fb_id_source]; + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb_dst.fbo); + glBindFramebuffer(GL_READ_FRAMEBUFFER, fb_src.fbo); + glBlitFramebuffer(0, 0, fb_src.width, fb_src.height, 0, 0, fb_dst.width, fb_dst.height, GL_COLOR_BUFFER_BIT, GL_NEAREST); + glBindFramebuffer(GL_FRAMEBUFFER, current_framebuffer); } -void gfx_opengl_select_texture_fb(int fbID) -{ +void *gfx_opengl_get_framebuffer_texture_id(int fb_id) { + return (void *)(uintptr_t)framebuffers[fb_id].clrbuf; +} + +void gfx_opengl_select_texture_fb(int fb_id) { //glDisable(GL_DEPTH_TEST); glActiveTexture(GL_TEXTURE0 + 0); - glBindTexture(GL_TEXTURE_2D, fb2tex[fbID].first); + glBindTexture(GL_TEXTURE_2D, framebuffers[fb_id].clrbuf); } -static uint16_t gfx_opengl_get_pixel_depth(float x, float y) { - float depth; - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); - glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth); - glBindFramebuffer(GL_FRAMEBUFFER, 0); - return depth * 65532.0f; +static std::map, uint16_t> gfx_opengl_get_pixel_depth(int fb_id, const std::set>& coordinates) { + std::map, uint16_t> res; + + Framebuffer& fb = framebuffers[fb_id]; + + if (coordinates.size() == 1) { + uint32_t depth_stencil_value; + glBindFramebuffer(GL_FRAMEBUFFER, fb.fbo); + int x = coordinates.begin()->first; + int y = coordinates.begin()->second; + glReadPixels(x, fb.invert_y ? fb.height - y : y, 1, 1, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, &depth_stencil_value); + res.emplace(*coordinates.begin(), (depth_stencil_value >> 18) << 2); + } else { + if (pixel_depth_rb_size < coordinates.size()) { + glBindRenderbuffer(GL_RENDERBUFFER, pixel_depth_rb); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, coordinates.size(), 1); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + + pixel_depth_rb_size = coordinates.size(); + } + + glBindFramebuffer(GL_READ_FRAMEBUFFER, fb.fbo); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, pixel_depth_fb); + + glDisable(GL_SCISSOR_TEST); // needed for the blit operation + + { + size_t i = 0; + for (const auto& coord : coordinates) { + int x = coord.first; + int y = coord.second; + if (fb.invert_y) { + y = fb.height - y; + } + glBlitFramebuffer(x, y, x + 1, y + 1, i, 0, i + 1, 1, GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST); + ++i; + } + } + + glBindFramebuffer(GL_READ_FRAMEBUFFER, pixel_depth_fb); + vector depth_stencil_values(coordinates.size()); + glReadPixels(0, 0, coordinates.size(), 1, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, depth_stencil_values.data()); + + { + size_t i = 0; + for (const auto& coord : coordinates) { + res.emplace(coord, (depth_stencil_values[i++] >> 18) << 2); + } + } + } + + glBindFramebuffer(GL_FRAMEBUFFER, current_framebuffer); + return res; } struct GfxRenderingAPI gfx_opengl_api = { - gfx_opengl_z_is_from_0_to_1, + gfx_opengl_get_clip_parameters, gfx_opengl_unload_shader, gfx_opengl_load_shader, gfx_opengl_create_and_load_new_shader, @@ -723,7 +803,6 @@ struct GfxRenderingAPI gfx_opengl_api = { gfx_opengl_upload_texture, gfx_opengl_set_sampler_parameters, gfx_opengl_set_depth_test_and_mask, - gfx_opengl_get_pixel_depth, gfx_opengl_set_zmode_decal, gfx_opengl_set_viewport, gfx_opengl_set_scissor, @@ -735,9 +814,12 @@ struct GfxRenderingAPI gfx_opengl_api = { gfx_opengl_end_frame, gfx_opengl_finish_render, gfx_opengl_create_framebuffer, - gfx_opengl_resize_framebuffer, - gfx_opengl_set_framebuffer, - gfx_opengl_reset_framebuffer, + gfx_opengl_update_framebuffer_parameters, + gfx_opengl_start_draw_to_framebuffer, + gfx_opengl_clear_framebuffer, + gfx_opengl_resolve_msaa_color_buffer, + gfx_opengl_get_pixel_depth, + gfx_opengl_get_framebuffer_texture_id, gfx_opengl_select_texture_fb, gfx_opengl_delete_texture }; diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp index 28debd120..5918ccaf7 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -27,6 +28,8 @@ #include "../../luslog.h" #include "../StrHash64.h" +#include "../../SohImGuiImpl.h" +#include "../../Environment.h" // OTRTODO: fix header files for these extern "C" { @@ -71,10 +74,6 @@ struct RGBA { uint8_t r, g, b, a; }; -struct XYWidthHeight { - uint16_t x, y, width, height; -}; - struct LoadedVertex { float x, y, z, w; float u, v; @@ -174,8 +173,16 @@ static struct RenderingState { TextureCacheNode *textures[2]; } rendering_state; +struct GfxDimensions gfx_current_window_dimensions; struct GfxDimensions gfx_current_dimensions; static struct GfxDimensions gfx_prev_dimensions; +struct XYWidthHeight gfx_current_game_window_viewport; + +static bool game_renders_to_framebuffer; +static int game_framebuffer; +static int game_framebuffer_msaa_resolved; + +uint32_t gfx_msaa_level = 1; static bool dropped_frame; @@ -198,6 +205,9 @@ static bool fbActive = 0; static map::iterator active_fb; static map framebuffers; +static set> get_pixel_depth_pending; +static map, uint16_t> get_pixel_depth_cached; + #ifdef _MSC_VER // TODO: Properly implement for MSVC static unsigned long get_time(void) @@ -1326,11 +1336,11 @@ static void gfx_sp_tri1(uint8_t vtx1_idx, uint8_t vtx2_idx, uint8_t vtx3_idx, bo gfx_rapi->shader_get_info(prg, &num_inputs, used_textures); - bool z_is_from_0_to_1 = gfx_rapi->z_is_from_0_to_1(); + struct GfxClipParameters clip_parameters = gfx_rapi->get_clip_parameters(); for (int i = 0; i < 3; i++) { float z = v_arr[i]->z, w = v_arr[i]->w; - if (z_is_from_0_to_1) { + if (clip_parameters.z_is_from_0_to_1) { z = (z + w) / 2.0f; } @@ -1340,7 +1350,7 @@ static void gfx_sp_tri1(uint8_t vtx1_idx, uint8_t vtx2_idx, uint8_t vtx3_idx, bo } buf_vbo[buf_vbo_len++] = v_arr[i]->x; - buf_vbo[buf_vbo_len++] = v_arr[i]->y; + buf_vbo[buf_vbo_len++] = clip_parameters.invert_y ? -v_arr[i]->y : v_arr[i]->y; buf_vbo[buf_vbo_len++] = z; buf_vbo[buf_vbo_len++] = w; @@ -1491,6 +1501,27 @@ static void gfx_sp_geometry_mode(uint32_t clear, uint32_t set) { rsp.geometry_mode |= set; } +static void gfx_adjust_viewport_or_scissor(XYWidthHeight *area) { + if (!fbActive) { + area->width *= RATIO_X; + area->height *= RATIO_Y; + area->x *= RATIO_X; + area->y = SCREEN_HEIGHT - area->y; + area->y *= RATIO_Y; + + if (!game_renders_to_framebuffer || (gfx_msaa_level > 1 && gfx_current_dimensions.width == gfx_current_game_window_viewport.width && gfx_current_dimensions.height == gfx_current_game_window_viewport.height)) { + area->x += gfx_current_game_window_viewport.x; + area->y += gfx_current_window_dimensions.height - (gfx_current_game_window_viewport.y + gfx_current_game_window_viewport.height); + } + } else { + area->width *= RATIO_Y; + area->height *= RATIO_Y; + area->x *= RATIO_Y; + area->y = active_fb->second.orig_height - area->y; + area->y *= RATIO_Y; + } +} + static void gfx_calc_and_set_viewport(const Vp_t *viewport) { // 2 bits fraction float width = 2.0f * viewport->vscale[0] / 4.0f; @@ -1498,25 +1529,13 @@ static void gfx_calc_and_set_viewport(const Vp_t *viewport) { float x = (viewport->vtrans[0] / 4.0f) - width / 2.0f; float y = ((viewport->vtrans[1] / 4.0f) + height / 2.0f); - if (!fbActive) { - width *= RATIO_X; - height *= RATIO_Y; - x *= RATIO_X; - y = SCREEN_HEIGHT - y; - y *= RATIO_Y; - } else { - width *= RATIO_Y; - height *= RATIO_Y; - x *= RATIO_Y; - y = active_fb->second.orig_height - y; - y *= RATIO_Y; - } - rdp.viewport.x = x; rdp.viewport.y = y; rdp.viewport.width = width; rdp.viewport.height = height; + gfx_adjust_viewport_or_scissor(&rdp.viewport); + rdp.viewport_or_scissor_changed = true; } @@ -1585,11 +1604,6 @@ static void gfx_sp_texture(uint16_t sc, uint16_t tc, uint8_t level, uint8_t tile rdp.textures_changed[1] = true; } - if (tile > 8) - { - int bp = 0; - } - rdp.first_tile_index = tile; } @@ -1599,25 +1613,13 @@ static void gfx_dp_set_scissor(uint32_t mode, uint32_t ulx, uint32_t uly, uint32 float width = (lrx - ulx) / 4.0f; float height = (lry - uly) / 4.0f; - if (!fbActive) { - x *= RATIO_X; - y = SCREEN_HEIGHT - y; - y *= RATIO_Y; - width *= RATIO_X; - height *= RATIO_Y; - } else { - width *= RATIO_Y; - height *= RATIO_Y; - x *= RATIO_Y; - y = active_fb->second.orig_height - y; - y *= RATIO_Y; - } - rdp.scissor.x = x; rdp.scissor.y = y; rdp.scissor.width = width; rdp.scissor.height = height; + gfx_adjust_viewport_or_scissor(&rdp.scissor); + rdp.viewport_or_scissor_changed = true; } @@ -1887,10 +1889,12 @@ static void gfx_draw_rectangle(int32_t ulx, int32_t uly, int32_t lrx, int32_t lr ur->w = 1.0f; // The coordinates for texture rectangle shall bypass the viewport setting - struct XYWidthHeight default_viewport = { 0, 0, gfx_current_dimensions.width, gfx_current_dimensions.height }; + struct XYWidthHeight default_viewport = { 0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT }; struct XYWidthHeight viewport_saved = rdp.viewport; uint32_t geometry_mode_saved = rsp.geometry_mode; + gfx_adjust_viewport_or_scissor(&default_viewport); + rdp.viewport = default_viewport; rdp.viewport_or_scissor_changed = true; rsp.geometry_mode = 0; @@ -2456,14 +2460,15 @@ static void gfx_run_dl(Gfx* cmd) { gfx_flush(); fbActive = 1; active_fb = framebuffers.find(cmd->words.w1); - gfx_rapi->set_framebuffer(active_fb->first); + gfx_rapi->start_draw_to_framebuffer(active_fb->first, (float)active_fb->second.applied_height / active_fb->second.orig_height); + gfx_rapi->clear_framebuffer(); } break; case G_RESETFB: { gfx_flush(); fbActive = 0; - gfx_rapi->reset_framebuffer(); + gfx_rapi->start_draw_to_framebuffer(game_renders_to_framebuffer ? game_framebuffer : 0, (float)gfx_current_dimensions.height / SCREEN_HEIGHT); break; } break; @@ -2547,7 +2552,7 @@ static void gfx_run_dl(Gfx* cmd) { gfx_dp_texture_rectangle(ulx, uly, lrx, lry, tile, uls, ult, dsdx, dtdy, opcode == G_TEXRECTFLIP); break; } - case G_TEXRECT_WIDE: + case G_TEXRECT_WIDE: { int32_t lrx, lry, tile, ulx, uly; uint32_t uls, ult, dsdx, dtdy; @@ -2630,9 +2635,12 @@ void gfx_init(struct GfxWindowManagerAPI *wapi, struct GfxRenderingAPI *rapi, co gfx_rapi = rapi; gfx_wapi->init(game_name, start_in_fullscreen); gfx_rapi->init(); + gfx_rapi->update_framebuffer_parameters(0, SCREEN_WIDTH, SCREEN_HEIGHT, 1, false, true, true, true); gfx_current_dimensions.internal_mul = 1; gfx_current_dimensions.width = SCREEN_WIDTH; gfx_current_dimensions.height = SCREEN_HEIGHT; + game_framebuffer = gfx_rapi->create_framebuffer(); + game_framebuffer_msaa_resolved = gfx_rapi->create_framebuffer(); for (int i = 0; i < 16; i++) segmentPointers[i] = NULL; @@ -2681,7 +2689,8 @@ struct GfxRenderingAPI *gfx_get_current_rendering_api(void) { void gfx_start_frame(void) { gfx_wapi->handle_events(); - // gfx_wapi->get_dimensions(&gfx_current_dimensions.width, &gfx_current_dimensions.height); + gfx_wapi->get_dimensions(&gfx_current_window_dimensions.width, &gfx_current_window_dimensions.height); + SohImGui::DrawMainMenuAndCalculateGameSize(); if (gfx_current_dimensions.height == 0) { // Avoid division by zero gfx_current_dimensions.height = 1; @@ -2693,7 +2702,7 @@ void gfx_start_frame(void) { uint32_t width = fb.second.orig_width, height = fb.second.orig_height; gfx_adjust_width_height_for_scale(width, height); if (width != fb.second.applied_width || height != fb.second.applied_height) { - gfx_rapi->resize_framebuffer(fb.first, width, height); + gfx_rapi->update_framebuffer_parameters(fb.first, width, height, 1, true, true, true, true); fb.second.applied_width = width; fb.second.applied_height = height; } @@ -2701,6 +2710,22 @@ void gfx_start_frame(void) { } gfx_prev_dimensions = gfx_current_dimensions; + bool different_size = gfx_current_dimensions.width != gfx_current_game_window_viewport.width || gfx_current_dimensions.height != gfx_current_game_window_viewport.height; + if (different_size || gfx_msaa_level > 1) { + game_renders_to_framebuffer = true; + if (different_size) { + gfx_rapi->update_framebuffer_parameters(game_framebuffer, gfx_current_dimensions.width, gfx_current_dimensions.height, gfx_msaa_level, true, true, true, true); + } else { + // MSAA framebuffer needs to be resolved to an equally sized target when complete, which must therefore match the window size + gfx_rapi->update_framebuffer_parameters(game_framebuffer, gfx_current_window_dimensions.width, gfx_current_window_dimensions.height, gfx_msaa_level, false, true, true, true); + } + if (gfx_msaa_level > 1 && different_size) { + gfx_rapi->update_framebuffer_parameters(game_framebuffer_msaa_resolved, gfx_current_dimensions.width, gfx_current_dimensions.height, 1, false, false, false, false); + } + } else { + game_renders_to_framebuffer = false; + } + fbActive = 0; } @@ -2708,17 +2733,44 @@ void gfx_run(Gfx *commands) { gfx_sp_reset(); //puts("New frame"); + get_pixel_depth_pending.clear(); + get_pixel_depth_cached.clear(); if (!gfx_wapi->start_frame()) { dropped_frame = true; + SohImGui::DrawFramebufferAndGameInput(); + SohImGui::CancelFrame(); return; } dropped_frame = false; double t0 = gfx_wapi->get_time(); + gfx_rapi->update_framebuffer_parameters(0, gfx_current_window_dimensions.width, gfx_current_window_dimensions.height, 1, false, true, true, !game_renders_to_framebuffer); gfx_rapi->start_frame(); + gfx_rapi->start_draw_to_framebuffer(game_renders_to_framebuffer ? game_framebuffer : 0, (float)gfx_current_dimensions.height / SCREEN_HEIGHT); + gfx_rapi->clear_framebuffer(); gfx_run_dl(commands); gfx_flush(); + SohUtils::saveEnvironmentVar("framebuffer", string()); + if (game_renders_to_framebuffer) { + gfx_rapi->start_draw_to_framebuffer(0, 1); + gfx_rapi->clear_framebuffer(); + + if (gfx_msaa_level > 1) { + bool different_size = gfx_current_dimensions.width != gfx_current_game_window_viewport.width || gfx_current_dimensions.height != gfx_current_game_window_viewport.height; + + if (different_size) { + gfx_rapi->resolve_msaa_color_buffer(game_framebuffer_msaa_resolved, game_framebuffer); + SohUtils::saveEnvironmentVar("framebuffer", std::to_string((uintptr_t)gfx_rapi->get_framebuffer_texture_id(game_framebuffer_msaa_resolved))); + } else { + gfx_rapi->resolve_msaa_color_buffer(0, game_framebuffer); + } + } else { + SohUtils::saveEnvironmentVar("framebuffer", std::to_string((uintptr_t)gfx_rapi->get_framebuffer_texture_id(game_framebuffer))); + } + } + SohImGui::DrawFramebufferAndGameInput(); + SohImGui::Render(); double t1 = gfx_wapi->get_time(); //printf("Process %f %f\n", t1, t1 - t0); gfx_rapi->end_frame(); @@ -2739,21 +2791,47 @@ void gfx_set_framedivisor(int divisor) { int gfx_create_framebuffer(uint32_t width, uint32_t height) { uint32_t orig_width = width, orig_height = height; gfx_adjust_width_height_for_scale(width, height); - int fb = gfx_rapi->create_framebuffer(width, height); + int fb = gfx_rapi->create_framebuffer(); + gfx_rapi->update_framebuffer_parameters(fb, width, height, 1, true, true, true, true); framebuffers[fb] = { orig_width, orig_height, width, height }; return fb; } -void gfx_set_framebuffer(int fb) -{ - gfx_rapi->set_framebuffer(fb); +void gfx_set_framebuffer(int fb, float noise_scale) { + gfx_rapi->start_draw_to_framebuffer(fb, noise_scale); + gfx_rapi->clear_framebuffer(); } -void gfx_reset_framebuffer() -{ - gfx_rapi->reset_framebuffer(); +void gfx_reset_framebuffer() { + gfx_rapi->start_draw_to_framebuffer(0, (float)gfx_current_dimensions.height / SCREEN_HEIGHT); +} + +static void adjust_pixel_depth_coordinates(float& x, float& y) { + x = x * RATIO_Y - (SCREEN_WIDTH * RATIO_Y - gfx_current_dimensions.width) / 2; + y *= RATIO_Y; + if (!game_renders_to_framebuffer || (gfx_msaa_level > 1 && gfx_current_dimensions.width == gfx_current_game_window_viewport.width && gfx_current_dimensions.height == gfx_current_game_window_viewport.height)) { + x += gfx_current_game_window_viewport.x; + y += gfx_current_window_dimensions.height - (gfx_current_game_window_viewport.y + gfx_current_game_window_viewport.height); + } +} + +void gfx_get_pixel_depth_prepare(float x, float y) { + adjust_pixel_depth_coordinates(x, y); + get_pixel_depth_pending.emplace(x, y); } uint16_t gfx_get_pixel_depth(float x, float y) { - return gfx_rapi->get_pixel_depth(x * RATIO_Y - (SCREEN_WIDTH * RATIO_Y - gfx_current_dimensions.width) / 2, y * RATIO_Y); + adjust_pixel_depth_coordinates(x, y); + + if (auto it = get_pixel_depth_cached.find(make_pair(x, y)); it != get_pixel_depth_cached.end()) { + return it->second; + } + + get_pixel_depth_pending.emplace(x, y); + + map, uint16_t> res = gfx_rapi->get_pixel_depth(game_renders_to_framebuffer ? game_framebuffer : 0, get_pixel_depth_pending); + get_pixel_depth_cached.merge(res); + get_pixel_depth_pending.clear(); + + return get_pixel_depth_cached.find(make_pair(x, y))->second; } \ No newline at end of file diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_pc.h b/libultraship/libultraship/Lib/Fast3D/gfx_pc.h index 446c0b6a4..d682991d9 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_pc.h +++ b/libultraship/libultraship/Lib/Fast3D/gfx_pc.h @@ -8,8 +8,11 @@ struct GfxRenderingAPI; struct GfxWindowManagerAPI; -struct GfxDimensions -{ +struct XYWidthHeight { + int16_t x, y, width, height; +}; + +struct GfxDimensions { uint32_t internal_mul; uint32_t width, height; float aspect_ratio; @@ -50,7 +53,10 @@ struct TextureCacheValue { #ifdef __cplusplus extern "C" { #endif -extern struct GfxDimensions gfx_current_dimensions; +extern struct GfxDimensions gfx_current_window_dimensions; // The dimensions of the window +extern struct GfxDimensions gfx_current_dimensions; // The dimensions of the draw area the game draws to, before scaling (if applicable) +extern struct XYWidthHeight gfx_current_game_window_viewport; // The area of the window the game is drawn to, (0, 0) is top-left corner +extern uint32_t gfx_msaa_level; void gfx_init(struct GfxWindowManagerAPI* wapi, struct GfxRenderingAPI* rapi, const char* game_name, bool start_in_fullscreen); struct GfxRenderingAPI* gfx_get_current_rendering_api(void); @@ -60,6 +66,7 @@ void gfx_end_frame(void); void gfx_set_framedivisor(int); void gfx_texture_cache_clear(); int gfx_create_framebuffer(uint32_t width, uint32_t height); +void gfx_get_pixel_depth_prepare(float x, float y); uint16_t gfx_get_pixel_depth(float x, float y); #ifdef __cplusplus diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_rendering_api.h b/libultraship/libultraship/Lib/Fast3D/gfx_rendering_api.h index 9283ee236..84247fe60 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_rendering_api.h +++ b/libultraship/libultraship/Lib/Fast3D/gfx_rendering_api.h @@ -5,10 +5,18 @@ #include #include +#include +#include + struct ShaderProgram; +struct GfxClipParameters { + bool z_is_from_0_to_1; + bool invert_y; +}; + struct GfxRenderingAPI { - bool (*z_is_from_0_to_1)(void); + struct GfxClipParameters (*get_clip_parameters)(void); void (*unload_shader)(struct ShaderProgram *old_prg); void (*load_shader)(struct ShaderProgram *new_prg); struct ShaderProgram *(*create_and_load_new_shader)(uint64_t shader_id0, uint32_t shader_id1); @@ -19,7 +27,6 @@ struct GfxRenderingAPI { void (*upload_texture)(const uint8_t *rgba32_buf, uint32_t width, uint32_t height); void (*set_sampler_parameters)(int sampler, bool linear_filter, uint32_t cms, uint32_t cmt); void (*set_depth_test_and_mask)(bool depth_test, bool z_upd); - uint16_t (*get_pixel_depth)(float x, float y); void (*set_zmode_decal)(bool zmode_decal); void (*set_viewport)(int x, int y, int width, int height); void (*set_scissor)(int x, int y, int width, int height); @@ -30,11 +37,14 @@ struct GfxRenderingAPI { void (*start_frame)(void); void (*end_frame)(void); void (*finish_render)(void); - int (*create_framebuffer)(uint32_t width, uint32_t height); - void (*resize_framebuffer)(int fb, uint32_t width, uint32_t height); - void (*set_framebuffer)(int fb); - void (*reset_framebuffer)(); - void (*select_texture_fb)(int fbID); + int (*create_framebuffer)(); + void (*update_framebuffer_parameters)(int fb_id, uint32_t width, uint32_t height, uint32_t msaa_level, bool opengl_invert_y, bool render_target, bool has_depth_buffer, bool can_extract_depth); + void (*start_draw_to_framebuffer)(int fb_id, float noise_scale); + void (*clear_framebuffer)(void); + void (*resolve_msaa_color_buffer)(int fb_id_target, int fb_id_source); + std::map, uint16_t> (*get_pixel_depth)(int fb_id, const std::set>& coordinates); + void *(*get_framebuffer_texture_id)(int fb_id); + void (*select_texture_fb)(int fb_id); void (*delete_texture)(uint32_t texID); }; diff --git a/libultraship/libultraship/Lib/ImGui/imgui.h b/libultraship/libultraship/Lib/ImGui/imgui.h index fa2dadcda..323832071 100644 --- a/libultraship/libultraship/Lib/ImGui/imgui.h +++ b/libultraship/libultraship/Lib/ImGui/imgui.h @@ -502,7 +502,6 @@ namespace ImGui IMGUI_API bool SmallButton(const char* label); // button with FramePadding=(0,0) to easily embed within text IMGUI_API bool InvisibleButton(const char* str_id, const ImVec2& size, ImGuiButtonFlags flags = 0); // flexible button behavior without the visuals, frequently useful to build custom behaviors using the public api (along with IsItemActive, IsItemHovered, etc.) IMGUI_API bool ArrowButton(const char* str_id, ImGuiDir dir); // square button with an arrow shape - IMGUI_API void ImageRotated(ImTextureID tex_id, ImVec2 center, ImVec2 size, float angle); IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1), const ImVec4& border_col = ImVec4(0,0,0,0)); IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0,0,0,0), const ImVec4& tint_col = ImVec4(1,1,1,1)); // <0 frame_padding uses default frame padding settings. 0 for no padding IMGUI_API bool Checkbox(const char* label, bool* v); diff --git a/libultraship/libultraship/Lib/ImGui/imgui_widgets.cpp b/libultraship/libultraship/Lib/ImGui/imgui_widgets.cpp index 3d165580b..8e4210002 100644 --- a/libultraship/libultraship/Lib/ImGui/imgui_widgets.cpp +++ b/libultraship/libultraship/Lib/ImGui/imgui_widgets.cpp @@ -1007,33 +1007,6 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS6 return held; } -void ImGui::ImageRotated(ImTextureID tex_id, ImVec2 center, ImVec2 size, float angle) { - - ImGuiWindow* window = GetCurrentWindow(); - if (window->SkipItems) - return; - - ImDrawList* draw_list = ImGui::GetWindowDrawList(); - ImRect bb(window->DC.CursorPos + ImVec2(size.x / 2, size.y / 2), window->DC.CursorPos + size); - - ImVec2 pos[4] = - { - center + bb.Min + ImVec2(-size.x * 0.5f, -size.y * 0.5f), - center + bb.Min + ImVec2(+size.x * 0.5f, -size.y * 0.5f), - center + bb.Min + ImVec2(+size.x * 0.5f, +size.y * 0.5f), - center + bb.Min + ImVec2(-size.x * 0.5f, +size.y * 0.5f) - }; - ImVec2 uvs[4] = - { - ImVec2(0.0f, 1.0f), - ImVec2(1.0f, 1.0f), - ImVec2(1.0f, 0.0f), - ImVec2(0.0f, 0.0f) - }; - - draw_list->AddImageQuad(tex_id, pos[0], pos[1], pos[2], pos[3], uvs[0], uvs[1], uvs[2], uvs[3], IM_COL32_WHITE); -} - void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col) { ImGuiWindow* window = GetCurrentWindow(); diff --git a/libultraship/libultraship/SohImGuiImpl.cpp b/libultraship/libultraship/SohImGuiImpl.cpp index 93dcd2492..860720433 100644 --- a/libultraship/libultraship/SohImGuiImpl.cpp +++ b/libultraship/libultraship/SohImGuiImpl.cpp @@ -123,14 +123,6 @@ namespace SohImGui { } } - bool UseInternalRes() { - switch (impl.backend) { - case Backend::SDL: - return true; - } - return false; - } - bool UseViewports() { switch (impl.backend) { case Backend::DX11: @@ -281,20 +273,16 @@ namespace SohImGui { } } - void Draw() { - + void DrawMainMenuAndCalculateGameSize() { console->Update(); ImGuiBackendNewFrame(); ImGuiWMNewFrame(); ImGui::NewFrame(); const std::shared_ptr wnd = GlobalCtx2::GetInstance()->GetWindow(); - ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDocking | + ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_NoResize; - if (UseViewports()) { - window_flags |= ImGuiWindowFlags_NoBackground; - } if (Game::Settings.debug.menu_bar) window_flags |= ImGuiWindowFlags_MenuBar; const ImGuiViewport* viewport = ImGui::GetMainViewport(); @@ -302,8 +290,12 @@ namespace SohImGui { ImGui::SetNextWindowSize(ImVec2(wnd->GetCurrentWidth(), wnd->GetCurrentHeight())); ImGui::SetNextWindowViewport(viewport->ID); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); + ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 0.0f); ImGui::Begin("Main - Deck", nullptr, window_flags); - ImGui::PopStyleVar(); + ImGui::PopStyleVar(3); + + ImVec2 top_left_pos = ImGui::GetWindowPos(); const ImGuiID dockId = ImGui::GetID("main_dock"); @@ -421,9 +413,7 @@ namespace SohImGui { ImGui::Text("Graphics"); ImGui::Separator(); - if (UseInternalRes()) { - HOOK(ImGui::Checkbox("N64 Mode", &Game::Settings.debug.n64mode)); - } + HOOK(ImGui::Checkbox("N64 Mode", &Game::Settings.debug.n64mode)); if (ImGui::Checkbox("Animated Link in Pause Menu", &Game::Settings.enhancements.animated_pause_menu)) { CVar_SetS32("gPauseLiveLink", Game::Settings.enhancements.animated_pause_menu); @@ -441,10 +431,10 @@ namespace SohImGui { if (ImGui::BeginMenu("Developer Tools")) { HOOK(ImGui::MenuItem("Stats", nullptr, &Game::Settings.debug.soh)); HOOK(ImGui::MenuItem("Console", nullptr, &console->opened)); - + ImGui::Text("Debug"); ImGui::Separator(); - + if (ImGui::Checkbox("Debug Mode", &Game::Settings.cheats.debug_mode)) { CVar_SetS32("gDebugEnabled", Game::Settings.cheats.debug_mode); needs_save = true; @@ -452,7 +442,12 @@ namespace SohImGui { ImGui::EndMenu(); } - + + if (ImGui::BeginMenu("Graphics")) { + HOOK(ImGui::MenuItem("Anti-aliasing", nullptr, &Game::Settings.graphics.show)); + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Cheats")) { if (ImGui::Checkbox("Infinite Money", &Game::Settings.cheats.infinite_money)) { CVar_SetS32("gInfiniteMoney", Game::Settings.cheats.infinite_money); @@ -473,22 +468,22 @@ namespace SohImGui { CVar_SetS32("gInfiniteMagic", Game::Settings.cheats.infinite_magic); needs_save = true; } - + if (ImGui::Checkbox("No Clip", &Game::Settings.cheats.no_clip)) { CVar_SetS32("gNoClip", Game::Settings.cheats.no_clip); needs_save = true; } - + if (ImGui::Checkbox("Climb Everything", &Game::Settings.cheats.climb_everything)) { CVar_SetS32("gClimbEverything", Game::Settings.cheats.climb_everything); needs_save = true; } - + if (ImGui::Checkbox("Moon Jump on L", &Game::Settings.cheats.moon_jump_on_l)) { CVar_SetS32("gMoonJumpOnL", Game::Settings.cheats.moon_jump_on_l); needs_save = true; } - + if (ImGui::Checkbox("Super Tunic", &Game::Settings.cheats.super_tunic)) { CVar_SetS32("gSuperTunic", Game::Settings.cheats.super_tunic); needs_save = true; @@ -509,36 +504,6 @@ namespace SohImGui { ImGui::EndMenuBar(); } - ImGui::End(); - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); - ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.0f, 0.0f, 0.0f, 0.0f)); - ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; - if (UseViewports()) { - flags |= ImGuiWindowFlags_NoBackground; - } - ImGui::Begin("OoT Master Quest", nullptr, flags); - ImGui::PopStyleVar(); - ImGui::PopStyleColor(); - - ImVec2 main_pos = ImGui::GetWindowPos(); - ImVec2 size = ImGui::GetContentRegionAvail(); - ImVec2 pos = ImVec2(0, 0); - gfx_current_dimensions.width = size.x * gfx_current_dimensions.internal_mul; - gfx_current_dimensions.height = size.y * gfx_current_dimensions.internal_mul; - if (UseInternalRes()) { - if (Game::Settings.debug.n64mode) { - gfx_current_dimensions.width = 320; - gfx_current_dimensions.height = 240; - const int sw = size.y * 320 / 240; - pos = ImVec2(size.x / 2 - sw / 2, 0); - size = ImVec2(sw, size.y); - } - } - - if (UseInternalRes()) { - int fbuf = std::stoi(SohUtils::getEnvironmentVar("framebuffer")); - ImGui::ImageRotated(reinterpret_cast(fbuf), pos, size, 0.0f); - } ImGui::End(); if (Game::Settings.debug.soh) { @@ -548,18 +513,84 @@ namespace SohImGui { ImGui::Text("Platform: Windows"); ImGui::Text("Status: %.3f ms/frame (%.1f FPS)", 1000.0f / framerate, framerate); - if (UseInternalRes()) { - ImGui::Text("Internal Resolution:"); - ImGui::SliderInt("Mul", reinterpret_cast(&gfx_current_dimensions.internal_mul), 1, 8); - } ImGui::End(); ImGui::PopStyleColor(); } + if (Game::Settings.graphics.show) { + ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(0, 0, 0, 0)); + ImGui::Begin("Anti-aliasing settings", nullptr, ImGuiWindowFlags_None); + ImGui::Text("Internal Resolution:"); + ImGui::SliderInt("Mul", reinterpret_cast(&gfx_current_dimensions.internal_mul), 1, 8); + ImGui::Text("MSAA:"); + ImGui::SliderInt("MSAA", reinterpret_cast(&gfx_msaa_level), 1, 8); + ImGui::End(); + ImGui::PopStyleColor(); + } + + console->Draw(); + + for (auto& windowIter : customWindows) { + CustomWindow& window = windowIter.second; + if (window.drawFunc != nullptr) { + window.drawFunc(window.enabled); + } + } + + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); + ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 0.0f); + ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.0f, 0.0f, 0.0f, 0.0f)); + ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground; + ImGui::Begin("OoT Master Quest", nullptr, flags); + ImGui::PopStyleVar(3); + ImGui::PopStyleColor(); + + ImVec2 main_pos = ImGui::GetWindowPos(); + main_pos.x -= top_left_pos.x; + main_pos.y -= top_left_pos.y; + ImVec2 size = ImGui::GetContentRegionAvail(); + ImVec2 pos = ImVec2(0, 0); + gfx_current_dimensions.width = size.x * gfx_current_dimensions.internal_mul; + gfx_current_dimensions.height = size.y * gfx_current_dimensions.internal_mul; + gfx_current_game_window_viewport.x = main_pos.x; + gfx_current_game_window_viewport.y = main_pos.y; + gfx_current_game_window_viewport.width = size.x; + gfx_current_game_window_viewport.height = size.y; + if (Game::Settings.debug.n64mode) { + gfx_current_dimensions.width = 320; + gfx_current_dimensions.height = 240; + const int sw = size.y * 320 / 240; + gfx_current_game_window_viewport.x += (size.x - sw) / 2; + gfx_current_game_window_viewport.width = sw; + pos = ImVec2(size.x / 2 - sw / 2, 0); + size = ImVec2(sw, size.y); + } + } + + void DrawFramebufferAndGameInput() { + ImVec2 main_pos = ImGui::GetWindowPos(); + ImVec2 size = ImGui::GetContentRegionAvail(); + ImVec2 pos = ImVec2(0, 0); + if (Game::Settings.debug.n64mode) { + const int sw = size.y * 320 / 240; + pos = ImVec2(size.x / 2 - sw / 2, 0); + size = ImVec2(sw, size.y); + } + std::string fb_str = SohUtils::getEnvironmentVar("framebuffer"); + if (!fb_str.empty()) { + uintptr_t fbuf = (uintptr_t)std::stoull(fb_str); + //ImGui::ImageSimple(reinterpret_cast(fbuf), pos, size); + ImGui::SetCursorPos(pos); + ImGui::Image(reinterpret_cast(fbuf), size); + } + + ImGui::End(); + const float scale = Game::Settings.controller.input_scale; ImVec2 BtnPos = ImVec2(160 * scale, 85 * scale); - if(Game::Settings.controller.input_enabled) { + if (Game::Settings.controller.input_enabled) { ImGui::SetNextWindowSize(BtnPos); ImGui::SetNextWindowPos(ImVec2(main_pos.x + size.x - BtnPos.x - 20, main_pos.y + size.y - BtnPos.y - 20)); @@ -605,16 +636,9 @@ namespace SohImGui { ImGui::End(); } } + } - console->Draw(); - - for (auto& windowIter : customWindows) { - CustomWindow& window = windowIter.second; - if (window.drawFunc != nullptr) { - window.drawFunc(window.enabled); - } - } - + void Render() { ImGui::Render(); ImGuiRenderDrawData(ImGui::GetDrawData()); if (UseViewports()) { @@ -623,6 +647,13 @@ namespace SohImGui { } } + void CancelFrame() { + ImGui::EndFrame(); + if (UseViewports()) { + ImGui::UpdatePlatformWindows(); + } + } + void BindCmd(const std::string& cmd, CommandEntry entry) { console->Commands[cmd] = std::move(entry); } diff --git a/libultraship/libultraship/SohImGuiImpl.h b/libultraship/libultraship/SohImGuiImpl.h index 58dac7a1b..dc6e24ee1 100644 --- a/libultraship/libultraship/SohImGuiImpl.h +++ b/libultraship/libultraship/SohImGuiImpl.h @@ -60,7 +60,10 @@ namespace SohImGui { extern Console* console; void Init(WindowImpl window_impl); void Update(EventImpl event); - void Draw(void); + void DrawMainMenuAndCalculateGameSize(void); + void DrawFramebufferAndGameInput(void); + void Render(void); + void CancelFrame(void); void ShowCursor(bool hide, Dialogues w); void BindCmd(const std::string& cmd, CommandEntry entry); void AddWindow(const std::string& category, const std::string& name, WindowDrawFunc drawFunc); diff --git a/libultraship/libultraship/Window.cpp b/libultraship/libultraship/Window.cpp index 665483b35..2b57c547e 100644 --- a/libultraship/libultraship/Window.cpp +++ b/libultraship/libultraship/Window.cpp @@ -286,6 +286,10 @@ namespace Ship { //gfx_set_framedivisor(0); } + void Window::GetPixelDepthPrepare(float x, float y) { + gfx_get_pixel_depth_prepare(x, y); + } + uint16_t Window::GetPixelDepth(float x, float y) { return gfx_get_pixel_depth(x, y); } diff --git a/libultraship/libultraship/Window.h b/libultraship/libultraship/Window.h index 6046ca4ae..0d12211b0 100644 --- a/libultraship/libultraship/Window.h +++ b/libultraship/libultraship/Window.h @@ -20,6 +20,7 @@ namespace Ship { void Init(); void RunCommands(Gfx* Commands); void SetFrameDivisor(int divisor); + void GetPixelDepthPrepare(float x, float y); uint16_t GetPixelDepth(float x, float y); void ToggleFullscreen(); void SetFullscreen(bool bIsFullscreen); diff --git a/soh/include/functions.h b/soh/include/functions.h index a4b1a9a99..13e3a7055 100644 --- a/soh/include/functions.h +++ b/soh/include/functions.h @@ -977,6 +977,7 @@ void LightContext_RemoveLight(GlobalContext* globalCtx, LightContext* lightCtx, Lights* Lights_NewAndDraw(GraphicsContext* gfxCtx, u8 ambientR, u8 ambientG, u8 ambientB, u8 numLights, u8 r, u8 g, u8 b, s8 x, s8 y, s8 z); Lights* Lights_New(GraphicsContext* gfxCtx, u8 ambientR, u8 ambientG, u8 ambientB); +void Lights_GlowCheckPrepare(GlobalContext* globalCtx); void Lights_GlowCheck(GlobalContext* globalCtx); void Lights_DrawGlow(GlobalContext* globalCtx); void ZeldaArena_CheckPointer(void* ptr, size_t size, const char* name, const char* action); diff --git a/soh/soh/GbiWrap.cpp b/soh/soh/GbiWrap.cpp index 1f6a8003c..c7f69f1a9 100644 --- a/soh/soh/GbiWrap.cpp +++ b/soh/soh/GbiWrap.cpp @@ -9,6 +9,7 @@ void Graph_ProcessGfxCommands(Gfx* commands); void OTRLogString(const char* src); void OTRGfxPrint(const char* str, void* printer, void (*printImpl)(void*, char)); void OTRSetFrameDivisor(int divisor); +void OTRGetPixelDepthPrepare(float x, float y); uint16_t OTRGetPixelDepth(float x, float y); int32_t OTRGetLastScancode(); void ResourceMgr_CacheDirectory(const char* resName); diff --git a/soh/soh/OTRGlobals.cpp b/soh/soh/OTRGlobals.cpp index 007afd702..fca6d6b7d 100644 --- a/soh/soh/OTRGlobals.cpp +++ b/soh/soh/OTRGlobals.cpp @@ -24,9 +24,17 @@ #include "../soh/Enhancements/debugconsole.h" #include "../soh/Enhancements/debugger/debugger.h" #include "Utils/BitConverter.h" +#include "variables.h" OTRGlobals* OTRGlobals::Instance; +static struct { + std::condition_variable cv_to_thread, cv_from_thread; + std::mutex mutex; + bool initialized; + bool processing; +} audio; + OTRGlobals::OTRGlobals() { context = Ship::GlobalCtx2::CreateInstance("Ship of Harkinian"); context->GetWindow()->Init(); @@ -39,6 +47,10 @@ extern uintptr_t clearMtx; extern "C" Mtx gMtxClear; extern "C" MtxF gMtxFClear; extern "C" void OTRMessage_Init(); +extern "C" void AudioMgr_CreateNextAudioBuffer(s16* samples, u32 num_samples); +extern "C" void AudioPlayer_Play(const uint8_t* buf, uint32_t len); +extern "C" int AudioPlayer_Buffered(void); +extern "C" int AudioPlayer_GetDesiredBuffered(void); // C->C++ Bridge extern "C" void InitOTR() { @@ -80,8 +92,67 @@ extern "C" void Graph_ProcessFrame(void (*run_one_game_iter)(void)) { // C->C++ Bridge extern "C" void Graph_ProcessGfxCommands(Gfx* commands) { + OTRGlobals::Instance->context->GetWindow()->SetFrameDivisor(R_UPDATE_RATE); + + if (!audio.initialized) { + audio.initialized = true; + std::thread([]() { + for (;;) { + { + std::unique_lock Lock(audio.mutex); + while (!audio.processing) { + audio.cv_to_thread.wait(Lock); + } + } + //AudioMgr_ThreadEntry(&gAudioMgr); + // 528 and 544 relate to 60 fps at 32 kHz 32000/60 = 533.333.. + // in an ideal world, one third of the calls should use num_samples=544 and two thirds num_samples=528 + #define SAMPLES_HIGH 560 + #define SAMPLES_LOW 528 + // PAL values + //#define SAMPLES_HIGH 656 + //#define SAMPLES_LOW 624 + #define AUDIO_FRAMES_PER_UPDATE (R_UPDATE_RATE > 0 ? R_UPDATE_RATE : 1 ) + #define NUM_AUDIO_CHANNELS 2 + int samples_left = AudioPlayer_Buffered(); + u32 num_audio_samples = samples_left < AudioPlayer_GetDesiredBuffered() ? SAMPLES_HIGH : SAMPLES_LOW; + // printf("Audio samples: %d %u\n", samples_left, num_audio_samples); + + // 3 is the maximum authentic frame divisor. + s16 audio_buffer[SAMPLES_HIGH * NUM_AUDIO_CHANNELS * 3]; + for (int i = 0; i < AUDIO_FRAMES_PER_UPDATE; i++) { + AudioMgr_CreateNextAudioBuffer(audio_buffer + i * (num_audio_samples * NUM_AUDIO_CHANNELS), num_audio_samples); + } + //for (uint32_t i = 0; i < 2 * num_audio_samples; i++) { + // audio_buffer[i] = Rand_Next() & 0xFF; + //} + // printf("Audio samples before submitting: %d\n", audio_api->buffered()); + AudioPlayer_Play((u8*)audio_buffer, num_audio_samples * (sizeof(int16_t) * NUM_AUDIO_CHANNELS * AUDIO_FRAMES_PER_UPDATE)); + + { + std::unique_lock Lock(audio.mutex); + audio.processing = false; + } + audio.cv_from_thread.notify_one(); + } + }).detach(); + } + + { + std::unique_lock Lock(audio.mutex); + audio.processing = true; + } + audio.cv_to_thread.notify_one(); + OTRGlobals::Instance->context->GetWindow()->RunCommands(commands); + { + std::unique_lock Lock(audio.mutex); + while (audio.processing) { + audio.cv_from_thread.wait(Lock); + } + } + // OTRTODO: FIGURE OUT END FRAME POINT /* if (OTRGlobals::Instance->context->GetWindow()->lastScancode != -1) OTRGlobals::Instance->context->GetWindow()->lastScancode = -1;*/ @@ -90,8 +161,8 @@ extern "C" void Graph_ProcessGfxCommands(Gfx* commands) { float divisor_num = 0.0f; -extern "C" void OTRSetFrameDivisor(int divisor) { - OTRGlobals::Instance->context->GetWindow()->SetFrameDivisor(divisor); +extern "C" void OTRGetPixelDepthPrepare(float x, float y) { + OTRGlobals::Instance->context->GetWindow()->GetPixelDepthPrepare(x, y); } extern "C" uint16_t OTRGetPixelDepth(float x, float y) { diff --git a/soh/soh/OTRGlobals.h b/soh/soh/OTRGlobals.h index 4f62f0694..20080fcb1 100644 --- a/soh/soh/OTRGlobals.h +++ b/soh/soh/OTRGlobals.h @@ -24,7 +24,7 @@ void Graph_ProcessFrame(void (*run_one_game_iter)(void)); void Graph_ProcessGfxCommands(Gfx* commands); void OTRLogString(const char* src); void OTRGfxPrint(const char* str, void* printer, void (*printImpl)(void*, char)); -void OTRSetFrameDivisor(int divisor); +void OTRGetPixelDepthPrepare(float x, float y); uint16_t OTRGetPixelDepth(float x, float y); int32_t OTRGetLastScancode(); uint32_t ResourceMgr_GetGameVersion(); diff --git a/soh/src/code/graph.c b/soh/src/code/graph.c index 9d2886ef1..34283f342 100644 --- a/soh/src/code/graph.c +++ b/soh/src/code/graph.c @@ -468,35 +468,6 @@ static void RunFrame() { uint64_t ticksA, ticksB; ticksA = GetPerfCounter(); - - OTRSetFrameDivisor(R_UPDATE_RATE); - //OTRSetFrameDivisor(0); - - - //AudioMgr_ThreadEntry(&gAudioMgr); - // 528 and 544 relate to 60 fps at 32 kHz 32000/60 = 533.333.. - // in an ideal world, one third of the calls should use num_samples=544 and two thirds num_samples=528 - #define SAMPLES_HIGH 560 - #define SAMPLES_LOW 528 - // PAL values - //#define SAMPLES_HIGH 656 - //#define SAMPLES_LOW 624 - #define AUDIO_FRAMES_PER_UPDATE (R_UPDATE_RATE > 0 ? R_UPDATE_RATE : 1 ) - #define NUM_AUDIO_CHANNELS 2 - int samples_left = AudioPlayer_Buffered(); - u32 num_audio_samples = samples_left < AudioPlayer_GetDesiredBuffered() ? SAMPLES_HIGH : SAMPLES_LOW; - // printf("Audio samples: %d %u\n", samples_left, num_audio_samples); - - // 3 is the maximum authentic frame divisor. - s16 audio_buffer[SAMPLES_HIGH * NUM_AUDIO_CHANNELS * 3]; - for (int i = 0; i < AUDIO_FRAMES_PER_UPDATE; i++) { - AudioMgr_CreateNextAudioBuffer(audio_buffer + i * (num_audio_samples * NUM_AUDIO_CHANNELS), num_audio_samples); - } - //for (uint32_t i = 0; i < 2 * num_audio_samples; i++) { - // audio_buffer[i] = Rand_Next() & 0xFF; - //} - // printf("Audio samples before submitting: %d\n", audio_api->buffered()); - AudioPlayer_Play((u8*)audio_buffer, num_audio_samples * (sizeof(int16_t) * NUM_AUDIO_CHANNELS * AUDIO_FRAMES_PER_UPDATE)); PadMgr_ThreadEntry(&gPadMgr); diff --git a/soh/src/code/z_kankyo.c b/soh/src/code/z_kankyo.c index afcd0f4fa..f3ac083ab 100644 --- a/soh/src/code/z_kankyo.c +++ b/soh/src/code/z_kankyo.c @@ -226,6 +226,9 @@ u16 Environment_GetPixelDepth(s32 x, s32 y) { void Environment_GraphCallback(GraphicsContext* gfxCtx, void* param) { GlobalContext* globalCtx = (GlobalContext*)param; + OTRGetPixelDepthPrepare(D_8015FD7E, D_8015FD80); + Lights_GlowCheckPrepare(globalCtx); + D_8011FB44 = Environment_GetPixelDepth(D_8015FD7E, D_8015FD80); Lights_GlowCheck(globalCtx); } diff --git a/soh/src/code/z_lights.c b/soh/src/code/z_lights.c index 2c7140c73..c9b60ceca 100644 --- a/soh/src/code/z_lights.c +++ b/soh/src/code/z_lights.c @@ -323,6 +323,44 @@ Lights* Lights_New(GraphicsContext* gfxCtx, u8 ambientR, u8 ambientG, u8 ambient return lights; } +void Lights_GlowCheckPrepare(GlobalContext* globalCtx) { + LightNode* node; + LightPoint* params; + Vec3f pos; + Vec3f multDest; + f32 wDest; + f32 wX; + f32 wY; + + node = globalCtx->lightCtx.listHead; + + while (node != NULL) { + params = &node->info->params.point; + + if (node->info->type == LIGHT_POINT_GLOW) { + f32 x, y; + u32 shrink; + uint32_t height; + + pos.x = params->x; + pos.y = params->y; + pos.z = params->z; + func_8002BE04(globalCtx, &pos, &multDest, &wDest); + wX = multDest.x * wDest; + wY = multDest.y * wDest; + + x = wX * 160 + 160; + y = wY * 120 + 120; + shrink = ShrinkWindow_GetCurrentVal(); + + if ((multDest.z > 1.0f) && y >= shrink && y <= SCREEN_HEIGHT - shrink) { + OTRGetPixelDepthPrepare(x, y); + } + } + node = node->next; + } +} + void Lights_GlowCheck(GlobalContext* globalCtx) { LightNode* node; LightPoint* params;