iOSWindow.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. // set the Platform to iOS
  97. Con::setVariable("$platform", "iOS");
  98. // Calculate the size of the screen
  99. CGRect screenBounds = [[UIScreen mainScreen] bounds];
  100. CGFloat screenScale = [[UIScreen mainScreen] scale];
  101. // Set the screen size to a variable
  102. Con::setFloatVariable("$pref::iOS::Width", screenBounds.size.width * screenScale);
  103. Con::setFloatVariable("$pref::iOS::Height", screenBounds.size.height * screenScale);
  104. // Set RetinaEnabled and the Scale
  105. // NOTE: I think we could get rid of RetinaEnabled through out and just multiply directly by the scale or use RetinaScale > 1 when needed because with iPhone 6 Plus the screenScale is 3.
  106. Con::setBoolVariable("$pref::iOS::RetinaEnabled", gOutlineEnabled);
  107. Con::setBoolVariable("$pref::iOS::RetinaScale", screenScale);
  108. // Determine the Aspect Ratio and set the device type from that
  109. float resolutionWidth = Con::getFloatVariable("$pref::iOS::Width");
  110. float resolutionHeight = Con::getFloatVariable("$pref::iOS::Height");
  111. // NOTE: This does not work properly for portrate orientation yet, but I think the way portrate is handled over all needs a rework.
  112. float Ratio = resolutionWidth/resolutionHeight;
  113. if (Ratio <= 1.4)// 1: iPad = 4:3
  114. Con::setIntVariable("$pref::iOS::DeviceType", 1);
  115. else if (Ratio > 1.4 && Ratio <= 1.6)// 0: iPhone = 3:2
  116. Con::setIntVariable("$pref::iOS::DeviceType", 0);
  117. else if (Ratio > 1.6)// 2: iPhone 5 and up = 16:9
  118. Con::setIntVariable("$pref::iOS::DeviceType", 2);
  119. // Add more aspcet ratios if needed for future devices
  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. // First fetch the values from the prefs.
  157. U32 iDeviceOrientation = (U32) Con::getIntVariable("$pref::iOS::ScreenOrientation");
  158. S32 resolutionWidth = Con::getIntVariable("$pref::iOS::Width");
  159. S32 resolutionHeight = Con::getIntVariable("$pref::iOS::Height");
  160. Point2I startRes;
  161. if (!iDeviceOrientation)
  162. {
  163. startRes.x = resolutionWidth;
  164. startRes.y = resolutionHeight;
  165. }
  166. else
  167. {
  168. //portrait, swap width height.
  169. startRes.x = resolutionHeight;
  170. startRes.y = resolutionWidth;
  171. }
  172. dSprintf(platState.appWindowTitle, sizeof(platState.appWindowTitle), name);
  173. platState.windowSize.x = startRes.x;
  174. platState.windowSize.y = startRes.y;
  175. //Get screen orientation prefs //Based on 0 Landscape, 1 Portrait
  176. gScreenOrientation = iDeviceOrientation;
  177. gScreenUpsideDown = Con::getBoolVariable("$pref::iOS::ScreenUpsideDown");
  178. //Default to landscape, and run into portrait if requested.
  179. platState.portrait = false;
  180. if (gScreenOrientation != 0) //fuzzytodo :add a constant
  181. {
  182. //Could handle other options here, later.
  183. platState.portrait = true;
  184. }
  185. //We should now have a good windowSize, it will be default if initial size was bad
  186. T2DView * glView;
  187. CGRect rect;
  188. rect.origin.x = 0;
  189. rect.origin.y = 0;
  190. rect.size.width = platState.windowSize.x;
  191. rect.size.height = platState.windowSize.y;
  192. glView = (T2DView *) platState.Window;
  193. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)
  194. glView.contentScaleFactor = [[UIScreen mainScreen] scale];
  195. platState.ctx = glView;
  196. //get status bar pref // 0 Hidden , 1 BlackOpaque , 2 BlackTranslucent
  197. S32 tempType = Con::getIntVariable("$pref::iOS::StatusBarType");
  198. setStatusBarType(tempType);
  199. //set screen orientation
  200. setScreenOrientation(platState.portrait, gScreenUpsideDown);
  201. bool fullScreen;
  202. U32 bpp = Con::getIntVariable("$pref::iOS::ScreenDepth"); //iOS_DEFAULT_RESOLUTION_BIT_DEPTH;
  203. if (!bpp)
  204. {
  205. Con::printf("Default BPP Chosen , $pref::iOS::ScreenDepth was not found.");
  206. bpp = IOS_DEFAULT_RESOLUTION_BIT_DEPTH;
  207. }
  208. fullScreen = true;
  209. //
  210. DisplayDevice::init();
  211. // this will create a rendering context & window
  212. bool ok = Video::setDevice("OpenGL", platState.windowSize.x, platState.windowSize.y, bpp, fullScreen);
  213. if (!ok)
  214. {
  215. AssertFatal( false, "Could not find a compatible display device!" );
  216. }
  217. //Luma: Clear frame buffer to BLACK to start with
  218. //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?
  219. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  220. glClear(GL_COLOR_BUFFER_BIT);
  221. }
  222. //--------------------------------------
  223. // run app function: not applicable to iOS
  224. //--------------------------------------
  225. // run other apps
  226. int runApp(const char *fileName, const char *batchArgs, bool blocking = false)
  227. {
  228. return 0;
  229. }
  230. bool appIsRunning(int batchId)
  231. {
  232. return false;
  233. }
  234. bool Platform::openWebBrowser(const char *webAddress)
  235. {
  236. NSString *string = [[NSString alloc] initWithUTF8String:webAddress];
  237. NSURL *url = [[NSURL alloc] initWithString:string];
  238. bool ret = [platState.application openURL:url];
  239. return ret;// this bails on the application, switching to Safari
  240. }
  241. bool isStatusBarHidden()
  242. {
  243. if (platState.application.statusBarHidden == YES)
  244. {
  245. return true;
  246. }
  247. else
  248. {
  249. return false;
  250. }
  251. }
  252. bool setStatusBarHidden(bool hidden)
  253. {
  254. if (hidden)
  255. {
  256. platState.application.statusBarHidden = YES;
  257. gStatusBarHidden = true;
  258. return true;
  259. }
  260. else
  261. {
  262. platState.application.statusBarHidden = NO;
  263. gStatusBarHidden = false;
  264. return false;
  265. }
  266. }
  267. void setStatusBarType(S32 type)
  268. {
  269. switch (type)
  270. {
  271. case 0: //Hidden
  272. setStatusBarHidden(true);
  273. break;
  274. case 1: //Black Opaque
  275. platState.application.statusBarStyle = UIStatusBarStyleBlackOpaque;
  276. setStatusBarHidden(false);
  277. break;
  278. case 2: //Black Transparent
  279. platState.application.statusBarStyle = UIStatusBarStyleBlackTranslucent;
  280. setStatusBarHidden(false);
  281. break;
  282. default:
  283. platState.application.statusBarStyle = UIStatusBarStyleDefault;
  284. }
  285. gStatusBarType = type;
  286. }
  287. bool setScreenOrientation(bool portrait, bool upsidedown)
  288. {
  289. bool success = false;
  290. CGPoint point;
  291. // Is the iOS version less than 8?
  292. if( [[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] == NSOrderedAscending )
  293. {
  294. if (platState.portrait)
  295. {
  296. point.x = platState.windowSize.x / 2;
  297. point.y = platState.windowSize.y / 2;
  298. }
  299. else
  300. {
  301. point.x = platState.windowSize.y / 2;
  302. point.y = platState.windowSize.x / 2;
  303. }
  304. [platState.ctx centerOnPoint:point];
  305. if (portrait)
  306. {//normal upright
  307. if (upsidedown)
  308. {//button on top
  309. [platState.ctx rotateToAngle:M_PI + (M_PI / 2.0)];//rotate to 90 degrees
  310. platState.application.statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
  311. success = true;
  312. } else
  313. {//button on bottom
  314. [platState.ctx rotateToAngle:(M_PI / 2.0)];//rotate to 270 degrees
  315. platState.application.statusBarOrientation = UIInterfaceOrientationPortrait;
  316. success = true;
  317. }
  318. } else
  319. {//landscape/ sideways
  320. if (upsidedown)
  321. {//button on left
  322. [platState.ctx rotateToAngle:0];//rotate to -180 (0) degrees
  323. platState.application.statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
  324. success = true;
  325. } else
  326. {//button on right
  327. [platState.ctx rotateToAngle:(M_PI)];//rotate to 180 degrees
  328. platState.application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
  329. success = true;
  330. }
  331. }
  332. }
  333. //Set the screen for iOS 8 and latter
  334. else
  335. {
  336. point.x = platState.windowSize.x / 2;
  337. point.y = platState.windowSize.y / 2;
  338. }
  339. return success;
  340. }
  341. ConsoleFunction(setScreenOrientation, bool, 3, 3, "Sets the orientation of the screen ( portrait/landscape, upside down or right-side up )\n"
  342. "@(bool portrait, bool upside_down)"){
  343. return setScreenOrientation(dAtob(argv[1]), dAtob(argv[2]));
  344. }
  345. ConsoleFunction(getStatusBarHidden, bool, 1, 1, " Checks whether the status bar is hidden\n"
  346. "@return Returns true if hidden and false if not"){
  347. return isStatusBarHidden();
  348. }
  349. ConsoleFunction(setStatusBarHidden, bool, 2, 2, " Hides/unhides the iOS status bar \n"
  350. "@return true == status bar is hidden, false == status bar is visible"){
  351. return setStatusBarHidden(dAtob(argv[1]));
  352. }
  353. ConsoleFunction(setStatusBarType, void, 2, 2, " Set the status bar type. 0 hidden, 1 Black Opaque, 2 Black Translucent \n"){
  354. return setStatusBarType(dAtoi(argv[1]));
  355. }