aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/saffron_api.h5
-rw-r--r--src/saffron_window.c84
2 files changed, 89 insertions, 0 deletions
diff --git a/include/saffron_api.h b/include/saffron_api.h
index 5a8935e..3c67f25 100644
--- a/include/saffron_api.h
+++ b/include/saffron_api.h
@@ -50,4 +50,9 @@ SDL_GLContext saffron_window_get_gl_context(SaffronWindow* window);
void saffron_window_detach_gl_context(SaffronWindow* window);
void saffron_window_reattach_gl_context(SaffronWindow* window);
+
+EGLDisplay saffron_window_get_egl_display(SaffronWindow* window);
+EGLConfig saffron_window_get_egl_config(SaffronWindow* window);
+EGLSurface saffron_window_get_egl_surface(SaffronWindow* window);
+
#endif
diff --git a/src/saffron_window.c b/src/saffron_window.c
index 085ee20..022c5e4 100644
--- a/src/saffron_window.c
+++ b/src/saffron_window.c
@@ -10,6 +10,90 @@
#include <saffron.h>
#include <SDL3/SDL_egl.h>
#include <SDL3/SDL_opengles2.h>
+#include <EGL/egl.h>
+
+EGLDisplay saffron_window_get_egl_display(SaffronWindow* window) {
+ if (!window || !window->gl_context) {
+ return EGL_NO_DISPLAY;
+ }
+
+ EGLDisplay display = eglGetCurrentDisplay();
+
+ if (display == EGL_NO_DISPLAY) {
+ display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+ }
+
+ return display;
+}
+
+EGLConfig saffron_window_get_egl_config(SaffronWindow* window) {
+ if (!window || !window->gl_context) {
+ return NULL;
+ }
+
+ EGLDisplay display = saffron_window_get_egl_display(window);
+ if (display == EGL_NO_DISPLAY) {
+ return NULL;
+ }
+
+ EGLConfig config = NULL;
+ EGLint config_id;
+
+ if (eglQueryContext(display, eglGetCurrentContext(), EGL_CONFIG_ID, &config_id)) {
+ EGLint num_configs;
+ EGLConfig configs[32];
+ EGLint attribs[] = {
+ EGL_CONFIG_ID, config_id,
+ EGL_NONE
+ };
+
+ if (eglChooseConfig(display, attribs, configs, 32, &num_configs) && num_configs > 0) {
+ config = configs[0];
+ }
+ }
+
+ if (!config) {
+ EGLint config_attribs[] = {
+ EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
+ EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
+ EGL_RED_SIZE, 8,
+ EGL_GREEN_SIZE, 8,
+ EGL_BLUE_SIZE, 8,
+ EGL_ALPHA_SIZE, 8,
+ EGL_DEPTH_SIZE, 24,
+ EGL_STENCIL_SIZE, 8,
+ EGL_NONE
+ };
+
+ EGLint num_configs;
+ if (eglChooseConfig(display, config_attribs, &config, 1, &num_configs) && num_configs > 0) {
+ return config;
+ }
+ return NULL;
+ }
+
+ return config;
+}
+
+EGLSurface saffron_window_get_egl_surface(SaffronWindow* window) {
+ if (!window || !window->gl_context) {
+ return EGL_NO_SURFACE;
+ }
+
+ EGLSurface surface = eglGetCurrentSurface(EGL_DRAW);
+
+ if (surface == EGL_NO_SURFACE) {
+ EGLDisplay display = saffron_window_get_egl_display(window);
+ EGLConfig config = saffron_window_get_egl_config(window);
+
+ if (display != EGL_NO_DISPLAY && config) {
+ surface = eglCreateWindowSurface(display, config,
+ (EGLNativeWindowType)window->sdl_window, NULL);
+ }
+ }
+
+ return surface;
+}
SaffronWindow* saffron_window_new(const char* title, int w, int h) {
SaffronWindow* window = malloc(sizeof(SaffronWindow));