[OpenGL] Merge "shared" files back to imgui_hud.cpp

pull/131/head
jackun 4 years ago
parent b40fb95a04
commit f35e49defa
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -1,11 +1,19 @@
#include <cstdlib>
#include <functional>
#include <thread>
#include <string>
#include <iostream>
#include <memory>
#include <imgui.h>
#include "font_default.h"
#include "cpu.h"
#include "file_utils.h"
#include "imgui_hud_shared.h"
#include "imgui_hud.h"
#include "notify.h"
#ifdef HAVE_DBUS
#include "dbus_info.h"
#endif
#include <glad/glad.h>
@ -46,6 +54,29 @@ static state state;
static uint32_t vendorID;
static std::string deviceName;
static notify_thread notifier;
static bool cfg_inited = false;
static ImVec2 window_size;
static bool inited = false;
overlay_params params {};
// seems to quit by itself though
static std::unique_ptr<notify_thread, std::function<void(notify_thread *)>>
stop_it(&notifier, [](notify_thread *n){ stop_notifier(*n); });
void imgui_init()
{
if (cfg_inited)
return;
parse_overlay_config(&params, getenv("MANGOHUD_CONFIG"));
notifier.params = &params;
start_notifier(notifier);
window_size = ImVec2(params.width, params.height);
init_system_info();
cfg_inited = true;
init_cpu_stats(params);
}
//static
void imgui_create(void *ctx)
{
@ -91,7 +122,7 @@ void imgui_create(void *ctx)
ImGui::GetIO().IniFilename = NULL;
ImGui::GetIO().DisplaySize = ImVec2(last_vp[2], last_vp[3]);
VARIANT(ImGui_ImplOpenGL3_Init)();
ImGui_ImplOpenGL3_Init();
// Make a dummy GL call (we don't actually need the result)
// IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code.
// Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above.
@ -118,34 +149,8 @@ void imgui_create(void *ctx)
// Restore global context or ours might clash with apps that use Dear ImGui
ImGui::SetCurrentContext(saved_ctx);
}
/*
#ifdef IMGUI_GLX
void VARIANT(imgui_create)(void *ctx)
{
if (inited)
return;
if (!ctx)
return;
imgui_create(ctx);
}
#endif
#ifdef IMGUI_EGL
void VARIANT(imgui_create)(void *ctx)
{
if (inited)
return;
if (!ctx)
return;
imgui_create(ctx);
}
#endif*/
void VARIANT(imgui_shutdown)()
void imgui_shutdown()
{
#ifndef NDEBUG
std::cerr << __func__ << std::endl;
@ -153,26 +158,26 @@ void VARIANT(imgui_shutdown)()
if (state.imgui_ctx) {
ImGui::SetCurrentContext(state.imgui_ctx);
VARIANT(ImGui_ImplOpenGL3_Shutdown)();
ImGui_ImplOpenGL3_Shutdown();
ImGui::DestroyContext(state.imgui_ctx);
state.imgui_ctx = nullptr;
}
inited = false;
}
void VARIANT(imgui_set_context)(void *ctx)
void imgui_set_context(void *ctx)
{
if (!ctx) {
VARIANT(imgui_shutdown)();
imgui_shutdown();
return;
}
#ifndef NDEBUG
std::cerr << __func__ << ": " << ctx << std::endl;
#endif
VARIANT(imgui_create)(ctx);
imgui_create(ctx);
}
void VARIANT(imgui_render)(unsigned int width, unsigned int height)
void imgui_render(unsigned int width, unsigned int height)
{
if (!state.imgui_ctx)
return;
@ -184,7 +189,7 @@ void VARIANT(imgui_render)(unsigned int width, unsigned int height)
ImGui::SetCurrentContext(state.imgui_ctx);
ImGui::GetIO().DisplaySize = ImVec2(width, height);
VARIANT(ImGui_ImplOpenGL3_NewFrame)();
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
{
std::lock_guard<std::mutex> lk(notifier.mutex);
@ -194,7 +199,7 @@ void VARIANT(imgui_render)(unsigned int width, unsigned int height)
ImGui::PopStyleVar(3);
ImGui::Render();
VARIANT(ImGui_ImplOpenGL3_RenderDrawData)(ImGui::GetDrawData());
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
ImGui::SetCurrentContext(saved_ctx);
}

@ -1,13 +1,15 @@
#pragma once
#include "overlay.h"
#include "imgui_impl_opengl3.h"
namespace MangoHud { namespace GL {
void VARIANT(imgui_init)();
void VARIANT(imgui_create)(void *ctx);
void VARIANT(imgui_shutdown)();
void VARIANT(imgui_set_context)(void *ctx);
void VARIANT(imgui_render)(unsigned int width, unsigned int height);
extern overlay_params params;
void imgui_init();
void imgui_create(void *ctx);
void imgui_shutdown();
void imgui_set_context(void *ctx);
void imgui_render(unsigned int width, unsigned int height);
}} // namespace

@ -1,36 +0,0 @@
#include <cstdlib>
#include <functional>
#include <thread>
#include <iostream>
#include "imgui_hud_shared.h"
#ifdef HAVE_DBUS
#include "dbus_info.h"
#endif
namespace MangoHud { namespace GL {
notify_thread notifier;
static bool cfg_inited = false;
ImVec2 window_size;
bool inited = false;
overlay_params params {};
// seems to quit by itself though
static std::unique_ptr<notify_thread, std::function<void(notify_thread *)>>
stop_it(&notifier, [](notify_thread *n){ stop_notifier(*n); });
void imgui_init()
{
if (cfg_inited)
return;
parse_overlay_config(&params, getenv("MANGOHUD_CONFIG"));
notifier.params = &params;
start_notifier(notifier);
window_size = ImVec2(params.width, params.height);
init_system_info();
cfg_inited = true;
init_cpu_stats(params);
}
}} // namespaces

@ -1,15 +0,0 @@
#pragma once
#include <imgui.h>
#include "overlay.h"
#include "notify.h"
namespace MangoHud { namespace GL {
extern notify_thread notifier;
extern ImVec2 window_size;
extern bool inited;
extern overlay_params params;
void imgui_init();
}} // namespaces

@ -400,7 +400,7 @@ void GetOpenGLVersion(int& major, int& minor, bool& isGLES)
//}
}
bool VARIANT(ImGui_ImplOpenGL3_Init)(const char* glsl_version)
bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
{
GLint major = 0, minor = 0;
GetOpenGLVersion(major, minor, g_IsGLES);
@ -458,18 +458,18 @@ bool VARIANT(ImGui_ImplOpenGL3_Init)(const char* glsl_version)
return true;
}
void VARIANT(ImGui_ImplOpenGL3_Shutdown)()
void ImGui_ImplOpenGL3_Shutdown()
{
ImGui_ImplOpenGL3_DestroyDeviceObjects();
}
void VARIANT(ImGui_ImplOpenGL3_NewFrame)()
void ImGui_ImplOpenGL3_NewFrame()
{
if (!g_ShaderHandle)
ImGui_ImplOpenGL3_CreateDeviceObjects();
}
static void VARIANT(ImGui_ImplOpenGL3_SetupRenderState)(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object)
static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object)
{
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
glEnable(GL_BLEND);
@ -523,7 +523,7 @@ static void VARIANT(ImGui_ImplOpenGL3_SetupRenderState)(ImDrawData* draw_data, i
// OpenGL3 Render function.
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so.
void VARIANT(ImGui_ImplOpenGL3_RenderDrawData)(ImDrawData* draw_data)
void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
{
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
@ -584,7 +584,7 @@ void VARIANT(ImGui_ImplOpenGL3_RenderDrawData)(ImDrawData* draw_data)
if (g_GlVersion >= 3000)
glGenVertexArrays(1, &vertex_array_object);
VARIANT(ImGui_ImplOpenGL3_SetupRenderState)(draw_data, fb_width, fb_height, vertex_array_object);
ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
// Will project scissor/clipping rectangles into framebuffer space
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
@ -607,7 +607,7 @@ void VARIANT(ImGui_ImplOpenGL3_RenderDrawData)(ImDrawData* draw_data)
// User callback, registered via ImDrawList::AddCallback()
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
VARIANT(ImGui_ImplOpenGL3_SetupRenderState)(draw_data, fb_width, fb_height, vertex_array_object);
ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
else
pcmd->UserCallback(cmd_list, pcmd);
}

@ -25,33 +25,13 @@
namespace MangoHud {
/*
#if defined(IMGUI_GLX) && defined(IMGUI_EGL)
#error Both IMGUI_GLX and IMGUI_EGL can not be defined at the same time!
#elif !defined(IMGUI_GLX) && !defined(IMGUI_EGL)
#error Define IMGUI_GLX or IMGUI_EGL!
#endif
#undef VARIANT
#ifdef IMGUI_GLX
#define VARIANT(x) x##_GLX
#endif
#ifdef IMGUI_EGL
#define VARIANT(x) x##_EGL
#endif
*/
// FIXME might not be needed, GLX/EGL _can_ maybe live together
#define VARIANT(x) x
void GetOpenGLVersion(int& major, int& minor, bool& isGLES);
// Backend API
IMGUI_IMPL_API bool VARIANT(ImGui_ImplOpenGL3_Init)(const char* glsl_version = nullptr);
IMGUI_IMPL_API void VARIANT(ImGui_ImplOpenGL3_Shutdown)();
IMGUI_IMPL_API void VARIANT(ImGui_ImplOpenGL3_NewFrame)();
IMGUI_IMPL_API void VARIANT(ImGui_ImplOpenGL3_RenderDrawData)(ImDrawData* draw_data);
IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = nullptr);
IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown();
IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame();
IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data);
// (Optional) Called by Init/NewFrame/Shutdown
//IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture();

@ -8,7 +8,6 @@
#include <chrono>
#include <iomanip>
#include "imgui_hud_shared.h"
#include "imgui_hud.h"
using namespace MangoHud::GL;
@ -59,12 +58,12 @@ EXPORT_C_(unsigned int) eglSwapBuffers( void* dpy, void* surf)
//std::cerr << __func__ << "\n";
VARIANT(imgui_create)(surf);
imgui_create(surf);
int width=0, height=0;
if (pfn_eglQuerySurface(dpy, surf, 0x3056, &height) &&
pfn_eglQuerySurface(dpy, surf, 0x3057, &width))
VARIANT(imgui_render)(width, height);
imgui_render(width, height);
//std::cerr << "\t" << width << " x " << height << "\n";

@ -11,7 +11,6 @@
#include <chrono>
#include <iomanip>
#include "imgui_hud_shared.h"
#include "imgui_hud.h"
using namespace MangoHud::GL;
@ -66,7 +65,7 @@ EXPORT_C_(int) glXMakeCurrent(void* dpy, void* drawable, void* ctx) {
int ret = glx.MakeCurrent(dpy, drawable, ctx);
if (ret)
VARIANT(imgui_set_context)(ctx);
imgui_set_context(ctx);
if (params.gl_vsync >= -1) {
if (glx.SwapIntervalEXT)
@ -82,7 +81,7 @@ EXPORT_C_(int) glXMakeCurrent(void* dpy, void* drawable, void* ctx) {
EXPORT_C_(void) glXSwapBuffers(void* dpy, void* drawable) {
glx.Load();
VARIANT(imgui_create)(glx.GetCurrentContext());
imgui_create(glx.GetCurrentContext());
unsigned int width = -1, height = -1;
@ -105,7 +104,7 @@ EXPORT_C_(void) glXSwapBuffers(void* dpy, void* drawable) {
width = vp[2];
height = vp[3];*/
VARIANT(imgui_render)(width, height);
imgui_render(width, height);
glx.SwapBuffers(dpy, drawable);
if (fps_limit_stats.targetFrameTime > 0){
fps_limit_stats.frameStart = os_time_get_nano();

@ -56,7 +56,6 @@ vklayer_files = files(
)
opengl_files = files(
'gl/imgui_hud_shared.cpp',
'gl/glad.c',
'gl/imgui_impl_opengl3.cpp',
'gl/imgui_hud.cpp',

Loading…
Cancel
Save