Oolite 1.91.0.7712-251017-85ce217
Loading...
Searching...
No Matches
main.m
Go to the documentation of this file.
1/*
2
3main.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 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
44GameController* controller;
45#endif
46
47
48#ifndef NDEBUG
49uint32_t gDebugFlags = 0;
50#endif
51
62int 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];
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"
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}
NSUInteger gDebugFlags
Definition main.m:7
#define OOLITE_WINDOWS
Definition OOCocoa.h:247
#define DESTROY(x)
Definition OOCocoa.h:77
#define OOLogERR(class, format,...)
Definition OOLogging.h:112
NSString *const kOOLogException
Definition OOLogging.m:651
#define OOLog(class, format,...)
Definition OOLogging.h:88
void OOLoggingInit(void)
Definition OOLogging.m:585
int main(int argc, const char *argv[])
Definition main.m:18
void applicationDidFinishLaunching:(NSNotification *notification)
void setPlayerFileToLoad:(NSString *filename)