AndroidWindow.cpp 13 KB

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