diff --git a/Makefile b/Makefile index 594124be1..e2a39267c 100644 --- a/Makefile +++ b/Makefile @@ -53,7 +53,7 @@ LDFLAGS=-rdynamic -framework Carbon -framework AGL -framework OpenGL -framework endif ifeq ($(PLAT),mac_x64) -OBJECTS+=interop_cocoa.o +OBJECTS+=src/interop_cocoa.o CFLAGS=-g -m64 -pipe -fno-math-errno LIBS= LDFLAGS=-rdynamic -framework Cocoa -framework OpenGL -framework IOKit -lobjc @@ -84,14 +84,14 @@ LIBS=-lexecinfo -lGL -lX11 -lXi -lm -lpthread endif ifeq ($(PLAT),haiku) -OBJECTS+=Window_Haiku.o +OBJECTS+=src/interop_BeOS.o CFLAGS=-g -pipe -fno-math-errno LDFLAGS=-g LIBS=-lm -lexecinfo -lGL -lnetwork -lstdc++ -lbe -lgame -ltracker endif ifeq ($(PLAT),beos) -OBJECTS+=Window_Haiku.o +OBJECTS+=src/interop_BeOS.o CFLAGS=-g -pipe LDFLAGS=-g LIBS=-lm -lexecinfo -lGL -lnetwork -lstdc++ -lbe -lgame -ltracker @@ -166,7 +166,7 @@ $(C_OBJECTS): %.o : %.c src/interop_cocoa.o: src/interop_cocoa.m $(CC) $(CFLAGS) -c $< -o $@ -src/Window_Haiku.o: src/Window_Haiku.cpp +src/interop_BeOS.o: src/interop_BeOS.cpp $(CC) $(CFLAGS) -c $< -o $@ # PSP requires fixups diff --git a/src/Core.h b/src/Core.h index 88c447be0..2aabbd69c 100644 --- a/src/Core.h +++ b/src/Core.h @@ -220,7 +220,7 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_CURL #define CC_BUILD_OPENAL #elif defined __BEOS__ -#define CC_BUILD_HAIKU +#define CC_BUILD_BEOS #define CC_BUILD_POSIX #define CC_BUILD_GL #define CC_BUILD_CURL diff --git a/src/Platform_GCWii.c b/src/Platform_GCWii.c index 30a800f53..9d3c00362 100644 --- a/src/Platform_GCWii.c +++ b/src/Platform_GCWii.c @@ -524,8 +524,48 @@ static void InitSockets(void) { /*########################################################################################################################* *-----------------------------------------------------Process/Module------------------------------------------------------* *#########################################################################################################################*/ +static char gameArgs[GAME_MAX_CMDARGS][STRING_SIZE]; +static int gameNumArgs; +static cc_bool gameHasArgs; cc_result Process_StartGame2(const cc_string* args, int numArgs) { - return ERR_NOT_SUPPORTED; + for (int i = 0; i < numArgs; i++) + { + String_CopyToRawArray(gameArgs[i], &args[i]); + } + + Platform_LogConst("START GAME"); + gameHasArgs = true; + gameNumArgs = numArgs; + return 0; +} +static int GetGameArgs(cc_string* args) { + int count = gameNumArgs; + for (int i = 0; i < count; i++) + { + args[i] = String_FromRawArray(gameArgs[i]); + } + + // clear arguments so after game is closed, launcher is started + gameNumArgs = 0; + return count; +} +int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, cc_string* args) { + if (gameHasArgs) return GetGameArgs(args); + + // GC/WII *sometimes* doesn't use argv[0] for program name and so argc will be 0 + if (!argc) return 0; + + int count = min(argc, GAME_MAX_CMDARGS); + for (int i = 0; i < count; i++) + { + args[i] = String_FromReadonly(argv[i]); + } + + return count; +} + +cc_result Platform_SetDefaultCurrentDirectory(int argc, char **argv) { + return 0; } void Process_Exit(cc_result code) { exit(code); } @@ -617,24 +657,4 @@ cc_result Platform_Encrypt(const void* data, int len, cc_string* dst) { cc_result Platform_Decrypt(const void* data, int len, cc_string* dst) { return ERR_NOT_SUPPORTED; } - - -/*########################################################################################################################* -*-----------------------------------------------------Configuration-------------------------------------------------------* -*#########################################################################################################################*/ -int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, cc_string* args) { - // GC/WII *sometimes* doesn't use argv[0] for program name and so argc will be 0 - if (!argc) return 0; - - int count = min(argc, GAME_MAX_CMDARGS); - for (int i = 0; i < count; i++) { - args[i] = String_FromReadonly(argv[i]); - } - - return count; -} - -cc_result Platform_SetDefaultCurrentDirectory(int argc, char **argv) { - return 0; -} -#endif \ No newline at end of file +#endif diff --git a/src/Platform_Posix.c b/src/Platform_Posix.c index 80b5d191d..cb684d035 100644 --- a/src/Platform_Posix.c +++ b/src/Platform_Posix.c @@ -83,13 +83,6 @@ void Mem_Free(void* mem) { /*########################################################################################################################* *------------------------------------------------------Logging/Time-------------------------------------------------------* *#########################################################################################################################*/ -/* TODO: check this is actually accurate */ -static cc_uint64 sw_freqMul = 1, sw_freqDiv = 1; -cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) { - if (end < beg) return 0; - return ((end - beg) * sw_freqMul) / sw_freqDiv; -} - #if defined CC_BUILD_ANDROID /* implemented in Platform_Android.c */ #elif defined CC_BUILD_IOS @@ -122,14 +115,44 @@ void DateTime_CurrentLocal(struct DateTime* t) { t->second = loc_time.tm_sec; } + +/*########################################################################################################################* +*--------------------------------------------------------Stopwatch--------------------------------------------------------* +*#########################################################################################################################*/ #define NS_PER_SEC 1000000000ULL + +#if defined CC_BUILD_HAIKU || defined CC_BUILD_BEOS +/* Implemented in interop_BeOS.cpp */ +#elif defined CC_BUILD_DARWIN +static cc_uint64 sw_freqMul, sw_freqDiv; +static void Stopwatch_Init(void) { + mach_timebase_info_data_t tb = { 0 }; + mach_timebase_info(&tb); + + sw_freqMul = tb.numer; + /* tb.denom may be large, so multiplying by 1000 overflows 32 bits */ + /* (one powerpc system had tb.denom of 33329426) */ + sw_freqDiv = (cc_uint64)tb.denom * 1000; +} + +cc_uint64 Stopwatch_Measure(void) { return mach_absolute_time(); } + +cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) { + if (end < beg) return 0; + return ((end - beg) * sw_freqMul) / sw_freqDiv; +} +#elif defined CC_BUILD_SOLARIS +/* https://docs.oracle.com/cd/E86824_01/html/E54766/gethrtime-3c.html */ +/* The gethrtime() function returns the current high-resolution real time. Time is expressed as nanoseconds since some arbitrary time in the past */ +cc_uint64 Stopwatch_Measure(void) { return gethrtime(); } + +cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) { + if (end < beg) return 0; + return (end - beg) / 1000; +} +#else /* clock_gettime is optional, see http://pubs.opengroup.org/onlinepubs/009696899/functions/clock_getres.html */ /* "... These functions are part of the Timers option and need not be available on all implementations..." */ -#if defined CC_BUILD_DARWIN -cc_uint64 Stopwatch_Measure(void) { return mach_absolute_time(); } -#elif defined CC_BUILD_SOLARIS -cc_uint64 Stopwatch_Measure(void) { return gethrtime(); } -#else cc_uint64 Stopwatch_Measure(void) { struct timespec t; #ifdef CC_BUILD_IRIX @@ -140,6 +163,11 @@ cc_uint64 Stopwatch_Measure(void) { #endif return (cc_uint64)t.tv_sec * NS_PER_SEC + t.tv_nsec; } + +cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) { + if (end < beg) return 0; + return (end - beg) / 1000; +} #endif @@ -580,8 +608,9 @@ void Socket_Close(cc_socket s) { close(s); } -#if defined CC_BUILD_DARWIN +#if defined CC_BUILD_DARWIN || defined CC_BUILD_BEOS /* poll is broken on old OSX apparently https://daniel.haxx.se/docs/poll-vs-select.html */ +/* BeOS lacks support for poll */ static cc_result Socket_Poll(cc_socket s, int mode, cc_bool* success) { fd_set set; struct timeval time = { 0 }; @@ -695,16 +724,8 @@ cc_result Process_StartOpen(const cc_string* args) { CFRelease(urlCF); return 0; } -#elif defined CC_BUILD_HAIKU -cc_result Process_StartOpen(const cc_string* args) { - char str[NATIVE_STR_LEN]; - char* cmd[3]; - String_EncodeUtf8(str, args); - - cmd[0] = "open"; cmd[1] = str; cmd[2] = NULL; - Process_RawStart("open", cmd); - return 0; -} +#elif defined CC_BUILD_HAIKU || defined CC_BUILD_BEOS +/* Implemented in interop_BeOS.cpp */ #else cc_result Process_StartOpen(const cc_string* args) { char str[NATIVE_STR_LEN]; @@ -995,9 +1016,6 @@ static void Platform_InitPosix(void) { signal(SIGCHLD, SIG_IGN); /* So writing to closed socket doesn't raise SIGPIPE */ signal(SIGPIPE, SIG_IGN); - /* Assume stopwatch is in nanoseconds */ - /* Some platforms (e.g. macOS) override this */ - sw_freqDiv = 1000; } void Platform_Free(void) { } @@ -1029,44 +1047,39 @@ cc_bool Platform_DescribeError(cc_result res, cc_string* dst) { #endif #if defined CC_BUILD_DARWIN -static void Platform_InitStopwatch(void) { - mach_timebase_info_data_t tb = { 0 }; - mach_timebase_info(&tb); - - sw_freqMul = tb.numer; - /* tb.denom may be large, so multiplying by 1000 overflows 32 bits */ - /* (one powerpc system had tb.denom of 33329426) */ - sw_freqDiv = (cc_uint64)tb.denom * 1000; -} #if defined CC_BUILD_MACOS static void Platform_InitSpecific(void) { ProcessSerialNumber psn = { 0, kCurrentProcess }; -#ifdef __ppc__ + #ifdef __ppc__ /* TransformProcessType doesn't work with kCurrentProcess on older macOS */ GetCurrentProcess(&psn); -#endif + #endif /* NOTE: Call as soon as possible, otherwise can't click on dialog boxes or create windows */ /* NOTE: TransformProcessType is macOS 10.3 or later */ TransformProcessType(&psn, kProcessTransformToForegroundApplication); } #else -/* Always foreground process on iOS */ -static void Platform_InitSpecific(void) { } +static void Platform_InitSpecific(void) { + Platform_SingleProcess = true; + /* Always foreground process on iOS */ +} #endif void Platform_Init(void) { -#ifdef CC_BUILD_MOBILE - Platform_SingleProcess = true; -#endif - + Stopwatch_Init(); Platform_InitPosix(); - Platform_InitStopwatch(); Platform_InitSpecific(); } #else -void Platform_Init(void) { Platform_InitPosix(); } +void Platform_Init(void) { + #ifdef CC_BUILD_MOBILE + Platform_SingleProcess = true; + #endif + + Platform_InitPosix(); +} #endif diff --git a/src/Program.c b/src/Program.c index be9ab7be0..753addb9c 100644 --- a/src/Program.c +++ b/src/Program.c @@ -138,7 +138,7 @@ void android_main(void) { SetupProgram(0, NULL); for (;;) { RunProgram(0, NULL); } } -#elif defined CC_BUILD_3DS || defined CC_BUILD_PSP +#elif defined CC_BUILD_3DS || defined CC_BUILD_PSP || defined CC_BUILD_GCWII int main(int argc, char** argv) { SetupProgram(argc, argc); for (;;) { RunProgram(argc, argv); } diff --git a/src/Window_Haiku.cpp b/src/interop_BeOS.cpp similarity index 91% rename from src/Window_Haiku.cpp rename to src/interop_BeOS.cpp index d826a43cb..b404bfff9 100644 --- a/src/Window_Haiku.cpp +++ b/src/interop_BeOS.cpp @@ -1,5 +1,5 @@ #include "Core.h" -#if defined CC_BUILD_HAIKU && !defined CC_BUILD_SDL +#if defined CC_BUILD_HAIKU || defined CC_BUILD_BEOS extern "C" { #include "_WindowBase.h" #include "Graphics.h" @@ -10,6 +10,9 @@ extern "C" { #include "Utils.h" } +// Other +#include +#include // AppKit #include #include @@ -25,6 +28,35 @@ extern "C" { #include #include +/*########################################################################################################################* +*--------------------------------------------------------Platform---------------------------------------------------------* +*#########################################################################################################################*/ +cc_uint64 Stopwatch_Measure(void) { + return system_time(); +} + +cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) { + if (end < beg) return 0; + return end - beg; +} + +cc_result Process_StartOpen(const cc_string* args) { + static const cc_string https_protocol = String_FromConst("https://"); + char str[NATIVE_STR_LEN]; + String_EncodeUtf8(str, args); + + cc_bool https = String_CaselessStarts(args, &https_protocol); + const char* mime = https ? "application/x-vnd.Be.URL.https" : "application/x-vnd.Be.URL.http"; + + const char* argv[] = { str, NULL }; + return be_roster->Launch(mime, 1, argv); +} + + +/*########################################################################################################################* +*---------------------------------------------------------Window----------------------------------------------------------* +*#########################################################################################################################*/ +#if !defined CC_BUILD_SDL static BApplication* app_handle; static BWindow* win_handle; static BView* view_handle; @@ -635,7 +667,11 @@ void GLContext_Free(void) { } void* GLContext_GetAddress(const char* function) { +#if defined CC_BUILD_BEOS + return NULL; +#else return view_3D->GetGLProcAddress(function); +#endif } cc_bool GLContext_SwapBuffers(void) { @@ -647,5 +683,6 @@ void GLContext_SetFpsLimit(cc_bool vsync, float minFrameMs) { win_vsync = vsync; } void GLContext_GetApiInfo(cc_string* info) { } -#endif +#endif // CC_BUILD_GL && !CC_BUILD_EGL +#endif // !CC_BUILD_SDL #endif