From 8971f2a2ff7f56cd90bf684c7e88963ec809a8e6 Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@pulkomandy.tk>
Date: Sat, 23 May 2020 17:26:58 +0200
Subject: Make it build on Haiku.

- Add a CMakeList as the source was released without project files to
build anything.
- Fix build with current libpng and libogg
- Use software rendering for now, the GL version needs BDirectGLWindow
(there appears to be bits of a BGLView based one but its not complete)
- Workaround a race condition in initialization

Currently didn't manage better than a white window...

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..6b15847
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,85 @@
+cmake_minimum_required (VERSION 2.8)
+
+# projectname is the same as the main-executable
+project(nogravity)
+
+add_definitions('-O2')
+add_definitions('-D_DEBUG')
+
+add_executable(nogravity
+	rlx32/src/beos/_stub_beos.cpp
+	rlx32/src/beos/snd_mediakit.cpp
+	rlx32/src/beos/sysdbg.cpp
+	rlx32/src/beos/sysio.c
+	rlx32/src/beos/sysjoys.cpp
+	rlx32/src/beos/syskeyb.cpp
+	rlx32/src/beos/sysmouse.cpp
+	rlx32/src/beos/sysreg.c
+	rlx32/src/beos/systime.c
+
+	#rlx32/src/renderers/opengl/beos/bdirectglwindow.cpp
+	rlx32/src/renderers/opengl/beos/gl_be.cpp
+	#rlx32/src/renderers/opengl/beos/be_gl.cpp
+	rlx32/src/renderers/opengl/gl_v3x.cpp
+	rlx32/src/renderers/opengl/gl_gx.cpp
+
+	rlx32/src/_stub.cpp
+	rlx32/src/gui_os.c
+	rlx32/src/gx_csp.c
+	rlx32/src/gx_flc.c
+	rlx32/src/gx_form.c
+	rlx32/src/gx_init.c
+	rlx32/src/gx_png.c
+	rlx32/src/gx_rgb.c
+	rlx32/src/gx_tool.c
+	rlx32/src/iss_cdc.c
+	rlx32/src/iss_db.c
+	rlx32/src/iss_form.c
+	rlx32/src/iss_ogg.c
+	rlx32/src/sysini.c
+	rlx32/src/sysresmx.c
+	rlx32/src/systools.c
+	rlx32/src/syszlib.c
+	rlx32/src/v3xcoll.c
+	rlx32/src/v3xdata.c
+	rlx32/src/v3xmaps.c
+	rlx32/src/v3xrend.c
+	rlx32/src/v3xscene.c
+	rlx32/src/v3xscen2.c
+	rlx32/src/v3xsoft.c
+	rlx32/src/v3x_1.c
+	rlx32/src/v3x_2.c
+	rlx32/src/v3x_3.c
+
+	src/BeOS/preferences.c
+
+	src/ccmx.c
+	src/console.c
+	src/lithium.c
+	src/lt_data.c
+	src/lt_info.c
+	src/lt_input.c
+	src/lt_fx.c
+	src/lt_game.c
+	src/lt_game2.c
+	src/lt_menu.c
+	src/lt_reso.c
+	src/lt_save.c
+	src/lt_sound.c
+	src/lt_start.c
+	src/lt_st2.c
+	src/lt_video.c
+	src/lt_weap.c
+)
+
+target_link_libraries(nogravity be device game GL ogg png vorbis z)
+
+target_include_directories(nogravity PRIVATE
+	${CMAKE_CURRENT_SOURCE_DIR}/rlx32/include/
+	${CMAKE_CURRENT_SOURCE_DIR}/rlx32/src/renderers/opengl/beos/
+)
+set_source_files_properties(
+	rlx32/src/beos/sysio.c
+	rlx32/src/beos/sysreg.c
+	src/BeOS/preferences.c
+	PROPERTIES LANGUAGE CXX)
diff --git a/rlx32/include/_rlx32.h b/rlx32/include/_rlx32.h
index 8fca697..89ff0eb 100644
--- a/rlx32/include/_rlx32.h
+++ b/rlx32/include/_rlx32.h
@@ -94,7 +94,7 @@ Prepared for public release: 02/24/2004 - Stephane Denis, realtech VR
 
 /* Data Types */
 #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
-    #if defined __LCC__ || defined __LINUX__ || defined LINUX || (defined __BEOS__ && B_BEOS_VERSION >=0x0520)
+    #if defined __LCC__ || defined __LINUX__ || defined LINUX || (defined __BEOS__ && B_BEOS_VERSION >=0x0520) || defined(__HAIKU__)
        #include <stdint.h>
        #include <stddef.h>       
     #elif defined __APPLE__ && defined __MACH__
diff --git a/rlx32/src/gx_png.c b/rlx32/src/gx_png.c
index 8707974..e17d3e8 100644
--- a/rlx32/src/gx_png.c
+++ b/rlx32/src/gx_png.c
@@ -50,7 +50,7 @@ pngx_readm_pData(png_structp png_ptr, png_bytep data, png_size_t length)
 	* instead of an int, which is what fread() actually returns.
 	*/
 	check = (png_size_t)FIO_cur->fread(data, (png_size_t)1, length,
-	  (SYS_FILEHANDLE)png_ptr->io_ptr);
+	  (SYS_FILEHANDLE)png_get_io_ptr(png_ptr));
 
 	if (check != length)
 	{
@@ -77,7 +77,7 @@ static void pngx_error(png_structp png_ptr, png_const_charp message)
 #ifdef _DEBUG
 	SYS_Msg("!%s", message);
 #endif
-	longjmp(png_ptr->jmpbuf, 1);
+	longjmp(png_jmpbuf(png_ptr), 1);
 }
 
 static void pngx_warning(png_structp png_ptr, png_const_charp message)
@@ -121,7 +121,7 @@ if (!pClut)
 	info_ptr = png_create_info_struct(png_ptr);
 	png_read_info(png_ptr, info_ptr);  /* read all PNG info up to image data */
 
-	if (setjmp(png_ptr->jmpbuf))
+	if (setjmp(png_jmpbuf(png_ptr)))
 	{
 		png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
 		return NULL;
diff --git a/rlx32/src/iss_ogg.c b/rlx32/src/iss_ogg.c
index 7cd5243..f0bd500 100644
--- a/rlx32/src/iss_ogg.c
+++ b/rlx32/src/iss_ogg.c
@@ -255,13 +255,13 @@ static void DeinterleaveAudio(void *outdata,
 /* convert floats to 16 bit signed ints (host order) and
    interleave */
    
-__inline ogg_int16_t FloatToShort(float v)
+__inline int16_t FloatToShort(float v)
 {
 	if (v>1)
 		return 32767;
 	if (v<-1)
 		return -32768;
-	return (ogg_int16_t)(v*32768.f);
+	return (int16_t)(v*32768.f);
 }
 
 static void ConvertFloatToInteger(void *outdata, 
@@ -275,7 +275,7 @@ static void ConvertFloatToInteger(void *outdata,
 	for(i=0;i<channels;i++)
 	{
 		const float *mono = pcm[i];
-		ogg_int16_t *ptr = (ogg_int16_t *)outdata + i;
+		int16_t *ptr = (int16_t *)outdata + i;
 		for(j=0;j<n;j++,ptr+=channels,mono++)
 		{
 			*ptr = FloatToShort(*mono);
diff --git a/rlx32/src/renderers/opengl/beos/bdirectglwindow.cpp b/rlx32/src/renderers/opengl/beos/bdirectglwindow.cpp
index eb22486..b5ae4c8 100644
--- a/rlx32/src/renderers/opengl/beos/bdirectglwindow.cpp
+++ b/rlx32/src/renderers/opengl/beos/bdirectglwindow.cpp
@@ -28,6 +28,7 @@ Prepared for public release: 02/24/2004 - Stephane Denis, realtech VR
 
 #include "_rlx32.h"
 #include "_rlx.h"
+#include "beos/stub.h"
 
 #include "systools.h"
 #include "gx_struc.h"
@@ -46,7 +47,7 @@ struct RLXSYSTEM	*	g_pRLX;
 inline struct GXSYSTEM *GET_GX() { return g_pRLX->pGX; }
 
 static u_int8_t RLXAPI *Lock(void)
-{	
+{
     return NULL;
 }
 
diff --git a/rlx32/src/renderers/opengl/beos/be_gl.cpp b/rlx32/src/renderers/opengl/beos/be_gl.cpp
index e67ec1b..11a421e 100644
--- a/rlx32/src/renderers/opengl/beos/be_gl.cpp
+++ b/rlx32/src/renderers/opengl/beos/be_gl.cpp
@@ -28,7 +28,7 @@ Prepared for public release: 02/24/2004 - Stephane Denis, realtech VR
 #include <GL/gl.h>
 #include "_rlx32.h"
 #include "_rlx.h"
-#include "_stub_be.h"
+#include "beos/stub.h"
 #include "systools.h"
 #include "sysresmx.h"
 #include "gx_struc.h"
@@ -39,6 +39,7 @@ Prepared for public release: 02/24/2004 - Stephane Denis, realtech VR
 #include "v3x_2.h"
 #include "v3xrend.h"
 
+struct RLXSYSTEM *g_pRLX;
 static int g_lx, g_ly, g_bpp;
 
 static void CALLING_C Flip(void)
@@ -64,10 +65,13 @@ static GXDISPLAYMODEINFO RLXAPI *EnumDisplayList(int bpp)
     return NULL;
 }
 
+extern GXGRAPHICINTERFACE GI_OpenGL;
+extern GXSPRITEINTERFACE CSP_OpenGL;
+
 static void RLXAPI GetDisplayInfo(GXDISPLAYMODEHANDLE mode)
 {   
     g_pRLX->pGX->View.Flip = Flip;
-    g_pRLX->pGX->gi = GX_OpenGL;
+    g_pRLX->pGX->gi = GI_OpenGL;
     g_pRLX->pGX->csp = CSP_OpenGL;
     g_pRLX->pGX->csp_cfg.put.fonct = g_pRLX->pGX->csp.put;
     g_pRLX->pGX->csp_cfg.pset.fonct = g_pRLX->pGX->csp.pset;
@@ -106,13 +110,15 @@ static int RLXAPI CreateSurface(int numberOfSparePages)
     g_pRLX->pGX->Surfaces.maxSurface = numberOfSparePages;
     for (i=2;i<g_pRLX->pGX->Surfaces.maxSurface+1;i++)
     {
-        g_pRLX->pGX->Surfaces.lpSurface[i] = (u_int8_t*)g_pRLX->mm_std->.malloc(sz);
+        g_pRLX->pGX->Surfaces.lpSurface[i] = /*(u_int8_t*)g_pRLX->mm_std->.*/malloc(sz);
         if (g_pRLX->pGX->Surfaces.lpSurface[i])
            SysMemZero(g_pRLX->pGX->Surfaces.lpSurface[i], sz);
         g_pRLX->pGX->Surfaces.flags[i] = 2;
     }
     g_pRLX->pGX->View.lpBackBuffer = NULL;
+#if 0
     g_pRLX->pGX->View.Flags &= ~GX_CAPS_BACKBUFFERPAGE;
+#endif
     g_pRLX->pGX->View.Flags |= GX_CAPS_BACKBUFFERINVIDEO;
     return 0;
 }
@@ -124,7 +130,7 @@ static void RLXAPI ReleaseSurfaces(void)
     {
         if (g_pRLX->pGX->Surfaces.lpSurface[i])
         {
-            g_pRLX->g_pRLX->mm_std->->free(g_pRLX->pGX->Surfaces.lpSurface[i]);
+            /*g_pRLX->g_pRLX->mm_std->->*/free(g_pRLX->pGX->Surfaces.lpSurface[i]);
             g_pRLX->pGX->Surfaces.flags[i] = 0;
         }
     }
@@ -159,7 +165,8 @@ static unsigned NotifyEvent(enum GX_EVENT_MODE mode, int x, int y)
     return mode;
 
 }
-static GXCLIENTDRIVER GX_OpenGL = {
+
+GXCLIENTDRIVER GX_OpenGL = {
     Lock, 
     Unlock, 
     EnumDisplayList, 
@@ -168,16 +175,15 @@ static GXCLIENTDRIVER GX_OpenGL = {
     SearchDisplayMode, 
     CreateSurface, 
     ReleaseSurfaces, 
-    GL_DownloadSprite, 
-    GL_ReleaseSprite, 
-    GL_UpdateSprite, 
+    NULL, 
+    NULL, 
+    NULL, 
     RegisterMode, 
     Shutdown, 
     Open,
     NotifyEvent
     "OpenGL"
 };
-
 _RLXEXPORTFUNC void RLXAPI GX_EntryPoint(struct RLXSYSTEM *p)
 {
     g_pRLX = p;
diff --git a/rlx32/src/renderers/opengl/beos/gl_be.cpp b/rlx32/src/renderers/opengl/beos/gl_be.cpp
index aae0ba3..31fc6dc 100644
--- a/rlx32/src/renderers/opengl/beos/gl_be.cpp
+++ b/rlx32/src/renderers/opengl/beos/gl_be.cpp
@@ -25,13 +25,13 @@ Prepared for public release: 02/24/2004 - Stephane Denis, realtech VR
 //-------------------------------------------------------------------------
 #include <stdio.h>
 #include <stdlib.h>
-#include <GL/gl.h.h>
+
 #include "_rlx32.h"
 #include "_rlx.h"
-#include "_stub_be.h"
+#include "beos/stub.h"
+
 #include "systools.h"
-#include "sysresmx.h"
-#include "gx_struc.h"            
+#include "gx_struc.h"
 #include "gx_init.h"
 #include "gx_csp.h"
 #include "gx_rgb.h"
@@ -39,16 +39,13 @@ Prepared for public release: 02/24/2004 - Stephane Denis, realtech VR
 #include "v3x_2.h"
 #include "v3xrend.h"
 
-static int g_lx, g_ly, g_bpp;
+#include "../gl_v3x.h"
 
-static void CALLING_C Flip(void)
-{
-    glFinish();
-    STUB_gl->SwapBuffers();
-    STUB_gl->UnlockGL();
-    return;
-}
+sysApplication		*	g_pApp;
+struct RLXSYSTEM	*	g_pRLX;
+static GLint gl_lx, gl_ly, gl_bpp;
 
+inline struct GXSYSTEM *GET_GX() { return g_pRLX->pGX; }
 
 static u_int8_t RLXAPI *Lock(void)
 {
@@ -60,43 +57,44 @@ static void RLXAPI Unlock(void)
     return;
 }
 
-static GXDISPLAYMODEINFO RLXAPI *EnumDisplayList(int bpp)
-{
-    // WTF ?
-    return NULL;
-}
+extern GXGRAPHICINTERFACE GI_OpenGL;
+extern GXSPRITEINTERFACE CSP_OpenGL;
 
 static void RLXAPI GetDisplayInfo(GXDISPLAYMODEHANDLE mode)
-{   
+{
+	g_pRLX->pfSetViewPort(&GET_GX()->View, gl_lx, gl_ly, gl_bpp);
+	GET_GX()->View.ColorMask.RedMaskSize	= 8;
+	GET_GX()->View.ColorMask.GreenMaskSize = 8;
+	GET_GX()->View.ColorMask.BlueMaskSize = 8;
+	GET_GX()->View.ColorMask.RsvdMaskSize = 8;
+	GET_GX()->View.ColorMask.RedFieldPosition = 0;
+	GET_GX()->View.ColorMask.GreenFieldPosition = 8;
+	GET_GX()->View.ColorMask.BlueFieldPosition  = 16;
+	GET_GX()->View.ColorMask.RsvdFieldPosition  = 24;
+	GET_GX()->gi = GI_OpenGL;
+	GET_GX()->csp = CSP_OpenGL;
+	GET_GX()->csp_cfg.put.fonct = GET_GX()->csp.put;
+    GET_GX()->csp_cfg.pset.fonct = GET_GX()->csp.pset;
+    GET_GX()->csp_cfg.transp.fonct = GET_GX()->csp.Trsp50;
+    GET_GX()->csp_cfg.op = GET_GX()->csp.put;
+	UNUSED(mode);
+#if 0
     g_pRLX->pGX->View.Flip = Flip;
-    g_pRLX->pGX->gi = GX_OpenGL;
+    g_pRLX->pGX->gi = GI_OpenGL;
     g_pRLX->pGX->csp = CSP_OpenGL;
     g_pRLX->pGX->csp_cfg.put.fonct = g_pRLX->pGX->csp.put;
     g_pRLX->pGX->csp_cfg.pset.fonct = g_pRLX->pGX->csp.pset;
     g_pRLX->pGX->csp_cfg.transp.fonct = g_pRLX->pGX->csp.Trsp50;
     g_pRLX->pGX->csp_cfg.op = g_pRLX->pGX->csp.put;
+#endif
     return;
 }
 
-static int RLXAPI SetDisplayMode(GXDISPLAYMODEHANDLE mode)
-{
-    STUB_App->SetWindowSize(g_pRLX->pGX->View.lWidth, g_pRLX->pGX->View.lHeight); // ??
-    return 0;
-}
-
 static GXDISPLAYMODEHANDLE RLXAPI SearchDisplayMode(int lx, int ly, int bpp)
 {
-    GXDISPLAYMODEHANDLE mode = 1;
-    if (g_pRLX->Video.Config&RLXVIDEO_Windowed)
-    {
-        g_lx = lx;
-        g_ly = ly;
-        g_bpp = bpp;
-    }
-    else
-    {
-    	// ???
-    }
+    gl_lx  = lx;
+    gl_ly  = ly;
+    gl_bpp = 32;
     return 1;
 }
 
@@ -112,13 +110,13 @@ static int RLXAPI CreateSurface(int numberOfSparePages)
     g_pRLX->pGX->Surfaces.maxSurface = numberOfSparePages;
     for (i=2;i<g_pRLX->pGX->Surfaces.maxSurface+1;i++)
     {
-        g_pRLX->pGX->Surfaces.lpSurface[i] = (u_int8_t*)g_pRLX->mm_std->.malloc(sz);
-        if (g_pRLX->pGX->Surfaces.lpSurface[i])
-        SysMemZero(g_pRLX->pGX->Surfaces.lpSurface[i], sz);
+        g_pRLX->pGX->Surfaces.lpSurface[i] = (u_int8_t*)/*g_pRLX->mm_std->.*/calloc(sz, 1);
         g_pRLX->pGX->Surfaces.flags[i] = 2;
     }
     g_pRLX->pGX->View.lpBackBuffer = NULL;
+#if 0
     g_pRLX->pGX->View.Flags &= ~GX_CAPS_BACKBUFFERPAGE;
+#endif
     g_pRLX->pGX->View.Flags |= GX_CAPS_BACKBUFFERINVIDEO;
     return 0;
 }
@@ -130,7 +128,7 @@ static void RLXAPI ReleaseSurfaces(void)
     {
         if (g_pRLX->pGX->Surfaces.lpSurface[i])
         {
-            g_pRLX->g_pRLX->mm_std->->free(g_pRLX->pGX->Surfaces.lpSurface[i]);
+            /*g_pRLX->g_pRLX->mm_std->->*/free(g_pRLX->pGX->Surfaces.lpSurface[i]);
             g_pRLX->pGX->Surfaces.flags[i] = 0;
         }
     }
@@ -149,7 +147,7 @@ static int RLXAPI RegisterMode(int mode)
 }
 
 static void RLXAPI Shutdown(void)
-{       
+{
     return;
 }
 
@@ -166,18 +164,19 @@ static unsigned NotifyEvent(enum GX_EVENT_MODE mode, int x, int y)
     return mode;
 
 }
-static GXCLIENTDRIVER GX_OpenGL = {
+
+GXCLIENTDRIVER GX_OpenGL = {
     Lock, 
     Unlock, 
-    EnumDisplayList, 
+    0, 
     GetDisplayInfo, 
-    SetDisplayMode, 
+    0, 
     SearchDisplayMode, 
     CreateSurface, 
     ReleaseSurfaces, 
-    GL_DownloadSprite, 
-    GL_ReleaseSprite, 
-    GL_UpdateSprite, 
+    NULL, 
+    NULL, 
+    NULL, 
     RegisterMode, 
     Shutdown, 
     Open,
@@ -185,13 +184,18 @@ static GXCLIENTDRIVER GX_OpenGL = {
     "OpenGL"
 };
 
+extern void SetPrimitiveSprites();
+
 _RLXEXPORTFUNC void RLXAPI GX_EntryPoint(struct RLXSYSTEM *p)
 {
     g_pRLX = p;
+	SetPrimitiveSprites();
 	g_pRLX->pGX->Client = &GX_OpenGL;
     return;
 }
 
+extern V3X_GXSystem V3X_OpenGL;
+
 _RLXEXPORTFUNC void RLXAPI V3X_EntryPoint(struct RLXSYSTEM *p)
 {
 	GX_EntryPoint(p);
diff --git a/rlx32/src/renderers/opengl/gl_v3x.h b/rlx32/src/renderers/opengl/gl_v3x.h
index def0cca..c30f157 100644
--- a/rlx32/src/renderers/opengl/gl_v3x.h
+++ b/rlx32/src/renderers/opengl/gl_v3x.h
@@ -29,7 +29,7 @@ Prepared for public release: 02/24/2004 - Stephane Denis, realtech VR
 #include <stdio.h>
 #include "_rlx32.h"
 
-#if defined __BEOS__
+#if defined(__BEOS__)
 #include "dwgl.h"
 
 #elif defined _WIN32
@@ -70,6 +70,8 @@ Prepared for public release: 02/24/2004 - Stephane Denis, realtech VR
 #define GL_UNPACK_CLIENT_STORAGE_APPLE 0x85B2
 #endif
 
+#elif (defined __HAIKU__)
+#include <GL/gl.h>
 #endif
 
 #include "gx_struc.h"
-- 
2.26.0

