From 97a1a74bd98ea803db7a89fb5e64ccdae30139b0 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Wed, 30 Dec 2020 15:35:17 +1000
Subject: Fix build for haiku


diff --git a/src/celengine/astro.cpp b/src/celengine/astro.cpp
index d5ae1f2..b196025 100644
--- a/src/celengine/astro.cpp
+++ b/src/celengine/astro.cpp
@@ -525,7 +525,7 @@ const char* astro::Date::toCStr(Format format) const
     cal_time.tm_sec = (int)seconds;
     cal_time.tm_wday = wday;
     cal_time.tm_gmtoff = utc_offset;
-#if defined(TARGET_OS_MAC) || defined(__FreeBSD__)
+#if defined(TARGET_OS_MAC) || defined(__FreeBSD__) || defined(__HAIKU__)
     // tm_zone is a non-const string field on the Mac and FreeBSD (why?)
     cal_time.tm_zone = const_cast<char*>(tzname.c_str());
 #else
diff --git a/src/celengine/cmdparser.cpp b/src/celengine/cmdparser.cpp
index e2daf31..32fe5de 100644
--- a/src/celengine/cmdparser.cpp
+++ b/src/celengine/cmdparser.cpp
@@ -713,8 +713,8 @@ Command* CommandParser::parseCommand()
         cmd = new CommandRenderPath(glcpath);
     }
     else if (commandName == "splitview")
-    {
-        unsigned int view = 1;
+    {    	
+        double view = 1;
         paramList->getNumber("view", view);
         string splitType;
         paramList->getString("type", splitType);
@@ -724,7 +724,7 @@ Command* CommandParser::parseCommand()
     }
     else if (commandName == "deleteview")
     {
-        unsigned int view = 1;
+        double view = 1;
         paramList->getNumber("view", view);
         cmd = new CommandDeleteView(view);
     }
@@ -734,7 +734,7 @@ Command* CommandParser::parseCommand()
     }
     else if (commandName == "setactiveview")
     {
-        unsigned int view = 1;
+        double view = 1;
         paramList->getNumber("view", view);
         cmd = new CommandSetActiveView(view);
     }
diff --git a/src/celengine/dds.cpp b/src/celengine/dds.cpp
index a09fc76..9105474 100644
--- a/src/celengine/dds.cpp
+++ b/src/celengine/dds.cpp
@@ -198,7 +198,7 @@ Image* LoadDDSImage(const string& filename)
     Image* img = new Image(format,
                            (int) ddsd.width,
                            (int) ddsd.height,
-                           max(ddsd.mipMapLevels, 1u));
+                           max((uint32)ddsd.mipMapLevels, (uint32)1u));
     if (img == NULL)
         return NULL;
 
diff --git a/src/celengine/glext.cpp b/src/celengine/glext.cpp
index 4bde792..386c348 100644
--- a/src/celengine/glext.cpp
+++ b/src/celengine/glext.cpp
@@ -12,10 +12,10 @@
 
 #ifndef _WIN32
 // Assume that this is a UNIX/X11 system if it's not Windows or Mac OS X.
-#ifndef TARGET_OS_MAC
+#if !defined(TARGET_OS_MAC) && !defined(__HAIKU__)
 #include "GL/glx.h"
 #include <dlfcn.h>
-#endif /* ! TARGET_OS_MAC */
+#endif /* ! TARGET_OS_MAC || HAIKU */
 #endif /* ! _WIN32 */
 
 #include "glext.h"
@@ -306,7 +306,7 @@ extern QGLContext* glctx;
 
 #else
 
-#ifndef TARGET_OS_MAC
+#if !defined(TARGET_OS_MAC) && !defined(__HAIKU__)
 #if defined(_WIN32)
 
 #define GET_GL_PROC_ADDRESS(name) wglGetProcAddress(name)
@@ -672,9 +672,11 @@ static void InitExt_EXT_swap_control()
 
 static void InitExt_GLX_SGI_video_sync()
 {
+#if defined(GET_GL_PROC_ADDRESS)
     glx::glXGetVideoSyncSGI = (glx::PFNGLXGETVIDEOSYNCSGIPROC) GET_GL_PROC_ADDRESS("glXGetVideoSyncSGI");
     glx::glXWaitVideoSyncSGI = (glx::PFNGLXWAITVIDEOSYNCSGIPROC) GET_GL_PROC_ADDRESS("glXWaitVideoSyncSGI");
     glx::glXGetRefreshRateSGI = (glx::PFNGLXGETREFRESHRATESGIPROC) GET_GL_PROC_ADDRESS("glXGetRefreshRateSGI");
+#endif
 }
 
 
diff --git a/src/celengine/image.cpp b/src/celengine/image.cpp
index 2058c94..51bee03 100644
--- a/src/celengine/image.cpp
+++ b/src/celengine/image.cpp
@@ -63,6 +63,10 @@ extern "C" {
 #include <algorithm>
 #include <cmath>
 
+#ifdef __HAIKU__
+#include <string.h>
+#endif
+
 using namespace std;
 
 
diff --git a/src/celengine/mesh.cpp b/src/celengine/mesh.cpp
index 38f32bf..2fc1277 100644
--- a/src/celengine/mesh.cpp
+++ b/src/celengine/mesh.cpp
@@ -509,6 +509,7 @@ Mesh::render(const std::vector<const Material*>& materials,
     // the possibility of deleting the original data.  We can always map
     // read-only later on for things like picking, but this could be a low
     // performance path.
+#ifndef __HAIKU__
     if (!vbInitialized && isVBOSupported())
     {
         vbInitialized = true;
@@ -526,7 +527,7 @@ Mesh::render(const std::vector<const Material*>& materials,
             }
         }
     }
-
+#endif
     if (vbObject != 0)
     {
         glx::glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbObject);
diff --git a/src/celengine/render.cpp b/src/celengine/render.cpp
index 279de3e..0e6d600 100644
--- a/src/celengine/render.cpp
+++ b/src/celengine/render.cpp
@@ -1537,7 +1537,7 @@ static void computeOrbitSectionBoundingVolumes(Renderer::CachedOrbit& orbit)
 
         Renderer::OrbitSection section;
         section.firstSample = samplesPerSection * i;
-        unsigned int lastSample = min((unsigned int) orbit.trajectory.size() - 1, section.firstSample + nSamples + 1);
+        unsigned int lastSample = min((unsigned int) orbit.trajectory.size() - 1, (unsigned int)(section.firstSample + nSamples + 1));
 
         // Set the initial axis and origin of the capsule bounding volume; they will be adjusted
         // to contain all points in the trajectory. The length of the axis may change, but the
diff --git a/src/celengine/texture.cpp b/src/celengine/texture.cpp
index 8e4bd67..4a9453c 100644
--- a/src/celengine/texture.cpp
+++ b/src/celengine/texture.cpp
@@ -29,6 +29,10 @@
 #include <cstring> /* for memcpy */
 #include <cassert>
 
+#ifdef __HAIKU__
+#include <string.h>
+#endif
+
 #ifndef _WIN32
 #ifndef TARGET_OS_MAC
 #include <config.h>
diff --git a/src/celestia/glutmain.cpp b/src/celestia/glutmain.cpp
index c7a8ad3..ffe2261 100644
--- a/src/celestia/glutmain.cpp
+++ b/src/celestia/glutmain.cpp
@@ -25,6 +25,9 @@
 #include <Carbon/Carbon.h>
 #include <GLUT/glut.h>
 #endif
+#ifdef __HAIKU__
+#include <Screen.h>
+#endif
 #include <celengine/celestia.h>
 #include <celmath/vecmath.h>
 #include <celmath/quaternion.h>
@@ -433,12 +436,14 @@ static void dirFixup(char *argv0) {
 
 int main(int argc, char* argv[])
 {
+#ifndef __HAIKU__
 	setlocale(LC_ALL, "");
 	setlocale(LC_NUMERIC, "C");
+#endif
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	bind_textdomain_codeset(PACKAGE, "UTF-8");
 	textdomain(PACKAGE);
-			
+
    #ifdef MACOSX
    #define BUNDLEONLY 1
    #ifndef BUNDLEONLY
@@ -496,12 +501,27 @@ int main(int argc, char* argv[])
     }
 
     glutInit(&argc, argv);
+#ifdef __HAIKU__
+	BScreen screen(B_MAIN_SCREEN_ID);
+	int screenWidth = screen.Frame().Width();
+	int screenHeight = screen.Frame().Height();
+	int winWidth = screenWidth >= 1024 ? 800 : 640;
+	int winHeight = screenHeight >= 768 ? 600 : 480;
+	int winPosX = (screenWidth - winWidth ) / 2;
+	int winPosY = (screenHeight - winHeight ) / 2;
+    glutInitWindowSize(winWidth, winHeight);
+    glutInitWindowPosition(winPosX, winPosY);
+    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
+    mainWindow = glutCreateWindow("Celestia");
+    Resize(winWidth, winHeight);
+#else
     glutInitWindowSize(480, 360);
     glutInitWindowPosition(0, 0);
     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
     mainWindow = glutCreateWindow("Celestia");
 
     Resize(480, 360);
+#endif
     glutReshapeFunc(Resize);
     glutDisplayFunc(Display);
     glutIdleFunc(Idle);
@@ -518,7 +538,9 @@ int main(int argc, char* argv[])
 
     // GL should be all set up, now initialize the renderer.
     appCore->initRenderer();
-
+#ifdef __HAIKU__
+	appCore->getRenderer()->getGLContext()->setRenderPath(GLContext::GLPath_Basic);
+#endif
     // Set the simulation starting time to the current system time
     time_t curtime=time(NULL);
     appCore->start((double) curtime / 86400.0 + (double) astro::Date(1970, 1, 1));
diff --git a/src/celestia/imagecapture.cpp b/src/celestia/imagecapture.cpp
index c094378..65c8887 100644
--- a/src/celestia/imagecapture.cpp
+++ b/src/celestia/imagecapture.cpp
@@ -182,7 +182,7 @@ bool CaptureGLBufferToPNG(const string& filename,
     // png_init_io(png_ptr, out);
     png_set_write_fn(png_ptr, (void*) out, PNGWriteData, NULL);
 
-    png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
+    png_set_compression_level(png_ptr, PNG_Z_DEFAULT_COMPRESSION);
     png_set_IHDR(png_ptr, info_ptr,
                  width, height,
                  8,
diff --git a/src/celutil/basictypes.h b/src/celutil/basictypes.h
index d64f523..d178af2 100644
--- a/src/celutil/basictypes.h
+++ b/src/celutil/basictypes.h
@@ -15,6 +15,9 @@
 typedef unsigned int   uint;
 
 // Fixed size types
+#ifdef __HAIKU__
+#include <SupportDefs.h>
+#else
 typedef int32_t        int32;
 typedef uint32_t       uint32;
 typedef int16_t        int16;
@@ -23,6 +26,7 @@ typedef int8_t         int8;
 typedef uint8_t        uint8;
 typedef int64_t        int64;
 typedef uint64_t       uint64;
+#endif
 
 #ifndef INT64_MAX
 #define INT64_MAX 9223372036854775807LL
diff --git a/src/celutil/unixdirectory.cpp b/src/celutil/unixdirectory.cpp
index bcbc1e8..71283ff 100644
--- a/src/celutil/unixdirectory.cpp
+++ b/src/celutil/unixdirectory.cpp
@@ -12,20 +12,20 @@
 #include <pwd.h>
 #include <unistd.h>
 #include <dirent.h>
+#ifndef __HAIKU__
 #include <wordexp.h>
+#endif
 #include <cstdlib>
 #include "directory.h"
 
 using namespace std;
 
 
-#ifdef TARGET_OS_MAC
-#ifdef QT_CORE_LIB
+#if defined(__HAIKU__) || defined(TARGET_OS_MAC) && defined(QT_CORE_LIB)
 // Crash on Mac OS X / Qt4 version when calling wordfree.
 // This seems to happen only with Leopard.
 #define WORDEXP_PROBLEM
 #endif
-#endif
 
 class UnixDirectory : public Directory
 {
-- 
2.28.0

