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/windows/OpenGL_Hook.cpp

210 lines
4.9 KiB
C++
Raw Normal View History

2022-08-05 08:06:42 +02:00
/*
* Copyright (C) Nemirtingas
* This file is part of the ingame overlay project
*
* The ingame overlay project is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* The ingame overlay project is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the ingame overlay project; if not, see
* <http://www.gnu.org/licenses/>.
*/
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 <imgui.h>
2022-08-05 08:06:42 +02:00
#include <backends/imgui_impl_opengl3.h>
2019-07-31 22:19:44 +02:00
2022-08-05 08:06:42 +02:00
#include <glad/gl.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
2022-08-05 08:06:42 +02:00
bool OpenGL_Hook::StartHook(std::function<bool(bool)> key_combination_callback)
2019-07-31 22:19:44 +02:00
{
2022-08-05 08:06:42 +02:00
if (!_Hooked)
2019-07-31 22:19:44 +02:00
{
2022-08-05 08:06:42 +02:00
if (wglSwapBuffers == nullptr)
{
SPDLOG_WARN("Failed to hook OpenGL: Rendering functions missing.");
return false;
2022-08-05 08:06:42 +02:00
}
2019-07-31 22:19:44 +02:00
2022-08-05 08:06:42 +02:00
if (!Windows_Hook::Inst()->StartHook(key_combination_callback))
return false;
2022-08-05 08:06:42 +02:00
_WindowsHooked = true;
2022-08-05 08:06:42 +02:00
SPDLOG_INFO("Hooked OpenGL");
2022-08-05 08:06:42 +02:00
_Hooked = true;
2022-08-05 08:06:42 +02:00
UnhookAll();
BeginHook();
HookFuncs(
std::make_pair<void**, void*>(&(PVOID&)wglSwapBuffers, &OpenGL_Hook::MywglSwapBuffers)
);
EndHook();
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
}
2022-08-05 08:06:42 +02:00
bool OpenGL_Hook::IsStarted()
2019-07-31 22:19:44 +02:00
{
2022-08-05 08:06:42 +02:00
return _Hooked;
}
void OpenGL_Hook::_ResetRenderState()
{
if (_Initialized)
2019-07-31 22:19:44 +02:00
{
2022-08-05 08:06:42 +02:00
OverlayHookReady(false);
2019-07-31 22:19:44 +02:00
ImGui_ImplOpenGL3_Shutdown();
2022-08-10 09:22:23 +02:00
Windows_Hook::Inst()->ResetRenderState();
2019-07-31 22:19:44 +02:00
ImGui::DestroyContext();
2022-08-10 09:22:23 +02:00
_LastWindow = nullptr;
2022-08-05 08:06:42 +02:00
_Initialized = false;
2019-07-31 22:19:44 +02:00
}
}
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.
2022-08-05 08:06:42 +02:00
void OpenGL_Hook::_PrepareForOverlay(HDC hDC)
2019-07-31 22:19:44 +02:00
{
HWND hWnd = WindowFromDC(hDC);
2022-08-10 09:22:23 +02:00
if (hWnd != _LastWindow)
2022-08-05 08:06:42 +02:00
_ResetRenderState();
2022-08-05 08:06:42 +02:00
if (!_Initialized)
2019-07-31 22:19:44 +02:00
{
ImGui::CreateContext();
ImGui_ImplOpenGL3_Init();
2022-08-10 09:22:23 +02:00
_LastWindow = hWnd;
Windows_Hook::Inst()->SetInitialWindowSize(hWnd);
2022-08-05 08:06:42 +02:00
_Initialized = true;
OverlayHookReady(true);
2019-07-31 22:19:44 +02:00
}
2021-01-18 04:58:42 +01:00
2022-08-10 09:22:23 +02:00
if (ImGui_ImplOpenGL3_NewFrame() && Windows_Hook::Inst()->PrepareForOverlay(hWnd))
{
ImGui::NewFrame();
2019-07-31 22:19:44 +02:00
2022-08-05 08:06:42 +02:00
OverlayProc();
2019-07-31 22:19:44 +02:00
ImGui::Render();
2019-07-31 22:19:44 +02:00
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
2019-07-31 22:19:44 +02:00
}
BOOL WINAPI OpenGL_Hook::MywglSwapBuffers(HDC hDC)
{
2022-08-05 08:06:42 +02:00
auto inst = OpenGL_Hook::Inst();
inst->_PrepareForOverlay(hDC);
return inst->wglSwapBuffers(hDC);
2019-07-31 22:19:44 +02:00
}
OpenGL_Hook::OpenGL_Hook():
2022-08-05 08:06:42 +02:00
_Hooked(false),
_WindowsHooked(false),
_Initialized(false),
2022-08-10 09:22:23 +02:00
_LastWindow(nullptr),
2019-07-31 22:19:44 +02:00
wglSwapBuffers(nullptr)
{
}
OpenGL_Hook::~OpenGL_Hook()
{
2022-08-05 08:06:42 +02:00
SPDLOG_INFO("OpenGL Hook removed");
if (_WindowsHooked)
delete Windows_Hook::Inst();
2019-07-31 22:19:44 +02:00
2022-08-05 08:06:42 +02:00
if (_Initialized)
{
ImGui_ImplOpenGL3_Shutdown();
ImGui::DestroyContext();
}
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
}
2022-08-05 08:06:42 +02:00
std::string OpenGL_Hook::GetLibraryName() const
{
2022-08-05 08:06:42 +02:00
return LibraryName;
}
2022-08-05 08:06:42 +02:00
void OpenGL_Hook::LoadFunctions(wglSwapBuffers_t pfnwglSwapBuffers)
{
wglSwapBuffers = pfnwglSwapBuffers;
}
2022-08-05 08:06:42 +02:00
std::weak_ptr<uint64_t> OpenGL_Hook::CreateImageResource(const void* image_data, uint32_t width, uint32_t height)
{
GLuint* texture = new GLuint(0);
glGenTextures(1, texture);
if (glGetError() != GL_NO_ERROR)
{
delete texture;
return std::shared_ptr<uint64_t>(nullptr);
}
// Save old texture id
GLint oldTex;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTex);
glBindTexture(GL_TEXTURE_2D, *texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Upload pixels into texture
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
glBindTexture(GL_TEXTURE_2D, oldTex);
auto ptr = std::shared_ptr<uint64_t>((uint64_t*)texture, [](uint64_t* handle)
{
if (handle != nullptr)
{
GLuint* texture = (GLuint*)handle;
glDeleteTextures(1, texture);
delete texture;
}
});
_ImageResources.emplace(ptr);
return ptr;
}
void OpenGL_Hook::ReleaseImageResource(std::weak_ptr<uint64_t> resource)
{
auto ptr = resource.lock();
if (ptr)
{
auto it = _ImageResources.find(ptr);
if (it != _ImageResources.end())
_ImageResources.erase(it);
}
}