1
0
Fork 0
mirror of https://gitlab.com/Mr_Goldberg/goldberg_emulator.git synced 2024-11-14 10:50:13 +01:00
goldberg_emulator/overlay_experimental/OpenGL_Hook.cpp

155 lines
3.7 KiB
C++
Raw Normal View History

2019-07-31 22:19:44 +02:00
#include "OpenGL_Hook.h"
#include "Windows_Hook.h"
2019-07-31 22:19:44 +02:00
#include "Hook_Manager.h"
#include "../dll/dll.h"
#ifndef NO_OVERLAY
2019-07-31 22:19:44 +02:00
#include <imgui.h>
#include <impls/imgui_impl_opengl3.h>
#include <GL/glew.h>
2019-08-16 10:28:23 +02:00
#include "steam_overlay.h"
2019-07-31 22:19:44 +02:00
2019-08-16 19:10:12 +02:00
OpenGL_Hook* OpenGL_Hook::_inst = nullptr;
2019-07-31 22:19:44 +02:00
2019-08-16 19:10:12 +02:00
bool OpenGL_Hook::start_hook()
2019-07-31 22:19:44 +02:00
{
2019-08-25 21:22:25 +02:00
bool res = true;
2019-07-31 22:19:44 +02:00
if (!_hooked)
{
if (!Windows_Hook::Inst().start_hook())
return false;
2019-07-31 22:19:44 +02:00
GLenum err = glewInit();
if (err == GLEW_OK)
{
PRINT_DEBUG("Hooked OpenGL\n");
_hooked = true;
Hook_Manager::Inst().FoundRenderer(this);
wglSwapBuffers = (decltype(wglSwapBuffers))GetProcAddress(reinterpret_cast<HMODULE>(_library), "wglSwapBuffers");
UnhookAll();
BeginHook();
HookFuncs(
std::make_pair<void**, void*>(&(PVOID&)wglSwapBuffers, &OpenGL_Hook::MywglSwapBuffers)
);
EndHook();
get_steam_client()->steam_overlay->HookReady();
}
else
2019-07-31 22:19:44 +02:00
{
PRINT_DEBUG("Failed to hook OpenGL\n");
2019-07-31 22:19:44 +02:00
/* Problem: glewInit failed, something is seriously wrong. */
PRINT_DEBUG("Error: %s\n", glewGetErrorString(err));
2019-08-25 21:22:25 +02:00
res = false;
2019-07-31 22:19:44 +02:00
}
}
2019-08-16 19:10:12 +02:00
return true;
2019-07-31 22:19:44 +02:00
}
void OpenGL_Hook::resetRenderState()
{
if (initialized)
{
ImGui_ImplOpenGL3_Shutdown();
Windows_Hook::Inst().resetRenderState();
2019-07-31 22:19:44 +02:00
ImGui::DestroyContext();
initialized = false;
}
}
2019-08-14 14:55:31 +02:00
// Try to make this function and overlay's proc as short as possible or it might affect game's fps.
2019-07-31 22:19:44 +02:00
void OpenGL_Hook::prepareForOverlay(HDC hDC)
{
HWND hWnd = WindowFromDC(hDC);
RECT rect;
GetClientRect(hWnd, &rect);
if (hWnd != Windows_Hook::Inst().GetGameHwnd())
resetRenderState();
2019-07-31 22:19:44 +02:00
if (!initialized)
{
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = NULL;
ImGui_ImplOpenGL3_Init();
initialized = true;
}
ImGui_ImplOpenGL3_NewFrame();
Windows_Hook::Inst().prepareForOverlay(hWnd);
2019-07-31 22:19:44 +02:00
ImGui::NewFrame();
get_steam_client()->steam_overlay->OverlayProc(rect.right, rect.bottom);
2019-07-31 22:19:44 +02:00
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
/////////////////////////////////////////////////////////////////////////////////////
// OpenGL Initialization functions
//BOOL WINAPI OpenGL_Hook::MywglMakeCurrent(HDC hDC, HGLRC hGLRC)
//{
// auto res = hook->wglMakeCurrent(hDC, hGLRC);
// hook->hook_ogl();
// return res;
//}
2019-07-31 22:19:44 +02:00
/////////////////////////////////////////////////////////////////////////////////////
BOOL WINAPI OpenGL_Hook::MywglSwapBuffers(HDC hDC)
{
2019-08-16 19:10:12 +02:00
OpenGL_Hook::Inst()->prepareForOverlay(hDC);
return OpenGL_Hook::Inst()->wglSwapBuffers(hDC);
2019-07-31 22:19:44 +02:00
}
OpenGL_Hook::OpenGL_Hook():
initialized(false),
wglSwapBuffers(nullptr)
{
_library = LoadLibrary(DLL_NAME);
2019-07-31 22:19:44 +02:00
// Hook to wglMakeCurrent so we know when it gets called.
// If its called, then OpenGL will be used to render the overlay.
//wglMakeCurrent = (decltype(wglMakeCurrent))GetProcAddress(_dll, "wglMakeCurrent");
//wglSwapBuffers = (decltype(wglSwapBuffers))GetProcAddress(_dll, "wglSwapBuffers");
//
//BeginHook();
//HookFuncs(
// std::make_pair<void**, void*>(&(PVOID&)wglMakeCurrent, &OpenGL_Hook::MywglMakeCurrent)
//);
//EndHook();
2019-07-31 22:19:44 +02:00
}
OpenGL_Hook::~OpenGL_Hook()
{
PRINT_DEBUG("OpenGL Hook removed\n");
resetRenderState();
FreeLibrary(reinterpret_cast<HMODULE>(_library));
2019-07-31 22:19:44 +02:00
2019-08-16 19:10:12 +02:00
_inst = nullptr;
2019-07-31 22:19:44 +02:00
}
2019-08-16 19:10:12 +02:00
OpenGL_Hook* OpenGL_Hook::Inst()
2019-07-31 22:19:44 +02:00
{
2019-08-16 19:10:12 +02:00
if (_inst == nullptr)
_inst = new OpenGL_Hook;
return _inst;
2019-08-14 14:55:31 +02:00
}
#endif//NO_OVERLAY