iOSWindow.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. // Create two variables that I use later to make the canvas the right size
  97. CGRect screenBounds = [[UIScreen mainScreen] bounds];
  98. CGFloat screenScale = [[UIScreen mainScreen] scale];
  99. Con::setFloatVariable("$iOSwidth", screenBounds.size.width * screenScale);
  100. Con::setFloatVariable("$iOSheight", screenBounds.size.height * screenScale);
  101. Con::setVariable("$platform", "iOS");
  102. if ([[UIScreen mainScreen] scale] == 2)
  103. Con::setBoolVariable("$pref::iOS::RetinaEnabled", true);
  104. else
  105. Con::setBoolVariable("$pref::iOS::RetinaEnabled", false);
  106. // Set the platform variable for the scripts
  107. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  108. {
  109. Con::setIntVariable("$pref::iOS::DeviceType", 1);
  110. }
  111. else
  112. {
  113. F32 screenHeight = [[UIScreen mainScreen] bounds].size.height;
  114. bool iPhone5 = (fabs((double)screenHeight - (double)568 ) < DBL_EPSILON);
  115. if (iPhone5)
  116. {
  117. Con::setIntVariable("$pref::iOS::DeviceType", 2);
  118. Con::setBoolVariable("$pref::iOS::RetinaEnabled", false);
  119. }
  120. else
  121. {
  122. Con::setIntVariable("$pref::iOS::DeviceType", 0);
  123. }
  124. }
  125. iOSConsole::create();
  126. //if ( !iOSConsole::isEnabled() )
  127. Input::init();
  128. // allow users to specify whether to capture the display or not when going fullscreen
  129. Con::addVariable("pref::mac::captureDisplay", TypeBool, &platState.captureDisplay);
  130. Con::addVariable("pref::mac::fadeWindows", TypeBool, &platState.fadeWindows);
  131. // create the opengl display device
  132. DisplayDevice *dev = NULL;
  133. Con::printf("Video Init:");
  134. Video::init();
  135. dev = OpenGLDevice::create();
  136. if (dev)
  137. Con::printf(" Accelerated OpenGL display device detected.");
  138. else
  139. Con::printf(" Accelerated OpenGL display device not detected.");
  140. // and now we can install the device.
  141. Video::installDevice(dev);
  142. Con::printf("");
  143. }
  144. //------------------------------------------------------------------------------
  145. void Platform::shutdown()
  146. {
  147. setMouseLock(false);
  148. Video::destroy();
  149. Input::destroy();
  150. iOSConsole::destroy();
  151. }
  152. //Hidden by Default. 1 Black Opaque, 2 Black Translucent
  153. S32 gStatusBarType = 0;
  154. bool gStatusBarHidden = true;
  155. //Landscape by default. 0 Landscape, 1 Portrait
  156. S32 gScreenOrientation = 0;
  157. bool gScreenUpsideDown = true;
  158. //------------------------------------------------------------------------------
  159. void Platform::initWindow(const Point2I &initialSize, const char *name)
  160. {
  161. S32 resolutionWidth = IOS_DEFAULT_RESOLUTION_X;
  162. S32 resolutionHeight = IOS_DEFAULT_RESOLUTION_Y;
  163. // First fetch the values from the prefs.
  164. U32 iDeviceType = (U32) Con::getIntVariable("$pref::iOS::DeviceType");
  165. U32 iDeviceOrientation = (U32) Con::getIntVariable("$pref::iOS::ScreenOrientation");
  166. bool retinaEnabled = Con::getBoolVariable("$pref::iOS::RetinaEnabled");
  167. // 0: iPhone
  168. // 1: iPad
  169. // 2: iPhone 5
  170. if (iDeviceType == 2)
  171. {
  172. resolutionWidth = 1136;
  173. resolutionHeight = 640;
  174. }
  175. else
  176. {
  177. U32 scaleFactor = retinaEnabled ? 2 : 1;
  178. resolutionWidth = iDeviceType ? (1024 * scaleFactor) : (480 * scaleFactor);
  179. resolutionHeight = iDeviceType ? (768 * scaleFactor) : (320 * scaleFactor);
  180. }
  181. Point2I startRes;
  182. if (!iDeviceOrientation)
  183. {
  184. startRes.x = resolutionWidth;
  185. startRes.y = resolutionHeight;
  186. }
  187. else
  188. {
  189. //portrait, swap width height.
  190. startRes.x = resolutionHeight;
  191. startRes.y = resolutionWidth;
  192. }
  193. dSprintf(platState.appWindowTitle, sizeof(platState.appWindowTitle), name);
  194. platState.windowSize.x = startRes.x;
  195. platState.windowSize.y = startRes.y;
  196. //Get screen orientation prefs //Based on 0 Landscape, 1 Portrait
  197. gScreenOrientation = iDeviceOrientation;
  198. gScreenUpsideDown = Con::getBoolVariable("$pref::iOS::ScreenUpsideDown");
  199. //Default to landscape, and run into portrait if requested.
  200. platState.portrait = false;
  201. if (gScreenOrientation != 0) //fuzzytodo :add a constant
  202. {
  203. //Could handle other options here, later.
  204. platState.portrait = true;
  205. }
  206. //We should now have a good windowSize, it will be default if initial size was bad
  207. T2DView * glView;
  208. CGRect rect;
  209. rect.origin.x = 0;
  210. rect.origin.y = 0;
  211. rect.size.width = platState.windowSize.x;
  212. rect.size.height = platState.windowSize.y;
  213. glView = (T2DView *) platState.Window;
  214. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)
  215. glView.contentScaleFactor = [[UIScreen mainScreen] scale];
  216. platState.ctx = glView;
  217. //get status bar pref // 0 Hidden , 1 BlackOpaque , 2 BlackTranslucent
  218. S32 tempType = Con::getIntVariable("$pref::iOS::StatusBarType");
  219. setStatusBarType(tempType);
  220. //set screen orientation
  221. setScreenOrientation(platState.portrait, gScreenUpsideDown);
  222. bool fullScreen;
  223. U32 bpp = Con::getIntVariable("$pref::iOS::ScreenDepth"); //iOS_DEFAULT_RESOLUTION_BIT_DEPTH;
  224. if (!bpp)
  225. {
  226. Con::printf("Default BPP Chosen , $pref::iOS::ScreenDepth was not found.");
  227. bpp = IOS_DEFAULT_RESOLUTION_BIT_DEPTH;
  228. }
  229. fullScreen = true;
  230. //
  231. DisplayDevice::init();
  232. // this will create a rendering context & window
  233. bool ok = Video::setDevice("OpenGL", platState.windowSize.x, platState.windowSize.y, bpp, fullScreen);
  234. if (!ok)
  235. {
  236. AssertFatal( false, "Could not find a compatible display device!" );
  237. }
  238. //Luma: Clear frame buffer to BLACK to start with
  239. //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?
  240. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  241. glClear(GL_COLOR_BUFFER_BIT);
  242. }
  243. //--------------------------------------
  244. // run app function: not applicable to iOS
  245. //--------------------------------------
  246. // run other apps
  247. int runApp(const char *fileName, const char *batchArgs, bool blocking = false)
  248. {
  249. return 0;
  250. }
  251. bool appIsRunning(int batchId)
  252. {
  253. return false;
  254. }
  255. bool Platform::openWebBrowser(const char *webAddress)
  256. {
  257. NSString *string = [[NSString alloc] initWithUTF8String:webAddress];
  258. NSURL *url = [[NSURL alloc] initWithString:string];
  259. bool ret = [platState.application openURL:url];
  260. return ret;// this bails on the application, switching to Safari
  261. }
  262. bool isStatusBarHidden()
  263. {
  264. if (platState.application.statusBarHidden == YES)
  265. {
  266. return true;
  267. }
  268. else
  269. {
  270. return false;
  271. }
  272. }
  273. bool setStatusBarHidden(bool hidden)
  274. {
  275. if (hidden)
  276. {
  277. platState.application.statusBarHidden = YES;
  278. gStatusBarHidden = true;
  279. return true;
  280. }
  281. else
  282. {
  283. platState.application.statusBarHidden = NO;
  284. gStatusBarHidden = false;
  285. return false;
  286. }
  287. }
  288. void setStatusBarType(S32 type)
  289. {
  290. switch (type)
  291. {
  292. case 0: //Hidden
  293. setStatusBarHidden(true);
  294. break;
  295. case 1: //Black Opaque
  296. platState.application.statusBarStyle = UIStatusBarStyleBlackOpaque;
  297. setStatusBarHidden(false);
  298. break;
  299. case 2: //Black Transparent
  300. platState.application.statusBarStyle = UIStatusBarStyleBlackTranslucent;
  301. setStatusBarHidden(false);
  302. break;
  303. default:
  304. platState.application.statusBarStyle = UIStatusBarStyleDefault;
  305. }
  306. gStatusBarType = type;
  307. }
  308. bool setScreenOrientation(bool portrait, bool upsidedown)
  309. {
  310. bool success = false;
  311. CGPoint point;
  312. if (platState.portrait)
  313. {
  314. point.x = platState.windowSize.x / 2;
  315. point.y = platState.windowSize.y / 2;
  316. }
  317. else
  318. {
  319. point.x = platState.windowSize.y / 2;
  320. point.y = platState.windowSize.x / 2;
  321. }
  322. [platState.ctx centerOnPoint:point];
  323. if (portrait)
  324. {//normal upright
  325. if (upsidedown)
  326. {//button on top
  327. [platState.ctx rotateToAngle:M_PI + (M_PI / 2.0)];//rotate to 90 degrees
  328. platState.application.statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
  329. success = true;
  330. } else
  331. {//button on bottom
  332. [platState.ctx rotateToAngle:(M_PI / 2.0)];//rotate to 270 degrees
  333. platState.application.statusBarOrientation = UIInterfaceOrientationPortrait;
  334. success = true;
  335. }
  336. } else
  337. {//landscape/ sideways
  338. if (upsidedown)
  339. {//button on left
  340. [platState.ctx rotateToAngle:0];//rotate to -180 (0) degrees
  341. platState.application.statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
  342. success = true;
  343. } else
  344. {//button on right
  345. [platState.ctx rotateToAngle:(M_PI)];//rotate to 180 degrees
  346. platState.application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
  347. success = true;
  348. }
  349. }
  350. return success;
  351. }
  352. ConsoleFunction(setScreenOrientation, bool, 3, 3, "Sets the orientation of the screen ( portrait/landscape, upside down or right-side up )\n"
  353. "@(bool portrait, bool upside_down)"){
  354. return setScreenOrientation(dAtob(argv[1]), dAtob(argv[2]));
  355. }
  356. ConsoleFunction(getStatusBarHidden, bool, 1, 1, " Checks whether the status bar is hidden\n"
  357. "@return Returns true if hidden and false if not"){
  358. return isStatusBarHidden();
  359. }
  360. ConsoleFunction(setStatusBarHidden, bool, 2, 2, " Hides/unhides the iOS status bar \n"
  361. "@return true == status bar is hidden, false == status bar is visible"){
  362. return setStatusBarHidden(dAtob(argv[1]));
  363. }
  364. ConsoleFunction(setStatusBarType, void, 2, 2, " Set the status bar type. 0 hidden, 1 Black Opaque, 2 Black Translucent \n"){
  365. return setStatusBarType(dAtoi(argv[1]));
  366. }