iOSWindow.mm 14 KB

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