From 78efec66c57165a39134c2e30c5e96d7a5946644 Mon Sep 17 00:00:00 2001 From: Carlos Aragones <> Date: Sun, 26 Apr 2020 19:50:53 +0200 Subject: [PATCH] fix issue with mfb_set_viewport on macos x --- src/macosx/MacMiniFB.m | 33 ++++++++++++++++++--------------- src/macosx/OSXWindowFrameView.h | 4 ---- src/macosx/OSXWindowFrameView.m | 4 +--- src/macosx/WindowData_OSX.h | 5 +++++ tests/multiple_windows.c | 2 +- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/macosx/MacMiniFB.m b/src/macosx/MacMiniFB.m index 41234c4..6b7ab38 100644 --- a/src/macosx/MacMiniFB.m +++ b/src/macosx/MacMiniFB.m @@ -21,13 +21,6 @@ void init_keycodes(); id g_metal_device = nil; id g_library = nil; -Vertex g_vertices[4] = { - {-1.0, -1.0, 0, 1}, - {-1.0, 1.0, 0, 1}, - { 1.0, -1.0, 0, 1}, - { 1.0, 1.0, 0, 1}, -}; - //------------------------------------- #define kShader(inc, src) @inc#src @@ -185,6 +178,14 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) if (!window_data->draw_buffer) return 0x0; + static Vertex s_vertices[4] = { + {-1.0, -1.0, 0, 1}, + {-1.0, 1.0, 0, 1}, + { 1.0, -1.0, 0, 1}, + { 1.0, 1.0, 0, 1}, + }; + memcpy(window_data_osx->metal.vertices, s_vertices, sizeof(s_vertices)); + // Setup command queue window_data_osx->metal.command_queue = [g_metal_device newCommandQueue]; @@ -431,17 +432,19 @@ mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y float y1 = ((float) offset_y / window_data->window_height) * 2.0f - 1.0f; float y2 = (((float) offset_y + height) / window_data->window_height) * 2.0f - 1.0f; - g_vertices[0].x = x1; - g_vertices[0].y = y1; + SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific; - g_vertices[1].x = x1; - g_vertices[1].y = y2; + window_data_osx->metal.vertices[0].x = x1; + window_data_osx->metal.vertices[0].y = y1; - g_vertices[2].x = x2; - g_vertices[2].y = y1; + window_data_osx->metal.vertices[1].x = x1; + window_data_osx->metal.vertices[1].y = y2; - g_vertices[3].x = x2; - g_vertices[3].y = y2; + window_data_osx->metal.vertices[2].x = x2; + window_data_osx->metal.vertices[2].y = y1; + + window_data_osx->metal.vertices[3].x = x2; + window_data_osx->metal.vertices[3].y = y2; #endif return true; diff --git a/src/macosx/OSXWindowFrameView.h b/src/macosx/OSXWindowFrameView.h index ad3b308..8636e09 100644 --- a/src/macosx/OSXWindowFrameView.h +++ b/src/macosx/OSXWindowFrameView.h @@ -5,10 +5,6 @@ #if defined(USE_METAL_API) #import -typedef struct Vertex { - float x, y, z, w; -} Vertex; - // Number of textures in flight (tripple buffered) enum { MaxBuffersInFlight = 3 }; diff --git a/src/macosx/OSXWindowFrameView.m b/src/macosx/OSXWindowFrameView.m index 1f5764a..2f678f3 100644 --- a/src/macosx/OSXWindowFrameView.m +++ b/src/macosx/OSXWindowFrameView.m @@ -9,8 +9,6 @@ extern id g_metal_device; extern id g_library; -extern Vertex g_vertices[4]; - @implementation WindowViewController - (void) mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size { @@ -69,7 +67,7 @@ extern Vertex g_vertices[4]; SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window->window_data->specific; [renderEncoder setRenderPipelineState:window_data_osx->metal.pipeline_state]; - [renderEncoder setVertexBytes:g_vertices length:sizeof(g_vertices) atIndex:0]; + [renderEncoder setVertexBytes:window_data_osx->metal.vertices length:sizeof(window_data_osx->metal.vertices) atIndex:0]; [renderEncoder setFragmentTexture:texture_buffers[current_buffer] atIndex:0]; diff --git a/src/macosx/WindowData_OSX.h b/src/macosx/WindowData_OSX.h index ec8bd5f..09f8aba 100644 --- a/src/macosx/WindowData_OSX.h +++ b/src/macosx/WindowData_OSX.h @@ -9,6 +9,10 @@ @class OSXWindow; +typedef struct Vertex { + float x, y, z, w; +} Vertex; + typedef struct { OSXWindow *window; struct mfb_timer *timer; @@ -17,6 +21,7 @@ typedef struct { struct { id command_queue; id pipeline_state; + Vertex vertices[4]; } metal; #endif } SWindowData_OSX; diff --git a/tests/multiple_windows.c b/tests/multiple_windows.c index da391da..caab598 100644 --- a/tests/multiple_windows.c +++ b/tests/multiple_windows.c @@ -107,7 +107,7 @@ main() mfb_set_mouse_scroll_callback(window_a, mouse_scroll); mfb_set_user_data(window_a, (void *) "Window A"); - mfb_set_viewport(window_a, 25, 25, WIDTH_A-25, HEIGHT_A-25); + mfb_set_viewport(window_a, 25, 25, WIDTH_A-50, HEIGHT_A-50); //-- struct mfb_window *window_b = mfb_open_ex("Secondary Window", WIDTH_B, HEIGHT_B, WF_RESIZABLE);