Files
__mahjong/components/raybri/ios/raybriIOS.c
T

208 lines
5.2 KiB
C

// iOS counterpart of raybri.c.
//
// The prebuilt ghera/raylib-iOS xcframework is callback-driven: its rcore_ios.c
// defines main(), AppDelegate, SceneDelegate and GameViewController, so linking
// it would hand the whole app to raylib (and clash with SwiftUI's own main()).
// Instead this bridge compiles raylib's renderer (rlgl) directly and owns only
// the GL side. The view and the frame timer stay in ver-ios, exactly like the
// macOS build where RenderVM drives the NSOpenGLView.
//
// GL runs on ANGLE (libEGL + libGLESv2, Metal backend), taken from the same
// ghera release as raylib's headers.
#include "../raybri.h"
#include <stdio.h>
#include <libEGL/libEGL.h>
// rlgl's iOS branch includes <libGLESv2/GLES/glext.h> before gl2/gl3, so the
// GL_API/GL_APIENTRY macros have to come from glplatform.h first. ghera's
// build script does this with -include; we do it explicitly.
#include <libGLESv2/GLES/gl.h>
#define RLGL_IMPLEMENTATION
#include <rlgl.h>
static EGLDisplay sr_display = EGL_NO_DISPLAY;
static EGLSurface sr_surface = EGL_NO_SURFACE;
static EGLContext sr_context = EGL_NO_CONTEXT;
static EGLConfig sr_config = NULL;
static void *sr_layer = NULL;
static int sr_ready = 0;
static int sr_width = 1;
static int sr_height = 1;
static void sr_log(const char *text)
{
fprintf(stderr, "RBIOS: %s\n", text);
fflush(stderr);
}
// (Re)create the EGL window surface on the layer handed to us by UIKit.
static int sr_create_surface(void)
{
if (sr_surface != EGL_NO_SURFACE)
{
eglDestroySurface(sr_display, sr_surface);
sr_surface = EGL_NO_SURFACE;
}
// ANGLE on iOS takes a CALayer as the native window.
sr_surface = eglCreateWindowSurface(sr_display, sr_config, (EGLNativeWindowType)sr_layer, NULL);
if (sr_surface == EGL_NO_SURFACE)
{
sr_log("failed to create EGL window surface");
return 0;
}
if (eglMakeCurrent(sr_display, sr_surface, sr_surface, sr_context) == EGL_FALSE)
{
sr_log("failed to make the EGL context current");
return 0;
}
rlViewport(0, 0, sr_width, sr_height);
rlSetFramebufferWidth(sr_width);
rlSetFramebufferHeight(sr_height);
return 1;
}
int raybri_init(void *layer, int width, int height)
{
if (sr_ready) return 1;
if (layer == NULL)
{
sr_log("no drawable layer");
return 0;
}
sr_layer = layer;
sr_width = (width > 0) ? width : 1;
sr_height = (height > 0) ? height : 1;
sr_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (sr_display == EGL_NO_DISPLAY)
{
sr_log("failed to get the EGL display");
return 0;
}
if (eglInitialize(sr_display, NULL, NULL) == EGL_FALSE)
{
sr_log("failed to initialize EGL");
return 0;
}
if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE)
{
sr_log("failed to bind the OpenGL ES API");
return 0;
}
const EGLint config_attribs[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 16,
EGL_NONE
};
EGLint config_count = 0;
if (eglChooseConfig(sr_display, config_attribs, &sr_config, 1, &config_count) == EGL_FALSE || config_count < 1)
{
sr_log("failed to choose an EGL config");
return 0;
}
const EGLint context_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 3,
EGL_NONE
};
sr_context = eglCreateContext(sr_display, sr_config, EGL_NO_CONTEXT, context_attribs);
if (sr_context == EGL_NO_CONTEXT)
{
sr_log("failed to create an EGL context");
return 0;
}
if (!sr_create_surface()) return 0;
// With GL_GLEXT_PROTOTYPES rlgl calls the ANGLE entry points directly, but
// the call keeps parity with the platform backends and is harmless.
rlLoadExtensions((void *)eglGetProcAddress);
rlglInit(sr_width, sr_height);
sr_log("EGL/ANGLE + rlgl initialized");
sr_ready = 1;
return 1;
}
void raybri_resize(int width, int height)
{
if (!sr_ready) return;
if (width <= 0 || height <= 0) return;
if (width == sr_width && height == sr_height) return;
sr_width = width;
sr_height = height;
if (!sr_create_surface()) return;
sr_log("surface recreated");
}
void raybri_render(void)
{
if (!sr_ready) return;
if (eglMakeCurrent(sr_display, sr_surface, sr_surface, sr_context) == EGL_FALSE) return;
rlClearColor(0, 0, 255, 255);
rlClearScreenBuffers();
eglSwapBuffers(sr_display, sr_surface);
}
void raybri_shutdown(void)
{
if (!sr_ready) return;
eglMakeCurrent(sr_display, sr_surface, sr_surface, sr_context);
rlglClose();
eglMakeCurrent(sr_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (sr_surface != EGL_NO_SURFACE)
{
eglDestroySurface(sr_display, sr_surface);
sr_surface = EGL_NO_SURFACE;
}
if (sr_context != EGL_NO_CONTEXT)
{
eglDestroyContext(sr_display, sr_context);
sr_context = EGL_NO_CONTEXT;
}
if (sr_display != EGL_NO_DISPLAY)
{
eglTerminate(sr_display);
sr_display = EGL_NO_DISPLAY;
}
sr_layer = NULL;
sr_ready = 0;
sr_log("shutdown");
}