main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #include "platformAndroid/platformAndroid.h"
  23. #include "platformAndroid/AndroidEvents.h"
  24. #include "platformAndroid/AndroidUtil.h"
  25. #include "platform/threads/thread.h"
  26. #include "game/gameInterface.h"
  27. #include "io/fileObject.h"
  28. extern void clearPendingMultitouchEvents( void );
  29. S32 gLastStart = 0;
  30. bool appIsRunning = true;
  31. int _AndroidRunTorqueMain(engine *eng)
  32. {
  33. platState.firstThreadId = ThreadManager::getCurrentThreadId();
  34. printf("performing mainInit()\n");
  35. platState.lastTimeTick = Platform::getRealMilliseconds();
  36. if(!Game->mainInitialize(platState.argc, platState.argv))
  37. {
  38. return 0;
  39. }
  40. return 1;
  41. }
  42. void _AndroidGameInnerLoop()
  43. {
  44. if (!appIsRunning)
  45. {
  46. return;
  47. }
  48. else if (Game->isRunning())
  49. {
  50. S32 start = Platform::getRealMilliseconds();
  51. Game->mainLoop();
  52. gLastStart = start;
  53. }
  54. else
  55. {
  56. Game->mainShutdown();
  57. // Need to actually exit the application now
  58. exit(0);
  59. }
  60. }
  61. void _AndroidGameResignActive()
  62. {
  63. if ( Con::isFunction("onAndroidResignActive") )
  64. Con::executef( 1, "onAndroidResignActive" );
  65. appIsRunning = false;
  66. }
  67. void _AndroidGameBecomeActive()
  68. {
  69. clearPendingMultitouchEvents( );
  70. if ( Con::isFunction("onAndroidBecomeActive") )
  71. Con::executef( 1, "onAndroidBecomeActive" );
  72. appIsRunning = true;
  73. }
  74. void _AndroidGameWillTerminate()
  75. {
  76. if ( Con::isFunction("onAndroidWillTerminate") )
  77. Con::executef( 1, "onAndroidWillTerminate" );
  78. Con::executef( 1, "onExit" );
  79. Game->mainShutdown();
  80. }
  81. static void _AndroidGetTxtFileArgs(int &argc, char** argv, int maxargc)
  82. {
  83. argc = 0;
  84. const U32 kMaxTextLen = 2048;
  85. U32 textLen;
  86. char* text = new char[kMaxTextLen];
  87. // Open the file, kick out if we can't
  88. File cmdfile;
  89. File::Status err = cmdfile.open("AndroidCmdLine.txt", cmdfile.Read);
  90. // Re-organise function to handle memory deletion better
  91. if (err == File::Ok)
  92. {
  93. // read in the first kMaxTextLen bytes, kick out if we get errors or no data
  94. err = cmdfile.read(kMaxTextLen-1, text, &textLen);
  95. if (((err == File::Ok || err == File::EOS) || textLen > 0))
  96. {
  97. // Null terminate
  98. text[textLen++] = '\0';
  99. // Truncate to the 1st line of the file
  100. for(int i = 0; i < textLen; i++)
  101. {
  102. if( text[i] == '\n' || text[i] == '\r' )
  103. {
  104. text[i] = '\0';
  105. textLen = i+1;
  106. break;
  107. }
  108. }
  109. // Tokenize the args with nulls, save them in argv, count them in argc
  110. char* tok;
  111. for(tok = dStrtok(text, " "); tok && argc < maxargc; tok = dStrtok(NULL, " "))
  112. argv[argc++] = tok;
  113. }
  114. }
  115. // Close file and delete memory before returning
  116. cmdfile.close();
  117. delete[] text;
  118. text = NULL;
  119. }