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