main.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <string>
  26. extern "C"
  27. {
  28. int (*torque_winmain)( HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow) = NULL;
  29. };
  30. bool getDllName(std::wstring& dllName)
  31. {
  32. wchar_t filenameBuf[MAX_PATH];
  33. DWORD length = GetModuleFileNameW( NULL, filenameBuf, MAX_PATH );
  34. if(length == 0) return false;
  35. dllName = std::wstring(filenameBuf);
  36. size_t dotPos = dllName.find_last_of(L".");
  37. if(dotPos == std::wstring::npos) return false;
  38. dllName.erase(dotPos);
  39. dllName += L".dll";
  40. return true;
  41. }
  42. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCommandShow)
  43. {
  44. std::wstring dllName = std::wstring();
  45. if(!getDllName(dllName))
  46. {
  47. MessageBoxW(NULL, L"Unable to find game dll", L"Error", MB_OK|MB_ICONWARNING);
  48. return -1;
  49. }
  50. HMODULE hGame = LoadLibraryW(dllName.c_str());
  51. if (!hGame)
  52. {
  53. wchar_t error[4096];
  54. _swprintf_l(error, sizeof(error), L"Unable to load game library: %s. Please make sure it exists and the latest DirectX is installed.", _get_current_locale(), dllName.c_str());
  55. MessageBoxW(NULL, error, L"Error", MB_OK|MB_ICONWARNING);
  56. return -1;
  57. }
  58. torque_winmain = (int (*)(HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow))GetProcAddress(hGame, "torque_winmain");
  59. if (!torque_winmain)
  60. {
  61. wchar_t error[4096];
  62. _swprintf_l(error, sizeof(error), 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());
  63. MessageBoxW(NULL, error, L"Error", MB_OK|MB_ICONWARNING);
  64. return -1;
  65. }
  66. int ret = torque_winmain(hInstance, hPrevInstance, lpszCmdLine, nCommandShow);
  67. FreeLibrary(hGame);
  68. return ret;
  69. }
  70. #endif // WIN32
  71. #ifdef __MACOSX__
  72. #include <dlfcn.h>
  73. #include <stdio.h>
  74. #include <unistd.h>
  75. #include <Carbon/Carbon.h>
  76. extern "C" {
  77. int (*torque_macmain)(int argc, const char **argv) = 0;
  78. }
  79. void GetBasePath(const char** cpath, const char** cname)
  80. {
  81. static char path[2049];
  82. static char name[2049];
  83. ProcessSerialNumber PSN;
  84. ProcessInfoRec pinfo;
  85. FSSpec pspec;
  86. FSRef fsr;
  87. OSStatus err;
  88. path[0] = 0;
  89. name[0] = 0;
  90. *cpath = path;
  91. *cname = name;
  92. // set up process serial number
  93. PSN.highLongOfPSN = 0;
  94. PSN.lowLongOfPSN = kCurrentProcess;
  95. // set up info block
  96. pinfo.processInfoLength = sizeof(pinfo);
  97. pinfo.processName = NULL;
  98. pinfo.processAppSpec = &pspec;
  99. // grab the vrefnum and directory
  100. err = GetProcessInformation(&PSN, &pinfo);
  101. if (! err ) {
  102. FSSpec fss2;
  103. strcpy(name, &pspec.name[1]);
  104. err = FSMakeFSSpec(pspec.vRefNum, pspec.parID, 0, &fss2);
  105. if ( ! err ) {
  106. err = FSpMakeFSRef(&fss2, &fsr);
  107. if ( ! err ) {
  108. err = (OSErr)FSRefMakePath(&fsr, (UInt8*)path, 2048);
  109. }
  110. }
  111. }
  112. }
  113. int main(int argc, const char **argv)
  114. {
  115. void *gameBundle = 0;
  116. char gameBundleFilename[2049];
  117. const char* basePath;
  118. const char* appName;
  119. // Get the path to our app binary and the app name
  120. GetBasePath(&basePath, &appName);
  121. if (!basePath[0] || !appName[0])
  122. return;
  123. char appNameNoDebug[2049];
  124. strcpy(appNameNoDebug, appName);
  125. int i = strlen(appName);
  126. while (i > 0)
  127. {
  128. if (!strcmp(&appName[i], "_DEBUG"))
  129. {
  130. appNameNoDebug[i] = 0;
  131. break;
  132. }
  133. i--;
  134. }
  135. sprintf(gameBundleFilename, "%s.app/Contents/Frameworks/%s Bundle.bundle/Contents/MacOS/%s Bundle", appName, appNameNoDebug, appNameNoDebug);
  136. // first see if the current directory is set properly
  137. gameBundle = dlopen(gameBundleFilename, RTLD_LAZY | RTLD_LOCAL);
  138. if (!gameBundle)
  139. {
  140. // Couldn't load the game bundle... so, using the path to the bundle binary fix up the cwd
  141. if (basePath[0]) {
  142. chdir( basePath );
  143. chdir( "../../../" );
  144. }
  145. // and try again
  146. gameBundle = dlopen( gameBundleFilename, RTLD_LAZY | RTLD_LOCAL);
  147. }
  148. if (!gameBundle)
  149. return -1;
  150. torque_macmain = (int (*)(int argc, const char **argv)) dlsym(gameBundle, "torque_macmain");
  151. if (!torque_macmain)
  152. return -1;
  153. return torque_macmain(argc, argv);
  154. }
  155. #endif // __MACOSX
  156. #ifdef __linux__
  157. #include <dlfcn.h>
  158. #include <stdio.h>
  159. #include <unistd.h>
  160. #include <string.h>
  161. extern "C"
  162. {
  163. int (*torque_unixmain)(int argc, const char **argv) = NULL;
  164. void(*setExePathName)(const char *exePathName) = NULL;
  165. }
  166. int main(int argc, const char **argv)
  167. {
  168. // assume bin name is in argv[0]
  169. int len = strlen(argv[0]);
  170. char *libName = new char[len+4]; // len + .so + NUL
  171. strcpy(libName, argv[0]);
  172. strcat(libName, ".so");
  173. // try to load the game lib
  174. void *gameLib = dlopen(libName, RTLD_LAZY | RTLD_LOCAL);
  175. delete [] libName;
  176. if(gameLib == NULL)
  177. {
  178. printf("%s\n", dlerror());
  179. return -1;
  180. }
  181. // set the filename of the exe image
  182. setExePathName = (void(*)(const char *)) dlsym(gameLib, "setExePathName");
  183. if(setExePathName == NULL)
  184. {
  185. printf("%s\n", dlerror());
  186. return -1;
  187. }
  188. setExePathName(argv[0]);
  189. // try to load the lib entry point
  190. torque_unixmain = (int(*)(int argc, const char **argv)) dlsym(gameLib, "torque_unixmain");
  191. if(torque_unixmain == NULL)
  192. {
  193. printf("%s\n", dlerror());
  194. return -1;
  195. }
  196. // Go!
  197. return torque_unixmain(argc, argv);
  198. }
  199. #endif // __linux__
  200. #else //static exe build
  201. #include "platform/platform.h"
  202. #include "app/mainLoop.h"
  203. #include "T3D/gameFunctions.h"
  204. // Entry point for your game.
  205. //
  206. // This is build by default using the "StandardMainLoop" toolkit. Feel free
  207. // to bring code over directly as you need to modify or extend things. You
  208. // will need to merge against future changes to the SML code if you do this.
  209. S32 TorqueMain(S32 argc, const char **argv)
  210. {
  211. // Some handy debugging code:
  212. // if (argc == 1) {
  213. // static const char* argvFake[] = { "dtest.exe", "-jload", "test.jrn" };
  214. // argc = 3;
  215. // argv = argvFake;
  216. // }
  217. // Memory::enableLogging("testMem.log");
  218. // Memory::setBreakAlloc(104717);
  219. // Initialize the subsystems.
  220. StandardMainLoop::init();
  221. // Handle any command line args.
  222. if(!StandardMainLoop::handleCommandLine(argc, argv))
  223. {
  224. Platform::AlertOK("Error", "Failed to initialize game, shutting down.");
  225. return 1;
  226. }
  227. // Main loop
  228. while(StandardMainLoop::doMainLoop());
  229. // Clean everything up.
  230. StandardMainLoop::shutdown();
  231. // Do we need to restart?
  232. if( StandardMainLoop::requiresRestart() )
  233. Platform::restartInstance();
  234. // Return.
  235. return 0;
  236. }
  237. #endif //TORQUE_SHARED