main.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #include <locale.h>
  27. extern "C"
  28. {
  29. int (*torque_winmain)( HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow) = NULL;
  30. };
  31. bool getDllName(std::wstring& dllName, const std::wstring& suffix)
  32. {
  33. wchar_t filenameBuf[MAX_PATH];
  34. DWORD length = GetModuleFileNameW( NULL, filenameBuf, MAX_PATH );
  35. if(length == 0) return false;
  36. dllName = std::wstring(filenameBuf);
  37. size_t dotPos = dllName.find_last_of(L".");
  38. if(dotPos == std::wstring::npos)
  39. {
  40. dllName.clear();
  41. return false;
  42. }
  43. dllName.erase(dotPos);
  44. dllName += suffix + L".dll";
  45. return true;
  46. }
  47. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCommandShow)
  48. {
  49. // Try to find the game DLL, which may have one of several file names.
  50. HMODULE hGame = NULL;
  51. std::wstring dllName = std::wstring();
  52. // The file name is the same as this executable's name, plus a suffix.
  53. const std::wstring dllSuffices[] = {L" DLL", L""};
  54. const unsigned int numSuffices = sizeof(dllSuffices) / sizeof(std::wstring);
  55. for (unsigned int i = 0; i < numSuffices; i++)
  56. {
  57. // Attempt to glue the suffix onto the current filename.
  58. if(!getDllName(dllName, dllSuffices[i]))
  59. continue;
  60. // Load the DLL at that address.
  61. hGame = LoadLibraryW(dllName.c_str());
  62. if (hGame)
  63. break;
  64. }
  65. if(!dllName.length())
  66. {
  67. MessageBoxW(NULL, L"Unable to find game dll", L"Error", MB_OK|MB_ICONWARNING);
  68. return -1;
  69. }
  70. enum { errorSize = 4096 };
  71. if (!hGame)
  72. {
  73. wchar_t error[errorSize];
  74. _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());
  75. MessageBoxW(NULL, error, L"Error", MB_OK|MB_ICONWARNING);
  76. return -1;
  77. }
  78. torque_winmain = (int (*)(HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow))GetProcAddress(hGame, "torque_winmain");
  79. if (!torque_winmain)
  80. {
  81. wchar_t error[errorSize];
  82. _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());
  83. MessageBoxW(NULL, error, L"Error", MB_OK|MB_ICONWARNING);
  84. return -1;
  85. }
  86. int ret = torque_winmain(hInstance, hPrevInstance, lpszCmdLine, nCommandShow);
  87. FreeLibrary(hGame);
  88. return ret;
  89. }
  90. #endif // WIN32
  91. #ifdef __MACOSX__
  92. #include <dlfcn.h>
  93. #include <stdio.h>
  94. #include <unistd.h>
  95. #include <libgen.h> // for dirname
  96. #include <filesystem>
  97. #include <mach-o/dyld.h> // for _NSGetExecutablePath
  98. #include <Carbon/Carbon.h>
  99. extern "C" {
  100. int (*torque_macmain)(int argc, const char **argv) = 0;
  101. }
  102. int main(int argc, const char **argv)
  103. {
  104. char path[PATH_MAX];
  105. uint32_t pathLen = sizeof(path);
  106. int err = _NSGetExecutablePath(path, &pathLen);
  107. char* executableDirectory = dirname(path);
  108. // Once the executable directory is determined, we search two possibilities: The frameworks and next to the app bundle
  109. chdir(executableDirectory);
  110. chdir("../Frameworks");
  111. void *gameLib = dlopen("libTorqueEngine.dylib", RTLD_LAZY | RTLD_LOCAL);
  112. if (!gameLib)
  113. {
  114. return -1;
  115. }
  116. torque_macmain = (int (*)(int argc, const char **argv)) dlsym(gameLib, "torque_macmain");
  117. if (!torque_macmain)
  118. return -2;
  119. return torque_macmain(argc, argv);
  120. }
  121. #endif // __MACOSX
  122. #ifdef __linux__
  123. #include <dlfcn.h>
  124. #include <stdio.h>
  125. #include <unistd.h>
  126. #include <string.h>
  127. extern "C"
  128. {
  129. int (*torque_unixmain)(int argc, const char **argv) = NULL;
  130. void(*setExePathName)(const char *exePathName) = NULL;
  131. }
  132. int main(int argc, const char **argv)
  133. {
  134. // assume bin name is in argv[0]
  135. int len = strlen(argv[0]);
  136. char *libName = new char[len+4]; // len + .so + NUL
  137. strcpy(libName, argv[0]);
  138. strcat(libName, ".so");
  139. // try to load the game lib
  140. void *gameLib = dlopen(libName, RTLD_LAZY | RTLD_LOCAL);
  141. delete [] libName;
  142. if(gameLib == NULL)
  143. {
  144. printf("%s\n", dlerror());
  145. return -1;
  146. }
  147. // set the filename of the exe image
  148. setExePathName = (void(*)(const char *)) dlsym(gameLib, "setExePathName");
  149. if(setExePathName == NULL)
  150. {
  151. printf("%s\n", dlerror());
  152. return -1;
  153. }
  154. setExePathName(argv[0]);
  155. // try to load the lib entry point
  156. torque_unixmain = (int(*)(int argc, const char **argv)) dlsym(gameLib, "torque_unixmain");
  157. if(torque_unixmain == NULL)
  158. {
  159. printf("%s\n", dlerror());
  160. return -1;
  161. }
  162. // Go!
  163. return torque_unixmain(argc, argv);
  164. }
  165. #endif // __linux__
  166. #else //static exe build
  167. #include "platform/platform.h"
  168. #include "app/mainLoop.h"
  169. #include "T3D/gameFunctions.h"
  170. #include "platform/platformMemory.h"
  171. #if defined(WIN32) || defined(_WIN32)
  172. //tell switchable graphics supported systems that they need to use the beefier GPU
  173. #include <windows.h>
  174. extern "C" { __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; }
  175. extern "C" { __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001; }
  176. #else
  177. extern "C" { int NvOptimusEnablement = 1; }
  178. extern "C" { int AmdPowerXpressRequestHighPerformance = 1; }
  179. #endif
  180. // Entry point for your game.
  181. //
  182. // This is build by default using the "StandardMainLoop" toolkit. Feel free
  183. // to bring code over directly as you need to modify or extend things. You
  184. // will need to merge against future changes to the SML code if you do this.
  185. S32 TorqueMain(S32 argc, const char **argv)
  186. {
  187. // Some handy debugging code:
  188. // if (argc == 1) {
  189. // static const char* argvFake[] = { "dtest.exe", "-jload", "test.jrn" };
  190. // argc = 3;
  191. // argv = argvFake;
  192. // }
  193. #if defined( TORQUE_ENABLE_ASSERTS ) && !defined(TORQUE_DISABLE_MEMORY_MANAGER)
  194. Memory::init();
  195. #endif
  196. // Initialize the subsystems.
  197. StandardMainLoop::init();
  198. // Handle any command line args.
  199. if(!StandardMainLoop::handleCommandLine(argc, argv))
  200. {
  201. Platform::AlertOK("Error", "Failed to initialize game, shutting down.");
  202. return 1;
  203. }
  204. // Main loop
  205. while(StandardMainLoop::doMainLoop());
  206. // Clean everything up.
  207. StandardMainLoop::shutdown();
  208. // Do we need to restart?
  209. if( StandardMainLoop::requiresRestart() )
  210. Platform::restartInstance();
  211. // Return.
  212. return StandardMainLoop::getReturnStatus();
  213. }
  214. #endif //TORQUE_SHARED