work with and without already created window

This commit is contained in:
Carlos Aragones 2020-04-26 17:42:23 +02:00
parent 6452ec1bfd
commit fdd7d8bedb
9 changed files with 232 additions and 86 deletions

View File

@ -2,7 +2,6 @@
#include <MiniFB_enums.h> #include <MiniFB_enums.h>
#include <WindowData.h> #include <WindowData.h>
#include <MetalKit/MetalKit.h> #include <MetalKit/MetalKit.h>
typedef struct Vertex { typedef struct Vertex {

View File

@ -8,17 +8,8 @@
#include "iOSViewController.h" #include "iOSViewController.h"
//------------------------------------- //-------------------------------------
struct mfb_window * SWindowData *
mfb_open(const char *title, unsigned width, unsigned height) { create_window_data(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; SWindowData *window_data;
window_data = malloc(sizeof(SWindowData)); window_data = malloc(sizeof(SWindowData));
@ -44,29 +35,54 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
return 0x0; 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; UIWindow *window;
NSArray *pWindows; NSArray *windows;
size_t numWindows; size_t numWindows;
pWindows = [[UIApplication sharedApplication] windows]; windows = [[UIApplication sharedApplication] windows];
numWindows = [windows count];
numWindows = [pWindows count]; if(numWindows > 0) {
//iOSViewController *controller = [[iOSViewController alloc] initWithFrame: [UIScreen mainScreen].bounds]; window = [windows objectAtIndex:0];
iOSViewController *controller = [[iOSViewController alloc] initWithWindowData:window_data];
if(numWindows > 0)
{
window = [pWindows objectAtIndex:0];
} }
else else {
{
// Notice that you need to set "Launch Screen File" in: // 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]; 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); 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]; if([window.rootViewController isKindOfClass:[iOSViewController class]] == false) {
controller = (iOSViewController *) window.rootViewController; 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]; [window makeKeyAndVisible];
return (struct mfb_window *) window_data; return (struct mfb_window *) window_data;
@ -125,9 +141,9 @@ mfb_wait_sync(struct mfb_window *window) {
return true; return true;
} }
//-------------------------------------
extern Vertex g_vertices[4]; extern Vertex g_vertices[4];
//-------------------------------------
bool bool
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) { mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
SWindowData *window_data = (SWindowData *) window; SWindowData *window_data = (SWindowData *) window;

9
src/ios/iOSView.h Normal file
View File

@ -0,0 +1,9 @@
#import <MetalKit/MetalKit.h>
#include "WindowData.h"
@interface iOSView : MTKView
{
@public SWindowData *window_data;
}
@end

80
src/ios/iOSView.m Normal file
View File

@ -0,0 +1,80 @@
#include "iOSView.h"
#include <MiniFB_internal.h>
//-------------------------------------
@implementation iOSView
//-------------------------------------
- (BOOL) canBecomeFirstResponder {
return YES;
}
//-------------------------------------
- (void) touchesBegan:(NSSet<UITouch *> *)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<UITouch *> *)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<UITouch *> *)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<UITouch *> *)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

View File

@ -10,6 +10,9 @@
#include "WindowData.h" #include "WindowData.h"
@interface iOSViewController : UIViewController @interface iOSViewController : UIViewController
{
@public SWindowData *window_data;
}
- (id) initWithWindowData:(SWindowData *) windowData; - (id) initWithWindowData:(SWindowData *) windowData;

View File

@ -6,17 +6,18 @@
// Copyright © 2020 Carlos Aragones. All rights reserved. // Copyright © 2020 Carlos Aragones. All rights reserved.
// //
#import "iOSViewController.h"
#import <Metal/Metal.h> #import <Metal/Metal.h>
#import <MetalKit/MetalKit.h> #import <MetalKit/MetalKit.h>
#import "iOSViewController.h"
#import "iOSViewDelegate.h" #import "iOSViewDelegate.h"
#import "iOSView.h"
#include "WindowData_IOS.h"
//------------------------------------- //-------------------------------------
@implementation iOSViewController @implementation iOSViewController
{ {
MTKView *metal_view; iOSView *metal_view;
iOSViewDelegate *view_delegate; iOSViewDelegate *view_delegate;
SWindowData *window_data;
} }
//------------------------------------- //-------------------------------------
@ -30,9 +31,18 @@
//------------------------------------- //-------------------------------------
- (void) loadView { - (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]; [self setView:view];
#if !__has_feature(objc_arc)
[view release]; [view release];
#endif
} }
//------------------------------------- //-------------------------------------
@ -40,7 +50,7 @@
{ {
[super viewDidLoad]; [super viewDidLoad];
metal_view = (MTKView *)self.view; metal_view = (iOSView *) self.view;
metal_view.device = MTLCreateSystemDefaultDevice(); metal_view.device = MTLCreateSystemDefaultDevice();
metal_view.backgroundColor = UIColor.blackColor; metal_view.backgroundColor = UIColor.blackColor;

View File

@ -181,7 +181,7 @@ NSString *g_shader_src = kShader(
// holding onto the drawable and blocking the display pipeline any longer than necessary // holding onto the drawable and blocking the display pipeline any longer than necessary
MTLRenderPassDescriptor* renderPassDescriptor = view.currentRenderPassDescriptor; MTLRenderPassDescriptor* renderPassDescriptor = view.currentRenderPassDescriptor;
if (renderPassDescriptor != nil) { 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 // Create a render command encoder so we can render into something
id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];

View File

@ -12,9 +12,10 @@
{ {
CADisplayLink *mDisplayLink; CADisplayLink *mDisplayLink;
} }
@property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UIWindow *window;
- (void) onUpdate; - (void) OnUpdateFrame;
@end @end

View File

@ -8,71 +8,40 @@
#import "AppDelegate.h" #import "AppDelegate.h"
#include <MiniFB.h> #include <MiniFB.h>
#include <iOSViewController.h>
//-------------------------------------
#define kUnused(var) (void) var; #define kUnused(var) (void) var;
//-------------------------------------
struct mfb_window *g_window = 0x0; struct mfb_window *g_window = 0x0;
uint32_t *g_buffer = 0x0; uint32_t *g_buffer = 0x0;
uint32_t g_width = 0; uint32_t g_width = 0;
uint32_t g_height = 0; uint32_t g_height = 0;
//-------------------------------------
@interface AppDelegate () @interface AppDelegate ()
@end @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 @implementation AppDelegate
//-------------------------------------
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - (void) OnUpdateFrame {
// 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 {
static int seed = 0xbeef; static int seed = 0xbeef;
int noise, carry; 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 @end