|
|
|
|
@@ -17,10 +17,15 @@
|
|
|
|
|
|
|
|
|
|
UIApplicationMain :: (argc: s32, argv: *void, principal_class: *void, delegate_class: *void) -> s32 #foreign;
|
|
|
|
|
dlsym :: (handle: *void, name: [*]u8) -> *void #foreign;
|
|
|
|
|
chdir :: (path: [*]u8) -> s32 #foreign;
|
|
|
|
|
|
|
|
|
|
// kEAGLRenderingAPIOpenGLES3 = 3
|
|
|
|
|
EAGL_API_GLES3 :: 3;
|
|
|
|
|
|
|
|
|
|
// CGFloat is a `double` on 64-bit Apple platforms; CGPoint = {x, y} fits in
|
|
|
|
|
// 16 bytes and returns via the FP-register path on arm64.
|
|
|
|
|
CGPoint :: struct { x: f64; y: f64; }
|
|
|
|
|
|
|
|
|
|
// GLenum constants for renderbuffer/framebuffer setup that aren't in opengl.sx's
|
|
|
|
|
// loader path (they live on the framework's symbol table directly).
|
|
|
|
|
GL_RENDERBUFFER :u32: 0x8D41;
|
|
|
|
|
@@ -70,10 +75,20 @@ impl Platform for UIKitPlatform {
|
|
|
|
|
self.dpi_scale = 1.0;
|
|
|
|
|
self.delta_time = 0.016;
|
|
|
|
|
self.has_frame_closure = false;
|
|
|
|
|
self.gl_initialized = false;
|
|
|
|
|
self.keyboard_visible = false;
|
|
|
|
|
self.keyboard_height = 0.0;
|
|
|
|
|
self.saved_title = title.ptr;
|
|
|
|
|
g_uikit_plat = self;
|
|
|
|
|
|
|
|
|
|
// iOS apps start with CWD=/. chdir to the bundle's resourcePath so the
|
|
|
|
|
// game's relative `fopen("assets/...")` calls find their data — must
|
|
|
|
|
// happen BEFORE any code that loads fonts/textures from disk.
|
|
|
|
|
inline if OS == .ios {
|
|
|
|
|
uikit_chdir_to_bundle();
|
|
|
|
|
uikit_register_classes();
|
|
|
|
|
uikit_create_gl_context(self);
|
|
|
|
|
}
|
|
|
|
|
true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -82,7 +97,7 @@ impl Platform for UIKitPlatform {
|
|
|
|
|
self.has_frame_closure = true;
|
|
|
|
|
g_uikit_plat = self;
|
|
|
|
|
inline if OS == .ios {
|
|
|
|
|
uikit_register_app_delegate_and_run();
|
|
|
|
|
UIApplicationMain(0, xx 0, xx 0, ns_string("SxAppDelegate".ptr));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -134,6 +149,8 @@ impl Platform for UIKitPlatform {
|
|
|
|
|
show_keyboard :: (self: *UIKitPlatform) { }
|
|
|
|
|
hide_keyboard :: (self: *UIKitPlatform) { }
|
|
|
|
|
|
|
|
|
|
stop :: (self: *UIKitPlatform) { }
|
|
|
|
|
|
|
|
|
|
shutdown :: (self: *UIKitPlatform) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -159,7 +176,19 @@ uikit_extern_nsstring :: (name: [*]u8) -> *void {
|
|
|
|
|
// so non-iOS builds never reference the unresolved UIKit symbols below.
|
|
|
|
|
// ───────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
uikit_register_app_delegate_and_run :: () {
|
|
|
|
|
uikit_chdir_to_bundle :: () {
|
|
|
|
|
inline if OS != .ios { return; }
|
|
|
|
|
NSBundle := objc_getClass("NSBundle".ptr);
|
|
|
|
|
sel_main_bundle := sel_registerName("mainBundle".ptr);
|
|
|
|
|
sel_resource_path := sel_registerName("resourcePath".ptr);
|
|
|
|
|
msg_o : (*void, *void) -> *void = xx objc_msgSend;
|
|
|
|
|
bundle := msg_o(NSBundle, sel_main_bundle);
|
|
|
|
|
rsrc := msg_o(bundle, sel_resource_path);
|
|
|
|
|
if rsrc == null { return; }
|
|
|
|
|
chdir(c_string(rsrc));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uikit_register_classes :: () {
|
|
|
|
|
inline if OS == .ios {
|
|
|
|
|
UIResponder := objc_getClass("UIResponder".ptr);
|
|
|
|
|
SxAppDelegate := objc_allocateClassPair(UIResponder, "SxAppDelegate".ptr, 0);
|
|
|
|
|
@@ -177,11 +206,38 @@ uikit_register_app_delegate_and_run :: () {
|
|
|
|
|
objc_registerClassPair(SxAppDelegate);
|
|
|
|
|
|
|
|
|
|
uikit_register_gl_view_class();
|
|
|
|
|
|
|
|
|
|
UIApplicationMain(0, xx 0, xx 0, ns_string("SxAppDelegate".ptr));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uikit_create_gl_context :: (plat: *UIKitPlatform) {
|
|
|
|
|
inline if OS != .ios { return; }
|
|
|
|
|
|
|
|
|
|
EAGLContext := objc_getClass("EAGLContext".ptr);
|
|
|
|
|
UIScreen := objc_getClass("UIScreen".ptr);
|
|
|
|
|
sel_alloc := sel_registerName("alloc".ptr);
|
|
|
|
|
sel_init_with_api := sel_registerName("initWithAPI:".ptr);
|
|
|
|
|
sel_set_current_ctx := sel_registerName("setCurrentContext:".ptr);
|
|
|
|
|
sel_main_screen := sel_registerName("mainScreen".ptr);
|
|
|
|
|
sel_native_scale := sel_registerName("nativeScale".ptr);
|
|
|
|
|
|
|
|
|
|
msg_o : (*void, *void) -> *void = xx objc_msgSend;
|
|
|
|
|
msg_oo : (*void, *void, *void) -> void = xx objc_msgSend;
|
|
|
|
|
msg_oi32 : (*void, *void, s32) -> *void = xx objc_msgSend;
|
|
|
|
|
msg_d : (*void, *void) -> f64 = xx objc_msgSend;
|
|
|
|
|
|
|
|
|
|
// Read the screen scale up-front so callers can size font caches and
|
|
|
|
|
// textures with the right DPI before the window even exists.
|
|
|
|
|
screen := msg_o(UIScreen, sel_main_screen);
|
|
|
|
|
scale_d : f64 = msg_d(screen, sel_native_scale);
|
|
|
|
|
plat.dpi_scale = xx scale_d;
|
|
|
|
|
|
|
|
|
|
ctx_raw := msg_o(EAGLContext, sel_alloc);
|
|
|
|
|
plat.gl_ctx = msg_oi32(ctx_raw, sel_init_with_api, EAGL_API_GLES3);
|
|
|
|
|
msg_oo(EAGLContext, sel_set_current_ctx, plat.gl_ctx);
|
|
|
|
|
|
|
|
|
|
load_gl(@ios_gl_proc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uikit_window_getter :: (self: *void, _cmd: *void) -> *void callconv(.c) {
|
|
|
|
|
if g_uikit_plat == null { return xx 0; }
|
|
|
|
|
g_uikit_plat.window;
|
|
|
|
|
@@ -244,13 +300,13 @@ uikit_did_finish_launching_ios :: (app: *void) -> u8 {
|
|
|
|
|
msg_oo : (*void, *void, *void) -> void = xx objc_msgSend;
|
|
|
|
|
msg_ooo : (*void, *void, *void) -> *void = xx objc_msgSend;
|
|
|
|
|
msg_oso : (*void, *void, *void, *void) -> *void = xx objc_msgSend;
|
|
|
|
|
msg_ofi : (*void, *void, f32) -> void = xx objc_msgSend;
|
|
|
|
|
msg_oi32 : (*void, *void, s32) -> *void = xx objc_msgSend;
|
|
|
|
|
msg_oou64 : (*void, *void, u64) -> void = xx objc_msgSend;
|
|
|
|
|
// float-returning msgSend uses a different ABI on x86_64 (objc_msgSend_fpret)
|
|
|
|
|
// but on arm64 it's the same `objc_msgSend`. We only target arm64-class
|
|
|
|
|
// devices/simulators here.
|
|
|
|
|
msg_f : (*void, *void) -> f32 = xx objc_msgSend;
|
|
|
|
|
// CGFloat-returning msgSend. CGFloat is `double` on 64-bit Apple — reading
|
|
|
|
|
// it as f32 reads the low 32 bits of `d0` which isn't a valid float
|
|
|
|
|
// representation of the underlying double, so the value comes back as 0.
|
|
|
|
|
msg_d : (*void, *void) -> f64 = xx objc_msgSend;
|
|
|
|
|
msg_odbl : (*void, *void, f64) -> void = xx objc_msgSend;
|
|
|
|
|
|
|
|
|
|
scenes := msg_o(app, sel_connected_scenes);
|
|
|
|
|
scene := msg_o(scenes, sel_any_object);
|
|
|
|
|
@@ -308,19 +364,17 @@ uikit_did_finish_launching_ios :: (app: *void) -> u8 {
|
|
|
|
|
msg_o3(dict, sel_set_obj_for_key, rgba8_value, colorformat_key);
|
|
|
|
|
msg_oo(plat.gl_layer, sel_set_drawable, dict);
|
|
|
|
|
|
|
|
|
|
// EAGLContext (GLES3) + make current.
|
|
|
|
|
ctx_raw := msg_o(EAGLContext, sel_alloc);
|
|
|
|
|
plat.gl_ctx = msg_oi32(ctx_raw, sel_init_with_api, EAGL_API_GLES3);
|
|
|
|
|
msg_oo(EAGLContext, sel_set_current_ctx, plat.gl_ctx);
|
|
|
|
|
// EAGLContext + load_gl were already done in uikit_create_gl_context()
|
|
|
|
|
// back when the game's main called plat.init() — so shaders/textures
|
|
|
|
|
// built before the window exists already work.
|
|
|
|
|
|
|
|
|
|
// Match the layer's drawable scale to the screen's native scale so we get
|
|
|
|
|
// pixel-accurate rendering on retina displays.
|
|
|
|
|
// pixel-accurate rendering on retina displays. CGFloat is `double` on
|
|
|
|
|
// 64-bit Apple platforms; reading as f32 would clobber the value.
|
|
|
|
|
screen := msg_o(plat.window, sel_screen);
|
|
|
|
|
scale := msg_f(screen, sel_native_scale);
|
|
|
|
|
plat.dpi_scale = scale;
|
|
|
|
|
msg_ofi(plat.gl_view, sel_set_content_scale, scale);
|
|
|
|
|
|
|
|
|
|
load_gl(@ios_gl_proc);
|
|
|
|
|
scale := msg_d(screen, sel_native_scale);
|
|
|
|
|
plat.dpi_scale = xx scale;
|
|
|
|
|
msg_odbl(plat.gl_view, sel_set_content_scale, scale);
|
|
|
|
|
|
|
|
|
|
// Renderbuffer is allocated lazily in -[SxGLView layoutSubviews] once
|
|
|
|
|
// the layer has its real on-screen bounds. makeKeyAndVisible triggers
|
|
|
|
|
@@ -430,6 +484,48 @@ uikit_gl_view_layout :: (self: *void, _cmd: *void) callconv(.c) {
|
|
|
|
|
plat.gl_initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Touch IMPs — UIKit fires touchesBegan/Moved/Ended/Cancelled with an
|
|
|
|
|
// NSSet<UITouch *> + UIEvent. We take the first touch (single-touch model
|
|
|
|
|
// matching the chess game's drag-and-tap UX) and push the resulting
|
|
|
|
|
// Event into the platform's queue for the next poll_events drain.
|
|
|
|
|
|
|
|
|
|
uikit_touch_location :: (touch: *void, view: *void) -> Point {
|
|
|
|
|
sel_location := sel_registerName("locationInView:".ptr);
|
|
|
|
|
msg_pt : (*void, *void, *void) -> CGPoint = xx objc_msgSend;
|
|
|
|
|
p := msg_pt(touch, sel_location, view);
|
|
|
|
|
Point.{ x = xx p.x, y = xx p.y };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uikit_first_touch :: (touches: *void) -> *void {
|
|
|
|
|
sel_any := sel_registerName("anyObject".ptr);
|
|
|
|
|
msg_o : (*void, *void) -> *void = xx objc_msgSend;
|
|
|
|
|
msg_o(touches, sel_any);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uikit_gl_view_touches_began :: (self: *void, _cmd: *void, touches: *void, event: *void) callconv(.c) {
|
|
|
|
|
if g_uikit_plat == null { return; }
|
|
|
|
|
touch := uikit_first_touch(touches);
|
|
|
|
|
if touch == null { return; }
|
|
|
|
|
pos := uikit_touch_location(touch, self);
|
|
|
|
|
g_uikit_plat.events.append(.mouse_down(.{ position = pos, button = .left }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uikit_gl_view_touches_moved :: (self: *void, _cmd: *void, touches: *void, event: *void) callconv(.c) {
|
|
|
|
|
if g_uikit_plat == null { return; }
|
|
|
|
|
touch := uikit_first_touch(touches);
|
|
|
|
|
if touch == null { return; }
|
|
|
|
|
pos := uikit_touch_location(touch, self);
|
|
|
|
|
g_uikit_plat.events.append(.mouse_moved(.{ position = pos, delta = Point.zero() }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uikit_gl_view_touches_ended :: (self: *void, _cmd: *void, touches: *void, event: *void) callconv(.c) {
|
|
|
|
|
if g_uikit_plat == null { return; }
|
|
|
|
|
touch := uikit_first_touch(touches);
|
|
|
|
|
if touch == null { return; }
|
|
|
|
|
pos := uikit_touch_location(touch, self);
|
|
|
|
|
g_uikit_plat.events.append(.mouse_up(.{ position = pos, button = .left }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uikit_register_gl_view_class :: () {
|
|
|
|
|
inline if OS == .ios {
|
|
|
|
|
UIView := objc_getClass("UIView".ptr);
|
|
|
|
|
@@ -450,6 +546,20 @@ uikit_register_gl_view_class :: () {
|
|
|
|
|
sel_registerName("layoutSubviews".ptr),
|
|
|
|
|
xx uikit_gl_view_layout, "v@:".ptr);
|
|
|
|
|
|
|
|
|
|
// Touch dispatch.
|
|
|
|
|
class_addMethod(SxGLView,
|
|
|
|
|
sel_registerName("touchesBegan:withEvent:".ptr),
|
|
|
|
|
xx uikit_gl_view_touches_began, "v@:@@".ptr);
|
|
|
|
|
class_addMethod(SxGLView,
|
|
|
|
|
sel_registerName("touchesMoved:withEvent:".ptr),
|
|
|
|
|
xx uikit_gl_view_touches_moved, "v@:@@".ptr);
|
|
|
|
|
class_addMethod(SxGLView,
|
|
|
|
|
sel_registerName("touchesEnded:withEvent:".ptr),
|
|
|
|
|
xx uikit_gl_view_touches_ended, "v@:@@".ptr);
|
|
|
|
|
class_addMethod(SxGLView,
|
|
|
|
|
sel_registerName("touchesCancelled:withEvent:".ptr),
|
|
|
|
|
xx uikit_gl_view_touches_ended, "v@:@@".ptr);
|
|
|
|
|
|
|
|
|
|
objc_registerClassPair(SxGLView);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|