test 2 & refactor

This commit is contained in:
Carlos Aragones
2020-04-26 13:16:25 +02:00
parent 8e1a981085
commit f5db43e07d
7 changed files with 124 additions and 116 deletions

View File

@ -5,6 +5,7 @@
#include "MiniFB_internal.h"
#include "WindowData.h"
#include "WindowData_IOS.h"
#include "iOSViewController.h"
//-------------------------------------
struct mfb_window *
@ -17,6 +18,7 @@ struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
kUnused(title);
kUnused(flags);
SWindowData *window_data;
window_data = malloc(sizeof(SWindowData));
@ -26,8 +28,8 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
memset((void *) window_data_ios, 0, sizeof(SWindowData_IOS));
window_data->specific = window_data_ios;
window_data->window_width = width;
window_data->window_height = height;
window_data->window_width = [UIScreen mainScreen].bounds.size.width;
window_data->window_height = [UIScreen mainScreen].bounds.size.height;
window_data->dst_width = width;
window_data->dst_height = height;
@ -39,8 +41,34 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
window_data->draw_buffer = malloc(width * height * 4);
if (!window_data->draw_buffer) {
NSLog(@"Unable to create draw buffer");
return 0x0;
}
UIWindow *window;
NSArray *pWindows;
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];
}
else
{
// Notice that you need to set "Launch Screen File" in:
// project > executable > general to get the real size
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;
[window makeKeyAndVisible];
return (struct mfb_window *) window_data;
}

View File

@ -7,8 +7,10 @@
//
#import <UIKit/UIKit.h>
#include "iOSViewDelegate.h"
#include "WindowData.h"
@interface iOSViewController : UIViewController
- (id) initWithWindowData:(SWindowData *) windowData;
@end

View File

@ -14,8 +14,18 @@
//-------------------------------------
@implementation iOSViewController
{
MTKView *_view;
iOSViewDelegate *_renderer;
MTKView *metal_view;
iOSViewDelegate *view_delegate;
SWindowData *window_data;
}
//-------------------------------------
- (id) initWithWindowData:(SWindowData *) windowData {
self = [super init];
if (self) {
window_data = windowData;
}
return self;
}
//-------------------------------------
@ -30,20 +40,20 @@
{
[super viewDidLoad];
_view = (MTKView *)self.view;
_view.device = MTLCreateSystemDefaultDevice();
_view.backgroundColor = UIColor.blackColor;
metal_view = (MTKView *)self.view;
metal_view.device = MTLCreateSystemDefaultDevice();
metal_view.backgroundColor = UIColor.blackColor;
if(!_view.device) {
if(!metal_view.device) {
NSLog(@"Metal is not supported on this device");
self.view = [[UIView alloc] initWithFrame:self.view.frame];
return;
}
_renderer = [[iOSViewDelegate alloc] initWithMetalKitView:_view];
[_renderer mtkView:_view drawableSizeWillChange:_view.bounds.size];
view_delegate = [[iOSViewDelegate alloc] initWithMetalKitView:metal_view windowData:window_data];
[view_delegate mtkView:metal_view drawableSizeWillChange:metal_view.bounds.size];
_view.delegate = _renderer;
metal_view.delegate = view_delegate;
}
@end

View File

@ -14,10 +14,10 @@
// update and drawable resize callbacks.
@interface iOSViewDelegate : NSObject <MTKViewDelegate>
{
@public SWindowData *m_window_data;
@public SWindowData *window_data;
}
-(nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)view;
-(nonnull instancetype) initWithMetalKitView:(nonnull MTKView *) view windowData:(nonnull SWindowData *) windowData;
@end

View File

@ -84,26 +84,21 @@ NSString *g_shader_src = kShader(
}
//-------------------------------------
-(nonnull instancetype) initWithMetalKitView:(nonnull MTKView *) view {
-(nonnull instancetype) initWithMetalKitView:(nonnull MTKView *) view windowData:(nonnull SWindowData *) windowData {
self = [super init];
if (self) {
self->window_data = windowData;
g_metal_device = view.device;
view.colorPixelFormat = MTLPixelFormatBGRA8Unorm;
view.sampleCount = 1;
uint32_t width = view.bounds.size.width;
uint32_t height = view.bounds.size.height;
m_window_data = (SWindowData *) mfb_open_ex("", width, height, 0);
m_semaphore = dispatch_semaphore_create(MaxBuffersInFlight);
m_command_queue = [g_metal_device newCommandQueue];
[self _createShaders];
[self _createAssets];
user_implemented_init((struct mfb_window *) m_window_data);
}
return self;
@ -154,8 +149,8 @@ NSString *g_shader_src = kShader(
- (void) _createAssets {
MTLTextureDescriptor *td;
td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
width:m_window_data->window_width
height:m_window_data->window_height
width:window_data->buffer_width
height:window_data->buffer_height
mipmapped:false];
m_texture_buffer = [g_metal_device newTextureWithDescriptor:td];
@ -165,8 +160,6 @@ NSString *g_shader_src = kShader(
- (void) drawInMTKView:(nonnull MTKView *) view
{
// Per frame updates here
user_implemented_update((struct mfb_window *) m_window_data);
dispatch_semaphore_wait(m_semaphore, DISPATCH_TIME_FOREVER);
m_current_buffer = (m_current_buffer + 1) % MaxBuffersInFlight;
@ -176,12 +169,13 @@ NSString *g_shader_src = kShader(
__block dispatch_semaphore_t block_sema = m_semaphore;
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
dispatch_semaphore_signal(block_sema);
(void)buffer;
dispatch_semaphore_signal(block_sema);
}];
// Copy the bytes from our data object into the texture
MTLRegion region = { { 0, 0, 0 }, { m_window_data->window_width, m_window_data->window_height, 1 } };
[m_texture_buffer replaceRegion:region mipmapLevel:0 withBytes:m_window_data->draw_buffer bytesPerRow:m_window_data->buffer_stride];
MTLRegion region = { { 0, 0, 0 }, { window_data->buffer_width, window_data->buffer_height, 1 } };
[m_texture_buffer replaceRegion:region mipmapLevel:0 withBytes:window_data->draw_buffer bytesPerRow:window_data->buffer_stride];
// Delay getting the currentRenderPassDescriptor until absolutely needed. This avoids
// holding onto the drawable and blocking the display pipeline any longer than necessary