main.cpp 4.2 KB

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