main.cpp 9.0 KB

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