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 "MiniFB_internal.h"
#include "WindowData.h" #include "WindowData.h"
#include "WindowData_IOS.h" #include "WindowData_IOS.h"
#include "iOSViewController.h"
//------------------------------------- //-------------------------------------
struct mfb_window * struct mfb_window *
@ -17,6 +18,7 @@ struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) { mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
kUnused(title); kUnused(title);
kUnused(flags); kUnused(flags);
SWindowData *window_data; SWindowData *window_data;
window_data = malloc(sizeof(SWindowData)); 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)); memset((void *) window_data_ios, 0, sizeof(SWindowData_IOS));
window_data->specific = window_data_ios; window_data->specific = window_data_ios;
window_data->window_width = width; window_data->window_width = [UIScreen mainScreen].bounds.size.width;
window_data->window_height = height; window_data->window_height = [UIScreen mainScreen].bounds.size.height;
window_data->dst_width = width; window_data->dst_width = width;
window_data->dst_height = height; 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); window_data->draw_buffer = malloc(width * height * 4);
if (!window_data->draw_buffer) { if (!window_data->draw_buffer) {
NSLog(@"Unable to create 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; return (struct mfb_window *) window_data;
} }

View File

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

View File

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

View File

@ -14,10 +14,10 @@
// update and drawable resize callbacks. // update and drawable resize callbacks.
@interface iOSViewDelegate : NSObject <MTKViewDelegate> @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 @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]; self = [super init];
if (self) { if (self) {
self->window_data = windowData;
g_metal_device = view.device; g_metal_device = view.device;
view.colorPixelFormat = MTLPixelFormatBGRA8Unorm; view.colorPixelFormat = MTLPixelFormatBGRA8Unorm;
view.sampleCount = 1; 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_semaphore = dispatch_semaphore_create(MaxBuffersInFlight);
m_command_queue = [g_metal_device newCommandQueue]; m_command_queue = [g_metal_device newCommandQueue];
[self _createShaders]; [self _createShaders];
[self _createAssets]; [self _createAssets];
user_implemented_init((struct mfb_window *) m_window_data);
} }
return self; return self;
@ -154,8 +149,8 @@ NSString *g_shader_src = kShader(
- (void) _createAssets { - (void) _createAssets {
MTLTextureDescriptor *td; MTLTextureDescriptor *td;
td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
width:m_window_data->window_width width:window_data->buffer_width
height:m_window_data->window_height height:window_data->buffer_height
mipmapped:false]; mipmapped:false];
m_texture_buffer = [g_metal_device newTextureWithDescriptor:td]; m_texture_buffer = [g_metal_device newTextureWithDescriptor:td];
@ -165,8 +160,6 @@ NSString *g_shader_src = kShader(
- (void) drawInMTKView:(nonnull MTKView *) view - (void) drawInMTKView:(nonnull MTKView *) view
{ {
// Per frame updates here // Per frame updates here
user_implemented_update((struct mfb_window *) m_window_data);
dispatch_semaphore_wait(m_semaphore, DISPATCH_TIME_FOREVER); dispatch_semaphore_wait(m_semaphore, DISPATCH_TIME_FOREVER);
m_current_buffer = (m_current_buffer + 1) % MaxBuffersInFlight; 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; __block dispatch_semaphore_t block_sema = m_semaphore;
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) { [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 // 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 } }; MTLRegion region = { { 0, 0, 0 }, { window_data->buffer_width, window_data->buffer_height, 1 } };
[m_texture_buffer replaceRegion:region mipmapLevel:0 withBytes:m_window_data->draw_buffer bytesPerRow:m_window_data->buffer_stride]; [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 // Delay getting the currentRenderPassDescriptor until absolutely needed. This avoids
// 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

View File

@ -14,7 +14,7 @@
} }
@property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UIWindow *window;
- (void) on_update; - (void) onUpdate;
@end @end

View File

@ -10,24 +10,69 @@
#include <MiniFB.h> #include <MiniFB.h>
#include <iOSViewController.h> #include <iOSViewController.h>
static uint32_t *g_buffer = 0x0; #define kUnused(var) (void) var;
static uint32_t g_width = 0;
static uint32_t g_height = 0; struct mfb_window *g_window = 0x0;
uint32_t *g_buffer = 0x0;
uint32_t g_width = 0;
uint32_t g_height = 0;
@interface AppDelegate ()
@end
@implementation AppDelegate
//------------------------------------- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
void // Override point for customization after application launch.
user_implemented_init(struct mfb_window *window) { kUnused(application);
g_width = mfb_get_window_width(window); kUnused(launchOptions);
g_height = mfb_get_window_height(window); if(g_window == 0x0) {
g_buffer = malloc(g_width * g_height * 4); g_width = [UIScreen mainScreen].bounds.size.width;
g_height = [UIScreen mainScreen].bounds.size.height;
// mfb_set_viewport(window, 50, 50, 200, 200); g_buffer = malloc(g_width * g_height * 4);
g_window = mfb_open("noise", g_width, g_height);
}
return YES;
} }
//------------------------------------- - (void) applicationWillResignActive:(UIApplication *)application {
void // 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.
user_implemented_update(struct mfb_window *window) { // 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;
@ -45,7 +90,7 @@ user_implemented_update(struct mfb_window *window) {
} }
} }
mfb_update_state state = mfb_update(window, g_buffer); mfb_update_state state = mfb_update(g_window, g_buffer);
if (state != STATE_OK) { if (state != STATE_OK) {
free(g_buffer); free(g_buffer);
g_buffer = 0x0; g_buffer = 0x0;
@ -54,75 +99,4 @@ user_implemented_update(struct mfb_window *window) {
} }
} }
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void) on_update {
NSLog(@"si");
}
- (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.
}
- (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.
}
- (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.
}
- (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.
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] init];
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];
mDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(on_update)];
[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:.
[mDisplayLink invalidate];
}
@end @end