From 4e352be0f5828c401176b96af3b703e8a56afb79 Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Sat, 5 Sep 2026 21:34:22 +1200 Subject: [feat] add some more EGL functions that SFWK needs --- src/saffron_window.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'src/saffron_window.c') 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 #include #include +#include + +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)); -- cgit v1.2.3