Skip to content

Commit 6469070

Browse files
authored
Merge pull request #419 from MacSourcePorts/master
Fixes for Mac App Bundle and OpenGL on macOS
2 parents 805f564 + 0c30fe4 commit 6469070

7 files changed

Lines changed: 79 additions & 18 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ savgam*.dat
5858
# Eclipse CDT
5959
.project
6060
.cproject
61+
.DS_Store

src/GameSrc/setup.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,13 @@ errtype load_savegame_names(void) {
12071207
for (i = 0; i < NUM_SAVE_SLOTS; i++) {
12081208
Poke_SaveName(i);
12091209

1210+
#ifdef __APPLE__
1211+
char full_save_game_name[1024];
1212+
sprintf(full_save_game_name, "%s%s", SDL_GetPrefPath("Interrupt", "SystemShock"), save_game_name);
1213+
if (access(full_save_game_name, F_OK) != -1) {
1214+
#else
12101215
if (access(save_game_name, F_OK) != -1) {
1216+
#endif
12111217
file = ResOpenFile(save_game_name);
12121218
if (ResInUse(OLD_SAVE_GAME_ID_BASE)) {
12131219
#ifdef OLD_SG_FORMAT

src/Libraries/RES/Source/caseless.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
#include <string.h>
2424
#include <stdio.h>
2525
#include <sys/stat.h>
26+
#ifdef __APPLE__
27+
#include <SDL.h>
28+
#endif
2629

2730
#ifndef PATH_MAX
2831
#define PATH_MAX 4096
@@ -245,7 +248,16 @@ FILE *fopen_caseless(const char *path, const char *mode) {
245248
if (path == NULL || mode == NULL)
246249
return NULL;
247250

251+
#ifdef __APPLE__
252+
char *macpath;
253+
const char * prefix = SDL_GetPrefPath("Interrupt", "SystemShock");
254+
macpath = (char *)malloc(strlen(prefix) + strlen(path) + 1);
255+
strcpy(macpath, prefix);
256+
strcpy(&macpath[strlen(prefix)], path);
257+
ret = fopen(macpath, mode);
258+
#else
248259
ret = fopen(path, mode);
260+
#endif
249261

250262
#ifndef _WIN32 // not windows
251263
if (ret == NULL) {

src/MacSrc/OpenGL.cc

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,13 @@ static GLuint loadShader(GLenum type, const char *filename) {
184184

185185
DEBUG("Loading shader %s", filename);
186186

187+
#ifdef __APPLE__
188+
char fb[1024];
189+
sprintf(fb, "%s/shaders/%s", SDL_GetBasePath(), filename);
190+
#else
187191
char fb[256];
188192
sprintf(fb, "shaders/%s", filename);
193+
#endif
189194

190195
FILE *file = fopen(fb, "r");
191196
if (file == nullptr) {
@@ -294,6 +299,7 @@ int init_opengl() {
294299
}
295300

296301
// Can we create the world rendering context?
302+
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
297303
context = SDL_GL_CreateContext(window);
298304
if (context == nullptr) {
299305
ERROR("Could not create an OpenGL context! Falling back to Software mode.");
@@ -444,21 +450,21 @@ void get_hdpi_scaling(int *x_scale, int *y_scale) {
444450
*y_scale = output_height / screen_height;
445451
}
446452

447-
void opengl_swap_and_restore() {
453+
void opengl_swap_and_restore(SDL_Surface *ui) {
448454
// restore the view backup (without HUD overlay) for incremental
449455
// updates in the subsequent frame
450456
SDL_GL_MakeCurrent(window, context);
451-
SDL_GL_SwapWindow(window);
452-
453457
glClear(GL_COLOR_BUFFER_BIT);
454458

455459
int x_hdpi_scale, y_hdpi_scale;
456460
get_hdpi_scaling(&x_hdpi_scale, &y_hdpi_scale);
457461

462+
// Set the drawable area for the 3d view
458463
glViewport(phys_offset_x * x_hdpi_scale, phys_offset_y * y_hdpi_scale, phys_width * x_hdpi_scale,
459464
phys_height * y_hdpi_scale);
460465
set_blend_mode(false);
461466

467+
// Bind and setup our general shader program
462468
glUseProgram(textureShaderProgram.shaderProgram);
463469
GLint tcAttrib = textureShaderProgram.tcAttrib;
464470
GLint lightAttrib = textureShaderProgram.lightAttrib;
@@ -468,6 +474,7 @@ void opengl_swap_and_restore() {
468474
glUniformMatrix4fv(textureShaderProgram.uniView, 1, false, IdentityMatrix);
469475
glUniformMatrix4fv(textureShaderProgram.uniProj, 1, false, IdentityMatrix);
470476

477+
// Draw the frame buffer to the screen as a quad
471478
bind_texture(backupBuffer.texture);
472479
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
473480
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@@ -487,14 +494,25 @@ void opengl_swap_and_restore() {
487494
glVertex3f(-1.0f, 1.0f, 0.0f);
488495
glEnd();
489496

497+
// Finish drawing the 3d view
490498
glFlush();
491499

492500
glUniform1i(textureShaderProgram.uniNightSight, false);
493501

494-
// check OpenGL error
502+
// Check for OpenGL errors that might have happened
495503
GLenum err = glGetError();
496504
if (err != GL_NO_ERROR)
497505
ERROR("OpenGL error: %i", err);
506+
507+
// Blit the UI canvas over the 3d view
508+
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, ui);
509+
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
510+
511+
SDL_RenderCopy(renderer, texture, NULL, NULL);
512+
SDL_DestroyTexture(texture);
513+
514+
// Finally, swap to the screen
515+
SDL_RenderPresent(renderer);
498516
}
499517

500518
void toggle_opengl() {

src/MacSrc/OpenGL.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern "C" {
66
#endif
77

88
#include <3d.h>
9+
#include <SDL.h>
910

1011
#ifdef USE_OPENGL
1112

@@ -18,7 +19,7 @@ bool use_opengl();
1819
void toggle_opengl();
1920
void opengl_resize(int width, int height);
2021
bool should_opengl_swap();
21-
void opengl_swap_and_restore();
22+
void opengl_swap_and_restore(SDL_Surface *ui);
2223
void opengl_change_palette();
2324

2425
void opengl_set_viewport(int x, int y, int width, int height);
@@ -46,7 +47,7 @@ static bool use_opengl() { return false; }
4647
static void toggle_opengl() {}
4748
static void opengl_resize(int width, int height) {}
4849
static bool should_opengl_swap() { return false; }
49-
static void opengl_swap_and_restore() {}
50+
static void opengl_swap_and_restore(SDL_Surface *ui) {}
5051
static void opengl_change_palette() {}
5152

5253
static void opengl_set_viewport(int x, int y, int width, int height) {}

src/MacSrc/Shock.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ void SetSDLPalette(int index, int count, uchar *pal) {
232232
static bool gammalut_init = 0;
233233
static uchar gammalut[100 - 10 + 1][256];
234234
if (!gammalut_init) {
235-
double factor = (can_use_opengl() ? 1.0 : 2.2); // OpenGL uses 2.2
235+
double factor = (use_opengl() ? 1.0 : 2.2); // OpenGL uses 2.2
236236
int i, j;
237237
for (i = 10; i <= 100; i++) {
238238
double gamma = (double)i * 1.0 / 100;
@@ -279,26 +279,28 @@ void SetSDLPalette(int index, int count, uchar *pal) {
279279

280280
void SDLDraw() {
281281
if (should_opengl_swap()) {
282+
// We want the UI background to be transparent!
282283
sdlPalette->colors[255].a = 0x00;
283-
}
284284

285-
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, drawSurface);
285+
// Draw the OpenGL view
286+
opengl_swap_and_restore(drawSurface);
286287

287-
if (should_opengl_swap()) {
288+
// Set the palette back, and we are done
288289
sdlPalette->colors[255].a = 0xff;
289-
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
290+
return;
290291
}
291292

293+
// Clear the screen!
294+
SDL_RenderClear(renderer);
295+
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, drawSurface);
296+
297+
// Blit to the screen by drawing the surface
292298
SDL_Rect srcRect = {0, 0, gScreenWide, gScreenHigh};
293299
SDL_RenderCopy(renderer, texture, &srcRect, NULL);
294300
SDL_DestroyTexture(texture);
295301

296-
if (should_opengl_swap()) {
297-
opengl_swap_and_restore();
298-
} else {
299-
SDL_RenderPresent(renderer);
300-
SDL_RenderClear(renderer);
301-
}
302+
// Show everything we've drawn
303+
SDL_RenderPresent(renderer);
302304
}
303305

304306
bool MouseCaptured = FALSE;

src/MusicSrc/MusicDevice.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
# include <sys/types.h>
1919
# include <dirent.h>
2020
#endif
21+
#ifdef __APPLE__
22+
#include <SDL.h>
23+
#endif
2124

2225
//------------------------------------------------------------------------------
2326
// Dummy MIDI player
@@ -1023,9 +1026,15 @@ static int FluidMidiInit(MusicDevice *dev, const unsigned int outputIndex, unsig
10231026
fluid_settings_t *settings;
10241027
fluid_synth_t *synth;
10251028
int sfid;
1026-
char fileName[1024] = "res/";
1029+
#ifdef __APPLE__
1030+
char fileName[1024] = "";
1031+
sprintf(fileName, "%sres/", SDL_GetBasePath());
10271032

1033+
FluidMidiGetOutputName(dev, outputIndex, &fileName[strlen(fileName)], 1024 - strlen(fileName));
1034+
#else
1035+
char fileName[1024] = "res/";
10281036
FluidMidiGetOutputName(dev, outputIndex, &fileName[4], 1020);
1037+
#endif
10291038
if (strlen(fileName) == 4)
10301039
{
10311040
WARN("Failed to locate SoundFont for outputIndex=%d", outputIndex);
@@ -1183,8 +1192,14 @@ static unsigned int FluidMidiGetOutputCount(MusicDevice *dev)
11831192
do { ++outputCount; } while (FindNextFile(hFind, &data));
11841193
FindClose(hFind);
11851194
}
1195+
#else
1196+
#ifdef __APPLE__
1197+
char *respath[1024];
1198+
sprintf(respath, "%sres/", SDL_GetBasePath());
1199+
DIR *dirp = opendir(respath);
11861200
#else
11871201
DIR *dirp = opendir("res");
1202+
#endif
11881203
struct dirent *dp = 0;
11891204
while ((dp = readdir(dirp)))
11901205
{
@@ -1237,7 +1252,13 @@ static void FluidMidiGetOutputName(MusicDevice *dev, const unsigned int outputIn
12371252
unsigned int outputCount = 0; // subtract 1 to get index
12381253
// count .sf2 files in res/ subdirectory until we find the one that the user
12391254
// probably wants
1255+
#ifdef __APPLE__
1256+
char *respath[1024];
1257+
sprintf(respath, "%sres/", SDL_GetBasePath());
1258+
DIR *dirp = opendir(respath);
1259+
#else
12401260
DIR *dirp = opendir("res");
1261+
#endif
12411262
struct dirent *dp = 0;
12421263
while ((outputCount <= outputIndex) &&
12431264
(dp = readdir(dirp)))

0 commit comments

Comments
 (0)