Skip to content

Commit e79d7ac

Browse files
authored
Merge pull request #100 from supermarsx/smx/implement-window-context-management-helpers-2025-11-02
Implement cross-platform GL context helpers for overlay
2 parents 84ae3a5 + 83a9b19 commit e79d7ac

6 files changed

Lines changed: 67 additions & 22 deletions

File tree

src/app/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ int main(int argc, char **argv) {
312312
fullscreen_thread.request_stop();
313313
overlay_thread.request_stop();
314314
hook->stop();
315+
overlay_thread.join();
315316
overlay.shutdown();
316317
engine.shutdown();
317318
lizard::platform::shutdown_tray();

src/overlay/overlay.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ void stbi_image_free(void *);
4545
#include <CoreGraphics/CoreGraphics.h>
4646
#endif
4747

48-
#ifdef __APPLE__
49-
#include <objc/message.h>
50-
#endif
51-
5248
#ifndef LIZARD_TEST
5349
#include "embedded.h"
5450
#endif
@@ -476,6 +472,8 @@ bool Overlay::init(const app::Config &cfg, std::optional<std::filesystem::path>
476472
glDeleteShader(vsId);
477473
glDeleteShader(fsId);
478474

475+
platform::clear_current_context(m_window);
476+
479477
#endif
480478
m_running = true;
481479
return true;
@@ -728,29 +726,13 @@ void Overlay::apply_pending_config() {
728726
void Overlay::shutdown() {
729727
#ifndef LIZARD_TEST
730728
stop();
731-
// Release GL resources after the render loop has stopped
732-
#ifdef _WIN32
733-
if (m_window.device && m_window.glContext) {
734-
wglMakeCurrent((HDC)m_window.device, (HGLRC)m_window.glContext);
735-
}
736-
#elif defined(__linux__)
737-
if (m_window.native && m_window.glContext) {
738-
Display *dpy = glXGetCurrentDisplay();
739-
if (dpy) {
740-
glXMakeCurrent(dpy, (GLXDrawable)m_window.native, (GLXContext)m_window.glContext);
741-
}
742-
}
743-
#elif defined(__APPLE__)
744-
if (m_window.glContext) {
745-
auto makeCurrent = reinterpret_cast<void (*)(id, SEL)>(objc_msgSend);
746-
makeCurrent((id)m_window.glContext, sel_getUid("makeCurrentContext"));
747-
}
748-
#endif
729+
platform::make_context_current(m_window);
749730
m_texture.reset();
750731
m_vbo.reset();
751732
m_instance.reset();
752733
m_vao.reset();
753734
m_program.reset();
735+
platform::clear_current_context(m_window);
754736
platform::destroy_window(m_window);
755737
#endif
756738
}
@@ -927,12 +909,14 @@ void Overlay::render() {
927909
glBindVertexArray(m_vao.id);
928910
glBindTexture(GL_TEXTURE_2D, m_texture.id);
929911
glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, static_cast<GLsizei>(m_badges.size()));
912+
platform::swap_buffers(m_window);
930913
platform::poll_events(m_window);
931914
#endif
932915
}
933916

934917
void Overlay::run(std::stop_token st) {
935918
#ifndef LIZARD_TEST
919+
platform::make_context_current(m_window);
936920
using clock = std::chrono::steady_clock;
937921
auto last = clock::now();
938922
while (m_running && !st.stop_requested()) {
@@ -958,6 +942,7 @@ void Overlay::run(std::stop_token st) {
958942
std::this_thread::sleep_for(frame - spend);
959943
}
960944
}
945+
platform::clear_current_context(m_window);
961946
stop();
962947
#else
963948
(void)st;

src/platform/linux/window.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,26 @@ bool fullscreen_window_present() {
250250
return full;
251251
}
252252

253+
void make_context_current(Window &window) {
254+
std::lock_guard<std::mutex> lock(g_display_mutex);
255+
if (g_display && window.native && window.glContext) {
256+
glXMakeCurrent(g_display, static_cast<GLXDrawable>(reinterpret_cast<::Window>(window.native)),
257+
window.glContext);
258+
}
259+
}
260+
261+
void clear_current_context(Window &) {
262+
std::lock_guard<std::mutex> lock(g_display_mutex);
263+
if (g_display) {
264+
glXMakeCurrent(g_display, None, nullptr);
265+
}
266+
}
267+
268+
void swap_buffers(Window &window) {
269+
std::lock_guard<std::mutex> lock(g_display_mutex);
270+
if (g_display && window.native) {
271+
glXSwapBuffers(g_display, static_cast<GLXDrawable>(reinterpret_cast<::Window>(window.native)));
272+
}
273+
}
274+
253275
} // namespace lizard::platform

src/platform/mac/window.mm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ bool fullscreen_window_present() {
141141
}
142142
}
143143

144+
void make_context_current(Window &window) {
145+
@autoreleasepool {
146+
if (window.glContext) {
147+
[(NSOpenGLContext *)window.glContext makeCurrentContext];
148+
}
149+
}
150+
}
151+
152+
void clear_current_context(Window &) {
153+
@autoreleasepool { [NSOpenGLContext clearCurrentContext]; }
154+
}
155+
156+
void swap_buffers(Window &window) {
157+
@autoreleasepool {
158+
if (window.glContext) {
159+
[(NSOpenGLContext *)window.glContext flushBuffer];
160+
}
161+
}
162+
}
163+
144164
} // namespace lizard::platform
145165

146166
#endif

src/platform/win/window.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ bool fullscreen_window_present() {
162162
return data.full;
163163
}
164164

165+
void make_context_current(Window &window) {
166+
if (window.device && window.glContext) {
167+
wglMakeCurrent(static_cast<HDC>(window.device), static_cast<HGLRC>(window.glContext));
168+
}
169+
}
170+
171+
void clear_current_context(Window &) { wglMakeCurrent(nullptr, nullptr); }
172+
173+
void swap_buffers(Window &window) {
174+
if (window.device) {
175+
SwapBuffers(static_cast<HDC>(window.device));
176+
}
177+
}
178+
165179
} // namespace lizard::platform
166180

167181
#endif

src/platform/window.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ void destroy_window(Window &window);
3838
void poll_events(Window &window);
3939
bool fullscreen_window_present();
4040
std::pair<float, float> cursor_pos();
41+
void make_context_current(Window &window);
42+
void clear_current_context(Window &window);
43+
void swap_buffers(Window &window);
4144
#if defined(__linux__)
4245
void init_xlib_threads();
4346
#endif

0 commit comments

Comments
 (0)