main.cpp 9.0 KB

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