27#ifndef GL_CONTEXT_MANAGER_H
28#define GL_CONTEXT_MANAGER_H
32#include <wx/glcanvas.h>
50 wxGLContext* CreateCtx( wxGLCanvas* aCanvas,
const wxGLContext* aOther =
nullptr );
59 void DestroyCtx( wxGLContext* aContext );
78 void LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas );
86 void UnlockCtx( wxGLContext* aContext );
105 auto it = m_glContexts.find( m_glCtx );
106 return it != m_glContexts.end() ? it->second :
nullptr;
114 template<
typename Func,
typename... Args>
117 wxGLContext* currentCtx = GetCurrentCtx();
118 wxGLCanvas* currentCanvas = GetCurrentCanvas();
119 UnlockCtx( currentCtx );
121 if constexpr (std::is_void_v<decltype(aFunction(std::forward<Args>(args)...))>)
123 std::forward<Func>(aFunction)(std::forward<Args>(args)...);
124 LockCtx( currentCtx, currentCanvas );
129 auto result = std::forward<Func>(aFunction)(std::forward<Args>(args)...);
130 LockCtx( currentCtx, currentCanvas );
wxGLContext * m_glCtx
Lock to prevent unexpected GL context switching.
wxGLContext * GetCurrentCtx() const
Get the currently bound GL context.
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
< Map of GL contexts & their parent canvases.
auto RunWithoutCtxLock(Func &&aFunction, Args &&... args)
Run the given function first releasing the GL context lock, then restoring it.
wxGLCanvas * GetCurrentCanvas() const
Get the currently bound GL canvas.