main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. // Portions Copyright (c) 2014 James S Urquhart
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "platformEmscripten/platformEmscripten.h"
  24. #include "platform/threads/thread.h"
  25. #include "game/gameInterface.h"
  26. #include "io/fileObject.h"
  27. #include "game/version.h"
  28. #ifndef DUMMY_PLATFORM
  29. #include <emscripten/emscripten.h>
  30. #endif
  31. //------------------------------------------------------------------------------
  32. S32 gLastStart = 0;
  33. EmscriptenPlatState gPlatState;
  34. bool appIsRunning = true;
  35. int _EmscriptenRunTorqueMain()
  36. {
  37. gPlatState.firstThreadId = ThreadManager::getCurrentThreadId();
  38. printf("performing mainInit()\n");
  39. gPlatState.lastTimeTick = Platform::getRealMilliseconds();
  40. if(!Game->mainInitialize(gPlatState.argc, gPlatState.argv))
  41. {
  42. return 0;
  43. }
  44. return 1;
  45. }
  46. void _EmscriptenGameInnerLoop()
  47. {
  48. if (!appIsRunning)
  49. {
  50. return;
  51. }
  52. else if (Game->isRunning())
  53. {
  54. S32 start = Platform::getRealMilliseconds();
  55. Game->mainLoop();
  56. gLastStart = start;
  57. }
  58. else
  59. {
  60. Game->mainShutdown();
  61. // Need to actually exit the application now
  62. exit(0);
  63. }
  64. }
  65. void _EmscriptenGameResignActive()
  66. {
  67. if ( Con::isFunction("onEmscriptenResignActive") )
  68. Con::executef( 1, "onEmscriptenResignActive" );
  69. appIsRunning = false;
  70. }
  71. void _EmscriptenGameBecomeActive()
  72. {
  73. if ( Con::isFunction("onEmscriptenBecomeActive") )
  74. Con::executef( 1, "onEmscriptenBecomeActive" );
  75. appIsRunning = true;
  76. }
  77. void _EmscriptenGameWillTerminate()
  78. {
  79. if ( Con::isFunction("onEmscriptenWillTerminate") )
  80. Con::executef( 1, "onEmscriptenWillTerminate" );
  81. Con::executef( 1, "onExit" );
  82. Game->mainShutdown();
  83. }
  84. static void _EmscriptenGetTxtFileArgs(int &argc, char** argv, int maxargc)
  85. {
  86. argc = 0;
  87. const U32 kMaxTextLen = 2048;
  88. U32 textLen;
  89. char* text = new char[kMaxTextLen];
  90. // Open the file, kick out if we can't
  91. File cmdfile;
  92. File::Status err = cmdfile.open("EmscriptenCmdLine.txt", cmdfile.Read);
  93. // Re-organise function to handle memory deletion better
  94. if (err == File::Ok)
  95. {
  96. // read in the first kMaxTextLen bytes, kick out if we get errors or no data
  97. err = cmdfile.read(kMaxTextLen-1, text, &textLen);
  98. if (((err == File::Ok || err == File::EOS) || textLen > 0))
  99. {
  100. // Null terminate
  101. text[textLen++] = '\0';
  102. // Truncate to the 1st line of the file
  103. for(int i = 0; i < textLen; i++)
  104. {
  105. if( text[i] == '\n' || text[i] == '\r' )
  106. {
  107. text[i] = '\0';
  108. textLen = i+1;
  109. break;
  110. }
  111. }
  112. // Tokenize the args with nulls, save them in argv, count them in argc
  113. char* tok;
  114. for(tok = dStrtok(text, " "); tok && argc < maxargc; tok = dStrtok(NULL, " "))
  115. argv[argc++] = tok;
  116. }
  117. }
  118. // Close file and delete memory before returning
  119. cmdfile.close();
  120. delete[] text;
  121. text = NULL;
  122. }
  123. int main(int argc, char **argv)
  124. {
  125. // get the actual command line args
  126. S32 newArgc = argc;
  127. const char* newArgv[10];
  128. for(int i=0; i < argc && i < 10; i++)
  129. newArgv[i] = argv[i];
  130. // get the text file args
  131. S32 textArgc;
  132. char* textArgv[10];
  133. _EmscriptenGetTxtFileArgs(textArgc, textArgv, 10);
  134. // merge them
  135. int i=0;
  136. while(i < textArgc && newArgc < 10)
  137. newArgv[newArgc++] = textArgv[i++];
  138. // store them in platState
  139. gPlatState.argc = newArgc;
  140. gPlatState.argv = newArgv;
  141. if (_EmscriptenRunTorqueMain() > 0)
  142. {
  143. #ifndef DUMMY_PLATFORM
  144. emscripten_set_main_loop(_EmscriptenGameInnerLoop, 60, false);
  145. #endif
  146. return 0;
  147. }
  148. else
  149. {
  150. return 1;
  151. }
  152. }