From 783c478ee2639b828e9d04d78081cbf9b173a168 Mon Sep 17 00:00:00 2001 From: Interrupt Date: Fri, 7 Apr 2023 13:59:02 -0700 Subject: [PATCH] Supporting native arm builds on OSX --- build_deps.sh | 4 ++-- src/MacSrc/OpenGL.cc | 23 +++++++++++++++++------ src/MacSrc/OpenGL.h | 5 +++-- src/MacSrc/Shock.c | 27 +++++++++++++++------------ 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/build_deps.sh b/build_deps.sh index df5d1280..d2e720a9 100755 --- a/build_deps.sh +++ b/build_deps.sh @@ -1,8 +1,8 @@ #!/bin/bash set -e -SDL_version=2.0.9 -SDL2_mixer_version=2.0.4 +SDL_version=2.26.0 +SDL2_mixer_version=2.6.3 if [ -d ./build_ext/ ]; then echo A directory named build_ext already exists. diff --git a/src/MacSrc/OpenGL.cc b/src/MacSrc/OpenGL.cc index cd2777c1..4056105e 100644 --- a/src/MacSrc/OpenGL.cc +++ b/src/MacSrc/OpenGL.cc @@ -294,6 +294,7 @@ int init_opengl() { } // Can we create the world rendering context? + SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); context = SDL_GL_CreateContext(window); if (context == nullptr) { ERROR("Could not create an OpenGL context! Falling back to Software mode."); @@ -444,21 +445,19 @@ void get_hdpi_scaling(int *x_scale, int *y_scale) { *y_scale = output_height / screen_height; } -void opengl_swap_and_restore() { - // restore the view backup (without HUD overlay) for incremental - // updates in the subsequent frame +void opengl_swap_and_restore(SDL_Surface *ui) { SDL_GL_MakeCurrent(window, context); - SDL_GL_SwapWindow(window); - glClear(GL_COLOR_BUFFER_BIT); int x_hdpi_scale, y_hdpi_scale; get_hdpi_scaling(&x_hdpi_scale, &y_hdpi_scale); + // Set the drawable area for the 3d view glViewport(phys_offset_x * x_hdpi_scale, phys_offset_y * y_hdpi_scale, phys_width * x_hdpi_scale, phys_height * y_hdpi_scale); set_blend_mode(false); + // Bind and setup our general shader program glUseProgram(textureShaderProgram.shaderProgram); GLint tcAttrib = textureShaderProgram.tcAttrib; GLint lightAttrib = textureShaderProgram.lightAttrib; @@ -468,6 +467,7 @@ void opengl_swap_and_restore() { glUniformMatrix4fv(textureShaderProgram.uniView, 1, false, IdentityMatrix); glUniformMatrix4fv(textureShaderProgram.uniProj, 1, false, IdentityMatrix); + // Draw the frame buffer to the screen as a quad bind_texture(backupBuffer.texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -487,14 +487,25 @@ void opengl_swap_and_restore() { glVertex3f(-1.0f, 1.0f, 0.0f); glEnd(); + // Finish drawing the 3d view glFlush(); glUniform1i(textureShaderProgram.uniNightSight, false); - // check OpenGL error + // Check for OpenGL errors that might have happened GLenum err = glGetError(); if (err != GL_NO_ERROR) ERROR("OpenGL error: %i", err); + + // Blit the UI canvas over the 3d view + SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, ui); + SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); + + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_DestroyTexture(texture); + + // Finally, swap to the screen + SDL_RenderPresent(renderer); } void toggle_opengl() { diff --git a/src/MacSrc/OpenGL.h b/src/MacSrc/OpenGL.h index bcab1b8e..b11c8936 100644 --- a/src/MacSrc/OpenGL.h +++ b/src/MacSrc/OpenGL.h @@ -6,6 +6,7 @@ extern "C" { #endif #include <3d.h> +#include #ifdef USE_OPENGL @@ -18,7 +19,7 @@ bool use_opengl(); void toggle_opengl(); void opengl_resize(int width, int height); bool should_opengl_swap(); -void opengl_swap_and_restore(); +void opengl_swap_and_restore(SDL_Surface *ui); void opengl_change_palette(); void opengl_set_viewport(int x, int y, int width, int height); @@ -46,7 +47,7 @@ static bool use_opengl() { return false; } static void toggle_opengl() {} static void opengl_resize(int width, int height) {} static bool should_opengl_swap() { return false; } -static void opengl_swap_and_restore() {} +static void opengl_swap_and_restore(SDL_Surface *ui) {} static void opengl_change_palette() {} static void opengl_set_viewport(int x, int y, int width, int height) {} diff --git a/src/MacSrc/Shock.c b/src/MacSrc/Shock.c index 973f9d72..7f7ee779 100644 --- a/src/MacSrc/Shock.c +++ b/src/MacSrc/Shock.c @@ -194,6 +194,7 @@ void InitSDL() { char window_title[128]; sprintf(window_title, "System Shock - %s", SHOCKOLATE_VERSION); + SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, grd_cap->w, grd_cap->h, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL); @@ -232,7 +233,7 @@ void SetSDLPalette(int index, int count, uchar *pal) { static bool gammalut_init = 0; static uchar gammalut[100 - 10 + 1][256]; if (!gammalut_init) { - double factor = (can_use_opengl() ? 1.0 : 2.2); // OpenGL uses 2.2 + double factor = (use_opengl() ? 1.0 : 2.2); // OpenGL uses 2.2 int i, j; for (i = 10; i <= 100; i++) { double gamma = (double)i * 1.0 / 100; @@ -279,26 +280,28 @@ void SetSDLPalette(int index, int count, uchar *pal) { void SDLDraw() { if (should_opengl_swap()) { + // We want the UI background to be transparent! sdlPalette->colors[255].a = 0x00; - } - - SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, drawSurface); - if (should_opengl_swap()) { + // Draw the OpenGL view + opengl_swap_and_restore(drawSurface); + + // Set the palette back, and we are done sdlPalette->colors[255].a = 0xff; - SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); + return; } + // Clear the screen! + SDL_RenderClear(renderer); + SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, drawSurface); + + // Blit to the screen by drawing the surface SDL_Rect srcRect = {0, 0, gScreenWide, gScreenHigh}; SDL_RenderCopy(renderer, texture, &srcRect, NULL); SDL_DestroyTexture(texture); - if (should_opengl_swap()) { - opengl_swap_and_restore(); - } else { - SDL_RenderPresent(renderer); - SDL_RenderClear(renderer); - } + // Show everything we've drawn + SDL_RenderPresent(renderer); } bool MouseCaptured = FALSE;