diff --git a/src/app/main.cpp b/src/app/main.cpp index 3df229a..7258a6a 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -312,6 +312,7 @@ int main(int argc, char **argv) { fullscreen_thread.request_stop(); overlay_thread.request_stop(); hook->stop(); + overlay_thread.join(); overlay.shutdown(); engine.shutdown(); lizard::platform::shutdown_tray(); diff --git a/src/overlay/overlay.cpp b/src/overlay/overlay.cpp index 0883915..7694939 100644 --- a/src/overlay/overlay.cpp +++ b/src/overlay/overlay.cpp @@ -44,10 +44,6 @@ void stbi_image_free(void *); #include #endif -#ifdef __APPLE__ -#include -#endif - #ifndef LIZARD_TEST #include "embedded.h" #endif @@ -464,6 +460,8 @@ bool Overlay::init(const app::Config &cfg, std::optional glDeleteShader(vsId); glDeleteShader(fsId); + platform::clear_current_context(m_window); + #endif m_running = true; return true; @@ -716,29 +714,13 @@ void Overlay::apply_pending_config() { void Overlay::shutdown() { #ifndef LIZARD_TEST stop(); - // Release GL resources after the render loop has stopped -#ifdef _WIN32 - if (m_window.device && m_window.glContext) { - wglMakeCurrent((HDC)m_window.device, (HGLRC)m_window.glContext); - } -#elif defined(__linux__) - if (m_window.native && m_window.glContext) { - Display *dpy = glXGetCurrentDisplay(); - if (dpy) { - glXMakeCurrent(dpy, (GLXDrawable)m_window.native, (GLXContext)m_window.glContext); - } - } -#elif defined(__APPLE__) - if (m_window.glContext) { - auto makeCurrent = reinterpret_cast(objc_msgSend); - makeCurrent((id)m_window.glContext, sel_getUid("makeCurrentContext")); - } -#endif + platform::make_context_current(m_window); m_texture.reset(); m_vbo.reset(); m_instance.reset(); m_vao.reset(); m_program.reset(); + platform::clear_current_context(m_window); platform::destroy_window(m_window); #endif } @@ -888,12 +870,14 @@ void Overlay::render() { glBindVertexArray(m_vao.id); glBindTexture(GL_TEXTURE_2D, m_texture.id); glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, static_cast(m_badges.size())); + platform::swap_buffers(m_window); platform::poll_events(m_window); #endif } void Overlay::run(std::stop_token st) { #ifndef LIZARD_TEST + platform::make_context_current(m_window); using clock = std::chrono::steady_clock; auto last = clock::now(); while (m_running && !st.stop_requested()) { @@ -918,6 +902,7 @@ void Overlay::run(std::stop_token st) { std::this_thread::sleep_for(frame - spend); } } + platform::clear_current_context(m_window); stop(); #else (void)st; diff --git a/src/platform/linux/window.cpp b/src/platform/linux/window.cpp index dcc732d..b3ccff1 100644 --- a/src/platform/linux/window.cpp +++ b/src/platform/linux/window.cpp @@ -250,4 +250,26 @@ bool fullscreen_window_present() { return full; } +void make_context_current(Window &window) { + std::lock_guard lock(g_display_mutex); + if (g_display && window.native && window.glContext) { + glXMakeCurrent(g_display, static_cast(reinterpret_cast<::Window>(window.native)), + window.glContext); + } +} + +void clear_current_context(Window &) { + std::lock_guard lock(g_display_mutex); + if (g_display) { + glXMakeCurrent(g_display, None, nullptr); + } +} + +void swap_buffers(Window &window) { + std::lock_guard lock(g_display_mutex); + if (g_display && window.native) { + glXSwapBuffers(g_display, static_cast(reinterpret_cast<::Window>(window.native))); + } +} + } // namespace lizard::platform diff --git a/src/platform/mac/window.mm b/src/platform/mac/window.mm index de9557d..41f91fb 100644 --- a/src/platform/mac/window.mm +++ b/src/platform/mac/window.mm @@ -141,6 +141,26 @@ bool fullscreen_window_present() { } } +void make_context_current(Window &window) { + @autoreleasepool { + if (window.glContext) { + [(NSOpenGLContext *)window.glContext makeCurrentContext]; + } + } +} + +void clear_current_context(Window &) { + @autoreleasepool { [NSOpenGLContext clearCurrentContext]; } +} + +void swap_buffers(Window &window) { + @autoreleasepool { + if (window.glContext) { + [(NSOpenGLContext *)window.glContext flushBuffer]; + } + } +} + } // namespace lizard::platform #endif diff --git a/src/platform/win/window.cpp b/src/platform/win/window.cpp index 0a5d490..8c767bf 100644 --- a/src/platform/win/window.cpp +++ b/src/platform/win/window.cpp @@ -162,6 +162,20 @@ bool fullscreen_window_present() { return data.full; } +void make_context_current(Window &window) { + if (window.device && window.glContext) { + wglMakeCurrent(static_cast(window.device), static_cast(window.glContext)); + } +} + +void clear_current_context(Window &) { wglMakeCurrent(nullptr, nullptr); } + +void swap_buffers(Window &window) { + if (window.device) { + SwapBuffers(static_cast(window.device)); + } +} + } // namespace lizard::platform #endif diff --git a/src/platform/window.hpp b/src/platform/window.hpp index 6a98367..530f238 100644 --- a/src/platform/window.hpp +++ b/src/platform/window.hpp @@ -38,6 +38,9 @@ void destroy_window(Window &window); void poll_events(Window &window); bool fullscreen_window_present(); std::pair cursor_pos(); +void make_context_current(Window &window); +void clear_current_context(Window &window); +void swap_buffers(Window &window); #if defined(__linux__) void init_xlib_threads(); #endif