Oolite 1.91.0.7745-260117-205bce7
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#if OO_GAME_DATA_TO_USER_FOLDER
117 SETENVVAR("HOMEPATH", strcat(SDL_getenv("LOCALAPPDATA"), "\\Oolite\\oolite.app"));
118#else
119 SETENVVAR("HOMEPATH", currentWorkingDir);
120#endif
121
122 SetCurrentDirectory(currentWorkingDir);
123
124 free(envVarString);
125 free(finalPath);
126
127 /* Windows amibtiously starts apps with the C library locale set to the
128 system locale rather than the "C" locale as per spec. Fixing here so
129 numbers don't behave strangely.
130 */
131 setlocale(LC_ALL, "C");
132
133#else // Linux
134 #define OO_SHOW_MSG(ooMsg, ooTitle, ooFlags) fprintf(stdout, "%s", ooMsg)
135 #define TABS1 "\t\t"
136 #define TABS2 "\t\t\t"
137 #define TABS3 "\t\t\t\t"
138 #define TABS4 "\t"
139#endif
140
141 // Need this because we're not using the default run loop's autorelease
142 // pool.
143 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
145
146 @try
147 {
148 // dajt: allocate and set the NSApplication delegate manually because not
149 // using NIB to do this
150 controller = [[GameController alloc] init];
151
152 for (i = 1; i < argc; i++)
153 {
154 if (strcmp("-load", argv[i]) == 0)
155 {
156 i++;
157 if (i < argc)
158 [controller setPlayerFileToLoad: [NSString stringWithCString: argv[i]]];
159 }
160
161 if (!strcmp("-help", argv[i]) || !strcmp("--help", argv[i]))
162 {
163 char const *processName = [[[NSProcessInfo processInfo] processName] UTF8String];
164 char s[2048];
165 snprintf(s, sizeof(s), "Usage: %s [options]\n\n"
166 "Options can be any of the following: \n\n"
167 "--compile-sysdesc"TABS2"Compile system descriptions *\n"
168 "--export-sysdesc"TABS2"Export system descriptions *\n"
170 "-hdr"TABS3"Start up in HDR mode\n"
171#endif
172 "-load [filepath]"TABS2"Load commander from [filepath]\n"
173 "-message [messageString]"TABS1"Display [messageString] at startup\n"
174 "-nodust "TABS2 TABS4"Do not draw space dust\n"
175 "-noshaders"TABS2 TABS4"Start up with shaders disabled\n"
176 "-nosplash "TABS2 TABS4"Force disable splash screen on startup\n"
177 "-nosound "TABS2 TABS4"Start up with sound disabled\n"
178 "-novsync"TABS3"Force disable V-Sync\n"
179 "--openstep"TABS2 TABS4"When compiling or exporting\n"TABS3 TABS4"system descriptions, use openstep\n"TABS3 TABS4"format *\n"
180 "-showversion"TABS2 TABS4"Display version at startup screen\n"
181 "-splash"TABS3 TABS4"Force splash screen on startup\n"
182 "-verify-oxp [filepath] "TABS1"Verify OXP at [filepath] *\n"
183 "--xml"TABS3 TABS4"When compiling or exporting\n"TABS3 TABS4"system descriptions, use xml\n"TABS3 TABS4"format *\n"
184 "\n"
185 "Options marked with \"*\" are available only in Test Release configuration.\n"
186 "Built with "
188 "Clang version " STRINGIFY(__clang_major__) "." STRINGIFY(__clang_minor__) "." STRINGIFY(__clang_patchlevel__)
189#else
190 "GCC version " STRINGIFY(__GNUC__) "." STRINGIFY(__GNUC_MINOR__) "." STRINGIFY(__GNUC_PATCHLEVEL__)
191#endif
192 "\n\n", processName
193 );
194 OO_SHOW_MSG(s, processName, MB_OK);
195 OOLog(@"process.args", @"%s option detected, exiting after help page has been displayed.", argv[i]);
196 return 0;
197 }
198 }
199
200 // Release anything allocated during the controller initialisation that
201 // is no longer required.
202 DESTROY(pool);
203
204 // Call applicationDidFinishLaunching because NSApp is not running in
205 // GNUstep port.
206 [controller applicationDidFinishLaunching: nil];
207 }
208 @catch (NSException *exception)
209 {
210 OOLogERR(kOOLogException, @"Root exception handler hit - terminating. This is an internal error, please report it. Exception name: %@, reason: %@", [exception name], [exception reason]);
211 return EXIT_FAILURE;
212 }
213#endif
214
215 // never reached
216 return 0;
217}
NSUInteger gDebugFlags
Definition main.m:7
#define OOLITE_WINDOWS
Definition OOCocoa.h:259
#define OOLITE_HAVE_CLANG
Definition OOCocoa.h:114
#define DESTROY(x)
Definition OOCocoa.h:85
#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)