27#ifndef GL_CONTEXT_MANAGER_H
28#define GL_CONTEXT_MANAGER_H
31#include <wx/glcanvas.h>
51 wxGLContext* CreateCtx( wxGLCanvas* aCanvas,
const wxGLContext* aOther =
nullptr );
60 void DestroyCtx( wxGLContext* aContext );
79 void LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas );
87 void UnlockCtx( wxGLContext* aContext );
106 auto it = m_glContexts.find( m_glCtx );
107 return it != m_glContexts.end() ? it->second :
nullptr;
115 template<
typename Func,
typename... Args>
118 wxGLContext* currentCtx = GetCurrentCtx();
119 wxGLCanvas* currentCanvas = GetCurrentCanvas();
120 UnlockCtx( currentCtx );
122 if constexpr (std::is_void_v<decltype(aFunction(std::forward<Args>(args)...))>)
124 std::forward<Func>(aFunction)(std::forward<Args>(args)...);
125 LockCtx( currentCtx, currentCanvas );
130 auto result = std::forward<Func>(aFunction)(std::forward<Args>(args)...);
131 LockCtx( currentCtx, currentCanvas );
wxGLContext * m_glCtx
Lock to prevent unexpected GL context switching.
GL_CONTEXT_MANAGER(const GL_CONTEXT_MANAGER &)
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.
void operator=(const GL_CONTEXT_MANAGER &)
wxGLCanvas * GetCurrentCanvas() const
Get the currently bound GL canvas.