Skip to content

Instantly share code, notes, and snippets.

@SasLuca
Created December 26, 2024 17:15
Show Gist options
  • Save SasLuca/307a523d2c6f2900af5823f0792a8a93 to your computer and use it in GitHub Desktop.
Save SasLuca/307a523d2c6f2900af5823f0792a8a93 to your computer and use it in GitHub Desktop.
How to use ANGLE with SDL3 on iOS
#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL_main.h>
#include <SDL3/SDL.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <GLES3/gl3.h>
struct AppState
{
int w;
int h;
SDL_Window* window;
EGLSurface egl_surface;
EGLDisplay egl_display;
};
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[])
{
auto app = new AppState();
*appstate = app;
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_VERBOSE);
SDL_Init(SDL_INIT_VIDEO);
app->window = SDL_CreateWindow("Experiment", 0, 0, SDL_WINDOW_METAL | SDL_WINDOW_HIGH_PIXEL_DENSITY);
SDL_SyncWindow(app->window);
SDL_GetWindowSizeInPixels(app->window, &app->w, &app->h);
auto metal_view = SDL_Metal_CreateView(app->window);
auto metal_layer = SDL_Metal_GetLayer(metal_view);
EGLAttrib egl_display_attribs[] = {
EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_METAL_ANGLE,
EGL_POWER_PREFERENCE_ANGLE, EGL_HIGH_POWER_ANGLE,
EGL_NONE
};
auto display = eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE, (void*) EGL_DEFAULT_DISPLAY, egl_display_attribs);
app->egl_display = display;
if (display == EGL_NO_DISPLAY)
{
printf("Failed to get EGL display");
return SDL_APP_FAILURE;
}
if (eglInitialize(display, NULL, NULL) == false)
{
printf("Failed to initialize EGL");
return SDL_APP_FAILURE;
}
EGLint egl_config_attribs[] = {
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 16,
EGL_STENCIL_SIZE, 8,
EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
EGL_NONE
};
EGLConfig config;
EGLint configs_count;
if (!eglChooseConfig(display, egl_config_attribs, &config, 1, &configs_count))
{
printf("Failed to choose EGL config");
return SDL_APP_FAILURE;
}
EGLint egl_context_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 3,
EGL_NONE
};
auto context = eglCreateContext(display, config, EGL_NO_CONTEXT, egl_context_attribs);
if (context == EGL_NO_CONTEXT) {
printf("Failed to create EGL context");
return SDL_APP_FAILURE;
}
auto surface = eglCreateWindowSurface(display, config, metal_layer, NULL);
app->egl_surface = surface;
if (surface == EGL_NO_SURFACE)
{
printf("Failed to create EGL surface");
return SDL_APP_FAILURE;
}
if (!eglMakeCurrent(display, surface, surface, context))
{
printf("Failed to make EGL context current");
return SDL_APP_FAILURE;
}
auto renderer = glGetString(GL_RENDERER);
auto vendor = glGetString(GL_VENDOR);
auto version = glGetString(GL_VERSION);
printf("\nOpenGL ES Renderer: %s, Vendor: %s, Version: %s\n", renderer, vendor, version);
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppIterate(void* appstate)
{
auto app = (AppState*) appstate;
auto now = ((double)SDL_GetTicks()) / 1000.0;
auto red = float(0.5 + 0.5 * SDL_sin(now));
auto green = float(0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 2 / 3));
auto blue = float(0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 4 / 3));
glClearColor(red, green, blue, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
eglSwapBuffers(app->egl_display, app->egl_surface);
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event)
{
if (event->type == SDL_EVENT_QUIT)
{
return SDL_APP_SUCCESS;
}
return SDL_APP_CONTINUE;
}
void SDL_AppQuit(void* appstate, SDL_AppResult result)
{
SDL_Quit();
}
@jeremyfa
Copy link

jeremyfa commented May 9, 2025

I managed to finally get something on screen via ANGLE on iOS with SDL3 thanks to your gist, so thanks a lot, it works! :D

@SasLuca
Copy link
Author

SasLuca commented May 9, 2025

Yo, I am glad I could help someone else also get this working. Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment