main.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifdef TORQUE_SHARED
  23. #ifdef WIN32
  24. #include <windows.h>
  25. #include <stdio.h>
  26. extern "C"
  27. {
  28. int (*torque_winmain)( HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow) = NULL;
  29. };
  30. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCommandShow)
  31. {
  32. char filename[4096];
  33. char gameLib[4096];
  34. GetModuleFileNameA(NULL, filename, 4096);
  35. filename[strlen(filename)-4] = 0;
  36. sprintf(gameLib, "%s.dll", filename);
  37. HMODULE hGame = LoadLibraryA(gameLib);
  38. if (hGame)
  39. torque_winmain = (int (*)(HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow))GetProcAddress(hGame, "torque_winmain");
  40. char error[4096];
  41. if (!hGame)
  42. {
  43. sprintf(error, "Unable to load game library: %s. Please make sure it exists and the latest DirectX is installed.", gameLib);
  44. MessageBoxA(NULL, error, "Error", MB_OK|MB_ICONWARNING);
  45. return -1;
  46. }
  47. if (!torque_winmain)
  48. {
  49. sprintf(error, "Missing torque_winmain export in game library: %s. Please make sure that it exists and the latest DirectX is installed.", gameLib);
  50. MessageBoxA(NULL, error, "Error", MB_OK|MB_ICONWARNING);
  51. return -1;
  52. }
  53. int ret = torque_winmain(hInstance, hPrevInstance, lpszCmdLine, nCommandShow );
  54. FreeLibrary(hGame);
  55. return ret;
  56. }
  57. #endif // WIN32
  58. #ifdef __MACOSX__
  59. #include <dlfcn.h>
  60. #include <stdio.h>
  61. #include <unistd.h>
  62. #include <Carbon/Carbon.h>
  63. extern "C" {
  64. int (*torque_macmain)(int argc, const char **argv) = 0;
  65. }
  66. void GetBasePath(const char** cpath, const char** cname)
  67. {
  68. static char path[2049];
  69. static char name[2049];
  70. ProcessSerialNumber PSN;
  71. ProcessInfoRec pinfo;
  72. FSSpec pspec;
  73. FSRef fsr;
  74. OSStatus err;
  75. path[0] = 0;
  76. name[0] = 0;
  77. *cpath = path;
  78. *cname = name;
  79. // set up process serial number
  80. PSN.highLongOfPSN = 0;
  81. PSN.lowLongOfPSN = kCurrentProcess;
  82. // set up info block
  83. pinfo.processInfoLength = sizeof(pinfo);
  84. pinfo.processName = NULL;
  85. pinfo.processAppSpec = &pspec;
  86. // grab the vrefnum and directory
  87. err = GetProcessInformation(&PSN, &pinfo);
  88. if (! err ) {
  89. FSSpec fss2;
  90. strcpy(name, &pspec.name[1]);
  91. err = FSMakeFSSpec(pspec.vRefNum, pspec.parID, 0, &fss2);
  92. if ( ! err ) {
  93. err = FSpMakeFSRef(&fss2, &fsr);
  94. if ( ! err ) {
  95. err = (OSErr)FSRefMakePath(&fsr, (UInt8*)path, 2048);
  96. }
  97. }
  98. }
  99. }
  100. int main(int argc, const char **argv)
  101. {
  102. void *gameBundle = 0;
  103. char gameBundleFilename[2049];
  104. const char* basePath;
  105. const char* appName;
  106. // Get the path to our app binary and the app name
  107. GetBasePath(&basePath, &appName);
  108. if (!basePath[0] || !appName[0])
  109. return;
  110. char appNameNoDebug[2049];
  111. strcpy(appNameNoDebug, appName);
  112. int i = strlen(appName);
  113. while (i > 0)
  114. {
  115. if (!strcmp(&appName[i], "_DEBUG"))
  116. {
  117. appNameNoDebug[i] = 0;
  118. break;
  119. }
  120. i--;
  121. }
  122. sprintf(gameBundleFilename, "%s.app/Contents/Frameworks/%s Bundle.bundle/Contents/MacOS/%s Bundle", appName, appNameNoDebug, appNameNoDebug);
  123. // first see if the current directory is set properly
  124. gameBundle = dlopen(gameBundleFilename, RTLD_LAZY | RTLD_LOCAL);
  125. if (!gameBundle)
  126. {
  127. // Couldn't load the game bundle... so, using the path to the bundle binary fix up the cwd
  128. if (basePath[0]) {
  129. chdir( basePath );
  130. chdir( "../../../" );
  131. }
  132. // and try again
  133. gameBundle = dlopen( gameBundleFilename, RTLD_LAZY | RTLD_LOCAL);
  134. }
  135. if (!gameBundle)
  136. return -1;
  137. torque_macmain = (int (*)(int argc, const char **argv)) dlsym(gameBundle, "torque_macmain");
  138. if (!torque_macmain)
  139. return -1;
  140. return torque_macmain(argc, argv);
  141. }
  142. #endif // __MACOSX
  143. #ifdef __linux__
  144. #include <dlfcn.h>
  145. #include <stdio.h>
  146. #include <unistd.h>
  147. #include <string.h>
  148. extern "C"
  149. {
  150. int (*torque_unixmain)(int argc, const char **argv) = NULL;
  151. void(*setExePathName)(const char *exePathName) = NULL;
  152. }
  153. int main(int argc, const char **argv)
  154. {
  155. // assume bin name is in argv[0]
  156. int len = strlen(argv[0]);
  157. char *libName = new char[len+4]; // len + .so + NUL
  158. strcpy(libName, argv[0]);
  159. strcat(libName, ".so");
  160. // try to load the game lib
  161. void *gameLib = dlopen(libName, RTLD_LAZY | RTLD_LOCAL);
  162. delete [] libName;
  163. if(gameLib == NULL)
  164. {
  165. printf("%s\n", dlerror());
  166. return -1;
  167. }
  168. // set the filename of the exe image
  169. setExePathName = (void(*)(const char *)) dlsym(gameLib, "setExePathName");
  170. if(setExePathName == NULL)
  171. {
  172. printf("%s\n", dlerror());
  173. return -1;
  174. }
  175. setExePathName(argv[0]);
  176. // try to load the lib entry point
  177. torque_unixmain = (int(*)(int argc, const char **argv)) dlsym(gameLib, "torque_unixmain");
  178. if(torque_unixmain == NULL)
  179. {
  180. printf("%s\n", dlerror());
  181. return -1;
  182. }
  183. // Go!
  184. return torque_unixmain(argc, argv);
  185. }
  186. #endif // __linux__
  187. #else //static exe build
  188. #include "platform/platform.h"
  189. #include "app/mainLoop.h"
  190. #include "T3D/gameFunctions.h"
  191. // Entry point for your game.
  192. //
  193. // This is build by default using the "StandardMainLoop" toolkit. Feel free
  194. // to bring code over directly as you need to modify or extend things. You
  195. // will need to merge against future changes to the SML code if you do this.
  196. S32 TorqueMain(S32 argc, const char **argv)
  197. {
  198. // Some handy debugging code:
  199. // if (argc == 1) {
  200. // static const char* argvFake[] = { "dtest.exe", "-jload", "test.jrn" };
  201. // argc = 3;
  202. // argv = argvFake;
  203. // }
  204. // Memory::enableLogging("testMem.log");
  205. // Memory::setBreakAlloc(104717);
  206. // Initialize the subsystems.
  207. StandardMainLoop::init();
  208. // Handle any command line args.
  209. if(!StandardMainLoop::handleCommandLine(argc, argv))
  210. {
  211. Platform::AlertOK("Error", "Failed to initialize game, shutting down.");
  212. return 1;
  213. }
  214. // Main loop
  215. while(StandardMainLoop::doMainLoop());
  216. // Clean everything up.
  217. StandardMainLoop::shutdown();
  218. // Do we need to restart?
  219. if( StandardMainLoop::requiresRestart() )
  220. Platform::restartInstance();
  221. // Return.
  222. return 0;
  223. }
  224. #endif //TORQUE_SHARED