diff --git a/.gitignore b/.gitignore index 162f817..f46601f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ gradle-daemon-jvm.properties local.properties Package.resolved *.xcodeproj +Frameworks diff --git a/AGENTS.md b/AGENTS.md index 9026946..7b333ba 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -63,7 +63,7 @@ game/ver-mac/ macOS app (SPM executable target "ogs-mahjong") raybri/raybri.c -> symlink components/raybri/raybri.c raybri/raybri.h -> symlink components/raybri/raybri.h render/render.swift -> symlink components/render/render.swift - render/renderAux.swift -> symlink components/render/renderAux.swift + render/renderAux.swift -> symlink components/render/mac/renderAux.swift render/renderConst.swift -> symlink components/render/renderConst.swift render/renderConstShader.swift -> symlink .../renderConstShader.swift render/renderEffect.swift -> symlink components/render/renderEffect.swift @@ -99,7 +99,7 @@ src/other/other.swift -> ../../../../components/other/other.swift ( src/raybri/raybri.c -> ../../../../components/raybri/raybri.c (4 up) src/raybri/raybri.h -> ../../../../components/raybri/raybri.h (4 up) src/render/render.swift -> ../../../../components/render/render.swift (4 up) -src/render/renderAux.swift -> ../../../../components/render/renderAux.swift (4 up) +src/render/renderAux.swift -> ../../../../components/render/mac/renderAux.swift (4 up) src/render/renderConst.swift-> ../../../../components/render/renderConst.swift (4 up) src/render/renderConstShader.swift -> ../../../../components/render/renderConstShader.swift (4 up) src/render/renderEffect.swift -> ../../../../components/render/renderEffect.swift (4 up) diff --git a/README.md b/README.md index e528e84..6db0e2d 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,7 @@ * macOS `brew install raylib` + +* iOS + + `brew install xcodegen` diff --git a/components/chkgfx/ios/view/ChkgfxView.swift b/components/chkgfx/ios/view/ChkgfxView.swift new file mode 120000 index 0000000..3a1e13d --- /dev/null +++ b/components/chkgfx/ios/view/ChkgfxView.swift @@ -0,0 +1 @@ +../../mac/view/ChkgfxView.swift \ No newline at end of file diff --git a/components/raybri/ios/RaybriIOS-Bridging-Header.h b/components/raybri/ios/RaybriIOS-Bridging-Header.h new file mode 100644 index 0000000..0bc63c3 --- /dev/null +++ b/components/raybri/ios/RaybriIOS-Bridging-Header.h @@ -0,0 +1,6 @@ +#ifndef OGS_MAHJONG_RAYLIB_IOS_BRIDGING_HEADER_H +#define OGS_MAHJONG_RAYLIB_IOS_BRIDGING_HEADER_H + +#include "../raybri.h" + +#endif diff --git a/components/raybri/ios/raybriIOS.c b/components/raybri/ios/raybriIOS.c new file mode 100644 index 0000000..dbcd755 --- /dev/null +++ b/components/raybri/ios/raybriIOS.c @@ -0,0 +1,207 @@ +// 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 + +#include + +// rlgl's iOS branch includes 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 + +#define RLGL_IMPLEMENTATION +#include + +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"); +} diff --git a/components/raybri/raybri.h b/components/raybri/raybri.h index 5493ad3..19de147 100644 --- a/components/raybri/raybri.h +++ b/components/raybri/raybri.h @@ -1,6 +1,8 @@ #ifndef OGS_MAHJONG_RAYLIB_BRIDGE_H #define OGS_MAHJONG_RAYLIB_BRIDGE_H +#include + #ifdef __cplusplus extern "C" { #endif @@ -14,6 +16,30 @@ typedef struct raybri_config { float ground_size; // ground plane extent } raybri_config; +// The bridge always renders into a GL context owned by the app (no raylib +// window), but the platform setup and the current feature set differ: +// macOS renders the textured cube through raybri.c, iOS currently only clears +// its EGL/ANGLE drawable through raybriIOS.c. The common names keep the Swift +// call sites and lifecycle symmetrical. + +// Resize the drawable (backing pixels). On macOS this only records the size; +// on iOS it recreates the EGL window surface when it changes. +void raybri_resize(int width, int height); + +// Render one frame. +void raybri_render(void); + +// Release the GL/EGL resources owned by the bridge. +void raybri_shutdown(void); + +#if TARGET_OS_IPHONE + +// Initialise EGL (ANGLE) on the given CALayer and bring up rlgl. +// Returns 1 on success, 0 on failure. +int raybri_init(void *layer, int width, int height); + +#else + // Initialise the OpenGL-backed raylib scene (no raylib window is created). // The four shader sources come from renderConstShader.swift. void raybri_init( @@ -27,17 +53,10 @@ void raybri_init( // Load the textured cube glTF model. void raybri_load_model(const char *gltf_path); -// Resize the viewport. -void raybri_resize(int width, int height); - // Cube rotation, driven by the application (Euler angles, degrees). void raybri_set_rotation(float x_deg, float y_deg, float z_deg); -// Render one frame. -void raybri_render(void); - -// Release all GPU resources. -void raybri_shutdown(void); +#endif #ifdef __cplusplus } diff --git a/components/render/ios/RenderVM.swift b/components/render/ios/RenderVM.swift new file mode 120000 index 0000000..05cef35 --- /dev/null +++ b/components/render/ios/RenderVM.swift @@ -0,0 +1 @@ +../renderVM.swift \ No newline at end of file diff --git a/components/render/ios/renderAux.swift b/components/render/ios/renderAux.swift new file mode 100644 index 0000000..0b91a78 --- /dev/null +++ b/components/render/ios/renderAux.swift @@ -0,0 +1,16 @@ +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_render() +} + +// 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] +} diff --git a/components/render/ios/renderConst.swift b/components/render/ios/renderConst.swift new file mode 100644 index 0000000..11f8268 --- /dev/null +++ b/components/render/ios/renderConst.swift @@ -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 diff --git a/components/render/ios/renderEffect.swift b/components/render/ios/renderEffect.swift new file mode 100644 index 0000000..297492d --- /dev/null +++ b/components/render/ios/renderEffect.swift @@ -0,0 +1,32 @@ +import Foundation +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_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) { + 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_shutdown() +} diff --git a/components/render/ios/view/RenderGLView.swift b/components/render/ios/view/RenderGLView.swift new file mode 100644 index 0000000..4afda0d --- /dev/null +++ b/components/render/ios/view/RenderGLView.swift @@ -0,0 +1,58 @@ +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? + + 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 let vm = RenderVM.shared, !vm.didPrepareGL else { return } + guard renderInit(vm) else { return } + + renderVM = vm + vm.didPrepareGL = true + vm.pixelSize = renderPixelSize(self) + renderStartTimer(vm) + } + + override func layoutSubviews() { + super.layoutSubviews() + + guard let vm = renderVM, vm.didPrepareGL else { return } + + let size = renderPixelSize(self) + guard size != vm.pixelSize else { return } + + vm.pixelSize = size + raybri_resize(size[0], size[1]) + } + + deinit { + guard let vm = renderVM, vm.didPrepareGL else { return } + renderDeinit(vm) + } +} diff --git a/components/render/ios/view/RenderView.swift b/components/render/ios/view/RenderView.swift new file mode 100644 index 0000000..d10d3fe --- /dev/null +++ b/components/render/ios/view/RenderView.swift @@ -0,0 +1,9 @@ +import SwiftUI + +struct RenderView: UIViewRepresentable { + func makeUIView(context: Context) -> RenderGLView { + RenderVM.shared!.glView + } + + func updateUIView(_ uiView: RenderGLView, context: Context) {} +} diff --git a/components/render/renderAux.swift b/components/render/mac/renderAux.swift similarity index 100% rename from components/render/renderAux.swift rename to components/render/mac/renderAux.swift diff --git a/components/render/mac/view/RenderView.swift b/components/render/mac/view/RenderView.swift index 42a503f..bd154e9 100644 --- a/components/render/mac/view/RenderView.swift +++ b/components/render/mac/view/RenderView.swift @@ -1,7 +1,6 @@ import AppKit import SwiftUI -/// Render view for SwiftUI struct RenderView: NSViewRepresentable { func makeNSView(context: Context) -> RenderGLView { RenderVM.shared!.glView diff --git a/ogs-chkgfx/dialect.yml b/ogs-chkgfx/dialect.yml index 074422f..ac44a8a 100644 --- a/ogs-chkgfx/dialect.yml +++ b/ogs-chkgfx/dialect.yml @@ -20,48 +20,3 @@ ChkgfxContext: fields: didLaunch: Bool didSetup: Bool - -RenderContext: - type: context - fields: - backingPixelSize: [Int32] - didLaunch: Bool - didPrepareGL: Bool - didSetup: Bool - pixelSize: [Int32] - reshapePixelSize: [Int32] - willDeinit: Bool - -RenderComponent: - type: component - oneliners: - - didPrepareGL: renderInit(RenderVM.shared!) - - didPrepareGL: RenderVM.shared!.didPrepareGL = true - - didPrepareGL: renderStartTimer(RenderVM.shared!) - - didPrepareGL: renderLoadCubeModel(RenderVM.shared!) - - pixelSize: RenderVM.shared!.pixelSize = c.pixelSize - - willDeinit: renderDeinit(RenderVM.shared!) - shoulds: - didLaunch: - 1. Only once during the first setup: - if: - c.recentField == F.didSetup && - c.didLaunch == false - then: - c.didLaunch = true - pixelSize: - 1. Upon launch: - if: - c.recentField == F.didLaunch - then: - c.pixelSize = [1, 1] - 2. Upon resize: - if: - c.recentField == F.reshapePixelSize - then: - c.pixelSize = c.reshapePixelSize - 3. Upon DPI change: - if: - c.recentField == F.backingPixelSize - then: - c.pixelSize = c.backingPixelSize diff --git a/ogs-chkgfx/ver-ios/Assets.xcassets/AppIcon.appiconset/AppIcon.png b/ogs-chkgfx/ver-ios/Assets.xcassets/AppIcon.appiconset/AppIcon.png new file mode 100644 index 0000000..f2520ea Binary files /dev/null and b/ogs-chkgfx/ver-ios/Assets.xcassets/AppIcon.appiconset/AppIcon.png differ diff --git a/ogs-chkgfx/ver-ios/Assets.xcassets/AppIcon.appiconset/Contents.json b/ogs-chkgfx/ver-ios/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..ca34fdb --- /dev/null +++ b/ogs-chkgfx/ver-ios/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,14 @@ +{ + "images" : [ + { + "filename" : "AppIcon.png", + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} \ No newline at end of file diff --git a/ogs-chkgfx/ver-ios/Info.plist b/ogs-chkgfx/ver-ios/Info.plist new file mode 100644 index 0000000..1b3f948 --- /dev/null +++ b/ogs-chkgfx/ver-ios/Info.plist @@ -0,0 +1,26 @@ + + + + + CFBundleName + OGS chkgfx + CFBundleDisplayName + OGS chkgfx + CFBundleIdentifier + org.opengamestudio.chkgfx + CFBundleExecutable + ogs-chkgfx + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + UILaunchScreen + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + + + diff --git a/ogs-chkgfx/ver-ios/project.yml b/ogs-chkgfx/ver-ios/project.yml new file mode 100644 index 0000000..f90f1e1 --- /dev/null +++ b/ogs-chkgfx/ver-ios/project.yml @@ -0,0 +1,35 @@ +name: OGS chkgfx +options: + bundleIdPrefix: org.opengamestudio +targets: + ogs-chkgfx: + type: application + platform: iOS + deploymentTarget: "15.6" + sources: + - path: Assets.xcassets + - path: src + - path: ../../components/render/ios + - path: ../../components/raybri/ios + dependencies: + - framework: Frameworks/libEGL.xcframework + embed: true + - framework: Frameworks/libGLESv2.xcframework + embed: true + settings: + base: + INFOPLIST_FILE: Info.plist + PRODUCT_BUNDLE_IDENTIFIER: org.opengamestudio.chkgfx + SWIFT_OBJC_BRIDGING_HEADER: ../../components/raybri/ios/RaybriIOS-Bridging-Header.h + HEADER_SEARCH_PATHS: + - "$(SRCROOT)/../../components/raybri/ios" + - "$(SRCROOT)/../../components/render/ios" + - "$(SRCROOT)/Frameworks/include" + - "$(SRCROOT)/Frameworks/raylib.xcframework/ios-arm64_x86_64-simulator/raylib.framework/Headers" + - "$(SRCROOT)/Frameworks/raylib.xcframework/ios-arm64/raylib.framework/Headers" + GCC_PREPROCESSOR_DEFINITIONS: + - PLATFORM_IOS=1 + - GRAPHICS_API_OPENGL_ES3=1 + - GL_GLEXT_PROTOTYPES=1 + LD_RUNPATH_SEARCH_PATHS: + - "$(inherited) @executable_path/Frameworks" diff --git a/ogs-chkgfx/ver-ios/src/AppDelegate.swift b/ogs-chkgfx/ver-ios/src/AppDelegate.swift new file mode 100644 index 0000000..5ae97d0 --- /dev/null +++ b/ogs-chkgfx/ver-ios/src/AppDelegate.swift @@ -0,0 +1,20 @@ +import UIKit + +final class AppDelegate: + NSObject, + UIApplicationDelegate +{ + let chkgfx = ChkgfxComponent() + let chkgfxVM = ChkgfxVM() + let renderVM = RenderVM() + + func application( + _ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil + ) -> Bool { + otherSetupConsoleLogging(chkgfx.ctrl, "Chkgfx") + chkgfx.setup() + + return true + } +} diff --git a/ogs-chkgfx/ver-ios/src/ChkgfxApp.swift b/ogs-chkgfx/ver-ios/src/ChkgfxApp.swift new file mode 100644 index 0000000..cf20ef5 --- /dev/null +++ b/ogs-chkgfx/ver-ios/src/ChkgfxApp.swift @@ -0,0 +1,16 @@ +import SwiftUI + +@main +struct ChkgfxApp: App { + @UIApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate + + var body: some Scene { + WindowGroup { + ZStack { + RenderView() + .ignoresSafeArea() + ChkgfxView(appDelegate.chkgfxVM) + } + } + } +} diff --git a/ogs-chkgfx/ver-ios/src/chkgfx/chkgfx.swift b/ogs-chkgfx/ver-ios/src/chkgfx/chkgfx.swift new file mode 120000 index 0000000..3c6bb5e --- /dev/null +++ b/ogs-chkgfx/ver-ios/src/chkgfx/chkgfx.swift @@ -0,0 +1 @@ +../../../../components/chkgfx/chkgfx.swift \ No newline at end of file diff --git a/ogs-chkgfx/ver-ios/src/chkgfx/chkgfxVM.swift b/ogs-chkgfx/ver-ios/src/chkgfx/chkgfxVM.swift new file mode 120000 index 0000000..cf74ca4 --- /dev/null +++ b/ogs-chkgfx/ver-ios/src/chkgfx/chkgfxVM.swift @@ -0,0 +1 @@ +../../../../components/chkgfx/chkgfxVM.swift \ No newline at end of file diff --git a/ogs-chkgfx/ver-ios/src/chkgfx/view/ChkgfxView.swift b/ogs-chkgfx/ver-ios/src/chkgfx/view/ChkgfxView.swift new file mode 120000 index 0000000..6c25613 --- /dev/null +++ b/ogs-chkgfx/ver-ios/src/chkgfx/view/ChkgfxView.swift @@ -0,0 +1 @@ +../../../../../components/chkgfx/ios/view/ChkgfxView.swift \ No newline at end of file diff --git a/ogs-chkgfx/ver-ios/src/dialect.swift b/ogs-chkgfx/ver-ios/src/dialect.swift new file mode 120000 index 0000000..a931c95 --- /dev/null +++ b/ogs-chkgfx/ver-ios/src/dialect.swift @@ -0,0 +1 @@ +../../ver-mac/src/dialect.swift \ No newline at end of file diff --git a/ogs-chkgfx/ver-ios/src/other/other.swift b/ogs-chkgfx/ver-ios/src/other/other.swift new file mode 120000 index 0000000..f91488c --- /dev/null +++ b/ogs-chkgfx/ver-ios/src/other/other.swift @@ -0,0 +1 @@ +../../../../components/other/other.swift \ No newline at end of file diff --git a/ogs-chkgfx/ver-mac/src/ChkgfxApp.swift b/ogs-chkgfx/ver-mac/src/ChkgfxApp.swift index 105f2dc..090c94b 100644 --- a/ogs-chkgfx/ver-mac/src/ChkgfxApp.swift +++ b/ogs-chkgfx/ver-mac/src/ChkgfxApp.swift @@ -1,4 +1,3 @@ -import AppKit import SwiftUI @main diff --git a/ogs-chkgfx/ver-mac/src/dialect.swift b/ogs-chkgfx/ver-mac/src/dialect.swift index db74862..8b3bd0a 100644 --- a/ogs-chkgfx/ver-mac/src/dialect.swift +++ b/ogs-chkgfx/ver-mac/src/dialect.swift @@ -119,13 +119,8 @@ func registerOneliners( // Context field names for static type check public struct F { - public static let backingPixelSize = "backingPixelSize" public static let didLaunch = "didLaunch" - public static let didPrepareGL = "didPrepareGL" public static let didSetup = "didSetup" - public static let pixelSize = "pixelSize" - public static let reshapePixelSize = "reshapePixelSize" - public static let willDeinit = "willDeinit" } public struct ChkgfxContext: DialectContext { @@ -159,72 +154,6 @@ public struct ChkgfxContext: DialectContext { } } -public struct RenderContext: DialectContext { - public var backingPixelSize = [Int32]() - public var didLaunch = Bool() - public var didPrepareGL = Bool() - public var didSetup = Bool() - public var pixelSize = [Int32]() - public var reshapePixelSize = [Int32]() - public var willDeinit = Bool() - - public var recentField = "" - - public func field(_ name: String) -> T { - if (name == "backingPixelSize") { - return backingPixelSize as! T - } - else if (name == "didLaunch") { - return didLaunch as! T - } - else if (name == "didPrepareGL") { - return didPrepareGL as! T - } - else if (name == "didSetup") { - return didSetup as! T - } - else if (name == "pixelSize") { - return pixelSize as! T - } - else if (name == "reshapePixelSize") { - return reshapePixelSize as! T - } - else if (name == "willDeinit") { - return willDeinit as! T - } - - return "unknown-field-name" as! T - } - - public mutating func setField( - _ name: String, - _ value: Any - ) { - if (name == "backingPixelSize") { - backingPixelSize = value as! [Int32] - } - else if (name == "didLaunch") { - didLaunch = value as! Bool - } - else if (name == "didPrepareGL") { - didPrepareGL = value as! Bool - } - else if (name == "didSetup") { - didSetup = value as! Bool - } - else if (name == "pixelSize") { - pixelSize = value as! [Int32] - } - else if (name == "reshapePixelSize") { - reshapePixelSize = value as! [Int32] - } - else if (name == "willDeinit") { - willDeinit = value as! Bool - } - - } -} - public func chkgfxSet( _ key: String, _ value: Any @@ -232,13 +161,6 @@ public func chkgfxSet( ChkgfxComponent.singleton!.ctrl.set(key, value) } -public func renderSet( - _ key: String, - _ value: Any -) { - RenderComponent.singleton!.ctrl.set(key, value) -} - func chkgfxShouldResetDidLaunch(_ c: ChkgfxContext) -> ChkgfxContext { var c = c @@ -257,59 +179,6 @@ func chkgfxShouldResetDidLaunch(_ c: ChkgfxContext) -> ChkgfxContext { return c } -func renderShouldResetDidLaunch(_ c: RenderContext) -> RenderContext { - var c = c - - /* 1. Only once during the first setup */ - if - c.recentField == F.didSetup && - c.didLaunch == false - { - c.didLaunch = true - c.recentField = F.didLaunch - return c - } - - - c.recentField = DIALECT_CONTEXT_RECENT_FIELD_NONE - return c -} - -func renderShouldResetPixelSize(_ c: RenderContext) -> RenderContext { - var c = c - - /* 1. Upon launch */ - if - c.recentField == F.didLaunch - { - c.pixelSize = [1, 1] - c.recentField = F.pixelSize - return c - } - - /* 2. Upon resize */ - if - c.recentField == F.reshapePixelSize - { - c.pixelSize = c.reshapePixelSize - c.recentField = F.pixelSize - return c - } - - /* 3. Upon DPI change */ - if - c.recentField == F.backingPixelSize - { - c.pixelSize = c.backingPixelSize - c.recentField = F.pixelSize - return c - } - - - c.recentField = DIALECT_CONTEXT_RECENT_FIELD_NONE - return c -} - func chkgfxRegisterShoulds(_ ctrl: DialectController) { [ chkgfxShouldResetDidLaunch, @@ -318,25 +187,3 @@ func chkgfxRegisterShoulds(_ ctrl: DialectController) { ctrl.registerFunction { c in f(c as! ChkgfxContext) } } } - -func renderRegisterShoulds(_ ctrl: DialectController) { - [ - renderShouldResetDidLaunch, - renderShouldResetPixelSize, - - ].forEach { f in - ctrl.registerFunction { c in f(c as! RenderContext) } - } -} - -func renderRegisterEffects(_ ctrl: DialectController) { - let _: RenderContext? = registerOneliners(ctrl, [ - F.didPrepareGL, { (c: RenderContext) in renderInit(RenderVM.shared!) }, - F.didPrepareGL, { (c: RenderContext) in RenderVM.shared!.didPrepareGL = true }, - F.didPrepareGL, { (c: RenderContext) in renderStartTimer(RenderVM.shared!) }, - F.didPrepareGL, { (c: RenderContext) in renderLoadCubeModel(RenderVM.shared!) }, - F.pixelSize, { (c: RenderContext) in RenderVM.shared!.pixelSize = c.pixelSize }, - F.willDeinit, { (c: RenderContext) in renderDeinit(RenderVM.shared!) }, - - ]) -} diff --git a/ogs-chkgfx/ver-mac/src/render/renderAux.swift b/ogs-chkgfx/ver-mac/src/render/renderAux.swift index 1975b82..a7dec7f 120000 --- a/ogs-chkgfx/ver-mac/src/render/renderAux.swift +++ b/ogs-chkgfx/ver-mac/src/render/renderAux.swift @@ -1 +1 @@ -../../../../components/render/renderAux.swift \ No newline at end of file +../../../../components/render/mac/renderAux.swift \ No newline at end of file diff --git a/ogs-mahjong/ver-mac/src/GameApp.swift b/ogs-mahjong/ver-mac/src/MahjongApp.swift similarity index 90% rename from ogs-mahjong/ver-mac/src/GameApp.swift rename to ogs-mahjong/ver-mac/src/MahjongApp.swift index 449a921..3d71dfe 100644 --- a/ogs-mahjong/ver-mac/src/GameApp.swift +++ b/ogs-mahjong/ver-mac/src/MahjongApp.swift @@ -1,8 +1,7 @@ -import AppKit import SwiftUI @main -struct GameApp: App { +struct MahjongApp: App { @NSApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate var body: some Scene { diff --git a/ogs-mahjong/ver-mac/src/render/renderAux.swift b/ogs-mahjong/ver-mac/src/render/renderAux.swift index 1975b82..a7dec7f 120000 --- a/ogs-mahjong/ver-mac/src/render/renderAux.swift +++ b/ogs-mahjong/ver-mac/src/render/renderAux.swift @@ -1 +1 @@ -../../../../components/render/renderAux.swift \ No newline at end of file +../../../../components/render/mac/renderAux.swift \ No newline at end of file diff --git a/util/build-ogs-chkgfx-ios b/util/build-ogs-chkgfx-ios new file mode 100755 index 0000000..e8f26c1 --- /dev/null +++ b/util/build-ogs-chkgfx-ios @@ -0,0 +1,14 @@ +#!/bin/bash -e +SDIR=$(cd "$(dirname "$0")" ; pwd -P) + +T=$SDIR/step +source $SDIR/paths + +TARGET=ogs-chkgfx + +STEP=0 +source $T/generateDialect $TARGET +source $T/prepareIOS $TARGET + +cd $SDIR/../$TARGET/ver-ios +xcodebuild -scheme "ogs-chkgfx" -sdk iphonesimulator -destination 'generic/platform=iOS Simulator' -derivedDataPath .build build diff --git a/util/prepare-ogs-chkgfx-ios b/util/prepare-ogs-chkgfx-ios new file mode 100755 index 0000000..80c6be5 --- /dev/null +++ b/util/prepare-ogs-chkgfx-ios @@ -0,0 +1,11 @@ +#!/bin/bash -e +SDIR=$(cd "$(dirname "$0")" ; pwd -P) + +T=$SDIR/step +source $SDIR/paths + +TARGET=ogs-chkgfx + +STEP=0 +source $T/generateDialect $TARGET +source $T/prepareIOS $TARGET diff --git a/util/run-ogs-chkgfx-ios b/util/run-ogs-chkgfx-ios new file mode 100755 index 0000000..d0ba3fd --- /dev/null +++ b/util/run-ogs-chkgfx-ios @@ -0,0 +1,26 @@ +#!/bin/bash -e +SDIR=$(cd "$(dirname "$0")" ; pwd -P) + +T=$SDIR/step +source $SDIR/paths + +TARGET=ogs-chkgfx +BUNDLE_ID=org.opengamestudio.chkgfx +APP="$SDIR/../$TARGET/ver-ios/.build/Build/Products/Debug-iphonesimulator/ogs-chkgfx.app" + +if [[ ! -d "$APP" ]]; then + echo "iOS app not built yet, run util/build-ogs-chkgfx-ios" + exit 1 +fi + +DEVICE=${DEVICE:-"iPhone 15 Pro"} +UDID=$(xcrun simctl list devices available | grep -m1 "$DEVICE (" | sed -E 's/.*\(([0-9A-F-]{36})\).*/\1/') +if [[ -z "$UDID" ]]; then + echo "Simulator '$DEVICE' not found" + exit 1 +fi + +xcrun simctl boot "$UDID" 2>/dev/null || true +sleep 2 +xcrun simctl install "$UDID" "$APP" +xcrun simctl launch "$UDID" "$BUNDLE_ID" diff --git a/util/step/prepareIOS b/util/step/prepareIOS new file mode 100755 index 0000000..45023b4 --- /dev/null +++ b/util/step/prepareIOS @@ -0,0 +1,49 @@ +#!/bin/bash -e + +TARGET=$1 + +STEP=$((STEP+1)) +echo -e "\n> > > > Step №$STEP. Generate iOS project for '$TARGET'" + +# iOS rendering goes through ghera/raylib-iOS: a raylib fork that renders +# OpenGL ES via ANGLE (Metal backend). Its stable releases ship prebuilt +# xcframeworks (raylib + libEGL + libGLESv2, ~46 MB), so they are downloaded +# on demand and never committed. +# +# Unlike the fork's own app, ver-ios does not link raylib.xcframework (its +# rcore.o defines main() and a private view controller). We only use its +# headers: raylib's renderer (rlgl) is compiled into components/raybri/ios, +# and EGL/ANGLE provides the GL context. +RAYLIB_IOS_TAG=6.0.7-iOS +FRAMEWORKS=$SDIR/../$TARGET/ver-ios/Frameworks + +MISSING=0 +for FRAMEWORK in raylib libEGL libGLESv2; do + [[ -d "$FRAMEWORKS/$FRAMEWORK.xcframework" ]] || MISSING=1 +done + +if [[ $MISSING -eq 1 ]]; then + echo "Raylib iOS xcframeworks missing, downloading $RAYLIB_IOS_TAG..." + mkdir -p "$FRAMEWORKS" + TMP=$(mktemp -d) + curl -sL --retry 3 -o "$TMP/raylib-ios.zip" \ + "https://github.com/ghera/raylib-iOS/releases/download/$RAYLIB_IOS_TAG/raylib-ios-xcframeworks-$RAYLIB_IOS_TAG.zip" + unzip -q "$TMP/raylib-ios.zip" -d "$TMP" + cp -R "$TMP/raylib-ios-xcframeworks/raylib.xcframework" "$FRAMEWORKS/" + cp -R "$TMP/raylib-ios-xcframeworks/libEGL.xcframework" "$FRAMEWORKS/" + cp -R "$TMP/raylib-ios-xcframeworks/libGLESv2.xcframework" "$FRAMEWORKS/" + rm -rf "$TMP" +fi + +# The ANGLE headers are shipped as framework Headers directories but their +# internal includes expect the `libEGL/...` and `libGLESv2/...` layout, so +# stage symlinks for the header search path. The headers are identical for +# device and simulator, so one slice is enough. +mkdir -p "$FRAMEWORKS/include" +ln -sfn "../libEGL.xcframework/ios-arm64_x86_64-simulator/libEGL.framework/Headers" \ + "$FRAMEWORKS/include/libEGL" +ln -sfn "../libGLESv2.xcframework/ios-arm64_x86_64-simulator/libGLESv2.framework/Headers" \ + "$FRAMEWORKS/include/libGLESv2" + +cd $SDIR/../$TARGET/ver-ios +xcodegen