iOSWindow.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 "platformiOS/platformiOS.h"
  23. #include "platform/platformVideo.h"
  24. #include "platformiOS/iOSOGLVideo.h"
  25. #include "platformiOS/iOSConsole.h"
  26. #include "platform/platformInput.h"
  27. #include "game/gameInterface.h"
  28. #include "console/consoleTypes.h"
  29. #include "console/console.h"
  30. #include "platformiOS/iOSEvents.h"
  31. #include "platform/threads/thread.h"
  32. #include "platformiOS/iOSWindow.h"
  33. #import <OpenGLES/EAGLDrawable.h>
  34. #include "platformiOS/platformGL.h"
  35. #import <GLKit/GLKit.h>
  36. bool setScreenOrientation(bool, bool);
  37. bool getStatusBarHidden();
  38. bool setStatusBarHidden(bool);
  39. void setStatusBarType(S32);
  40. //------------------------------------------------------------------------------
  41. #pragma mark ---- PlatState ----
  42. iOSPlatState platState;
  43. iOSPlatState::iOSPlatState()
  44. {
  45. captureDisplay = true;
  46. fadeWindows = true;
  47. backgrounded = false;
  48. minimized = false;
  49. quit = false;
  50. portrait = true;//-Mat iOS is in portrait mode by default
  51. // start with something reasonable.
  52. desktopBitsPixel = IOS_DEFAULT_RESOLUTION_BIT_DEPTH;
  53. desktopWidth = IOS_DEFAULT_RESOLUTION_X;
  54. desktopHeight = IOS_DEFAULT_RESOLUTION_Y;
  55. fullscreen = true;
  56. osVersion = 0;
  57. dStrcpy(appWindowTitle, "iOS Torque Game Engine");
  58. // Semaphore for alerts. We put the app in a modal state by blocking the main
  59. alertSemaphore = Semaphore::createSemaphore(0);
  60. // directory that contains main.cs . This will help us detect whether we are
  61. // running with the scripts in the bundle or not.
  62. mainDotCsDir = NULL;
  63. mainLoopTimer = NULL;
  64. }
  65. //------------------------------------------------------------------------------
  66. // DGL, the Gui, and TS use this for various purposes.
  67. const Point2I &Platform::getWindowSize()
  68. {
  69. return platState.windowSize;
  70. }
  71. //------------------------------------------------------------------------------
  72. // save the window size, for DGL's use
  73. void Platform::setWindowSize(U32 newWidth, U32 newHeight)
  74. {
  75. platState.windowSize.set(newWidth, newHeight);
  76. }
  77. //------------------------------------------------------------------------------
  78. // Issue a minimize event. The standard handler will handle it.
  79. void Platform::minimizeWindow()
  80. {
  81. //no minimizing on iOS
  82. }
  83. void Platform::restoreWindow()
  84. {
  85. //no minimizing on iOS
  86. }
  87. //------------------------------------------------------------------------------
  88. void Platform::setWindowTitle(const char *title)
  89. {
  90. //no window titles on iOS
  91. }
  92. #pragma mark ---- Init funcs ----
  93. //------------------------------------------------------------------------------
  94. void Platform::init()
  95. {
  96. Con::setVariable("$platform", "iOS");
  97. if ([[UIScreen mainScreen] scale] == 2)
  98. Con::setBoolVariable("$pref::iOS::RetinaEnabled", true);
  99. else
  100. Con::setBoolVariable("$pref::iOS::RetinaEnabled", false);
  101. // Set the platform variable for the scripts
  102. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  103. {
  104. Con::setIntVariable("$pref::iOS::DeviceType", 1);
  105. }
  106. else
  107. {
  108. F32 screenHeight = [[UIScreen mainScreen] bounds].size.height;
  109. bool iPhone5 = (fabs((double)screenHeight - (double)568 ) < DBL_EPSILON);
  110. if (iPhone5)
  111. {
  112. Con::setIntVariable("$pref::iOS::DeviceType", 2);
  113. Con::setBoolVariable("$pref::iOS::RetinaEnabled", false);
  114. }
  115. else
  116. {
  117. Con::setIntVariable("$pref::iOS::DeviceType", 0);
  118. }
  119. }
  120. iOSConsole::create();
  121. //if ( !iOSConsole::isEnabled() )
  122. Input::init();
  123. // allow users to specify whether to capture the display or not when going fullscreen
  124. Con::addVariable("pref::mac::captureDisplay", TypeBool, &platState.captureDisplay);
  125. Con::addVariable("pref::mac::fadeWindows", TypeBool, &platState.fadeWindows);
  126. // create the opengl display device
  127. DisplayDevice *dev = NULL;
  128. Con::printf("Video Init:");
  129. Video::init();
  130. dev = OpenGLDevice::create();
  131. if (dev)
  132. Con::printf(" Accelerated OpenGL display device detected.");
  133. else
  134. Con::printf(" Accelerated OpenGL display device not detected.");
  135. // and now we can install the device.
  136. Video::installDevice(dev);
  137. Con::printf("");
  138. }
  139. //------------------------------------------------------------------------------
  140. void Platform::shutdown()
  141. {
  142. setMouseLock(false);
  143. Video::destroy();
  144. Input::destroy();
  145. iOSConsole::destroy();
  146. }
  147. //Hidden by Default. 1 Black Opaque, 2 Black Translucent
  148. S32 gStatusBarType = 0;
  149. bool gStatusBarHidden = true;
  150. //Landscape by default. 0 Landscape, 1 Portrait
  151. S32 gScreenOrientation = 0;
  152. bool gScreenUpsideDown = true;
  153. //------------------------------------------------------------------------------
  154. void Platform::initWindow(const Point2I &initialSize, const char *name)
  155. {
  156. S32 resolutionWidth = IOS_DEFAULT_RESOLUTION_X;
  157. S32 resolutionHeight = IOS_DEFAULT_RESOLUTION_Y;
  158. // First fetch the values from the prefs.
  159. U32 iDeviceType = (U32) Con::getIntVariable("$pref::iOS::DeviceType");
  160. U32 iDeviceOrientation = (U32) Con::getIntVariable("$pref::iOS::ScreenOrientation");
  161. bool retinaEnabled = Con::getBoolVariable("$pref::iOS::RetinaEnabled");
  162. // 0: iPhone
  163. // 1: iPad
  164. // 2: iPhone 5
  165. if (iDeviceType == 2)
  166. {
  167. resolutionWidth = 1136;
  168. resolutionHeight = 640;
  169. }
  170. else
  171. {
  172. U32 scaleFactor = retinaEnabled ? 2 : 1;
  173. resolutionWidth = iDeviceType ? (1024 * scaleFactor) : (480 * scaleFactor);
  174. resolutionHeight = iDeviceType ? (768 * scaleFactor) : (320 * scaleFactor);
  175. }
  176. Point2I startRes;
  177. if (!iDeviceOrientation)
  178. {
  179. startRes.x = resolutionWidth;
  180. startRes.y = resolutionHeight;
  181. }
  182. else
  183. {
  184. //portrait, swap width height.
  185. startRes.x = resolutionHeight;
  186. startRes.y = resolutionWidth;
  187. }
  188. dSprintf(platState.appWindowTitle, sizeof(platState.appWindowTitle), name);
  189. platState.windowSize.x = startRes.x;
  190. platState.windowSize.y = startRes.y;
  191. //Get screen orientation prefs //Based on 0 Landscape, 1 Portrait
  192. gScreenOrientation = iDeviceOrientation;
  193. gScreenUpsideDown = Con::getBoolVariable("$pref::iOS::ScreenUpsideDown");
  194. //Default to landscape, and run into portrait if requested.
  195. platState.portrait = false;
  196. if (gScreenOrientation != 0) //fuzzytodo :add a constant
  197. {
  198. //Could handle other options here, later.
  199. platState.portrait = true;
  200. }
  201. //We should now have a good windowSize, it will be default if initial size was bad
  202. T2DView * glView;
  203. CGRect rect;
  204. rect.origin.x = 0;
  205. rect.origin.y = 0;
  206. rect.size.width = platState.windowSize.x;
  207. rect.size.height = platState.windowSize.y;
  208. glView = (T2DView *) platState.Window;
  209. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)
  210. glView.contentScaleFactor = [[UIScreen mainScreen] scale];
  211. platState.ctx = glView;
  212. //get status bar pref // 0 Hidden , 1 BlackOpaque , 2 BlackTranslucent
  213. S32 tempType = Con::getIntVariable("$pref::iOS::StatusBarType");
  214. setStatusBarType(tempType);
  215. //set screen orientation
  216. setScreenOrientation(platState.portrait, gScreenUpsideDown);
  217. bool fullScreen;
  218. U32 bpp = Con::getIntVariable("$pref::iOS::ScreenDepth"); //iOS_DEFAULT_RESOLUTION_BIT_DEPTH;
  219. if (!bpp)
  220. {
  221. Con::printf("Default BPP Chosen , $pref::iOS::ScreenDepth was not found.");
  222. bpp = IOS_DEFAULT_RESOLUTION_BIT_DEPTH;
  223. }
  224. fullScreen = true;
  225. //
  226. DisplayDevice::init();
  227. // this will create a rendering context & window
  228. bool ok = Video::setDevice("OpenGL", platState.windowSize.x, platState.windowSize.y, bpp, fullScreen);
  229. if (!ok)
  230. {
  231. AssertFatal( false, "Could not find a compatible display device!" );
  232. }
  233. //Luma: Clear frame buffer to BLACK to start with
  234. //NOTE: This should probably be set by the user to be the color closest to Default.png in order to minimize any popping effect... $pref:: anyone? Are $pref::s even valid at this point in the Init process?
  235. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  236. glClear(GL_COLOR_BUFFER_BIT);
  237. }
  238. //--------------------------------------
  239. // run app function: not applicable to iOS
  240. //--------------------------------------
  241. // run other apps
  242. int runApp(const char *fileName, const char *batchArgs, bool blocking = false)
  243. {
  244. return 0;
  245. }
  246. bool appIsRunning(int batchId)
  247. {
  248. return false;
  249. }
  250. bool Platform::openWebBrowser(const char *webAddress)
  251. {
  252. NSString *string = [[NSString alloc] initWithUTF8String:webAddress];
  253. NSURL *url = [[NSURL alloc] initWithString:string];
  254. bool ret = [platState.application openURL:url];
  255. return ret;// this bails on the application, switching to Safari
  256. }
  257. bool isStatusBarHidden()
  258. {
  259. if (platState.application.statusBarHidden == YES)
  260. {
  261. return true;
  262. }
  263. else
  264. {
  265. return false;
  266. }
  267. }
  268. bool setStatusBarHidden(bool hidden)
  269. {
  270. if (hidden)
  271. {
  272. platState.application.statusBarHidden = YES;
  273. gStatusBarHidden = true;
  274. return true;
  275. }
  276. else
  277. {
  278. platState.application.statusBarHidden = NO;
  279. gStatusBarHidden = false;
  280. return false;
  281. }
  282. }
  283. void setStatusBarType(S32 type)
  284. {
  285. switch (type)
  286. {
  287. case 0: //Hidden
  288. setStatusBarHidden(true);
  289. break;
  290. case 1: //Black Opaque
  291. platState.application.statusBarStyle = UIStatusBarStyleBlackOpaque;
  292. setStatusBarHidden(false);
  293. break;
  294. case 2: //Black Transparent
  295. platState.application.statusBarStyle = UIStatusBarStyleBlackTranslucent;
  296. setStatusBarHidden(false);
  297. break;
  298. default:
  299. platState.application.statusBarStyle = UIStatusBarStyleDefault;
  300. }
  301. gStatusBarType = type;
  302. }
  303. bool setScreenOrientation(bool portrait, bool upsidedown)
  304. {
  305. bool success = false;
  306. CGPoint point;
  307. // Is the iOS version less than 8?
  308. if( [[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] == NSOrderedAscending )
  309. {
  310. if (platState.portrait)
  311. {
  312. point.x = platState.windowSize.x / 2;
  313. point.y = platState.windowSize.y / 2;
  314. }
  315. else
  316. {
  317. point.x = platState.windowSize.y / 2;
  318. point.y = platState.windowSize.x / 2;
  319. }
  320. [platState.ctx centerOnPoint:point];
  321. if (portrait)
  322. {//normal upright
  323. if (upsidedown)
  324. {//button on top
  325. [platState.ctx rotateToAngle:M_PI + (M_PI / 2.0)];//rotate to 90 degrees
  326. platState.application.statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
  327. success = true;
  328. } else
  329. {//button on bottom
  330. [platState.ctx rotateToAngle:(M_PI / 2.0)];//rotate to 270 degrees
  331. platState.application.statusBarOrientation = UIInterfaceOrientationPortrait;
  332. success = true;
  333. }
  334. } else
  335. {//landscape/ sideways
  336. if (upsidedown)
  337. {//button on left
  338. [platState.ctx rotateToAngle:0];//rotate to -180 (0) degrees
  339. platState.application.statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
  340. success = true;
  341. } else
  342. {//button on right
  343. [platState.ctx rotateToAngle:(M_PI)];//rotate to 180 degrees
  344. platState.application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
  345. success = true;
  346. }
  347. }
  348. }
  349. //Set the screen for iOS 8 and latter
  350. else
  351. {
  352. point.x = platState.windowSize.x / 2;
  353. point.y = platState.windowSize.y / 2;
  354. }
  355. return success;
  356. }
  357. ConsoleFunction(setScreenOrientation, bool, 3, 3, "Sets the orientation of the screen ( portrait/landscape, upside down or right-side up )\n"
  358. "@(bool portrait, bool upside_down)"){
  359. return setScreenOrientation(dAtob(argv[1]), dAtob(argv[2]));
  360. }
  361. ConsoleFunction(getStatusBarHidden, bool, 1, 1, " Checks whether the status bar is hidden\n"
  362. "@return Returns true if hidden and false if not"){
  363. return isStatusBarHidden();
  364. }
  365. ConsoleFunction(setStatusBarHidden, bool, 2, 2, " Hides/unhides the iOS status bar \n"
  366. "@return true == status bar is hidden, false == status bar is visible"){
  367. return setStatusBarHidden(dAtob(argv[1]));
  368. }
  369. ConsoleFunction(setStatusBarType, void, 2, 2, " Set the status bar type. 0 hidden, 1 Black Opaque, 2 Black Translucent \n"){
  370. return setStatusBarType(dAtoi(argv[1]));
  371. }