Skip to content

Commit

Permalink
Add Support for macOS (HarbourMasters#441)
Browse files Browse the repository at this point in the history
* Fixed soh filters

* add more makefile changes

* almost ready

* more updates

* update

* update

* Update Makefiles to handle both platforms

* Allow for overriding the CXX and CC executables

* Restore original structure while supporting custom CXX flags

* Remove some platform specific libs

* Dynamic target name

* Make X11 paths package-agnostic

* Remove changes to `gfx_opengl.cpp`

* Use OpenGL2 on MacOS instead of OpenGL3

* make it actually render something

* render at least the first texture, still need to figure out the second
one

* Let’s use OpenGL 3 again

* maybe this works to get the right texture? link's eyes still look off a bit

* did this work?

* set the platform to macos

* actual numbers are right, but logic is ugly XXX/TODO, i know

* add zlib to ldflags for ZAPDUtils

* A bit of cleanup

* Revert unneeded changes

* Remove GL_CHECK

* Fix issues with z64 branch

* use an std::map instead of a giant array

* three point filter fix (#2)

* Fix mac compilation

* fix audio for 64 bit

* revert audio heap size, keep bigger pools

* Add more Apple specific checks to our modifications

* Add building instructions for macOS

* Remove unecessary step from building instructions

* Add missing SDL2 & GLEW to Linux LDLIBS

* Update BUILDING.md

Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com>

* Update soh/.gitignore to include other arch binaries

Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com>

* Use right platform name for debugging window

Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com>

* Fix stormlib on macos (arm64)

* Simplify some of the ifdef checks

* Revert an older no longer necessary fix

* Remove remaining unecessary deviations

* Update building instructions after StormLib changes

* Feature: Use OpenGL 4.1 (#1)

* Further tweak the BUILDING

* Tidy up

* reword -j message

* Add Jenkins CI Support (#2)

* Fix type issues

* add target <appbundle> and <filledappbundle>

add makefile targets to create an .app
`filledappbundle` creates the target with the .otr included

this should perhaps be moved to Application Support though

* pull gcc's rpath from otool output

* move make target to the end so it's not default

* Add Jenkins and make exe in par with other platforms

* Actually save build artefacts

* Fix artefact path

* Remove x11 mentions and linking (not used)

* Update building instructions for generating app

* use appsupport directory

* Add new app icon

* Update target to match macOS types

* Update more audio types

* fix null deref in Audio_PlayFanfare

* Remove old import from z64

* address final nit with apple ifdefs

Co-authored-by: KiritoDev <36680385+KiritoDv@users.noreply.github.com>
Co-authored-by: Jeffrey Crowell <github@crowell.biz>
Co-authored-by: BountyChocolate123456 <101743444+BountyChocolate123456@users.noreply.github.com>
  • Loading branch information
4 people authored and Kenix3 committed Oct 18, 2022
1 parent cfbf469 commit 9576f51
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 32 deletions.
17 changes: 12 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Only used for standalone compilation, usually inherits these from the main makefile

CXX := g++
CC := gcc
CXX ?= g++
CC ?= gcc
AR := ar
FORMAT := clang-format-11
UNAME := $(shell uname)

ASAN ?= 0
DEBUG ?= 1
Expand All @@ -16,7 +17,8 @@ WARN := -Wall -Wextra -Werror \
-Wno-unused-function \
-Wno-parentheses \
-Wno-narrowing \
-Wno-missing-field-initializers
-Wno-missing-field-initializers \
-Wno-error=multichar

CWARN :=
CXXWARN := -Wno-deprecated-enum-enum-conversion
Expand All @@ -25,6 +27,10 @@ CXXFLAGS := $(WARN) $(CXXWARN) -std=c++20 -D_GNU_SOURCE -DENABLE_OPENGL -DSPDLOG
CFLAGS := $(WARN) $(CWARN) -std=c99 -D_GNU_SOURCE -DENABLE_OPENGL -DSPDLOG_ACTIVE_LEVEL=0
CPPFLAGS := -MMD

ifeq ($(UNAME), Darwin) #APPLE
CPPFLAGS += $(shell pkg-config --cflags sdl2 glew) -framework OpenGL
endif

ifneq ($(DEBUG),0)
CXXFLAGS += -g -D_DEBUG
CFLAGS += -g -D_DEBUG
Expand All @@ -40,7 +46,7 @@ ifneq ($(LTO),0)
CFLAGS += -flto
endif

SRC_DIRS := $(shell find -type d -not -path "*build*")
SRC_DIRS := $(shell find . -type d -not -path "*build*")

CXX_FILES := \
$(shell find libultraship/Factories -name "*.cpp") \
Expand Down Expand Up @@ -71,6 +77,7 @@ INC_DIRS := $(addprefix -I, \
libultraship/Lib/Fast3D/U64 \
libultraship/Lib/spdlog \
libultraship/Lib/spdlog/include \
libultraship/Lib/ImGui \
libultraship \
../StormLib/src \
)
Expand All @@ -97,4 +104,4 @@ build/%.o: %.c
$(LIB): $(O_FILES)
$(AR) rcs $@ $^

-include $(D_FILES)
-include $(D_FILES)
36 changes: 18 additions & 18 deletions libultraship/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ namespace Ship

ResourceFile::ParseFileBinary(reader, res);

int seqDataSize = reader->ReadInt32();
uint32_t seqDataSize = reader->ReadInt32();

seq->seqData.reserve(seqDataSize);

for (int i = 0; i < seqDataSize; i++)
for (uint32_t i = 0; i < seqDataSize; i++)
seq->seqData.push_back(reader->ReadUByte());

seq->seqNumber = reader->ReadUByte();
seq->medium = reader->ReadUByte();
seq->cachePolicy = reader->ReadUByte();

int numFonts = reader->ReadInt32();
uint32_t numFonts = reader->ReadInt32();

for (int i = 0; i < numFonts; i++)
for (uint32_t i = 0; i < numFonts; i++)
seq->fonts.push_back(reader->ReadUByte());
}

Expand All @@ -36,26 +36,26 @@ namespace Ship
entry->unk_bit26 = reader->ReadByte();
entry->unk_bit25 = reader->ReadByte();

int dataSize = reader->ReadInt32();
uint32_t dataSize = reader->ReadInt32();

for (int i = 0; i < dataSize; i++)
for (uint32_t i = 0; i < dataSize; i++)
entry->data.push_back(reader->ReadUByte());

entry->loop.start = reader->ReadUInt32();
entry->loop.end = reader->ReadUInt32();
entry->loop.count = reader->ReadUInt32();

int loopStateCnt = reader->ReadUInt32();
uint32_t loopStateCnt = reader->ReadUInt32();

for (int i = 0; i < loopStateCnt; i++)
for (uint32_t i = 0; i < loopStateCnt; i++)
entry->loop.states.push_back(reader->ReadInt16());

entry->book.order = reader->ReadInt32();
entry->book.npredictors = reader->ReadInt32();

int bookSize = reader->ReadInt32();
uint32_t bookSize = reader->ReadInt32();

for (int i = 0; i < bookSize; i++)
for (uint32_t i = 0; i < bookSize; i++)
entry->book.books.push_back(reader->ReadInt16());
}

Expand All @@ -72,11 +72,11 @@ namespace Ship
soundFont->data2 = reader->ReadInt16();
soundFont->data3 = reader->ReadInt16();

int drumCnt = reader->ReadInt32();
int instrumentCnt = reader->ReadInt32();
int sfxCnt = reader->ReadInt32();
uint32_t drumCnt = reader->ReadInt32();
uint32_t instrumentCnt = reader->ReadInt32();
uint32_t sfxCnt = reader->ReadInt32();

for (int i = 0; i < drumCnt; i++)
for (uint32_t i = 0; i < drumCnt; i++)
{
DrumEntry drum;
drum.releaseRate = reader->ReadUByte();
Expand All @@ -92,7 +92,7 @@ namespace Ship
soundFont->drums.push_back(drum);
}

for (int i = 0; i < instrumentCnt; i++)
for (uint32_t i = 0; i < instrumentCnt; i++)
{
InstrumentEntry entry;

Expand Down Expand Up @@ -143,7 +143,7 @@ namespace Ship
soundFont->instruments.push_back(entry);
}

for (int i = 0; i < sfxCnt; i++)
for (uint32_t i = 0; i < sfxCnt; i++)
{
SoundFontEntry* entry = new SoundFontEntry();

Expand All @@ -164,9 +164,9 @@ namespace Ship
{
std::vector<AdsrEnvelope*> envelopes;

int envelopeCnt = reader->ReadInt32();
uint32_t envelopeCnt = reader->ReadInt32();

for (int i = 0; i < envelopeCnt; i++)
for (uint32_t i = 0; i < envelopeCnt; i++)
{
AdsrEnvelope* env = new AdsrEnvelope();
env->delay = reader->ReadInt16();
Expand Down
4 changes: 2 additions & 2 deletions libultraship/Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace Ship

struct AdpcmBook
{
/* 0x00 */ int32_t order;
/* 0x04 */ int32_t npredictors;
/* 0x00 */ uint32_t order;
/* 0x04 */ uint32_t npredictors;
/* 0x08 */ std::vector<int16_t> books; // size 8 * order * npredictors. 8-byte aligned
};

Expand Down
88 changes: 86 additions & 2 deletions libultraship/Lib/Fast3D/gfx_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include "SDL.h"
#define GL_GLEXT_PROTOTYPES 1
#include "SDL_opengl.h"
#elif __APPLE__
#include <SDL.h>
#include <GL/glew.h>
#else
#include <SDL2/SDL.h>
#include <GL/glew.h>
Expand Down Expand Up @@ -67,6 +70,9 @@ struct Framebuffer {

static map<pair<uint64_t, uint32_t>, struct ShaderProgram> shader_program_pool;
static GLuint opengl_vbo;
#ifdef __APPLE__
static GLuint opengl_vao;
#endif
static bool current_depth_mask;

static uint32_t frame_count;
Expand Down Expand Up @@ -220,37 +226,67 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad
size_t num_floats = 4;

// Vertex shader
#ifdef __APPLE__
append_line(vs_buf, &vs_len, "#version 410 core");
append_line(vs_buf, &vs_len, "in vec4 aVtxPos;");
#else
append_line(vs_buf, &vs_len, "#version 110");
append_line(vs_buf, &vs_len, "attribute vec4 aVtxPos;");
#endif
for (int i = 0; i < 2; i++) {
if (cc_features.used_textures[i]) {
#ifdef __APPLE__
vs_len += sprintf(vs_buf + vs_len, "in vec2 aTexCoord%d;\n", i);
vs_len += sprintf(vs_buf + vs_len, "out vec2 vTexCoord%d;\n", i);
#else
vs_len += sprintf(vs_buf + vs_len, "attribute vec2 aTexCoord%d;\n", i);
vs_len += sprintf(vs_buf + vs_len, "varying vec2 vTexCoord%d;\n", i);
#endif
num_floats += 2;
for (int j = 0; j < 2; j++) {
if (cc_features.clamp[i][j]) {
#ifdef __APPLE__
vs_len += sprintf(vs_buf + vs_len, "in float aTexClamp%s%d;\n", j == 0 ? "S" : "T", i);
vs_len += sprintf(vs_buf + vs_len, "out float vTexClamp%s%d;\n", j == 0 ? "S" : "T", i);
#else
vs_len += sprintf(vs_buf + vs_len, "attribute float aTexClamp%s%d;\n", j == 0 ? "S" : "T", i);
vs_len += sprintf(vs_buf + vs_len, "varying float vTexClamp%s%d;\n", j == 0 ? "S" : "T", i);
#endif
num_floats += 1;
}
}
}
}
if (cc_features.opt_fog) {
#ifdef __APPLE__
append_line(vs_buf, &vs_len, "in vec4 aFog;");
append_line(vs_buf, &vs_len, "out vec4 vFog;");
#else
append_line(vs_buf, &vs_len, "attribute vec4 aFog;");
append_line(vs_buf, &vs_len, "varying vec4 vFog;");
#endif
num_floats += 4;
}

if (cc_features.opt_grayscale) {
#ifdef __APPLE__
append_line(vs_buf, &vs_len, "in vec4 aGrayscaleColor;");
append_line(vs_buf, &vs_len, "out vec4 vGrayscaleColor;");
#else
append_line(vs_buf, &vs_len, "attribute vec4 aGrayscaleColor;");
append_line(vs_buf, &vs_len, "varying vec4 vGrayscaleColor;");
#endif
num_floats += 4;
}

for (int i = 0; i < cc_features.num_inputs; i++) {
#ifdef __APPLE__
vs_len += sprintf(vs_buf + vs_len, "in vec%d aInput%d;\n", cc_features.opt_alpha ? 4 : 3, i + 1);
vs_len += sprintf(vs_buf + vs_len, "out vec%d vInput%d;\n", cc_features.opt_alpha ? 4 : 3, i + 1);
#else
vs_len += sprintf(vs_buf + vs_len, "attribute vec%d aInput%d;\n", cc_features.opt_alpha ? 4 : 3, i + 1);
vs_len += sprintf(vs_buf + vs_len, "varying vec%d vInput%d;\n", cc_features.opt_alpha ? 4 : 3, i + 1);
#endif
num_floats += cc_features.opt_alpha ? 4 : 3;
}
append_line(vs_buf, &vs_len, "void main() {");
Expand All @@ -277,26 +313,50 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad
append_line(vs_buf, &vs_len, "}");

// Fragment shader
append_line(fs_buf, &fs_len, "#version 130");
#ifdef __APPLE__
append_line(fs_buf, &fs_len, "#version 410 core");
#else
append_line(fs_buf, &fs_len, "#version 120");
#endif
//append_line(fs_buf, &fs_len, "precision mediump float;");
for (int i = 0; i < 2; i++) {
if (cc_features.used_textures[i]) {
#ifdef __APPLE__
fs_len += sprintf(fs_buf + fs_len, "in vec2 vTexCoord%d;\n", i);
#else
fs_len += sprintf(fs_buf + fs_len, "varying vec2 vTexCoord%d;\n", i);
#endif
for (int j = 0; j < 2; j++) {
if (cc_features.clamp[i][j]) {
#ifdef __APPLE__
fs_len += sprintf(fs_buf + fs_len, "in float vTexClamp%s%d;\n", j == 0 ? "S" : "T", i);
#else
fs_len += sprintf(fs_buf + fs_len, "varying float vTexClamp%s%d;\n", j == 0 ? "S" : "T", i);
#endif
}
}
}
}
if (cc_features.opt_fog) {
#ifdef __APPLE__
append_line(fs_buf, &fs_len, "in vec4 vFog;");
#else
append_line(fs_buf, &fs_len, "varying vec4 vFog;");
#endif
}
if (cc_features.opt_grayscale) {
#ifdef __APPLE__
append_line(fs_buf, &fs_len, "in vec4 vGrayscaleColor;");
#else
append_line(fs_buf, &fs_len, "varying vec4 vGrayscaleColor;");
#endif
}
for (int i = 0; i < cc_features.num_inputs; i++) {
#ifdef __APPLE__
fs_len += sprintf(fs_buf + fs_len, "in vec%d vInput%d;\n", cc_features.opt_alpha ? 4 : 3, i + 1);
#else
fs_len += sprintf(fs_buf + fs_len, "varying vec%d vInput%d;\n", cc_features.opt_alpha ? 4 : 3, i + 1);
#endif
}
if (cc_features.used_textures[0]) {
append_line(fs_buf, &fs_len, "uniform sampler2D uTex0;");
Expand All @@ -316,7 +376,11 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad
}

if (current_filter_mode == THREE_POINT) {
#if __APPLE__
append_line(fs_buf, &fs_len, "#define TEX_OFFSET(off) texture(tex, texCoord - (off)/texSize)");
#else
append_line(fs_buf, &fs_len, "#define TEX_OFFSET(off) texture2D(tex, texCoord - (off)/texSize)");
#endif
append_line(fs_buf, &fs_len, "vec4 filter3point(in sampler2D tex, in vec2 texCoord, in vec2 texSize) {");
append_line(fs_buf, &fs_len, " vec2 offset = fract(texCoord*texSize - vec2(0.5));");
append_line(fs_buf, &fs_len, " offset -= step(1.0, offset.x + offset.y);");
Expand All @@ -330,10 +394,18 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad
append_line(fs_buf, &fs_len, "}");
} else {
append_line(fs_buf, &fs_len, "vec4 hookTexture2D(in sampler2D tex, in vec2 uv, in vec2 texSize) {");
#if __APPLE__
append_line(fs_buf, &fs_len, " return texture(tex, uv);");
#else
append_line(fs_buf, &fs_len, " return texture2D(tex, uv);");
#endif
append_line(fs_buf, &fs_len, "}");
}

#if __APPLE__
append_line(fs_buf, &fs_len, "out vec4 outColor;");
#endif

append_line(fs_buf, &fs_len, "void main() {");

for (int i = 0; i < 2; i++) {
Expand Down Expand Up @@ -405,9 +477,17 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad
if (cc_features.opt_invisible) {
append_line(fs_buf, &fs_len, "texel.a = 0.0;");
}
#if __APPLE__
append_line(fs_buf, &fs_len, "outColor = texel;");
#else
append_line(fs_buf, &fs_len, "gl_FragColor = texel;");
#endif
} else {
#if __APPLE__
append_line(fs_buf, &fs_len, "outColor = vec4(texel, 1.0);");
#else
append_line(fs_buf, &fs_len, "gl_FragColor = vec4(texel, 1.0);");
#endif
}
append_line(fs_buf, &fs_len, "}");

Expand Down Expand Up @@ -557,7 +637,6 @@ static void gfx_opengl_select_texture(int tile, GLuint texture_id) {
glActiveTexture(GL_TEXTURE0 + tile);
glBindTexture(GL_TEXTURE_2D, texture_id);
}

static void gfx_opengl_upload_texture(const uint8_t *rgba32_buf, uint32_t width, uint32_t height) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba32_buf);
}
Expand Down Expand Up @@ -636,6 +715,11 @@ static void gfx_opengl_init(void) {
glGenBuffers(1, &opengl_vbo);
glBindBuffer(GL_ARRAY_BUFFER, opengl_vbo);

#ifdef __APPLE__
glGenVertexArrays(1, &opengl_vao);
glBindVertexArray(opengl_vao);
#endif

glEnable(GL_DEPTH_CLAMP);
glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Expand Down
1 change: 1 addition & 0 deletions libultraship/Lib/Fast3D/gfx_pc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GFX_PC_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <unordered_map>
#include <list>
Expand Down
Loading

0 comments on commit 9576f51

Please sign in to comment.