main.cpp 8.0 KB

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