From 5f9594cb2307de818bf12efdcb934f5b243cc811 Mon Sep 17 00:00:00 2001 From: Carlos Aragones <> Date: Tue, 15 Sep 2020 21:05:49 +0200 Subject: [PATCH] update_ex working on iOS --- src/ios/WindowData_IOS.h | 5 ++++- src/ios/iOSMiniFB.m | 11 +++++++++++ src/ios/iOSViewController.m | 9 +++++---- src/ios/iOSViewDelegate.h | 1 + src/ios/iOSViewDelegate.m | 13 +++++++++++++ tests/ios/AppDelegate.m | 2 +- 6 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/ios/WindowData_IOS.h b/src/ios/WindowData_IOS.h index 93b97a6..b4e31f9 100644 --- a/src/ios/WindowData_IOS.h +++ b/src/ios/WindowData_IOS.h @@ -4,10 +4,13 @@ #include #include +@class iOSViewDelegate; + typedef struct Vertex { float x, y, z, w; } Vertex; typedef struct { - Vertex vertices[4]; + iOSViewDelegate *view_delegate; + Vertex vertices[4]; } SWindowData_IOS; diff --git a/src/ios/iOSMiniFB.m b/src/ios/iOSMiniFB.m index 231a6cd..9475dd0 100644 --- a/src/ios/iOSMiniFB.m +++ b/src/ios/iOSMiniFB.m @@ -150,6 +150,17 @@ mfb_update_ex(struct mfb_window *window, void *buffer, unsigned width, unsigned return STATE_INVALID_BUFFER; } + SWindowData_IOS *window_data_ios = (SWindowData_IOS *) window_data->specific; + + if(window_data->buffer_width != width || window_data->buffer_height != height) { + window_data->buffer_width = width; + window_data->buffer_stride = width * 4; + window_data->buffer_height = height; + window_data->draw_buffer = realloc(window_data->draw_buffer, window_data->buffer_stride * window_data->buffer_height); + + [window_data_ios->view_delegate resizeTextures]; + } + memcpy(window_data->draw_buffer, buffer, window_data->buffer_width * window_data->buffer_height * 4); return STATE_OK; diff --git a/src/ios/iOSViewController.m b/src/ios/iOSViewController.m index 07ff135..23c85c6 100644 --- a/src/ios/iOSViewController.m +++ b/src/ios/iOSViewController.m @@ -17,7 +17,7 @@ @implementation iOSViewController { iOSView *metal_view; - iOSViewDelegate *view_delegate; + //iOSViewDelegate *view_delegate; } //------------------------------------- @@ -60,10 +60,11 @@ return; } - view_delegate = [[iOSViewDelegate alloc] initWithMetalKitView:metal_view windowData:window_data]; - [view_delegate mtkView:metal_view drawableSizeWillChange:metal_view.bounds.size]; + SWindowData_IOS *window_data_ios = (SWindowData_IOS *) window_data->specific; + window_data_ios->view_delegate = [[iOSViewDelegate alloc] initWithMetalKitView:metal_view windowData:window_data]; + [window_data_ios->view_delegate mtkView:metal_view drawableSizeWillChange:metal_view.bounds.size]; - metal_view.delegate = view_delegate; + metal_view.delegate = window_data_ios->view_delegate; } @end diff --git a/src/ios/iOSViewDelegate.h b/src/ios/iOSViewDelegate.h index b434760..4387626 100644 --- a/src/ios/iOSViewDelegate.h +++ b/src/ios/iOSViewDelegate.h @@ -17,6 +17,7 @@ } -(nonnull instancetype) initWithMetalKitView:(nonnull MTKView *) view windowData:(nonnull SWindowData *) windowData; +- (void) resizeTextures; @end diff --git a/src/ios/iOSViewDelegate.m b/src/ios/iOSViewDelegate.m index 6162693..cee6825 100644 --- a/src/ios/iOSViewDelegate.m +++ b/src/ios/iOSViewDelegate.m @@ -167,6 +167,19 @@ NSString *g_shader_src = kShader( texture_buffer = [metal_device newTextureWithDescriptor:td]; } +//------------------------------------- +- (void) resizeTextures { + MTLTextureDescriptor *td; + td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm + width:window_data->buffer_width + height:window_data->buffer_height + mipmapped:false]; + + // Create the texture from the device by using the descriptor + [texture_buffer release]; + texture_buffer = [metal_device newTextureWithDescriptor:td]; +} + //------------------------------------- - (void) drawInMTKView:(nonnull MTKView *) view { // Wait to ensure only MaxBuffersInFlight number of frames are getting proccessed diff --git a/tests/ios/AppDelegate.m b/tests/ios/AppDelegate.m index 2eac44d..d10f2c0 100644 --- a/tests/ios/AppDelegate.m +++ b/tests/ios/AppDelegate.m @@ -74,7 +74,7 @@ resize(struct mfb_window *window, int width, int height) { } } - mfb_update_state state = mfb_update(g_window, g_buffer); + mfb_update_state state = mfb_update_ex(g_window, g_buffer, g_width, g_height); if (state != STATE_OK) { free(g_buffer); g_buffer = 0x0;