try my way

This commit is contained in:
Михаил Капелько committed 2026-10-22 18:33:09 +03:00
1 parent 6fecc1e95e
commit fa1cd053c8
16 files changed
+521 -2

No files matched your search

@@ -0,0 +1,6 @@
#ifndef OGS_MAHJONG_RAYLIB_IOS_BRIDGING_HEADER_H
#define OGS_MAHJONG_RAYLIB_IOS_BRIDGING_HEADER_H
#import "raybriIOS.h"
#endif
+28
View File
@@ -0,0 +1,28 @@
#ifndef OGS_MAHJONG_RAYLIB_IOS_BRIDGE_H
#define OGS_MAHJONG_RAYLIB_IOS_BRIDGE_H
#ifdef __cplusplus
extern "C" {
#endif
// Initialise EGL (ANGLE) on the given CALayer and bring up raylib's rlgl.
// Unlike the ghera/raylib-iOS backend, no raylib window, view controller or
// app delegate is involved: the caller owns the layer and the frame timer.
// Returns 1 on success, 0 on failure.
int raybri_ios_init(void *layer, int width, int height);
// Recreate the EGL window surface for a new drawable size (backing pixels).
void raybri_ios_resize(int width, int height);
// Render one frame: clear the drawable to a time-based background colour and
// present it. `time` is monotonically increasing seconds.
void raybri_ios_render(double time);
// Release rlgl state and the EGL surface/context/display.
void raybri_ios_shutdown(void);
#ifdef __cplusplus
}
#endif
#endif
+236
View File
@@ -0,0 +1,236 @@
// 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.
#import <UIKit/UIKit.h>
#include "raybriIOS.h"
#include <math.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);
}
static void sr_hsv_to_rgb(float h, float s, float v, float *r, float *g, float *b)
{
float i = floorf(h * 6.0f);
float f = h * 6.0f - i;
float p = v * (1.0f - s);
float q = v * (1.0f - f * s);
float t = v * (1.0f - (1.0f - f) * s);
switch (((int)i) % 6)
{
case 0: *r = v; *g = t; *b = p; break;
case 1: *r = q; *g = v; *b = p; break;
case 2: *r = p; *g = v; *b = t; break;
case 3: *r = p; *g = q; *b = v; break;
case 4: *r = t; *g = p; *b = v; break;
default: *r = v; *g = p; *b = q; break;
}
}
// (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_ios_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_ios_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_ios_render(double time)
{
if (!sr_ready) return;
if (eglMakeCurrent(sr_display, sr_surface, sr_surface, sr_context) == EGL_FALSE) return;
// Background colour cycles through the hue wheel over time.
float hue = fmodf((float)time * 0.15f, 1.0f);
float r = 0.0f;
float g = 0.0f;
float b = 0.0f;
sr_hsv_to_rgb(hue, 0.65f, 0.85f, &r, &g, &b);
rlClearColor((unsigned char)(r * 255.0f), (unsigned char)(g * 255.0f), (unsigned char)(b * 255.0f), 255);
rlClearScreenBuffers();
eglSwapBuffers(sr_display, sr_surface);
}
void raybri_ios_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");
}
+60
View File
@@ -0,0 +1,60 @@
import UIKit
// UIView counterpart of the macOS RenderGLView. UIKit never draws into it: the
// EGL/ANGLE surface renders directly into this view's layer, and the timer in
// RenderVM presents a new frame. The view only reports when it gains a window
// and when its backing size changes.
final class RenderGLView: UIView {
private weak var renderVM: RenderVM?
private var didInit = false
private var lastPixelSize: [Int32] = [0, 0]
override init(frame: CGRect) {
super.init(frame: frame)
isOpaque = true
// Touch handling is not wired to raylib yet, so keep the GL view
// transparent to the SwiftUI hierarchy above it.
isUserInteractionEnabled = false
contentScaleFactor = UIScreen.main.scale
layer.contentsScale = contentScaleFactor
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func didMoveToWindow() {
super.didMoveToWindow()
guard let window else { return }
contentScaleFactor = window.screen.nativeScale
layer.contentsScale = contentScaleFactor
guard !didInit, let vm = RenderVM.shared else { return }
guard renderInit(vm) else { return }
renderVM = vm
didInit = true
lastPixelSize = renderPixelSize(self)
renderStartTimer(vm)
}
override func layoutSubviews() {
super.layoutSubviews()
guard didInit else { return }
let size = renderPixelSize(self)
guard size != lastPixelSize else { return }
lastPixelSize = size
raybri_ios_resize(size[0], size[1])
}
deinit {
guard didInit, let vm = renderVM else { return }
renderDeinit(vm)
}
}
+16
View File
@@ -0,0 +1,16 @@
import Combine
import Foundation
// iOS counterpart of the macOS RenderVM: it owns the view raylib draws into
// and the timer that drives it. The actual rendering lives in raybriIOS.mm.
final class RenderVM: ObservableObject {
static private(set) weak var shared: RenderVM?
let glView = RenderGLView(frame: .zero)
var startTime: CFTimeInterval = 0
var timer: Timer?
init() {
Self.shared = self
}
}
+11
View File
@@ -0,0 +1,11 @@
import SwiftUI
// SwiftUI counterpart of the macOS RenderView. It vends the shared GL view,
// which RenderVM also keeps alive and drives with a timer.
struct RenderView: UIViewRepresentable {
func makeUIView(context: Context) -> RenderGLView {
RenderVM.shared!.glView
}
func updateUIView(_ uiView: RenderGLView, context: Context) {}
}
+17
View File
@@ -0,0 +1,17 @@
import QuartzCore
import UIKit
// Render a single frame. The timer calls this directly: an EGL/ANGLE surface
// presents into the view's layer, so there is no draw(_:) / needsDisplay pass
// like in the NSOpenGLView on macOS.
func renderDraw(_ vm: RenderVM) {
raybri_ios_render(CACurrentMediaTime() - vm.startTime)
}
// Backing-pixel size of the view, which is what EGL/ANGLE renders into.
func renderPixelSize(_ view: UIView) -> [Int32] {
let scale = view.contentScaleFactor
let w = Int32(max(1, (view.bounds.width * scale).rounded()))
let h = Int32(max(1, (view.bounds.height * scale).rounded()))
return [w, h]
}
+3
View File
@@ -0,0 +1,3 @@
// iOS render constants. The macOS renderConst.swift depends on AppKit and the
// RaylibBridge SPM module, so the two platforms keep separate files.
let RENDER_FPS = 60.0
+35
View File
@@ -0,0 +1,35 @@
import Foundation
import QuartzCore
import UIKit
// Bring up EGL/ANGLE on the view's layer. Returns false when the surface could
// not be created (the caller then leaves the timer stopped).
func renderInit(_ vm: RenderVM) -> Bool {
let size = renderPixelSize(vm.glView)
let layer = Unmanaged.passUnretained(vm.glView.layer).toOpaque()
let ok = raybri_ios_init(layer, size[0], size[1]) != 0
if !ok {
otherPrintStderr("ИГР renderInit: EGL/ANGLE initialization failed")
}
return ok
}
// Same shape as the macOS timer: a repeating timer on the main run loop, but
// here it calls the renderer directly instead of flagging the view for redraw.
func renderStartTimer(_ vm: RenderVM) {
vm.startTime = CACurrentMediaTime()
let timer = Timer(timeInterval: 1.0 / RENDER_FPS, repeats: true) { [weak vm] _ in
guard let vm else { return }
renderDraw(vm)
}
vm.timer = timer
// .common keeps rendering alive while the user holds a control.
RunLoop.main.add(timer, forMode: .common)
}
func renderDeinit(_ vm: RenderVM) {
vm.timer?.invalidate()
vm.timer = nil
raybri_ios_shutdown()
}