cae  0.0.0
Cross-API graphics engine
Loading...
Searching...
No Matches
EGLContext.cpp
Go to the documentation of this file.
1#ifdef __linux__
2
4
5#include <stdexcept>
6
7cae::EGLContext_::~EGLContext_()
8{
9 if (m_display != EGL_NO_DISPLAY)
10 {
11 eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
12 if (m_context != EGL_NO_CONTEXT)
13 {
14 eglDestroyContext(m_display, m_context);
15 }
16 if (m_surface != EGL_NO_SURFACE)
17 {
18 eglDestroySurface(m_display, m_surface);
19 }
20 eglTerminate(m_display);
21 }
22}
23
24void cae::EGLContext_::initialize(const NativeWindowHandle &window)
25{
26 if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE)
27 {
28 throw std::runtime_error("Failed to bind OpenGL API");
29 }
30
31 m_display = eglGetDisplay(window.display);
32 if (m_display == EGL_NO_DISPLAY)
33 {
34 throw std::runtime_error("Failed to get EGL display");
35 }
36
37 if (eglInitialize(m_display, nullptr, nullptr) == EGL_FALSE)
38 {
39 throw std::runtime_error("Failed to initialize EGL");
40 }
41
42 constexpr EGLint configAttribs[] = {EGL_RED_SIZE,
43 8,
44 EGL_GREEN_SIZE,
45 8,
46 EGL_BLUE_SIZE,
47 8,
48 EGL_DEPTH_SIZE,
49 24,
50 EGL_SURFACE_TYPE,
51 EGL_WINDOW_BIT,
52 EGL_RENDERABLE_TYPE,
53 EGL_OPENGL_BIT,
54 EGL_NONE};
55
56 EGLConfig config = nullptr;
57 EGLint numConfigs = 0;
58 if (eglChooseConfig(m_display, configAttribs, &config, 1, &numConfigs) == EGL_FALSE || numConfigs == 0)
59 {
60 throw std::runtime_error("Failed to choose EGL config");
61 }
62
63 m_surface =
64 eglCreateWindowSurface(m_display, config, reinterpret_cast<EGLNativeWindowType>(window.window), nullptr);
65 if (m_surface == EGL_NO_SURFACE)
66 {
67 throw std::runtime_error("Failed to create EGL surface");
68 }
69
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)
73 {
74 throw std::runtime_error("Failed to create EGL context");
75 }
76
77 if (eglMakeCurrent(m_display, m_surface, m_surface, m_context) == EGL_FALSE)
78 {
79 throw std::runtime_error("Failed to make EGL context current");
80 }
81
82 eglSwapInterval(m_display, 0);
83
84 if (gladLoadGLContext(&gl, eglGetProcAddress) == 0)
85 {
86 throw std::runtime_error("Failed to initialize GLAD");
87 }
88}
89
90void cae::EGLContext_::swapBuffers()
91{
92 if (m_display != EGL_NO_DISPLAY && m_surface != EGL_NO_SURFACE)
93 {
94 eglSwapBuffers(m_display, m_surface);
95 }
96}
97
98void cae::EGLContext_::setVSyncEnabled(const bool enabled)
99{
100 if (m_display != EGL_NO_DISPLAY)
101 {
102 eglSwapInterval(m_display, enabled ? 1 : 0);
103 }
104}
105
106#endif
This file contains the EGLContext_ class declaration.