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 <WindowData.h>
#include <MetalKit/MetalKit.h>
typedef struct Vertex {

View File

@ -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
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"
@interface iOSViewController : UIViewController
{
@public SWindowData *window_data;
}
- (id) initWithWindowData:(SWindowData *) windowData;

View File

@ -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;

View File

@ -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];