7cae::EGLContext_::~EGLContext_()
9 if (m_display != EGL_NO_DISPLAY)
11 eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
12 if (m_context != EGL_NO_CONTEXT)
14 eglDestroyContext(m_display, m_context);
16 if (m_surface != EGL_NO_SURFACE)
18 eglDestroySurface(m_display, m_surface);
20 eglTerminate(m_display);
24void cae::EGLContext_::initialize(
const NativeWindowHandle &window)
26 if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE)
28 throw std::runtime_error(
"Failed to bind OpenGL API");
31 m_display = eglGetDisplay(window.display);
32 if (m_display == EGL_NO_DISPLAY)
34 throw std::runtime_error(
"Failed to get EGL display");
37 if (eglInitialize(m_display,
nullptr,
nullptr) == EGL_FALSE)
39 throw std::runtime_error(
"Failed to initialize EGL");
42 constexpr EGLint configAttribs[] = {EGL_RED_SIZE,
56 EGLConfig config =
nullptr;
57 EGLint numConfigs = 0;
58 if (eglChooseConfig(m_display, configAttribs, &config, 1, &numConfigs) == EGL_FALSE || numConfigs == 0)
60 throw std::runtime_error(
"Failed to choose EGL config");
64 eglCreateWindowSurface(m_display, config,
reinterpret_cast<EGLNativeWindowType
>(window.window),
nullptr);
65 if (m_surface == EGL_NO_SURFACE)
67 throw std::runtime_error(
"Failed to create EGL surface");
70 constexpr EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE};
71 m_context = eglCreateContext(m_display, config, EGL_NO_CONTEXT, contextAttribs);
72 if (m_context == EGL_NO_CONTEXT)
74 throw std::runtime_error(
"Failed to create EGL context");
77 if (eglMakeCurrent(m_display, m_surface, m_surface, m_context) == EGL_FALSE)
79 throw std::runtime_error(
"Failed to make EGL context current");
82 eglSwapInterval(m_display, 0);
84 if (gladLoadGLContext(&gl, eglGetProcAddress) == 0)
86 throw std::runtime_error(
"Failed to initialize GLAD");
90void cae::EGLContext_::swapBuffers()
92 if (m_display != EGL_NO_DISPLAY && m_surface != EGL_NO_SURFACE)
94 eglSwapBuffers(m_display, m_surface);
98void cae::EGLContext_::setVSyncEnabled(
const bool enabled)
100 if (m_display != EGL_NO_DISPLAY)
102 eglSwapInterval(m_display, enabled ? 1 : 0);
This file contains the EGLContext_ class declaration.