Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macOS support #70

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions .github/workflows/build-cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ on:
workflow_dispatch:

jobs:
build-with-cmake:
ubuntu:
name: "Ubuntu (${{ matrix.compiler.cc }})"
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
compiler:
- [cc: gcc, cxx: g++]
- [cc: clang, cxx: clang++]
- {cc: gcc, cxx: g++}
- {cc: clang, cxx: clang++}

steps:
- uses: actions/checkout@v2
Expand All @@ -37,7 +39,11 @@ jobs:
run: sudo apt update

- name: Install dependencies
run: sudo apt install --yes clang cmake libglew-dev libglfw3-dev libglm-dev libopenal-dev xorg-dev
run: sudo apt install --yes cmake libglew-dev libglfw3-dev libglm-dev libopenal-dev xorg-dev

- name: Install clang
run: sudo apt install --yes clang
if: ${{ matrix.compiler.cc == 'clang' }}

- name: cmake
uses: ashutoshvarma/action-cmake-build@master
Expand All @@ -48,3 +54,36 @@ jobs:
build-type: Release
configure-options: -DWITH_BOX2D=Yes
parallel: 2

macos:
name: "macOS (${{ matrix.compiler.cc }}, box2d: ${{ matrix.with_box2d }})"
runs-on: macos-latest

strategy:
fail-fast: false
matrix:
with_box2d: [yes, no]
compiler:
- {cc: clang, cxx: clang++}

steps:
- uses: actions/checkout@v2
with:
submodules: true

- name: Install dependencies
run: brew install glew glfw3 glm

- name: Install Box2D
run: brew install box2d
if: ${{ matrix.with_box2d == 'no' }}

- name: cmake
uses: ashutoshvarma/action-cmake-build@master
with:
build-dir: ${{ github.workspace }}/build
cc: ${{ matrix.compiler.cc }}
cxx: ${{ matrix.compiler.cxx }}
build-type: Release
configure-options: -DWITH_BOX2D=${{ matrix.with_box2d }}
parallel: 2
1 change: 1 addition & 0 deletions cmake/Carnage3D.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ endif()
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(glfw3 REQUIRED)
find_package(glm REQUIRED)

if(NOT(APPLE))
target_link_libraries(carnage3d stdc++fs)
Expand Down
64 changes: 32 additions & 32 deletions src/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ CvarString gCvarGtaDataPath("g_gtadata", "", "GTA data location", CvarFlags_Arch

FileSystem gFiles;

bool FileSystem::Initialize()
{
mExecutablePath = cxx::get_executable_path();
mExecutableDirectory = cxx::get_parent_directory(mExecutablePath);
mWorkingDirectoryPath = mExecutableDirectory;
mWorkingDirectoryPath = cxx::get_parent_directory(mWorkingDirectoryPath); // root
if (!mWorkingDirectoryPath.empty())
{
mWorkingDirectoryPath.append("/");
}
mWorkingDirectoryPath.append("gamedata");
AddSearchPlace(mWorkingDirectoryPath);

gConsole.LogMessage(eLogMessage_Info, "Working directory: '%s'", mWorkingDirectoryPath.c_str());
return true;
}

void FileSystem::Deinit()
{
mExecutablePath.clear();
mExecutableDirectory.clear();
mWorkingDirectoryPath.clear();
mGameMapsList.clear();
}

bool FileSystem::Initialize()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line ending changes 馃檮

{
mExecutablePath = cxx::get_executable_path();
mExecutableDirectory = cxx::get_parent_directory(mExecutablePath);
mWorkingDirectoryPath = mExecutableDirectory;
mWorkingDirectoryPath = cxx::get_parent_directory(mWorkingDirectoryPath); // root
if (!mWorkingDirectoryPath.empty())
{
mWorkingDirectoryPath.append("/");
}
mWorkingDirectoryPath.append("gamedata");
AddSearchPlace(mWorkingDirectoryPath);
gConsole.LogMessage(eLogMessage_Info, "Working directory: '%s'", mWorkingDirectoryPath.c_str());
return true;
}
void FileSystem::Deinit()
{
mExecutablePath.clear();
mExecutableDirectory.clear();
mWorkingDirectoryPath.clear();
mGameMapsList.clear();
}
bool FileSystem::OpenBinaryFile(const std::string& objectName, std::ifstream& instream)
{
instream.close();
Expand Down Expand Up @@ -160,11 +160,11 @@ bool FileSystem::ReadTextFile(const std::string& objectName, std::string& output
if (!OpenTextFile(objectName, fileStream))
return false;

std::string stringLine {};
while (std::getline(fileStream, stringLine, '\n'))
{
output.append(stringLine);
output.append("\n");
std::string stringLine {};
while (std::getline(fileStream, stringLine, '\n'))
{
output.append(stringLine);
output.append("\n");
}
return true;
}
Expand Down Expand Up @@ -294,7 +294,7 @@ bool FileSystem::ScanGtaMaps()
{
if (cxx::get_file_extension(curr) == GTA1MapFileExtension)
{
mGameMapsList.push_back(curr);
mGameMapsList.push_back(curr);
}
});
}
Expand Down Expand Up @@ -333,4 +333,4 @@ bool FileSystem::SaveConfig(const std::string& filePath, const cxx::json_documen

outputFile << documentContent;
return true;
}
}
21 changes: 13 additions & 8 deletions src/OpenALDefs.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#pragma once

#include <AL/al.h>
#include "AL/alc.h"

#pragma once

#if OS_NAME == OS_MACOS
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <AL/al.h>
#include <AL/alc.h>
#endif

// checks current openal error code
#ifdef _DEBUG
#define alCheckError()\
Expand All @@ -22,7 +27,7 @@
// resets current openal error code
inline void alClearError()
{
for (ALenum alErrorCode = ::alGetError(); alErrorCode != AL_NO_ERROR; alErrorCode = ::alGetError())
for (ALenum alErrorCode = ::alGetError(); alErrorCode != AL_NO_ERROR; alErrorCode = ::alGetError())
{
}
}
Expand All @@ -34,6 +39,6 @@ struct AudioDeviceCaps
AudioDeviceCaps() = default;

public:
int mMaxSourcesMono = 0;
int mMaxSourcesMono = 0;
int mMaxSourcesStereo = 0;
};
};
47 changes: 26 additions & 21 deletions src/macro.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#pragma once

#define OS_WINDOWS 1
#define OS_LINUX 2
#define OS_UNIX 3
#define OS_UNKNOWN 0
#define OS_WINDOWS 1
#define OS_LINUX 2
#define OS_UNIX 3
#define OS_MACOS 4
#define OS_UNKNOWN 0

#if defined(_WIN32)
#define OS_NAME OS_WINDOWS
#elif defined(__APPLE__)
#define OS_NAME OS_MACOS
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
#define OS_NAME OS_LINUX
#else
Expand All @@ -17,28 +20,30 @@
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS

#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif

#elif OS_NAME == OS_LINUX
#include <assert.h>
#elif OS_NAME == OS_MACOS
#include <mach-o/dyld.h>
#endif

#ifdef _DEBUG
#if OS_NAME == OS_WINDOWS
#define debug_assert(expr) _ASSERTE(expr)
#define release_assert(expr)
#else
#define debug_assert(expr) assert(expr)
#define release_assert(expr)
#endif
#else
#define debug_assert(expr)
#define release_assert(expr)
#endif

#ifdef _DEBUG
#if OS_NAME == OS_WINDOWS
#define debug_assert(expr) _ASSERTE(expr)
#define release_assert(expr)
#else
#define debug_assert(expr) assert(expr)
#define release_assert(expr)
#endif
#else
#define debug_assert(expr)
#define release_assert(expr)
#endif

#if OS_NAME == OS_WINDOWS
#define cxx_stricmp _stricmp
#define cxx_strnicmp _strnicmp
Expand All @@ -48,4 +53,4 @@
#define cxx_strnicmp strncasecmp
#endif

#define BIT(i) (1U << (i))
#define BIT(i) (1U << (i))
43 changes: 27 additions & 16 deletions src/path_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "stdafx.h"
#include "path_utils.h"

#ifdef __EMSCRIPTEN__
#if OS_NAME == OS_MACOS
namespace filesystem = std::filesystem;
#elif defined(__EMSCRIPTEN__)
#include <filesystem>
namespace filesystem = std::__fs::filesystem;
#else
#include <experimental/filesystem>
namespace filesystem = std::experimental::filesystem;
#endif // #ifndef __EMSCRIPTEN__
namespace filesystem = std::experimental::filesystem;
#endif // #ifndef __EMSCRIPTEN__

namespace cxx
{
Expand Down Expand Up @@ -39,13 +41,22 @@ std::string get_file_extension(std::string pathto)
std::string get_executable_path()
{
#if (OS_NAME == OS_WINDOWS)
char buffer[MAX_PATH + 1];
int count = ::GetModuleFileNameA(NULL, buffer, MAX_PATH);
char buffer[MAX_PATH + 1];
int count = ::GetModuleFileNameA(NULL, buffer, MAX_PATH);
return std::string(buffer, (count > 0) ? count : 0);
#elif (OS_NAME == OS_LINUX)
char buffer[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", buffer, PATH_MAX);
return std::string(buffer, (count > 0) ? count : 0);
#elif (OS_NAME == OS_MACOS)
char buffer[PATH_MAX];
uint32_t count = sizeof(buffer);
if (_NSGetExecutablePath(buffer, &count) == 0) {
char resolved[PATH_MAX];
realpath(buffer, resolved);

return std::string(resolved, count);
}
#else
debug_assert(false);
return std::string();
Expand All @@ -72,12 +83,12 @@ bool ensure_path_exists(std::string pathto)

void enum_files(std::string pathto, enum_files_proc enumproc)
{
filesystem::path sourcePath {pathto};
if (!filesystem::exists(sourcePath))
return;

filesystem::path sourcePath {pathto};
if (!filesystem::exists(sourcePath))
return;
filesystem::directory_iterator iter_directory_end;
for (filesystem::directory_iterator iter_directory(sourcePath);
for (filesystem::directory_iterator iter_directory(sourcePath);
iter_directory != iter_directory_end; ++iter_directory)
{
const filesystem::path& currentFile = iter_directory->path();
Expand All @@ -87,17 +98,17 @@ void enum_files(std::string pathto, enum_files_proc enumproc)

void enum_files_recursive(std::string pathto, enum_files_proc enumproc)
{
filesystem::path sourcePath {pathto};
if (!filesystem::exists(sourcePath))
return;

filesystem::path sourcePath {pathto};
if (!filesystem::exists(sourcePath))
return;
filesystem::recursive_directory_iterator iter_directory_end;
for (filesystem::recursive_directory_iterator iter_directory(sourcePath);
for (filesystem::recursive_directory_iterator iter_directory(sourcePath);
iter_directory != iter_directory_end; ++iter_directory)
{
const filesystem::path& currentFile = iter_directory->path();
enumproc(currentFile.filename().generic_string());
}
}

} // namespace cxx
} // namespace cxx
9 changes: 6 additions & 3 deletions src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#define NOMINMAX
#include <windows.h>
#include <mmsystem.h> // for multimedia timers
#elif OS_NAME == OS_LINUX
#elif OS_NAME == OS_LINUX || OS_NAME == OS_MACOS
#include <limits.h>
#include <unistd.h>
#endif
Expand Down Expand Up @@ -52,8 +52,11 @@

// opengl
#include <GL/glew.h>
#if OS_NAME == OS_MACOS
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>

#endif
#include "GLFW/glfw3.h"

// glm
Expand Down Expand Up @@ -102,4 +105,4 @@
#include "GuiManager.h"
#include "PixelsArray.h"
#include "GameDefs.h"
#include "Convert.h"
#include "Convert.h"