main.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. //TODO: android
  32. int _AndroidRunTorqueMain()
  33. {
  34. /*
  35. UIApplication *app = [UIApplication sharedApplication];
  36. platState.viewController = viewController;
  37. platState.appID = appID;
  38. platState.firstThreadId = ThreadManager::getCurrentThreadId();
  39. platState.Window = Window;
  40. platState.application = app;
  41. // Hidden by default.
  42. platState.application.statusBarHidden = YES;
  43. printf("performing mainInit()\n");
  44. platState.lastTimeTick = Platform::getRealMilliseconds();
  45. if(!Game->mainInitialize(platState.argc, platState.argv))
  46. {
  47. return 0;
  48. }
  49. */
  50. }
  51. void _AndroidGameInnerLoop()
  52. {
  53. if (!appIsRunning)
  54. {
  55. return;
  56. }
  57. else if (Game->isRunning())
  58. {
  59. S32 start = Platform::getRealMilliseconds();
  60. Game->mainLoop();
  61. gLastStart = start;
  62. }
  63. else
  64. {
  65. Game->mainShutdown();
  66. // Need to actually exit the application now
  67. exit(0);
  68. }
  69. }
  70. void _AndroidGameResignActive()
  71. {
  72. if ( Con::isFunction("onAndroidResignActive") )
  73. Con::executef( 1, "onAndroidResignActive" );
  74. appIsRunning = false;
  75. }
  76. void _AndroidGameBecomeActive()
  77. {
  78. clearPendingMultitouchEvents( );
  79. if ( Con::isFunction("onAndroidBecomeActive") )
  80. Con::executef( 1, "onAndroidBecomeActive" );
  81. appIsRunning = true;
  82. }
  83. void _AndroidGameWillTerminate()
  84. {
  85. if ( Con::isFunction("onAndroidWillTerminate") )
  86. Con::executef( 1, "onAndroidWillTerminate" );
  87. Con::executef( 1, "onExit" );
  88. Game->mainShutdown();
  89. }
  90. // Store current orientation for easy access
  91. void _AndroidGameChangeOrientation(S32 newOrientation)
  92. {
  93. _AndroidGameSetCurrentOrientation(newOrientation);
  94. return;
  95. /*
  96. bool enableAutoOrientation = Con::getBoolVariable("$pref::Android::EnableOrientationRotation");
  97. int screenOrientation = Con::getIntVariable("$pref::Android::ScreenOrientation");
  98. bool allowOtherOrientation = Con::getBoolVariable("$pref::Android::EnableOtherOrientationRotation");
  99. // The rotation matching the project orientation must be allowed for any to occur
  100. if (enableAutoOrientation)
  101. {
  102. // Start "collecting animations"
  103. [UIView beginAnimations: nil context: nil];
  104. // If the project is designed for landscape or it allows landscape rotation
  105. if (screenOrientation == 0 || allowOtherOrientation)
  106. {
  107. if (newOrientation == UIDeviceOrientationLandscapeLeft)
  108. {
  109. platState.Window.transform = CGAffineTransformMakeRotation(M_PI*1.5);
  110. Con::executef(1, "onAndroidOrientationToLandscapeLeft");
  111. // Show animations
  112. [UIView commitAnimations];
  113. return;
  114. }
  115. if (newOrientation == UIDeviceOrientationLandscapeRight)
  116. {
  117. platState.Window.transform = CGAffineTransformMakeRotation(M_PI_2);
  118. Con::executef(1, "onAndroidOrientationToLandscapeRight");
  119. // Show animations
  120. [UIView commitAnimations];
  121. return;
  122. }
  123. }
  124. // If the project is designed for portrait or it allows portrait rotation
  125. if (screenOrientation == 1 || allowOtherOrientation)
  126. {
  127. if (newOrientation == UIDeviceOrientationPortrait)
  128. {
  129. platState.Window.transform = CGAffineTransformMakeRotation(M_PI*1.5);
  130. Con::executef(1, "onAndroidOrientationToPortrait");
  131. // Show animations
  132. [UIView commitAnimations];
  133. return;
  134. }
  135. if (newOrientation == UIDeviceOrientationPortraitUpsideDown)
  136. {
  137. platState.Window.transform = CGAffineTransformMakeRotation(M_PI_2);
  138. Con::executef(1, "onAndroidOrientationToPortraitUpsideDown");
  139. // Show animations
  140. [UIView commitAnimations];
  141. return;
  142. }
  143. }
  144. // Show animations
  145. [UIView commitAnimations];
  146. }*/
  147. }
  148. static void _AndroidGetTxtFileArgs(int &argc, char** argv, int maxargc)
  149. {
  150. argc = 0;
  151. const U32 kMaxTextLen = 2048;
  152. U32 textLen;
  153. char* text = new char[kMaxTextLen];
  154. // Open the file, kick out if we can't
  155. File cmdfile;
  156. File::Status err = cmdfile.open("AndroidCmdLine.txt", cmdfile.Read);
  157. // Re-organise function to handle memory deletion better
  158. if (err == File::Ok)
  159. {
  160. // read in the first kMaxTextLen bytes, kick out if we get errors or no data
  161. err = cmdfile.read(kMaxTextLen-1, text, &textLen);
  162. if (((err == File::Ok || err == File::EOS) || textLen > 0))
  163. {
  164. // Null terminate
  165. text[textLen++] = '\0';
  166. // Truncate to the 1st line of the file
  167. for(int i = 0; i < textLen; i++)
  168. {
  169. if( text[i] == '\n' || text[i] == '\r' )
  170. {
  171. text[i] = '\0';
  172. textLen = i+1;
  173. break;
  174. }
  175. }
  176. // Tokenize the args with nulls, save them in argv, count them in argc
  177. char* tok;
  178. for(tok = dStrtok(text, " "); tok && argc < maxargc; tok = dStrtok(NULL, " "))
  179. argv[argc++] = tok;
  180. }
  181. }
  182. // Close file and delete memory before returning
  183. cmdfile.close();
  184. delete[] text;
  185. text = NULL;
  186. }
  187. int main(int argc, char *argv[])
  188. {
  189. //TODO: android
  190. /*
  191. int kMaxCmdlineArgs = 32; //Arbitrary
  192. printf("Initial Command Line\n");
  193. for( int i = 0; i < argc; i++ )
  194. {
  195. printf("%i : %s", i, argv[i] );
  196. }
  197. NSString *nsStrVersion = [UIDevice currentDevice ].systemVersion;
  198. const char *strVersion = [nsStrVersion UTF8String ];
  199. platState.osVersion = dAtof( strVersion);
  200. // Find Main.cs .
  201. const char* cwd = Platform::getMainDotCsDir();
  202. // Change to the directory that contains main.cs
  203. Platform::setCurrentDirectory(cwd);
  204. // get the actual command line args
  205. S32 newArgc = argc;
  206. const char* newArgv[kMaxCmdlineArgs];
  207. for(int i=0; i < argc && i < kMaxCmdlineArgs; i++)
  208. newArgv[i] = argv[i];
  209. // get the text file args
  210. S32 textArgc;
  211. char* textArgv[kMaxCmdlineArgs];
  212. _AndroidGetTxtFileArgs(textArgc, textArgv, kMaxCmdlineArgs);
  213. // merge them
  214. int i=0;
  215. while(i < textArgc && newArgc < kMaxCmdlineArgs)
  216. newArgv[newArgc++] = textArgv[i++];
  217. // store them in platState
  218. platState.argc = newArgc;
  219. platState.argv = newArgv;
  220. printf("\nMerged Command Line\n");
  221. for( int i = 0; i < platState.argc; i++ )
  222. {
  223. printf("%i : %s", i, platState.argv[i] );
  224. }
  225. printf("\n");
  226. // now, we run UIApplication which calls back and starts thread or timer
  227. platState.appReturn = UIApplicationMain(argc, argv, nil, NSStringFromClass([T2DAppDelegate class]));
  228. printf("exiting...\n");
  229. return(platState.appReturn);
  230. */
  231. }