test 2 & refactor
This commit is contained in:
parent
8e1a981085
commit
f5db43e07d
@ -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;
|
||||
}
|
||||
|
||||
|
@ -7,8 +7,10 @@
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#include "iOSViewDelegate.h"
|
||||
#include "WindowData.h"
|
||||
|
||||
@interface iOSViewController : UIViewController
|
||||
|
||||
- (id) initWithWindowData:(SWindowData *) windowData;
|
||||
|
||||
@end
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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) {
|
||||
(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
|
||||
|
@ -14,7 +14,7 @@
|
||||
}
|
||||
@property (strong, nonatomic) UIWindow *window;
|
||||
|
||||
- (void) on_update;
|
||||
- (void) onUpdate;
|
||||
|
||||
@end
|
||||
|
||||
|
@ -10,24 +10,69 @@
|
||||
#include <MiniFB.h>
|
||||
#include <iOSViewController.h>
|
||||
|
||||
static uint32_t *g_buffer = 0x0;
|
||||
static uint32_t g_width = 0;
|
||||
static uint32_t g_height = 0;
|
||||
#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
|
||||
|
||||
@implementation AppDelegate
|
||||
|
||||
|
||||
//-------------------------------------
|
||||
void
|
||||
user_implemented_init(struct mfb_window *window) {
|
||||
g_width = mfb_get_window_width(window);
|
||||
g_height = mfb_get_window_height(window);
|
||||
- (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);
|
||||
|
||||
// mfb_set_viewport(window, 50, 50, 200, 200);
|
||||
g_window = mfb_open("noise", g_width, g_height);
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
void
|
||||
user_implemented_update(struct mfb_window *window) {
|
||||
- (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;
|
||||
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) {
|
||||
free(g_buffer);
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user