Public Functions
TorqueMain(S32 argc, const char ** argv)
1
2//-----------------------------------------------------------------------------
3// Copyright (c) 2012 GarageGames, LLC
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to
7// deal in the Software without restriction, including without limitation the
8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22//-----------------------------------------------------------------------------
23
24#ifdef TORQUE_SHARED
25
26#ifdef WIN32
27
28#include <windows.h>
29#include <string>
30
31extern "C"
32{
33 int (*torque_winmain)( HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow) = NULL;
34};
35
36bool getDllName(std::wstring& dllName, const std::wstring& suffix)
37{
38 wchar_t filenameBuf[MAX_PATH];
39 DWORD length = GetModuleFileNameW( NULL, filenameBuf, MAX_PATH );
40 if(length == 0) return false;
41 dllName = std::wstring(filenameBuf);
42 size_t dotPos = dllName.find_last_of(L".");
43 if(dotPos == std::wstring::npos)
44 {
45 dllName.clear();
46 return false;
47 }
48 dllName.erase(dotPos);
49 dllName += suffix + L".dll";
50 return true;
51}
52
53int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCommandShow)
54{
55 // Try to find the game DLL, which may have one of several file names.
56 HMODULE hGame = NULL;
57 std::wstring dllName = std::wstring();
58 // The file name is the same as this executable's name, plus a suffix.
59 const std::wstring dllSuffices[] = {L" DLL", L""};
60 const unsigned int numSuffices = sizeof(dllSuffices) / sizeof(std::wstring);
61
62 for (unsigned int i = 0; i < numSuffices; i++)
63 {
64 // Attempt to glue the suffix onto the current filename.
65 if(!getDllName(dllName, dllSuffices[i]))
66 continue;
67 // Load the DLL at that address.
68 hGame = LoadLibraryW(dllName.c_str());
69 if (hGame)
70 break;
71 }
72
73 if(!dllName.length())
74 {
75 MessageBoxW(NULL, L"Unable to find game dll", L"Error", MB_OK|MB_ICONWARNING);
76 return -1;
77 }
78
79 enum { errorSize = 4096 };
80 if (!hGame)
81 {
82 wchar_t error[errorSize];
83 _swprintf_l(error, errorSize, L"Unable to load game library: %s. Please make sure it exists and the latest DirectX is installed.", _get_current_locale(), dllName.c_str());
84 MessageBoxW(NULL, error, L"Error", MB_OK|MB_ICONWARNING);
85 return -1;
86 }
87
88 torque_winmain = (int (*)(HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow))GetProcAddress(hGame, "torque_winmain");
89 if (!torque_winmain)
90 {
91 wchar_t error[errorSize];
92 _swprintf_l(error, errorSize, L"Missing torque_winmain export in game library: %s. Please make sure that it exists and the latest DirectX is installed.", _get_current_locale(), dllName.c_str());
93 MessageBoxW(NULL, error, L"Error", MB_OK|MB_ICONWARNING);
94 return -1;
95 }
96
97 int ret = torque_winmain(hInstance, hPrevInstance, lpszCmdLine, nCommandShow);
98
99 FreeLibrary(hGame);
100 return ret;
101}
102#endif // WIN32
103
104
105#ifdef __MACOSX__
106
107#include <dlfcn.h>
108#include <stdio.h>
109#include <unistd.h>
110#include <Carbon/Carbon.h>
111
112extern "C" {
113
114 int (*torque_macmain)(int argc, const char **argv) = 0;
115
116}
117
118void GetBasePath(const char** cpath, const char** cname)
119{
120 static char path[2049];
121 static char name[2049];
122
123 ProcessSerialNumber PSN;
124 ProcessInfoRec pinfo;
125 FSSpec pspec;
126 FSRef fsr;
127 OSStatus err;
128
129 path[0] = 0;
130 name[0] = 0;
131
132 *cpath = path;
133 *cname = name;
134
135 // set up process serial number
136 PSN.highLongOfPSN = 0;
137 PSN.lowLongOfPSN = kCurrentProcess;
138
139 // set up info block
140 pinfo.processInfoLength = sizeof(pinfo);
141 pinfo.processName = NULL;
142 pinfo.processAppSpec = &pspec;
143
144 // grab the vrefnum and directory
145 err = GetProcessInformation(&PSN, &pinfo);
146 if (! err ) {
147
148 FSSpec fss2;
149
150 strcpy(name, &pspec.name[1]);
151
152 err = FSMakeFSSpec(pspec.vRefNum, pspec.parID, 0, &fss2);
153
154 if ( ! err ) {
155 err = FSpMakeFSRef(&fss2, &fsr);
156 if ( ! err ) {
157 err = (OSErr)FSRefMakePath(&fsr, (UInt8*)path, 2048);
158 }
159 }
160 }
161}
162
163int main(int argc, const char **argv)
164{
165 void *gameBundle = 0;
166 char gameBundleFilename[2049];
167
168 const char* basePath;
169 const char* appName;
170
171 // Get the path to our app binary and the app name
172
173 GetBasePath(&basePath, &appName);
174
175 if (!basePath[0] || !appName[0])
176 return;
177
178 char appNameNoDebug[2049];
179
180 strcpy(appNameNoDebug, appName);
181
182 int i = strlen(appName);
183 while (i > 0)
184 {
185 if (!strcmp(&appName[i], "_DEBUG"))
186 {
187 appNameNoDebug[i] = 0;
188 break;
189 }
190
191 i--;
192 }
193
194 sprintf(gameBundleFilename, "%s.app/Contents/Frameworks/%s Bundle.bundle/Contents/MacOS/%s Bundle", appName, appNameNoDebug, appNameNoDebug);
195
196 // first see if the current directory is set properly
197 gameBundle = dlopen(gameBundleFilename, RTLD_LAZY | RTLD_LOCAL);
198
199 if (!gameBundle)
200 {
201 // Couldn't load the game bundle... so, using the path to the bundle binary fix up the cwd
202
203 if (basePath[0]) {
204 chdir( basePath );
205 chdir( "../../../" );
206 }
207
208 // and try again
209 gameBundle = dlopen( gameBundleFilename, RTLD_LAZY | RTLD_LOCAL);
210 }
211
212 if (!gameBundle)
213 return -1;
214
215 torque_macmain = (int (*)(int argc, const char **argv)) dlsym(gameBundle, "torque_macmain");
216
217 if (!torque_macmain)
218 return -1;
219
220 return torque_macmain(argc, argv);
221}
222
223#endif // __MACOSX
224
225#ifdef __linux__
226
227#include <dlfcn.h>
228#include <stdio.h>
229#include <unistd.h>
230#include <string.h>
231
232extern "C"
233{
234 int (*torque_unixmain)(int argc, const char **argv) = NULL;
235 void(*setExePathName)(const char *exePathName) = NULL;
236}
237
238int main(int argc, const char **argv)
239{
240 // assume bin name is in argv[0]
241 int len = strlen(argv[0]);
242 char *libName = new char[len+4]; // len + .so + NUL
243
244 strcpy(libName, argv[0]);
245 strcat(libName, ".so");
246
247 // try to load the game lib
248 void *gameLib = dlopen(libName, RTLD_LAZY | RTLD_LOCAL);
249 delete [] libName;
250
251 if(gameLib == NULL)
252 {
253 printf("%s\n", dlerror());
254 return -1;
255 }
256
257 // set the filename of the exe image
258 setExePathName = (void(*)(const char *)) dlsym(gameLib, "setExePathName");
259 if(setExePathName == NULL)
260 {
261 printf("%s\n", dlerror());
262 return -1;
263 }
264 setExePathName(argv[0]);
265
266 // try to load the lib entry point
267 torque_unixmain = (int(*)(int argc, const char **argv)) dlsym(gameLib, "torque_unixmain");
268
269 if(torque_unixmain == NULL)
270 {
271 printf("%s\n", dlerror());
272 return -1;
273 }
274
275 // Go!
276 return torque_unixmain(argc, argv);
277}
278#endif // __linux__
279
280
281#else //static exe build
282
283#include "platform/platform.h"
284#include "app/mainLoop.h"
285#include "T3D/gameFunctions.h"
286
287#if defined(WIN32) || defined(_WIN32)
288//tell switchable graphics supported systems that they need to use the beefier GPU
289#include <windows.h>
290extern "C" { __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; }
291extern "C" { __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001; }
292#else
293extern "C" { int NvOptimusEnablement = 1; }
294extern "C" { int AmdPowerXpressRequestHighPerformance = 1; }
295#endif
296
297// Entry point for your game.
298//
299// This is build by default using the "StandardMainLoop" toolkit. Feel free
300// to bring code over directly as you need to modify or extend things. You
301// will need to merge against future changes to the SML code if you do this.
302S32 TorqueMain(S32 argc, const char **argv)
303{
304 // Some handy debugging code:
305 // if (argc == 1) {
306 // static const char* argvFake[] = { "dtest.exe", "-jload", "test.jrn" };
307 // argc = 3;
308 // argv = argvFake;
309 // }
310
311 // Memory::enableLogging("testMem.log");
312 // Memory::setBreakAlloc(104717);
313
314 // Initialize the subsystems.
315 StandardMainLoop::init();
316
317 // Handle any command line args.
318 if(!StandardMainLoop::handleCommandLine(argc, argv))
319 {
320 Platform::AlertOK("Error", "Failed to initialize game, shutting down.");
321
322 return 1;
323 }
324
325 // Main loop
326 while(StandardMainLoop::doMainLoop());
327
328 // Clean everything up.
329 StandardMainLoop::shutdown();
330
331 // Do we need to restart?
332 if( StandardMainLoop::requiresRestart() )
333 Platform::restartInstance();
334
335 // Return.
336 return StandardMainLoop::getReturnStatus();
337}
338
339#endif //TORQUE_SHARED
340