work with and without already created window
This commit is contained in:
parent
6452ec1bfd
commit
fdd7d8bedb
@ -2,7 +2,6 @@
|
||||
|
||||
#include <MiniFB_enums.h>
|
||||
#include <WindowData.h>
|
||||
|
||||
#include <MetalKit/MetalKit.h>
|
||||
|
||||
typedef struct Vertex {
|
||||
|
@ -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;
|
||||
|
9
src/ios/iOSView.h
Normal file
9
src/ios/iOSView.h
Normal 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
80
src/ios/iOSView.m
Normal 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
|
@ -10,6 +10,9 @@
|
||||
#include "WindowData.h"
|
||||
|
||||
@interface iOSViewController : UIViewController
|
||||
{
|
||||
@public SWindowData *window_data;
|
||||
}
|
||||
|
||||
- (id) initWithWindowData:(SWindowData *) windowData;
|
||||
|
||||
|
@ -6,17 +6,18 @@
|
||||
// Copyright © 2020 Carlos Aragones. All rights reserved.
|
||||
//
|
||||
|
||||
#import "iOSViewController.h"
|
||||
#import <Metal/Metal.h>
|
||||
#import <MetalKit/MetalKit.h>
|
||||
#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;
|
||||
|
||||
|
@ -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<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
|
||||
|
@ -12,9 +12,10 @@
|
||||
{
|
||||
CADisplayLink *mDisplayLink;
|
||||
}
|
||||
|
||||
@property (strong, nonatomic) UIWindow *window;
|
||||
|
||||
- (void) onUpdate;
|
||||
- (void) OnUpdateFrame;
|
||||
|
||||
@end
|
||||
|
||||
|
@ -8,71 +8,40 @@
|
||||
|
||||
#import "AppDelegate.h"
|
||||
#include <MiniFB.h>
|
||||
#include <iOSViewController.h>
|
||||
|
||||
//-------------------------------------
|
||||
#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
|
||||
|
Loading…
Reference in New Issue
Block a user