Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
user-grinch committed Oct 4, 2024
1 parent 7823b0e commit 11604d4
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 319 deletions.
2 changes: 1 addition & 1 deletion src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void ImGuiThread(void* param) {
}
}

if (!Hook::Inject(&ScriptExData::DrawFrames)) {
if (!Hook::Inject(&ScriptExData::RenderFrames)) {
MessageBox(HWND_DESKTOP, "Failed to inject dxhook..", "ImGuiRedux", MB_ICONERROR);
}

Expand Down
2 changes: 2 additions & 0 deletions src/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ void Hook::ProcessFrame(void* ptr) {
ScriptExData::SetScaling({scaleX, scaleY});
}

ScriptExData::InitRenderStates();

ImGui_ImplWin32_NewFrame();
if (gRenderer == eRenderer::Dx9) {
ImGui_ImplDX9_NewFrame();
Expand Down
109 changes: 109 additions & 0 deletions src/imguiframe.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#pragma once
#include "imgui.h"
#include <vector>
#include <functional>
#include <time.h>

extern enum class eGameVer;
extern eGameVer gGameVer;

struct FontInfo {
bool m_bFontLoaded = false;
ImFont *m_pFont = nullptr;
std::string m_Path;
float m_nSize = 14.0f;
size_t m_nStart, m_nEnd;
};

class ImGuiFrame {
public:
ImGuiContext *m_pContext = nullptr;

// Scaling related
ImVec2 m_vecScaling = ImVec2(1, 1);
bool m_bWasScalingUpdatedThisFrame;
bool m_bNeedToUpdateScaling;
long long m_nLastScriptCallMS;

// Render buffers
bool m_bIsBackBufferReady;
std::vector<std::function<void()>> m_RenderBuffer, m_BackBuffer;

// for ImGui::ImageButton()
ImVec4 m_vecImgTint = ImVec4(1, 1, 1, 1);
ImVec4 m_vecImgBgCol = ImVec4(1, 1, 1, 1);

// Fonts
std::vector<std::pair<size_t, size_t>> m_FontGlyphRange;
std::vector<FontInfo> m_FontTable;

ImGuiFrame() {
// m_pContext = ImGui::CreateContext();
}

ImGuiFrame& operator+=(std::function<void()> f) {
if (!m_bIsBackBufferReady) {
m_BackBuffer.push_back(f);
}
return *this;
}

void BeforeRender() {
// bool buildRequired = false;
// for (auto& e: m_FontTable) {
// if (!e.m_bFontLoaded) {
// ImWchar ranges[] = {
// e.m_nStart, e.m_nEnd, 0
// };
// ImGui::GetIO().Fonts->AddFontFromFileTTF(e.m_Path.c_str(), e.m_nSize, NULL, ranges);
// buildRequired = true;
// }
// }

// if (buildRequired) {
// ImGui::GetIO().Fonts->Build();
// }
}

void OnRender() {
for (auto func : m_RenderBuffer) {
func();
}

// if back buffer is render ready switch the buffer and reset render state
if (m_bIsBackBufferReady) {
m_RenderBuffer = std::move(m_BackBuffer);
m_bIsBackBufferReady = false;
}

time_t curTime = time(NULL);
// Clear buffer when script stops responding
bool scriptsPaused = false;
switch(static_cast<int>(gGameVer)) {
case 0: // III
scriptsPaused = *(bool*)0x95CD7C;
break;
case 1: // VC
scriptsPaused = *(bool*)0xA10B36;
break;
case 2: // SA
scriptsPaused = *(bool*)0xB7CB49;
break;
default:
break;
}

if (curTime-m_nLastScriptCallMS > 2 || scriptsPaused) {
OnClear();
}

if (m_bWasScalingUpdatedThisFrame) {
m_bNeedToUpdateScaling = false;
m_bWasScalingUpdatedThisFrame = false;
}
}

void OnClear() {
m_RenderBuffer.clear();
}
};
Loading

0 comments on commit 11604d4

Please sign in to comment.