REVIEWED: iOS implementation -WIP-

REVIEWED: iOS formatting
REVIEWED: Some OpenGL details
REVIEWED: Move platform code to platform directory
WARNING: Changes not tested!
This commit is contained in:
Ray 2026-06-28 23:53:17 +02:00
commit 5d8a8e8885
4 changed files with 96 additions and 59 deletions

View file

@ -37,7 +37,7 @@ set(raylib_sources
) )
if (${PLATFORM} STREQUAL "iOS") if (${PLATFORM} STREQUAL "iOS")
list(APPEND raylib_sources rcore_ios_main.m) list(APPEND raylib_sources platforms/rcore_ios_main.m)
endif() endif()
# Only build raudio if it's enabled # Only build raudio if it's enabled

View file

@ -30,37 +30,37 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
// UIKit/OpenGL bridge functions implemented in rcore_ios_main.m // UIKit/OpenGL bridge functions implemented in [rcore_ios_main.m]
RLAPI bool ios_initialize_window(int requestedWidth, int requestedHeight, int *screenWidth, int *screenHeight, int *renderWidth, int *renderHeight, float *scaleX, float *scaleY); RLAPI bool ios_initialize_window(int requestedWidth, int requestedHeight, int *screenWidth, int *screenHeight, int *renderWidth, int *renderHeight, float *scaleX, float *scaleY);
RLAPI void ios_make_current_context(void);
RLAPI void ios_close_platform(void); RLAPI void ios_close_platform(void);
RLAPI void ios_make_current_context(void);
RLAPI void ios_present_frame(void); RLAPI void ios_present_frame(void);
RLAPI void *ios_get_window_handle(void); RLAPI void *ios_get_window_handle(void);
RLAPI void ios_get_window_metrics(int *screenWidth, int *screenHeight, int *renderWidth, int *renderHeight, float *scaleX, float *scaleY); RLAPI void ios_get_window_metrics(int *screenWidth, int *screenHeight, int *renderWidth, int *renderHeight, float *scaleX, float *scaleY);
RLAPI void *ios_get_proc_address(const char *name); RLAPI void *ios_get_proc_address(const char *name);
RLAPI void ios_open_url(const char *url); RLAPI void ios_open_url(const char *url);
RLAPI void ios_set_target_fps(float fps);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Types and Structures Definition // Types and Structures Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
typedef struct { typedef struct {
bool initialized; bool initialized;
struct {
// TODO: Use CORE.Input.Touch data... why is it required to duplicate this data?
int32_t pointCount; // Number of active touch points
int32_t pointId[MAX_TOUCH_POINTS]; // Touch point ids
Vector2 position[MAX_TOUCH_POINTS]; // Touch point positions
int32_t hoverPoints[MAX_TOUCH_POINTS]; // Hover point slots
} TouchRaw;
} PlatformData; } PlatformData;
typedef struct {
int32_t pointCount; // Number of active touch points
int32_t pointId[MAX_TOUCH_POINTS]; // Touch point ids
Vector2 position[MAX_TOUCH_POINTS]; // Touch point positions
int32_t hoverPoints[MAX_TOUCH_POINTS]; // Hover point slots
} TouchRaw;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Global Variables Definition // Global Variables Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
extern CoreData CORE; // Global CORE state context extern CoreData CORE; // Global CORE state context
static PlatformData platform = { 0 }; // Platform specific data static PlatformData platform = { 0 }; // Platform specific data
static TouchRaw touchRaw = { 0 }; // Raw touch state
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Internal Functions Declaration // Module Internal Functions Declaration
@ -230,6 +230,7 @@ Vector2 GetWindowScaleDPI(void)
float scaleX = 1.0f, scaleY = 1.0f; float scaleX = 1.0f, scaleY = 1.0f;
ios_get_window_metrics(&screenWidth, &screenHeight, &renderWidth, &renderHeight, &scaleX, &scaleY); ios_get_window_metrics(&screenWidth, &screenHeight, &renderWidth, &renderHeight, &scaleX, &scaleY);
(void)screenWidth; (void)screenHeight; (void)screenWidth; (void)screenHeight;
(void)renderWidth; (void)renderHeight; (void)renderWidth; (void)renderHeight;
@ -280,6 +281,8 @@ void DisableCursor(void)
// Swap back buffer with front buffer (screen drawing) // Swap back buffer with front buffer (screen drawing)
void SwapScreenBuffer(void) void SwapScreenBuffer(void)
{ {
if (CORE.Time.target > 0) ios_set_target_fps((float)(1.0/CORE.Time.target));
ios_present_frame(); ios_present_frame();
} }
@ -305,6 +308,7 @@ double GetTime(void)
void OpenURL(const char *url) void OpenURL(const char *url)
{ {
// Security check to (partially) avoid malicious code // Security check to (partially) avoid malicious code
// TODO: Security concerns, this cod should b reviewed (not only for iOS)
if (strchr(url, '\'') != NULL) TRACELOG(LOG_WARNING, "SYSTEM: Provided URL could be potentially malicious, avoid [\'] character"); if (strchr(url, '\'') != NULL) TRACELOG(LOG_WARNING, "SYSTEM: Provided URL could be potentially malicious, avoid [\'] character");
else ios_open_url(url); else ios_open_url(url);
} }
@ -395,7 +399,7 @@ int InitPlatform(void)
float scaleX = 1.0f, scaleY = 1.0f; float scaleX = 1.0f, scaleY = 1.0f;
if (!ios_initialize_window(CORE.Window.screen.width, CORE.Window.screen.height, if (!ios_initialize_window(CORE.Window.screen.width, CORE.Window.screen.height,
&screenWidth, &screenHeight, &renderWidth, &renderHeight, &scaleX, &scaleY)) &screenWidth, &screenHeight, &renderWidth, &renderHeight, &scaleX, &scaleY))
{ {
TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize iOS window and graphics context"); TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize iOS window and graphics context");
return -1; return -1;
@ -403,19 +407,19 @@ int InitPlatform(void)
ios_make_current_context(); ios_make_current_context();
CORE.Window.screen.width = screenWidth; CORE.Window.screen.width = screenWidth;
CORE.Window.screen.height = screenHeight; CORE.Window.screen.height = screenHeight;
CORE.Window.display.width = (int)((float)screenWidth*scaleX); CORE.Window.display.width = (int)((float)screenWidth*scaleX);
CORE.Window.display.height = (int)((float)screenHeight*scaleY); CORE.Window.display.height = (int)((float)screenHeight*scaleY);
CORE.Window.render.width = renderWidth; CORE.Window.render.width = renderWidth;
CORE.Window.render.height = renderHeight; CORE.Window.render.height = renderHeight;
CORE.Window.currentFbo.width = CORE.Window.render.width; CORE.Window.currentFbo.width = CORE.Window.render.width;
CORE.Window.currentFbo.height = CORE.Window.render.height; CORE.Window.currentFbo.height = CORE.Window.render.height;
CORE.Window.screenScale = MatrixScale(scaleX, scaleY, 1.0f); CORE.Window.screenScale = MatrixScale(scaleX, scaleY, 1.0f);
rlLoadExtensions(ios_get_proc_address); rlLoadExtensions(ios_get_proc_address);
for (int i = 0; i < MAX_TOUCH_POINTS; i++) touchRaw.hoverPoints[i] = -1; for (int i = 0; i < MAX_TOUCH_POINTS; i++) platform.TouchRaw.hoverPoints[i] = -1;
platform.initialized = true; platform.initialized = true;
CORE.Window.ready = true; CORE.Window.ready = true;
@ -437,19 +441,19 @@ void ClosePlatform(void)
CORE = (CoreData){ 0 }; CORE = (CoreData){ 0 };
platform = (PlatformData){ 0 }; platform = (PlatformData){ 0 };
touchRaw = (TouchRaw){ 0 }; platform.TouchRaw = (TouchRaw){ 0 };
} }
// Find or allocate a touch slot for the given touchId // Find or allocate a touch slot for the given touchId
static int FindTouchSlot(intptr_t touchId, bool allowAllocate) static int FindTouchSlot(intptr_t touchId, bool allowAllocate)
{ {
for (int i = 0; i < touchRaw.pointCount; i++) for (int i = 0; i < platform.TouchRaw.pointCount; i++)
{ {
if (touchRaw.pointId[i] == (int32_t)touchId) return i; if (platform.TouchRaw.pointId[i] == (int32_t)touchId) return i;
} }
if (!allowAllocate) return -1; if (!allowAllocate) return -1;
if (touchRaw.pointCount < MAX_TOUCH_POINTS) return touchRaw.pointCount; if (platform.TouchRaw.pointCount < MAX_TOUCH_POINTS) return platform.TouchRaw.pointCount;
return -1; return -1;
} }
@ -457,10 +461,10 @@ static int FindTouchSlot(intptr_t touchId, bool allowAllocate)
// Sync mouse position/state from primary touch point // Sync mouse position/state from primary touch point
static void UpdateMouseFromTouches(void) static void UpdateMouseFromTouches(void)
{ {
if (touchRaw.pointCount > 0) if (platform.TouchRaw.pointCount > 0)
{ {
CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition; CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
CORE.Input.Mouse.currentPosition = touchRaw.position[0]; CORE.Input.Mouse.currentPosition = platform.TouchRaw.position[0];
CORE.Input.Mouse.currentWheelMove = (Vector2){ 0.0f, 0.0f }; CORE.Input.Mouse.currentWheelMove = (Vector2){ 0.0f, 0.0f };
} }
} }
@ -474,19 +478,20 @@ void ios_handle_touch_began(intptr_t touchId, float x, float y)
int index = FindTouchSlot(touchId, true); int index = FindTouchSlot(touchId, true);
if (index < 0) return; if (index < 0) return;
touchRaw.pointId[index] = (int32_t)touchId; platform.TouchRaw.pointId[index] = (int32_t)touchId;
touchRaw.position[index] = (Vector2){ x, y }; platform.TouchRaw.position[index] = (Vector2){ x, y };
CORE.Input.Touch.pointId[index] = (int)touchId; CORE.Input.Touch.pointId[index] = (int)touchId;
CORE.Input.Touch.position[index] = (Vector2){ x, y }; CORE.Input.Touch.position[index] = (Vector2){ x, y };
CORE.Input.Touch.currentTouchState[index] = 1; CORE.Input.Touch.currentTouchState[index] = 1;
CORE.Input.Touch.previousTouchState[index] = 0; CORE.Input.Touch.previousTouchState[index] = 0;
CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_LEFT] = 1; CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_LEFT] = 1;
CORE.Input.Mouse.previousButtonState[MOUSE_BUTTON_LEFT] = 0; CORE.Input.Mouse.previousButtonState[MOUSE_BUTTON_LEFT] = 0;
if (touchRaw.pointCount < MAX_TOUCH_POINTS) touchRaw.pointCount++; if (platform.TouchRaw.pointCount < MAX_TOUCH_POINTS) platform.TouchRaw.pointCount++;
CORE.Input.Touch.pointCount = touchRaw.pointCount;
CORE.Input.Touch.pointCount = platform.TouchRaw.pointCount;
UpdateMouseFromTouches(); UpdateMouseFromTouches();
} }
@ -496,12 +501,12 @@ void ios_handle_touch_moved(intptr_t touchId, float x, float y)
int index = FindTouchSlot(touchId, false); int index = FindTouchSlot(touchId, false);
if (index < 0) return; if (index < 0) return;
touchRaw.position[index] = (Vector2){ x, y }; platform.TouchRaw.position[index] = (Vector2){ x, y };
CORE.Input.Touch.pointId[index] = (int)touchId; CORE.Input.Touch.pointId[index] = (int)touchId;
CORE.Input.Touch.position[index] = (Vector2){ x, y }; CORE.Input.Touch.position[index] = (Vector2){ x, y };
CORE.Input.Touch.currentTouchState[index] = 1; CORE.Input.Touch.currentTouchState[index] = 1;
CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_LEFT] = 1; CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_LEFT] = 1;
CORE.Input.Touch.pointCount = touchRaw.pointCount; CORE.Input.Touch.pointCount = platform.TouchRaw.pointCount;
UpdateMouseFromTouches(); UpdateMouseFromTouches();
} }
@ -511,28 +516,28 @@ void ios_handle_touch_ended(intptr_t touchId, float x, float y)
int index = FindTouchSlot(touchId, false); int index = FindTouchSlot(touchId, false);
if (index < 0) return; if (index < 0) return;
touchRaw.position[index] = (Vector2){ x, y }; platform.TouchRaw.position[index] = (Vector2){ x, y };
CORE.Input.Touch.position[index] = (Vector2){ x, y }; CORE.Input.Touch.position[index] = (Vector2){ x, y };
CORE.Input.Touch.currentTouchState[index] = 0; CORE.Input.Touch.currentTouchState[index] = 0;
CORE.Input.Touch.previousTouchState[index] = 1; CORE.Input.Touch.previousTouchState[index] = 1;
CORE.Input.Touch.pointId[index] = 0; CORE.Input.Touch.pointId[index] = 0;
// Compact the touch arrays — shift remaining points down // Compact the touch arrays — shift remaining points down
for (int i = index; i < touchRaw.pointCount - 1; i++) for (int i = index; i < platform.TouchRaw.pointCount - 1; i++)
{ {
touchRaw.pointId[i] = touchRaw.pointId[i + 1]; platform.TouchRaw.pointId[i] = platform.TouchRaw.pointId[i + 1];
touchRaw.position[i] = touchRaw.position[i + 1]; platform.TouchRaw.position[i] = platform.TouchRaw.position[i + 1];
CORE.Input.Touch.pointId[i] = CORE.Input.Touch.pointId[i + 1]; CORE.Input.Touch.pointId[i] = CORE.Input.Touch.pointId[i + 1];
CORE.Input.Touch.position[i] = CORE.Input.Touch.position[i + 1]; CORE.Input.Touch.position[i] = CORE.Input.Touch.position[i + 1];
CORE.Input.Touch.currentTouchState[i] = CORE.Input.Touch.currentTouchState[i + 1]; CORE.Input.Touch.currentTouchState[i] = CORE.Input.Touch.currentTouchState[i + 1];
CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.previousTouchState[i + 1]; CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.previousTouchState[i + 1];
} }
if (touchRaw.pointCount > 0) touchRaw.pointCount--; if (platform.TouchRaw.pointCount > 0) platform.TouchRaw.pointCount--;
CORE.Input.Touch.pointCount = touchRaw.pointCount; CORE.Input.Touch.pointCount = platform.TouchRaw.pointCount;
CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_LEFT] = (touchRaw.pointCount > 0)? 1 : 0; CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_LEFT] = (platform.TouchRaw.pointCount > 0)? 1 : 0;
CORE.Input.Mouse.previousButtonState[MOUSE_BUTTON_LEFT] = 1; CORE.Input.Mouse.previousButtonState[MOUSE_BUTTON_LEFT] = 1;
UpdateMouseFromTouches(); UpdateMouseFromTouches();

View file

@ -0,0 +1,645 @@
/**********************************************************************************************
*
* rcore_ios_main.m - Functions required by iOS platform implementation
*
* PLATFORM: IOS
* - iOS (arm64)
* - iOS Simulator
*
* LIMITATIONS:
* - No keyboard input support
* - No gamepad input support
*
* DEPENDENCIES:
* - UIKit/QuartzCore/OpenGLES (provided by iOS SDK)
*
* LICENSE: zlib/libpng
*
* Copyright (c) 2013-2026 Ramon Santamaria (@raysan5) and contributors
*
**********************************************************************************************/
#import <Foundation/Foundation.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES3/gl.h>
#import <QuartzCore/CADisplayLink.h>
#import <QuartzCore/CAFrameRateRange.h>
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#include <dlfcn.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
extern int raylib_main(int argc, char *argv[]);
extern void ios_handle_touch_began(intptr_t touchId, float x, float y);
extern void ios_handle_touch_moved(intptr_t touchId, float x, float y);
extern void ios_handle_touch_ended(intptr_t touchId, float x, float y);
extern void ios_handle_touch_cancelled(intptr_t touchId, float x, float y);
extern void ios_request_close(void);
extern void ios_set_window_focused(bool focused);
typedef struct IOSBridgeState {
int argc;
char **argv;
UIWindow *window;
UIViewController *viewController;
UIView *glView;
EAGLContext *context;
GLuint framebuffer;
GLuint colorBuffer;
GLuint depthBuffer;
int screenWidth;
int screenHeight;
int renderWidth;
int renderHeight;
float scaleX;
float scaleY;
bool shuttingDown;
bool initialized;
} IOSBridgeState;
static IOSBridgeState ios = {0};
@interface RaylibController : NSObject
+ (instancetype)sharedController;
- (void)startInternal;
- (void)startDisplayLink:(float)minFPS;
- (void)stopDisplayLink;
- (void)waitForVSync;
- (void)setTargetFPS:(float)fps;
@end
@implementation RaylibController {
CADisplayLink *_displayLink;
dispatch_semaphore_t _frameSemaphore;
dispatch_queue_t _raylibQueue;
int64_t _vsyncCount;
int64_t _lastConsumedVsync;
float _minimumFPS;
BOOL _raylibStarted;
}
+ (instancetype)sharedController {
static RaylibController *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
_frameSemaphore = dispatch_semaphore_create(0);
_raylibQueue =
dispatch_queue_create("com.raylib.engine", DISPATCH_QUEUE_SERIAL);
_vsyncCount = 0;
_lastConsumedVsync = 0;
_raylibStarted = NO;
_minimumFPS = 60.0f;
}
return self;
}
- (void)startInternal {
if (_raylibStarted)
return;
_raylibStarted = YES;
dispatch_async(_raylibQueue, ^{
[[NSThread currentThread] setName:@"com.raylib.engine"];
// NSLog(@"[raylib][IOS] raylib thread: %@", [NSThread currentThread].name);
raylib_main(ios.argc, ios.argv);
});
}
- (void)startDisplayLink:(float)minFPS {
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self startDisplayLink:_minimumFPS];
});
return;
}
_minimumFPS = minFPS;
if (_displayLink != nil)
return;
_displayLink =
[CADisplayLink displayLinkWithTarget:self
selector:@selector(onDisplayFrame:)];
NSInteger maxFPS = [UIScreen mainScreen].maximumFramesPerSecond;
// Use the display native refresh rate
// NOTE: Driven dynamically via setTargetFPS, no hardcoded maxFPS
//_displayLink.preferredFrameRateRange =
// CAFrameRateRangeMake(_minimumFPS, maxFPS, maxFPS);
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop]
forMode:NSRunLoopCommonModes];
}
- (void)onDisplayFrame:(CADisplayLink *)link {
(void)link;
__atomic_fetch_add(&_vsyncCount, 1, __ATOMIC_RELEASE);
dispatch_semaphore_signal(_frameSemaphore);
}
- (void)stopDisplayLink {
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self stopDisplayLink];
});
return;
}
if (_displayLink == nil)
return;
[_displayLink invalidate];
_displayLink = nil;
dispatch_semaphore_signal(_frameSemaphore);
}
- (void)waitForVSync {
int64_t current = __atomic_load_n(&_vsyncCount, __ATOMIC_ACQUIRE);
while ((current - _lastConsumedVsync) < 1 && !ios.shuttingDown) {
dispatch_semaphore_wait(_frameSemaphore, DISPATCH_TIME_FOREVER);
current = __atomic_load_n(&_vsyncCount, __ATOMIC_ACQUIRE);
}
_lastConsumedVsync = current;
}
- (void)setTargetFPS:(float)fps {
_minimumFPS = fps;
if (_displayLink != nil)
_displayLink.preferredFrameRateRange =
CAFrameRateRangeMake(fps, fps, fps);
}
@end
@interface RaylibGLView : UIView
@end
@implementation RaylibGLView
+ (Class)layerClass {
return [CAEAGLLayer class];
}
@end
@interface RaylibViewController : UIViewController
@end
@implementation RaylibViewController
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (void)loadView {
self.view = [[RaylibGLView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view.multipleTouchEnabled = YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
ios_set_window_focused(true);
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
ios_set_window_focused(false);
}
- (void)forwardTouches:(NSSet<UITouch *> *)touches action:(int)action {
for (UITouch *touch in touches) {
CGPoint point = [touch locationInView:self.view];
intptr_t touchId = (intptr_t)(__bridge void *)touch;
switch (action) {
case 0:
ios_handle_touch_began(touchId, point.x, point.y);
break;
case 1:
ios_handle_touch_moved(touchId, point.x, point.y);
break;
case 2:
ios_handle_touch_ended(touchId, point.x, point.y);
break;
case 3:
ios_handle_touch_cancelled(touchId, point.x, point.y);
break;
default:
break;
}
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
(void)event;
[self forwardTouches:touches action:0];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
(void)event;
[self forwardTouches:touches action:1];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
(void)event;
[self forwardTouches:touches action:2];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches
withEvent:(UIEvent *)event {
(void)event;
[self forwardTouches:touches action:3];
}
@end
static void shutdown_internal(void) {
[[RaylibController sharedController] stopDisplayLink];
if (ios.context != nil) {
[EAGLContext setCurrentContext:ios.context];
if (ios.framebuffer != 0)
glDeleteFramebuffers(1, &ios.framebuffer);
if (ios.colorBuffer != 0)
glDeleteRenderbuffers(1, &ios.colorBuffer);
if (ios.depthBuffer != 0)
glDeleteRenderbuffers(1, &ios.depthBuffer);
ios.framebuffer = 0;
ios.colorBuffer = 0;
ios.depthBuffer = 0;
ios.context = nil;
}
ios.glView = nil;
ios.viewController = nil;
ios.window = nil;
ios.initialized = false;
}
void ios_close_platform(void) {
if (!ios.initialized || ios.shuttingDown)
return;
ios.shuttingDown = true;
if ([NSThread isMainThread])
shutdown_internal();
else
dispatch_async(dispatch_get_main_queue(), ^{
shutdown_internal();
});
}
@interface RaylibAppDelegate : UIResponder <UIApplicationDelegate>
@end
@interface RaylibSceneDelegate : UIResponder <UIWindowSceneDelegate>
@property(strong, nonatomic) UIWindow *window;
@end
@implementation RaylibAppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
(void)application;
(void)launchOptions;
return YES;
}
- (UISceneConfiguration *)application:(UIApplication *)application
configurationForConnectingSceneSession:
(UISceneSession *)connectingSceneSession
options:(UISceneConnectionOptions *)options {
(void)application;
(void)connectingSceneSession;
(void)options;
UISceneConfiguration *configuration = [UISceneConfiguration
configurationWithName:@"Default Configuration"
sessionRole:UIWindowSceneSessionRoleApplication];
configuration.delegateClass = [RaylibSceneDelegate class];
return configuration;
}
- (void)applicationWillResignActive:(UIApplication *)application {
(void)application;
ios_set_window_focused(false);
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
(void)application;
ios_set_window_focused(true);
}
- (void)applicationWillTerminate:(UIApplication *)application {
(void)application;
ios_request_close();
}
@end
@implementation RaylibSceneDelegate
- (void)scene:(UIScene *)scene
willConnectToSession:(UISceneSession *)session
options:(UISceneConnectionOptions *)connectionOptions {
(void)session;
(void)connectionOptions;
if (![scene isKindOfClass:[UIWindowScene class]])
return;
UIWindowScene *windowScene = (UIWindowScene *)scene;
self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
ios.window = self.window;
if (ios.viewController == nil) {
ios.viewController = [[RaylibViewController alloc] init];
}
self.window.rootViewController = ios.viewController;
[self.window makeKeyAndVisible];
[[RaylibController sharedController] startDisplayLink:60.0f];
[[RaylibController sharedController] startInternal];
}
- (void)sceneDidBecomeActive:(UIScene *)scene {
(void)scene;
ios_set_window_focused(true);
}
- (void)sceneWillResignActive:(UIScene *)scene {
(void)scene;
ios_set_window_focused(false);
}
- (void)sceneDidDisconnect:(UIScene *)scene {
(void)scene;
ios_request_close();
ios_close_platform();
}
@end
static BOOL ios_setup_gl(void) {
if (!ios.window || !ios.viewController)
return NO;
ios.glView = ios.viewController.view;
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)ios.glView.layer;
eaglLayer.opaque = YES;
eaglLayer.drawableProperties = @{
kEAGLDrawablePropertyRetainedBacking : @NO,
kEAGLDrawablePropertyColorFormat : kEAGLColorFormatRGBA8
};
// Ensure layer uses native scale for proper retina resolution
CGFloat nativeScale = [UIScreen mainScreen].nativeScale;
eaglLayer.contentsScale = nativeScale;
ios.glView.contentScaleFactor = nativeScale;
ios.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
if (!ios.context) {
ios.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
}
if (!ios.context)
return NO;
[EAGLContext setCurrentContext:ios.context];
glGenFramebuffers(1, &ios.framebuffer);
glGenRenderbuffers(1, &ios.colorBuffer);
glGenRenderbuffers(1, &ios.depthBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, ios.framebuffer);
glBindRenderbuffer(GL_RENDERBUFFER, ios.colorBuffer);
if (![ios.context renderbufferStorage:GL_RENDERBUFFER
fromDrawable:eaglLayer]) {
return NO;
}
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER, ios.colorBuffer);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH,
&ios.renderWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT,
&ios.renderHeight);
glBindRenderbuffer(GL_RENDERBUFFER, ios.depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, ios.renderWidth,
ios.renderHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER, ios.depthBuffer);
GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fbStatus != GL_FRAMEBUFFER_COMPLETE) {
NSLog(@"[raylib][IOS] Framebuffer incomplete: 0x%04X", fbStatus);
} else {
NSLog(@"[raylib][IOS] Framebuffer complete (%d x %d)", ios.renderWidth,
ios.renderHeight);
}
[EAGLContext setCurrentContext:nil];
return fbStatus == GL_FRAMEBUFFER_COMPLETE;
}
bool ios_initialize_window(int requestedWidth, int requestedHeight,
int *screenWidth, int *screenHeight,
int *renderWidth, int *renderHeight, float *scaleX,
float *scaleY) {
(void)requestedWidth;
(void)requestedHeight;
__block bool ok = false;
dispatch_sync(dispatch_get_main_queue(), ^{
if (ios.window == nil) {
ios.window =
[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
if (ios.viewController == nil) {
ios.viewController = [[RaylibViewController alloc] init];
}
ios.window.rootViewController = ios.viewController;
[ios.window makeKeyAndVisible];
ok = ios_setup_gl();
UIScreen *screen = [UIScreen mainScreen];
CGSize boundsSize = screen.bounds.size;
ios.screenWidth = (int)boundsSize.width;
ios.screenHeight = (int)boundsSize.height;
ios.scaleX = (float)screen.nativeScale;
ios.scaleY = (float)screen.nativeScale;
if (screenWidth)
*screenWidth = ios.screenWidth;
if (screenHeight)
*screenHeight = ios.screenHeight;
if (renderWidth)
*renderWidth = ios.renderWidth;
if (renderHeight)
*renderHeight = ios.renderHeight;
if (scaleX)
*scaleX = ios.scaleX;
if (scaleY)
*scaleY = ios.scaleY;
});
ios.initialized = ok;
return ok;
}
void ios_make_current_context(void) {
if (ios.context != nil)
[EAGLContext setCurrentContext:ios.context];
if (ios.framebuffer != 0)
glBindFramebuffer(GL_FRAMEBUFFER, ios.framebuffer);
if (ios.colorBuffer != 0)
glBindRenderbuffer(GL_RENDERBUFFER, ios.colorBuffer);
glViewport(0, 0, ios.renderWidth, ios.renderHeight);
// NSLog(@"[raylib][IOS] IOSMakeCurrentContext: thread=%@, fb=%u, rb=%u, "
// @"viewport=%dx%d",
// [NSThread currentThread], ios.framebuffer, ios.colorBuffer,
// ios.renderWidth, ios.renderHeight);
}
void ios_present_frame(void) {
if (ios.context == nil || ios.shuttingDown)
return;
if ([EAGLContext currentContext] != ios.context) {
[EAGLContext setCurrentContext:ios.context];
}
// NSLog(@"[raylib][IOS] IOSPresentFrame: thread=%@, currentContext=%@",
// [NSThread currentThread], [EAGLContext currentContext]);
[ios.context presentRenderbuffer:GL_RENDERBUFFER];
#if RLGL_SHOW_GL_DETAILS_INFO
GLenum err = glGetError();
if (err != GL_NO_ERROR)
NSLog(@"[raylib][IOS] glGetError after present: 0x%04X", err);
#endif
if (!ios.shuttingDown) {
[[RaylibController sharedController] waitForVSync];
}
}
void *ios_get_window_handle(void) { return (__bridge void *)ios.window; }
void ios_get_window_metrics(int *screenWidth, int *screenHeight,
int *renderWidth, int *renderHeight, float *scaleX,
float *scaleY) {
if (screenWidth)
*screenWidth = ios.screenWidth;
if (screenHeight)
*screenHeight = ios.screenHeight;
if (renderWidth)
*renderWidth = ios.renderWidth;
if (renderHeight)
*renderHeight = ios.renderHeight;
if (scaleX)
*scaleX = ios.scaleX;
if (scaleY)
*scaleY = ios.scaleY;
}
void *ios_get_proc_address(const char *name) {
return dlsym(RTLD_DEFAULT, name);
}
void ios_open_url(const char *url) {
if (!url) {
NSLog(@"[raylib][IOS] invalid URL: null");
return;
}
NSURLComponents *components = [NSURLComponents
componentsWithString:[NSString stringWithUTF8String:url]];
if (components == nil || components.scheme == nil) {
NSLog(@"[raylib][IOS] invalid URL: %s, Scheme: %s", url,
components ? components.scheme.UTF8String : "nil");
return;
}
if (components.URL != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] openURL:components.URL
options:@{}
completionHandler:nil];
});
}
}
void ios_set_target_fps(float fps) {
[[RaylibController sharedController] setTargetFPS:fps];
}
int main(int argc, char *argv[]) {
ios.argc = argc;
ios.argv = argv;
@autoreleasepool {
return UIApplicationMain(argc, argv, nil,
NSStringFromClass([RaylibAppDelegate class]));
}
}

View file

@ -113,7 +113,6 @@
#define RLAPI // Functions defined as 'extern' by default (implicit specifiers) #define RLAPI // Functions defined as 'extern' by default (implicit specifiers)
#endif #endif
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Defines and Macros // Defines and Macros
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------