update_ex working on iOS

This commit is contained in:
Carlos Aragones 2020-09-15 21:05:49 +02:00
parent f4c537d6f0
commit 5f9594cb23
6 changed files with 35 additions and 6 deletions

View File

@ -4,10 +4,13 @@
#include <WindowData.h> #include <WindowData.h>
#include <MetalKit/MetalKit.h> #include <MetalKit/MetalKit.h>
@class iOSViewDelegate;
typedef struct Vertex { typedef struct Vertex {
float x, y, z, w; float x, y, z, w;
} Vertex; } Vertex;
typedef struct { typedef struct {
Vertex vertices[4]; iOSViewDelegate *view_delegate;
Vertex vertices[4];
} SWindowData_IOS; } SWindowData_IOS;

View File

@ -150,6 +150,17 @@ mfb_update_ex(struct mfb_window *window, void *buffer, unsigned width, unsigned
return STATE_INVALID_BUFFER; 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); memcpy(window_data->draw_buffer, buffer, window_data->buffer_width * window_data->buffer_height * 4);
return STATE_OK; return STATE_OK;

View File

@ -17,7 +17,7 @@
@implementation iOSViewController @implementation iOSViewController
{ {
iOSView *metal_view; iOSView *metal_view;
iOSViewDelegate *view_delegate; //iOSViewDelegate *view_delegate;
} }
//------------------------------------- //-------------------------------------
@ -60,10 +60,11 @@
return; return;
} }
view_delegate = [[iOSViewDelegate alloc] initWithMetalKitView:metal_view windowData:window_data]; SWindowData_IOS *window_data_ios = (SWindowData_IOS *) window_data->specific;
[view_delegate mtkView:metal_view drawableSizeWillChange:metal_view.bounds.size]; 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 @end

View File

@ -17,6 +17,7 @@
} }
-(nonnull instancetype) initWithMetalKitView:(nonnull MTKView *) view windowData:(nonnull SWindowData *) windowData; -(nonnull instancetype) initWithMetalKitView:(nonnull MTKView *) view windowData:(nonnull SWindowData *) windowData;
- (void) resizeTextures;
@end @end

View File

@ -167,6 +167,19 @@ NSString *g_shader_src = kShader(
texture_buffer = [metal_device newTextureWithDescriptor:td]; 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 { - (void) drawInMTKView:(nonnull MTKView *) view {
// Wait to ensure only MaxBuffersInFlight number of frames are getting proccessed // Wait to ensure only MaxBuffersInFlight number of frames are getting proccessed

View File

@ -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) { if (state != STATE_OK) {
free(g_buffer); free(g_buffer);
g_buffer = 0x0; g_buffer = 0x0;