Skip to content

Commit

Permalink
Fix macos build (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettjoecox authored Nov 22, 2024
1 parent 56ff59f commit 040ed21
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 29 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
file (GLOB future__imgui_backend
extern/imgui/backends/imgui_impl_dx11.cpp
extern/imgui/backends/imgui_impl_win32.cpp)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
elseif ((CMAKE_SYSTEM_NAME STREQUAL "Linux") OR (CMAKE_SYSTEM_NAME STREQUAL "Darwin"))
file (GLOB future__imgui_backend
extern/imgui/backends/imgui_impl_sdl2.cpp
extern/imgui/backends/imgui_impl_opengl3.cpp)
Expand Down Expand Up @@ -126,7 +126,7 @@ target_link_libraries(future PRIVATE libzip::zip )

file (COPY assets/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/assets)

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
if ((CMAKE_SYSTEM_NAME STREQUAL "Linux") OR (CMAKE_SYSTEM_NAME STREQUAL "Darwin"))

find_package(SDL2 REQUIRED)
target_link_libraries(future PRIVATE SDL2::SDL2)
Expand Down
16 changes: 8 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <d3d11.h>
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "Dwmapi.lib")
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include "imgui_impl_sdl2.h"
Expand Down Expand Up @@ -38,7 +38,7 @@ static UINT g_ResizeWidth = 0, g_ResizeHeight = 0;
static ID3D11RenderTargetView* g_mainRenderTargetView = nullptr;
static WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"Future", nullptr };
HWND gHwnd;
#elif __linux__
#elif defined(__linux__) || defined(__APPLE__)
static SDL_Window* window;
static SDL_GLContext gl_context;
#endif
Expand Down Expand Up @@ -66,7 +66,7 @@ int main(int, char**)

#if defined (_WIN32)
io.Fonts->AddFontFromFileTTF("assets/Roboto-Medium.ttf", 32.0f);
#elif defined (__linux__)
#elif defined(__linux__) || defined(__APPLE__)
// Get the path of the mounted app image and build the font path from there. Is this better than just embedding the font into the binary?
char* appDir = getenv("APPDIR");

Expand Down Expand Up @@ -112,7 +112,7 @@ int main(int, char**)
// Helper functions

static int InitState() {
#if defined(__linux__)
#if defined(__linux__) || defined(__APPLE__)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
printf("Error: %s\n", SDL_GetError());
return -1;
Expand Down Expand Up @@ -205,7 +205,7 @@ static void StartFrame() {
#if defined(_WIN32)
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
#endif
Expand All @@ -223,7 +223,7 @@ static bool HandleEvents() {
rv = true;
}
return rv;
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
SDL_Event event;
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
Expand All @@ -248,7 +248,7 @@ static void Render(const ImVec4& clear_color) {
HRESULT hr = g_pSwapChain->Present(1, 0); // Present with vsync
//HRESULT hr = g_pSwapChain->Present(0, 0); // Present without vsync
g_SwapChainOccluded = (hr == DXGI_STATUS_OCCLUDED);
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
ImGui::Render();
ImGuiIO& io = ImGui::GetIO(); (void)io;
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
Expand All @@ -269,7 +269,7 @@ static void Shutdown() {
CleanupDeviceD3D();
::DestroyWindow(gHwnd);
::UnregisterClassW(wc.lpszClassName, wc.hInstance);
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
Expand Down
16 changes: 8 additions & 8 deletions utils/filebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <shobjidl_core.h>
#include <filesystem>
extern HWND gHwnd;
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
#include "portable-file-dialogs.h"
#include "SDL2/SDL.h"
#include <fcntl.h>
Expand Down Expand Up @@ -52,7 +52,7 @@ bool GetOpenDirPath(char** inputBuffer) {
result->Release();
}
pfd->Release();
#elif defined (__linux__)
#elif defined(__linux__) || defined(__APPLE__)
auto selection = pfd::select_folder("Open Directory").result();

if (selection.empty()) {
Expand Down Expand Up @@ -113,7 +113,7 @@ bool GetOpenFilePath(char** inputBuffer, FileBoxType type) {
result->Release();
}
pfd->Release();
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
//std::vector<std::string> filters;
auto selection = pfd::open_file("Select a file", ".", { "All Supported Archives", "*.otr *.mpq *.o2r *.zip", "OTR Archives", "*.otr *.mpq", "O2R Archives", "*.o2r *.zip",}).result();

Expand Down Expand Up @@ -159,7 +159,7 @@ bool GetSaveFilePath(char** inputBuffer) {
result->Release();
}
pfd->Release();
#elif (__linux__)
#elif defined(__linux__) || defined(__APPLE__)
auto selection = pfd::save_file("Save File").result();
size_t len = selection.length();
if (*inputBuffer != nullptr){
Expand Down Expand Up @@ -214,7 +214,7 @@ size_t GetDiskFileSize(char* path) {
// The std::fs function is faster on Windows but not linux.
#if defined (_WIN32)
return std::filesystem::file_size(path);
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
struct stat st;
stat(path, &st);
return (size_t)st.st_size;
Expand All @@ -226,7 +226,7 @@ size_t GetDiskFileSize(char* path) {
int CopyFileData(char* src, char* dest) {
#if defined (_WIN32)
CopyFileA(src, dest, false);
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
int srcFd = open(src, O_RDONLY);
struct stat st;
fstat(srcFd, &st);
Expand All @@ -244,7 +244,7 @@ int CopyFileData(char* src, char* dest) {
int CreateDir(const char* dir) {
#if defined (_WIN32)
CreateDirectoryA(dir, nullptr);
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
mkdir(dir, 0777);
#endif
return 0;
Expand All @@ -253,7 +253,7 @@ int CreateDir(const char* dir) {
void UnmapFile(void* data, [[maybe_unused]] size_t size) {
#if defined (_WIN32)
UnmapViewOfFile(data);
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
munmap(data, size);
#endif
}
5 changes: 3 additions & 2 deletions utils/filebox.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#include <cstddef>
#include <stdio.h>
#include <cstring>
#include <filesystem>

#if defined (_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
constexpr const char PATH_SEPARATOR = '\\';
#elif defined (__linux__)
#elif defined(__linux__) || defined(__APPLE__)
constexpr const char PATH_SEPARATOR = '/';
#include <fcntl.h>
#include <unistd.h>
Expand Down Expand Up @@ -75,7 +76,7 @@ static void FillFileQueue(T& dest, char* mBasePath, ExtCheckCallback cb) {
} while (FindNextFileA(h, &ffd) != 0);
FindClose(h);
SetCurrentDirectoryA(oldWorkingDir);
#elif unix
#elif defined(__linux__) || defined(__APPLE__)
// Change the current working directory to the path selected.
char oldWorkingDir[PATH_MAX];
getcwd(oldWorkingDir, PATH_MAX);
Expand Down
13 changes: 9 additions & 4 deletions utils/images.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#if defined (_WIN32)
#include <D3d11.h>
#elif defined(__linux__)
#include <GL/gl.h>
#else
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
#if defined(__linux__)
#include <GL/gl.h>
#elif defined(__APPLE__)
#include <GL/glew.h>
#endif
#endif
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <unordered_map>
Expand Down Expand Up @@ -50,7 +55,7 @@ static inline ID3D11ShaderResourceView* LoadTextureDX11(void* data, int width, i
pTexture->Release();
return view;
}
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
static inline GLuint LoadTextureGL(void* data, int width, int height) {
GLuint image_texture;
glGenTextures(1, &image_texture);
Expand Down Expand Up @@ -100,7 +105,7 @@ void* LoadTextureByName(const char* path, int* width, int* height) {
CloseHandle(mappingObj);
CloseHandle(hFile);

#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
int fd;
fd = open(path, O_RDONLY);
if (fd < 0) {
Expand Down
3 changes: 2 additions & 1 deletion utils/mpq_archive.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "mpq_archive.h"
#include "filebox.h"
#include <cstring>
#include <filesystem>

#if defined (__linux__)
#if defined(__linux__) || defined(__APPLE__)
// Stormlib uses the windows error codes which linux doesn't have
#define ERROR_FILE_EXISTS 0x80
// MSVC throws an error saying strdup is deprecated. Linux doesn't have _strdup.
Expand Down
2 changes: 1 addition & 1 deletion utils/zip_archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void ZipArchive::RegisterProgressCallback(zip_progress_callback cb, void* callin

#if defined (_WIN32)
#define CREATE_MAPPED_INFO(data, size) {data}
#elif defined (__linux__)
#elif defined(__linux__) || defined(__APPLE__)
#define CREATE_MAPPED_INFO(data, size) {data, size}
#endif

Expand Down
2 changes: 1 addition & 1 deletion windows/CreateFromDir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static void FillFileQueueImpl(std::vector<char*>& files, char* startPath, int ne
}
} while (FindNextFileA(h, &ffd) != 0);
FindClose(h);
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
//I give up...
size_t basePathLen = strlen(startPath);
char buffer[PATH_MAX];
Expand Down
2 changes: 2 additions & 0 deletions windows/CustomStreamedAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <array>
#include <atomic>
#include <cmath>
#include <thread>
#include <filesystem>

#include "mio.hpp"
#undef max
Expand Down
4 changes: 2 additions & 2 deletions windows/ExploreArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#if defined (_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#elif defined (__linux__)
#elif defined(__linux__) || defined(__APPLE__)
#include <fcntl.h>
#include <unistd.h>
#endif
Expand Down Expand Up @@ -160,7 +160,7 @@ bool ExploreWindow::ValidateInputFile() {
}

CloseHandle(inFile);
#elif defined(__linux__)
#elif defined(__linux__) || defined(__APPLE__)
int fd;
fd = open(mPathBuff, O_RDONLY);
if (read(fd, &header, 4) != 4) {
Expand Down

0 comments on commit 040ed21

Please sign in to comment.