From fdd7d8bedbfb6bf0576daf1eaaa4c2535c59c891 Mon Sep 17 00:00:00 2001 From: Carlos Aragones <> Date: Sun, 26 Apr 2020 17:42:23 +0200 Subject: [PATCH] work with and without already created window --- src/ios/WindowData_IOS.h | 1 - src/ios/iOSMiniFB.m | 70 +++++++++++-------- src/ios/iOSView.h | 9 +++ src/ios/iOSView.m | 80 ++++++++++++++++++++++ src/ios/iOSViewController.h | 3 + src/ios/iOSViewController.m | 20 ++++-- src/ios/iOSViewDelegate.m | 2 +- tests/ios/AppDelegate.h | 3 +- tests/ios/AppDelegate.m | 130 ++++++++++++++++++++++-------------- 9 files changed, 232 insertions(+), 86 deletions(-) create mode 100644 src/ios/iOSView.h create mode 100644 src/ios/iOSView.m diff --git a/src/ios/WindowData_IOS.h b/src/ios/WindowData_IOS.h index 55f1de6..51fd80a 100644 --- a/src/ios/WindowData_IOS.h +++ b/src/ios/WindowData_IOS.h @@ -2,7 +2,6 @@ #include #include - #include typedef struct Vertex { diff --git a/src/ios/iOSMiniFB.m b/src/ios/iOSMiniFB.m index f7b5e3e..3ff2218 100644 --- a/src/ios/iOSMiniFB.m +++ b/src/ios/iOSMiniFB.m @@ -8,17 +8,8 @@ #include "iOSViewController.h" //------------------------------------- -struct mfb_window * -mfb_open(const char *title, unsigned width, unsigned height) { - return mfb_open_ex(title, width, height, 0); -} - -//------------------------------------- -struct mfb_window * -mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) { - kUnused(title); - kUnused(flags); - +SWindowData * +create_window_data(unsigned width, unsigned height) { SWindowData *window_data; window_data = malloc(sizeof(SWindowData)); @@ -43,30 +34,55 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) NSLog(@"Unable to create draw buffer"); return 0x0; } + + return window_data; +} + +//------------------------------------- +struct mfb_window * +mfb_open(const char *title, unsigned width, unsigned height) { + return mfb_open_ex(title, width, height, 0); +} + +//------------------------------------- +struct mfb_window * +mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) { + kUnused(title); + kUnused(flags); + + SWindowData *window_data = create_window_data(width, height); + if (window_data == 0x0) { + return 0x0; + } UIWindow *window; - NSArray *pWindows; + NSArray *windows; size_t numWindows; - pWindows = [[UIApplication sharedApplication] windows]; - - numWindows = [pWindows count]; - //iOSViewController *controller = [[iOSViewController alloc] initWithFrame: [UIScreen mainScreen].bounds]; - iOSViewController *controller = [[iOSViewController alloc] initWithWindowData:window_data]; - if(numWindows > 0) - { - window = [pWindows objectAtIndex:0]; + windows = [[UIApplication sharedApplication] windows]; + numWindows = [windows count]; + if(numWindows > 0) { + window = [windows objectAtIndex:0]; } - else - { + else { // Notice that you need to set "Launch Screen File" in: - // project > executable > general to get the real size + // project > executable > general + // to get the real size with [UIScreen mainScreen].bounds]. window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; NSLog(@"UIApplication has no window. We create one (%f, %f).", [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); } - [window setRootViewController:controller]; - [controller release]; - controller = (iOSViewController *) window.rootViewController; + + if([window.rootViewController isKindOfClass:[iOSViewController class]] == false) { + iOSViewController *controller = [[iOSViewController alloc] initWithWindowData:window_data]; + [window setRootViewController:controller]; +#if !__has_feature(objc_arc) + [controller release]; +#endif + controller = (iOSViewController *) window.rootViewController; + } + else { + ((iOSViewController *) window.rootViewController)->window_data = window_data; + } [window makeKeyAndVisible]; return (struct mfb_window *) window_data; @@ -125,9 +141,9 @@ mfb_wait_sync(struct mfb_window *window) { return true; } +//------------------------------------- extern Vertex g_vertices[4]; -//------------------------------------- bool mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) { SWindowData *window_data = (SWindowData *) window; diff --git a/src/ios/iOSView.h b/src/ios/iOSView.h new file mode 100644 index 0000000..c9d925b --- /dev/null +++ b/src/ios/iOSView.h @@ -0,0 +1,9 @@ +#import +#include "WindowData.h" + +@interface iOSView : MTKView +{ + @public SWindowData *window_data; +} + +@end diff --git a/src/ios/iOSView.m b/src/ios/iOSView.m new file mode 100644 index 0000000..ea10d5d --- /dev/null +++ b/src/ios/iOSView.m @@ -0,0 +1,80 @@ +#include "iOSView.h" +#include + +//------------------------------------- +@implementation iOSView + +//------------------------------------- +- (BOOL) canBecomeFirstResponder { + return YES; +} + +//------------------------------------- +- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + kUnused(event); + + if(window_data != 0x0) { + CGPoint point; + int buttonNumber = MOUSE_BTN_0; + for(UITouch *touch in touches) { + point = [touch locationInView:self]; + window_data->mouse_pos_x = point.x; + window_data->mouse_pos_y = point.y; + kCall(mouse_btn_func, buttonNumber, 0, true); + ++buttonNumber; + } + } +} + +//------------------------------------- +- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { + kUnused(event); + + if(window_data != 0x0) { + CGPoint point; + int buttonNumber = MOUSE_BTN_0; + for(UITouch *touch in touches) { + point = [touch locationInView:self]; + window_data->mouse_pos_x = point.x; + window_data->mouse_pos_y = point.y; + kCall(mouse_move_func, point.x, point.y); + ++buttonNumber; + } + } +} + +//------------------------------------- +- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { + kUnused(event); + + if(window_data != 0x0) { + CGPoint point; + int buttonNumber = MOUSE_BTN_0; + for(UITouch *touch in touches) { + point = [touch locationInView:self]; + window_data->mouse_pos_x = point.x; + window_data->mouse_pos_y = point.y; + kCall(mouse_btn_func, buttonNumber, 0, false); + ++buttonNumber; + } + } +} + +//------------------------------------- +- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { + kUnused(event); + + if(window_data != 0x0) { + CGPoint point; + int buttonNumber = MOUSE_BTN_0; + for(UITouch *touch in touches) { + point = [touch locationInView:self]; + window_data->mouse_pos_x = point.x; + window_data->mouse_pos_y = point.y; + kCall(mouse_btn_func, buttonNumber, 0, false); + ++buttonNumber; + } + } +} + +@end diff --git a/src/ios/iOSViewController.h b/src/ios/iOSViewController.h index dbfd3d6..8e175cf 100644 --- a/src/ios/iOSViewController.h +++ b/src/ios/iOSViewController.h @@ -10,6 +10,9 @@ #include "WindowData.h" @interface iOSViewController : UIViewController +{ + @public SWindowData *window_data; +} - (id) initWithWindowData:(SWindowData *) windowData; diff --git a/src/ios/iOSViewController.m b/src/ios/iOSViewController.m index ffa0619..07ff135 100644 --- a/src/ios/iOSViewController.m +++ b/src/ios/iOSViewController.m @@ -6,17 +6,18 @@ // Copyright © 2020 Carlos Aragones. All rights reserved. // -#import "iOSViewController.h" #import #import +#import "iOSViewController.h" #import "iOSViewDelegate.h" +#import "iOSView.h" +#include "WindowData_IOS.h" //------------------------------------- @implementation iOSViewController { - MTKView *metal_view; + iOSView *metal_view; iOSViewDelegate *view_delegate; - SWindowData *window_data; } //------------------------------------- @@ -30,9 +31,18 @@ //------------------------------------- - (void) loadView { - UIView *view = [[MTKView alloc] initWithFrame:[UIScreen mainScreen].bounds]; + iOSView *view = [[iOSView alloc] initWithFrame:[UIScreen mainScreen].bounds]; + // Probably the window was created automatically by an storyboard or similar + if(window_data == 0x0) { + NSLog(@"WindowData is null!"); + } + view->window_data = window_data; + view.userInteractionEnabled = true; + [self setView:view]; +#if !__has_feature(objc_arc) [view release]; +#endif } //------------------------------------- @@ -40,7 +50,7 @@ { [super viewDidLoad]; - metal_view = (MTKView *)self.view; + metal_view = (iOSView *) self.view; metal_view.device = MTLCreateSystemDefaultDevice(); metal_view.backgroundColor = UIColor.blackColor; diff --git a/src/ios/iOSViewDelegate.m b/src/ios/iOSViewDelegate.m index f24d043..aad5104 100644 --- a/src/ios/iOSViewDelegate.m +++ b/src/ios/iOSViewDelegate.m @@ -181,7 +181,7 @@ NSString *g_shader_src = kShader( // holding onto the drawable and blocking the display pipeline any longer than necessary MTLRenderPassDescriptor* renderPassDescriptor = view.currentRenderPassDescriptor; if (renderPassDescriptor != nil) { - renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); + //renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); // Create a render command encoder so we can render into something id renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; diff --git a/tests/ios/AppDelegate.h b/tests/ios/AppDelegate.h index 11bba05..5d58618 100644 --- a/tests/ios/AppDelegate.h +++ b/tests/ios/AppDelegate.h @@ -12,9 +12,10 @@ { CADisplayLink *mDisplayLink; } + @property (strong, nonatomic) UIWindow *window; -- (void) onUpdate; +- (void) OnUpdateFrame; @end diff --git a/tests/ios/AppDelegate.m b/tests/ios/AppDelegate.m index 2ad2ea9..2841103 100644 --- a/tests/ios/AppDelegate.m +++ b/tests/ios/AppDelegate.m @@ -8,71 +8,40 @@ #import "AppDelegate.h" #include -#include +//------------------------------------- #define kUnused(var) (void) var; +//------------------------------------- struct mfb_window *g_window = 0x0; uint32_t *g_buffer = 0x0; uint32_t g_width = 0; uint32_t g_height = 0; +//------------------------------------- @interface AppDelegate () @end +//------------------------------------- +void +mouse_btn(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed) { + kUnused(mod); + NSLog(@"Touch: %d at %d, %d is %d", (int)button - MOUSE_BTN_0, mfb_get_mouse_x(window), mfb_get_mouse_y(window), (int) isPressed); +} + +//------------------------------------- +void +mouse_move(struct mfb_window *window, int x, int y) { + kUnused(window); + NSLog(@"Touch moved %d, %d", x, y); +} + +//------------------------------------- @implementation AppDelegate - -- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - // Override point for customization after application launch. - kUnused(application); - kUnused(launchOptions); - if(g_window == 0x0) { - g_width = [UIScreen mainScreen].bounds.size.width; - g_height = [UIScreen mainScreen].bounds.size.height; - g_buffer = malloc(g_width * g_height * 4); - - g_window = mfb_open("noise", g_width, g_height); - } - return YES; -} - -- (void) applicationWillResignActive:(UIApplication *)application { - // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. - // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. - kUnused(application); -} - -- (void)applicationDidEnterBackground:(UIApplication *)application { - // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. - // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. - kUnused(application); - [mDisplayLink invalidate]; -} - - -- (void)applicationWillEnterForeground:(UIApplication *)application { - // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. - kUnused(application); -} - - -- (void)applicationDidBecomeActive:(UIApplication *)application { - // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. - kUnused(application); - mDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onUpdate)]; - [mDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; -} - - -- (void)applicationWillTerminate:(UIApplication *)application { - // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. - kUnused(application); - [mDisplayLink invalidate]; -} - -- (void) onUpdate { +//------------------------------------- +- (void) OnUpdateFrame { static int seed = 0xbeef; int noise, carry; @@ -99,4 +68,63 @@ uint32_t g_height = 0; } } +//------------------------------------- +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + // Override point for customization after application launch. + kUnused(application); + kUnused(launchOptions); + + if(g_window == 0x0) { + g_width = [UIScreen mainScreen].bounds.size.width; + g_height = [UIScreen mainScreen].bounds.size.height; + g_window = mfb_open("noise", g_width, g_height); + if(g_window != 0x0) { + g_buffer = malloc(g_width * g_height * 4); + mfb_set_mouse_move_callback(g_window, mouse_move); + mfb_set_mouse_button_callback(g_window, mouse_btn); + } + } + + return YES; +} + +//------------------------------------- +- (void) applicationWillResignActive:(UIApplication *)application { + // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. + // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. + kUnused(application); +} + +//------------------------------------- +- (void)applicationDidEnterBackground:(UIApplication *)application { + // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. + // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. + kUnused(application); + [mDisplayLink invalidate]; +} + +//------------------------------------- +- (void)applicationWillEnterForeground:(UIApplication *)application { + // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. + kUnused(application); +} + +//------------------------------------- +- (void)applicationDidBecomeActive:(UIApplication *)application { + // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. + kUnused(application); + + mDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(OnUpdateFrame)]; + [mDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; +} + +//------------------------------------- +- (void)applicationWillTerminate:(UIApplication *)application { + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. + kUnused(application); + + [mDisplayLink invalidate]; + mfb_close(g_window); +} + @end