LCOV - code coverage report
Current view: top level - SDL - main.m (source / functions) Hit Total Coverage
Test: coverxygen.info Lines: 1 3 33.3 %
Date: 2025-10-17 07:16:03 Functions: 0 0 -

          Line data    Source code
       1           0 : /*
       2             : 
       3             : main.m
       4             : 
       5             : Oolite
       6             : Copyright (C) 2004-2013 Giles C Williams and contributors
       7             : 
       8             : This program is free software; you can redistribute it and/or
       9             : modify it under the terms of the GNU General Public License
      10             : as published by the Free Software Foundation; either version 2
      11             : of the License, or (at your option) any later version.
      12             : 
      13             : This program is distributed in the hope that it will be useful,
      14             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             : GNU General Public License for more details.
      17             : 
      18             : You should have received a copy of the GNU General Public License
      19             : along with this program; if not, write to the Free Software
      20             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
      21             : MA 02110-1301, USA.
      22             : 
      23             : */
      24             : 
      25             : 
      26             : #ifdef GNUSTEP
      27             : #import <Foundation/NSAutoreleasePool.h>
      28             : #if (GNUSTEP_BASE_MAJOR_VERSION == 1 && (GNUSTEP_BASE_MINOR_VERSION == 24 && GNUSTEP_BASE_SUBMINOR_VERSION >= 9) || (GNUSTEP_BASE_MINOR_VERSION > 24)) || (GNUSTEP_BASE_MAJOR_VERSION > 1)
      29             : #import <Foundation/NSDate.h>
      30             : #endif
      31             : #import <Foundation/NSString.h>
      32             : #import "GameController.h"
      33             : #import "OOLoggingExtended.h"
      34             : 
      35             : #if OOLITE_WINDOWS
      36             : #include <locale.h>
      37             : #include <SDL.h>
      38             : // Make sure that a high performance GPU is
      39             : // selected, if more than one are available
      40             : __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
      41             : __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
      42             : #endif
      43             : 
      44             : GameController* controller;
      45             : #endif
      46             : 
      47             : 
      48             : #ifndef NDEBUG
      49           0 : uint32_t gDebugFlags = 0;
      50             : #endif
      51             : 
      52             : /**
      53             :  * \ingroup cli
      54             :  * Entry point for Linux and Windows systems.
      55             :  * Initializes logging. If -load is passed, the argument after that is loaded
      56             :  * as savegame.
      57             :  *
      58             :  * @param argc the number of command line arguments
      59             :  * @param argv the string array values of the command line arguments
      60             :  * @return returns 0 on success, or EXITFAILURE when an exception is caught
      61             :  */
      62           1 : int main(int argc, char *argv[])
      63             : {
      64             : #ifdef GNUSTEP
      65             :         int i;
      66             : 
      67             : #if (GNUSTEP_BASE_MAJOR_VERSION == 1 && (GNUSTEP_BASE_MINOR_VERSION == 24 && GNUSTEP_BASE_SUBMINOR_VERSION >= 9) || (GNUSTEP_BASE_MINOR_VERSION > 24)) || (GNUSTEP_BASE_MAJOR_VERSION > 1)
      68             :         [NSDate class]; // See github issue #202
      69             : #endif
      70             :         
      71             : #if OOLITE_WINDOWS
      72             : 
      73             :         #define OO_SHOW_MSG(ooMsg, ooMsgTitle, ooMsgFlags)      MessageBox(NULL, ooMsg, ooMsgTitle, ooMsgFlags)
      74             :         #define TABS1   "\t"
      75             :         #define TABS2   "\t\t"
      76             :         #define TABS3   "\t\t\t"
      77             :         #define TABS4   ""
      78             :         
      79             :         // Detect current working directory and set up GNUstep environment variables
      80             :         #define MAX_PATH_LEN 256
      81             :         char currentWorkingDir[MAX_PATH_LEN];
      82             :         DWORD bufferSize = MAX_PATH_LEN;
      83             :         
      84             :         QueryFullProcessImageName(GetCurrentProcess(), 0, currentWorkingDir, &bufferSize);
      85             :         // Strip the exe filenameb (from last backslash onwards), leave just the path
      86             :         char *probeString = strrchr(currentWorkingDir, '\\');
      87             :         if (probeString)  *probeString = '\0'; // currentWorkingDir now contains the path we need
      88             :         
      89             :         // Prepend system PATH env variable with our own executable's path
      90             :         char pathEnvVar[] = "PATH";
      91             :         char *systemPath = SDL_getenv(pathEnvVar);
      92             :         size_t currentWorkingDirLen = strlen(currentWorkingDir);
      93             :         size_t systemPathLen = strlen(systemPath);
      94             :         // the max possible length of the string below is systemPath plus the path
      95             :         // we have determined for us, plus one char for the ";" and one char for the null terminator
      96             :         char *finalPath = malloc(systemPathLen + currentWorkingDirLen + 2 * sizeof(char));
      97             :         // the max possible length of the string below is systemPath plus the path we have
      98             :         // determined for us, plus one char for the ";", plus the string "PATH", plus one char for the
      99             :         // "=" of the final string that will be passed on to SDL_putenv and one char for the null terminator
     100             :         char *envVarString = malloc(systemPathLen + currentWorkingDirLen + strlen(pathEnvVar) + 3 * sizeof(char));
     101             :         strcpy(finalPath, currentWorkingDir);
     102             :         strcat(finalPath, ";");
     103             :         strcat(finalPath, systemPath);
     104             : 
     105             :         #define SETENVVAR(var, value) do {\
     106             :                         sprintf(envVarString, "%s=%s", (var), (value));\
     107             :                         SDL_putenv (envVarString);\
     108             :                         } while (0);
     109             :         
     110             :         SETENVVAR("GNUSTEP_PATH_HANDLING", "windows");
     111             :         SETENVVAR(pathEnvVar, finalPath);
     112             :         SETENVVAR("GNUSTEP_SYSTEM_ROOT", currentWorkingDir);
     113             :         SETENVVAR("GNUSTEP_LOCAL_ROOT", currentWorkingDir);
     114             :         SETENVVAR("GNUSTEP_NETWORK_ROOT", currentWorkingDir);
     115             :         SETENVVAR("GNUSTEP_USERS_ROOT", currentWorkingDir);
     116             :         SETENVVAR("HOMEPATH", currentWorkingDir);
     117             :         
     118             :         SetCurrentDirectory(currentWorkingDir);
     119             :         
     120             :         free(envVarString);
     121             :         free(finalPath);
     122             : 
     123             :         /*      Windows amibtiously starts apps with the C library locale set to the
     124             :                 system locale rather than the "C" locale as per spec. Fixing here so
     125             :                 numbers don't behave strangely.
     126             :         */
     127             :         setlocale(LC_ALL, "C");
     128             : 
     129             : #else // Linux
     130             :         #define OO_SHOW_MSG(ooMsg, ooTitle, ooFlags)    fprintf(stdout, "%s", ooMsg)
     131             :         #define TABS1   "\t\t"
     132             :         #define TABS2   "\t\t\t"
     133             :         #define TABS3   "\t\t\t\t"
     134             :         #define TABS4   "\t"
     135             : #endif
     136             : 
     137             :         // Need this because we're not using the default run loop's autorelease
     138             :         // pool.
     139             :         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     140             :         OOLoggingInit();
     141             :         
     142             :         @try
     143             :         {
     144             :                 // dajt: allocate and set the NSApplication delegate manually because not
     145             :                 // using NIB to do this
     146             :                 controller = [[GameController alloc] init];
     147             :                 
     148             :                 for (i = 1; i < argc; i++)
     149             :                 {
     150             :                         if (strcmp("-load", argv[i]) == 0)
     151             :                         {
     152             :                                 i++;
     153             :                                 if (i < argc)
     154             :                                         [controller setPlayerFileToLoad: [NSString stringWithCString: argv[i]]];
     155             :                         }
     156             : 
     157             :                         if (!strcmp("-help", argv[i]) || !strcmp("--help", argv[i]))
     158             :                         {
     159             :                                 char const *processName = [[[NSProcessInfo processInfo] processName] UTF8String];
     160             :                                 char s[2048];
     161             :                                 snprintf(s, sizeof(s), "Usage: %s [options]\n\n"
     162             :                                                         "Options can be any of the following: \n\n"
     163             :                                                         "--compile-sysdesc"TABS2"Compile system descriptions *\n"
     164             :                                                         "--export-sysdesc"TABS2"Export system descriptions *\n"
     165             : #if OOLITE_WINDOWS
     166             :                                                         "-hdr"TABS3"Start up in HDR mode\n"
     167             : #endif
     168             :                                                         "-load [filepath]"TABS2"Load commander from [filepath]\n"
     169             :                                                         "-message [messageString]"TABS1"Display [messageString] at startup\n"
     170             :                                                         "-nodust    "TABS2 TABS4"Do not draw space dust\n"
     171             :                                                         "-noshaders"TABS2 TABS4"Start up with shaders disabled\n"
     172             :                                                         "-nosplash    "TABS2 TABS4"Force disable splash screen on startup\n"
     173             :                                                         "-nosound    "TABS2 TABS4"Start up with sound disabled\n"
     174             :                                                         "-novsync"TABS3"Force disable V-Sync\n"
     175             :                                                         "--openstep"TABS2 TABS4"When compiling or exporting\n"TABS3 TABS4"system descriptions, use openstep\n"TABS3 TABS4"format *\n"
     176             :                                                         "-showversion"TABS2 TABS4"Display version at startup screen\n"
     177             :                                                         "-splash"TABS3 TABS4"Force splash screen on startup\n"
     178             :                                                         "-verify-oxp [filepath]    "TABS1"Verify OXP at [filepath] *\n"
     179             :                                                         "--xml"TABS3 TABS4"When compiling or exporting\n"TABS3 TABS4"system descriptions, use xml\n"TABS3 TABS4"format *\n"
     180             :                                                         "\n"
     181             :                                                         "Options marked with \"*\" are available only in Test Release configuration.\n\n", processName);
     182             :                                 OO_SHOW_MSG(s, processName, MB_OK);
     183             :                                 OOLog(@"process.args", @"%s option detected, exiting after help page has been displayed.", argv[i]);
     184             :                                 return 0;
     185             :                         }
     186             :                 }
     187             :                 
     188             :                 // Release anything allocated during the controller initialisation that
     189             :                 // is no longer required.
     190             :                 DESTROY(pool);
     191             :                 
     192             :                 // Call applicationDidFinishLaunching because NSApp is not running in
     193             :                 // GNUstep port.
     194             :                 [controller applicationDidFinishLaunching: nil];
     195             :         }
     196             :         @catch (NSException *exception)
     197             :         {
     198             :                 OOLogERR(kOOLogException, @"Root exception handler hit - terminating. This is an internal error, please report it. Exception name: %@, reason: %@", [exception name], [exception reason]);
     199             :                 return EXIT_FAILURE;
     200             :         }
     201             : #endif
     202             : 
     203             :         // never reached
     204             :         return 0;
     205             : }

Generated by: LCOV version 1.14