From 6043dca6b8c05351f19f8fe4a06570f41b686879 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Mon, 12 Apr 2021 16:56:08 +1000
Subject: Fix for Haiku


diff --git a/src/file.c b/src/file.c
index 2be6f6f..d65d508 100644
--- a/src/file.c
+++ b/src/file.c
@@ -55,6 +55,34 @@ FILE *openWritableFile(const char *pathname, const char *mode)
 
     return fopen(finalPathname, mode);
 }
+#elif defined(__HAIKU__)
+#include <sys/stat.h>
+
+#define HAIKU_DATA_PATH "/system/apps/OpenSupaplex/data/"
+#define HAIKU_WRITABLE_PATH "/boot/home/config/settings/OpenSupaplex/"
+
+void getReadonlyFilePath(const char *pathname, char outPath[kMaxFilePathLength])
+{
+    snprintf(outPath, kMaxFilePathLength, HAIKU_DATA_PATH "%s", pathname);
+}
+void getWritableFilePath(const char *pathname, char outPath[kMaxFilePathLength])
+{
+    snprintf(outPath, kMaxFilePathLength, HAIKU_WRITABLE_PATH "%s", pathname);
+}
+FILE *openReadonlyFile(const char *pathname, const char *mode)
+{
+    char finalPathname[kMaxFilePathLength];
+    getReadonlyFilePath(pathname, finalPathname);
+    return fopen(finalPathname, mode);
+}
+
+FILE *openWritableFile(const char *pathname, const char *mode)
+{
+    mkdir(HAIKU_WRITABLE_PATH, ALLPERMS);
+    char finalPathname[kMaxFilePathLength];
+    getWritableFilePath(pathname, finalPathname);
+    return fopen(finalPathname, mode);
+}
 #elif defined(FILE_FHS_XDG_DIRS)
 
 #if defined(FILE_DATA_PATH)
diff --git a/src/sdl2/video.c b/src/sdl2/video.c
index 694c4af..161d682 100644
--- a/src/sdl2/video.c
+++ b/src/sdl2/video.c
@@ -20,6 +20,9 @@
 #include <math.h>
 #include <SDL2/SDL.h>
 #include <stdlib.h>
+#ifdef __HAIKU__
+#include <unistd.h>
+#endif
 
 #include "../logging.h"
 #include "../utils.h"
@@ -33,6 +36,9 @@ static const int kWindowHeight = 544;
 #elif defined(__PSL1GHT__) || defined(__WIIU__)
 static const int kWindowWidth = 1280;
 static const int kWindowHeight = 720;
+#elif defined(__HAIKU__)
+static const int kWindowWidth = kScreenWidth * 3;
+static const int kWindowHeight = kScreenHeight * 3;
 #else
 static const int kWindowWidth = kScreenWidth * 4;
 static const int kWindowHeight = kScreenHeight * 4;
@@ -229,16 +235,19 @@ void getMouseState(int *x, int *y, uint8_t *leftButton, uint8_t *rightButton)
 void toggleFullscreen()
 {
     setFullscreenMode(getFullscreenMode() == 0);
+#ifdef __HAIKU__
+	usleep(250000);
+#endif
 }
 
 void setFullscreenMode(uint8_t fullscreen)
 {
-    SDL_SetWindowFullscreen(gWindow, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
+    SDL_SetWindowFullscreen(gWindow, fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
 }
 
 uint8_t getFullscreenMode(void)
 {
-    uint8_t isFullscreen = SDL_GetWindowFlags(gWindow) & SDL_WINDOW_FULLSCREEN;
+    uint8_t isFullscreen = SDL_GetWindowFlags(gWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP;
     return (isFullscreen != 0);
 }
 
@@ -258,8 +267,10 @@ int windowResizingEventWatcher(void* data, SDL_Event* event)
         && event->window.event == SDL_WINDOWEVENT_RESIZED)
     {
         updateWindowViewport();
+#ifndef __HAIKU__
         render();
         present();
+#endif
     }
 
     return 0;
diff --git a/src/sdl_common/audio.c b/src/sdl_common/audio.c
index 797c678..1de79b8 100644
--- a/src/sdl_common/audio.c
+++ b/src/sdl_common/audio.c
@@ -81,6 +81,8 @@ static const char *kBaseAudioFolder = "app0:/audio";
 static const char *kBaseAudioFolder = "sdmc:/OpenSupaplex/audio";
 #elif defined(__PSL1GHT__)
 static const char *kBaseAudioFolder = "/dev_hdd0/game/" PS3APPID "/USRDIR/audio";
+#elif defined(__HAIKU__)
+static const char *kBaseAudioFolder = "/system/apps/OpenSupaplex/audio";
 #elif defined(__WII__)
 static const char *kBaseAudioFolder = "/apps/OpenSupaplex/audio";
 #elif defined(__WIIU__)
-- 
2.30.2

