AndroidWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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::Android::captureDisplay", TypeBool, &platState.captureDisplay);
  100. Con::addVariable("pref::Android::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. //TODO: android
  183. //if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)
  184. // glView.contentScaleFactor = [[UIScreen mainScreen] scale];
  185. platState.ctx = glView;
  186. //get status bar pref // 0 Hidden , 1 BlackOpaque , 2 BlackTranslucent
  187. S32 tempType = Con::getIntVariable("$pref::Android::StatusBarType");
  188. setStatusBarType(tempType);
  189. //set screen orientation
  190. setScreenOrientation(platState.portrait, gScreenUpsideDown);
  191. bool fullScreen;
  192. U32 bpp = Con::getIntVariable("$pref::Android::ScreenDepth"); //ANDROID_DEFAULT_RESOLUTION_BIT_DEPTH;
  193. if (!bpp)
  194. {
  195. Con::printf("Default BPP Chosen , $pref::Android::ScreenDepth was not found.");
  196. bpp = ANDROID_DEFAULT_RESOLUTION_BIT_DEPTH;
  197. }
  198. fullScreen = true;
  199. //
  200. DisplayDevice::init();
  201. // this will create a rendering context & window
  202. bool ok = Video::setDevice("OpenGL", platState.windowSize.x, platState.windowSize.y, bpp, fullScreen);
  203. if (!ok)
  204. {
  205. AssertFatal( false, "Could not find a compatible display device!" );
  206. }
  207. //Luma: Clear frame buffer to BLACK to start with
  208. //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?
  209. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  210. glClear(GL_COLOR_BUFFER_BIT);
  211. }
  212. //--------------------------------------
  213. // run app function: not applicable to Android
  214. //--------------------------------------
  215. // run other apps
  216. int runApp(const char *fileName, const char *batchArgs, bool blocking = false)
  217. {
  218. return 0;
  219. }
  220. bool appIsRunning(int batchId)
  221. {
  222. return false;
  223. }
  224. bool Platform::openWebBrowser(const char *webAddress)
  225. {
  226. //TODO: android
  227. /*NSString *string = [[NSString alloc] initWithUTF8String:webAddress];
  228. NSURL *url = [[NSURL alloc] initWithString:string];
  229. bool ret = [platState.application openURL:url];
  230. return ret;// this bails on the application, switching to Safari
  231. */
  232. return false;
  233. }
  234. bool setScreenOrientation(bool portrait, bool upsidedown)
  235. {
  236. bool success = false;
  237. CGPoint point;
  238. if (platState.portrait)
  239. {
  240. point.x = platState.windowSize.x / 2;
  241. point.y = platState.windowSize.y / 2;
  242. }
  243. else
  244. {
  245. point.x = platState.windowSize.y / 2;
  246. point.y = platState.windowSize.x / 2;
  247. }
  248. //TODO: android
  249. /*[platState.ctx centerOnPoint:point];
  250. if (portrait)
  251. {//normal upright
  252. if (upsidedown)
  253. {//button on top
  254. [platState.ctx rotateToAngle:M_PI + (M_PI / 2.0)];//rotate to 90 degrees
  255. platState.application.statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
  256. success = true;
  257. } else
  258. {//button on bottom
  259. [platState.ctx rotateToAngle:(M_PI / 2.0)];//rotate to 270 degrees
  260. platState.application.statusBarOrientation = UIInterfaceOrientationPortrait;
  261. success = true;
  262. }
  263. } else
  264. {//landscape/ sideways
  265. if (upsidedown)
  266. {//button on left
  267. [platState.ctx rotateToAngle:0];//rotate to -180 (0) degrees
  268. platState.application.statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
  269. success = true;
  270. } else
  271. {//button on right
  272. [platState.ctx rotateToAngle:(M_PI)];//rotate to 180 degrees
  273. platState.application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
  274. success = true;
  275. }
  276. }
  277. */
  278. return success;
  279. }
  280. ConsoleFunction(setScreenOrientation, bool, 3, 3, "Sets the orientation of the screen ( portrait/landscape, upside down or right-side up )\n"
  281. "@(bool portrait, bool upside_down)"){
  282. return setScreenOrientation(dAtob(argv[1]), dAtob(argv[2]));
  283. }