main.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <libgen.h> // for dirname
  95. #include <filesystem>
  96. #include <mach-o/dyld.h> // for _NSGetExecutablePath
  97. #include <Carbon/Carbon.h>
  98. extern "C" {
  99. int (*torque_macmain)(int argc, const char **argv) = 0;
  100. }
  101. int main(int argc, const char **argv)
  102. {
  103. char path[PATH_MAX];
  104. uint32_t pathLen = sizeof(path);
  105. int err = _NSGetExecutablePath(path, &pathLen);
  106. char* executableDirectory = dirname(path);
  107. // Once the executable directory is determined, we search two possibilities: The frameworks and next to the app bundle
  108. chdir(executableDirectory);
  109. chdir("../Frameworks");
  110. void *gameLib = dlopen("libTorqueEngine.dylib", RTLD_LAZY | RTLD_LOCAL);
  111. if (!gameLib)
  112. {
  113. return -1;
  114. }
  115. torque_macmain = (int (*)(int argc, const char **argv)) dlsym(gameLib, "torque_macmain");
  116. if (!torque_macmain)
  117. return -2;
  118. return torque_macmain(argc, argv);
  119. }
  120. #endif // __MACOSX
  121. #ifdef __linux__
  122. #include <dlfcn.h>
  123. #include <stdio.h>
  124. #include <unistd.h>
  125. #include <string.h>
  126. extern "C"
  127. {
  128. int (*torque_unixmain)(int argc, const char **argv) = NULL;
  129. void(*setExePathName)(const char *exePathName) = NULL;
  130. }
  131. int main(int argc, const char **argv)
  132. {
  133. // assume bin name is in argv[0]
  134. int len = strlen(argv[0]);
  135. char *libName = new char[len+4]; // len + .so + NUL
  136. strcpy(libName, argv[0]);
  137. strcat(libName, ".so");
  138. // try to load the game lib
  139. void *gameLib = dlopen(libName, RTLD_LAZY | RTLD_LOCAL);
  140. delete [] libName;
  141. if(gameLib == NULL)
  142. {
  143. printf("%s\n", dlerror());
  144. return -1;
  145. }
  146. // set the filename of the exe image
  147. setExePathName = (void(*)(const char *)) dlsym(gameLib, "setExePathName");
  148. if(setExePathName == NULL)
  149. {
  150. printf("%s\n", dlerror());
  151. return -1;
  152. }
  153. setExePathName(argv[0]);
  154. // try to load the lib entry point
  155. torque_unixmain = (int(*)(int argc, const char **argv)) dlsym(gameLib, "torque_unixmain");
  156. if(torque_unixmain == NULL)
  157. {
  158. printf("%s\n", dlerror());
  159. return -1;
  160. }
  161. // Go!
  162. return torque_unixmain(argc, argv);
  163. }
  164. #endif // __linux__
  165. #else //static exe build
  166. #include "platform/platform.h"
  167. #include "app/mainLoop.h"
  168. #include "T3D/gameFunctions.h"
  169. #if defined(WIN32) || defined(_WIN32)
  170. //tell switchable graphics supported systems that they need to use the beefier GPU
  171. #include <windows.h>
  172. extern "C" { __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; }
  173. extern "C" { __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001; }
  174. #else
  175. extern "C" { int NvOptimusEnablement = 1; }
  176. extern "C" { int AmdPowerXpressRequestHighPerformance = 1; }
  177. #endif
  178. // Entry point for your game.
  179. //
  180. // This is build by default using the "StandardMainLoop" toolkit. Feel free
  181. // to bring code over directly as you need to modify or extend things. You
  182. // will need to merge against future changes to the SML code if you do this.
  183. S32 TorqueMain(S32 argc, const char **argv)
  184. {
  185. // Some handy debugging code:
  186. // if (argc == 1) {
  187. // static const char* argvFake[] = { "dtest.exe", "-jload", "test.jrn" };
  188. // argc = 3;
  189. // argv = argvFake;
  190. // }
  191. // Memory::enableLogging("testMem.log");
  192. // Memory::setBreakAlloc(104717);
  193. // Initialize the subsystems.
  194. StandardMainLoop::init();
  195. // Handle any command line args.
  196. if(!StandardMainLoop::handleCommandLine(argc, argv))
  197. {
  198. Platform::AlertOK("Error", "Failed to initialize game, shutting down.");
  199. return 1;
  200. }
  201. // Main loop
  202. while(StandardMainLoop::doMainLoop());
  203. // Clean everything up.
  204. StandardMainLoop::shutdown();
  205. // Do we need to restart?
  206. if( StandardMainLoop::requiresRestart() )
  207. Platform::restartInstance();
  208. // Return.
  209. return StandardMainLoop::getReturnStatus();
  210. }
  211. #endif //TORQUE_SHARED