AndroidWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. //TODO: android
  62. //mainLoopTimer = NULL;
  63. }
  64. //------------------------------------------------------------------------------
  65. // DGL, the Gui, and TS use this for various purposes.
  66. const Point2I &Platform::getWindowSize()
  67. {
  68. return platState.windowSize;
  69. }
  70. //------------------------------------------------------------------------------
  71. // save the window size, for DGL's use
  72. void Platform::setWindowSize(U32 newWidth, U32 newHeight)
  73. {
  74. platState.windowSize.set(newWidth, newHeight);
  75. }
  76. //------------------------------------------------------------------------------
  77. // Issue a minimize event. The standard handler will handle it.
  78. void Platform::minimizeWindow()
  79. {
  80. //no minimizing on Android
  81. }
  82. void Platform::restoreWindow()
  83. {
  84. //no minimizing on Android
  85. }
  86. //------------------------------------------------------------------------------
  87. void Platform::setWindowTitle(const char *title)
  88. {
  89. //no window titles on Android
  90. }
  91. #pragma mark ---- Init funcs ----
  92. //------------------------------------------------------------------------------
  93. void Platform::init()
  94. {
  95. Con::setVariable("$platform", "Android");
  96. AndroidConsole::create();
  97. //if ( !AndroidConsole::isEnabled() )
  98. Input::init();
  99. // allow users to specify whether to capture the display or not when going fullscreen
  100. Con::addVariable("pref::Android::captureDisplay", TypeBool, &platState.captureDisplay);
  101. Con::addVariable("pref::Android::fadeWindows", TypeBool, &platState.fadeWindows);
  102. // create the opengl display device
  103. DisplayDevice *dev = NULL;
  104. Con::printf("Video Init:");
  105. Video::init();
  106. dev = OpenGLDevice::create();
  107. if (dev)
  108. Con::printf(" Accelerated OpenGL display device detected.");
  109. else
  110. Con::printf(" Accelerated OpenGL display device not detected.");
  111. // and now we can install the device.
  112. Video::installDevice(dev);
  113. Con::printf("");
  114. }
  115. //------------------------------------------------------------------------------
  116. void Platform::shutdown()
  117. {
  118. setMouseLock(false);
  119. Video::destroy();
  120. Input::destroy();
  121. AndroidConsole::destroy();
  122. }
  123. //Hidden by Default. 1 Black Opaque, 2 Black Translucent
  124. S32 gStatusBarType = 0;
  125. bool gStatusBarHidden = true;
  126. //Landscape by default. 0 Landscape, 1 Portrait
  127. S32 gScreenOrientation = 0;
  128. bool gScreenUpsideDown = true;
  129. //------------------------------------------------------------------------------
  130. void Platform::initWindow(const Point2I &initialSize, const char *name)
  131. {
  132. S32 resolutionWidth = ANDROID_DEFAULT_RESOLUTION_X;
  133. S32 resolutionHeight = ANDROID_DEFAULT_RESOLUTION_Y;
  134. // First fetch the values from the prefs.
  135. U32 iDeviceOrientation = (U32) Con::getIntVariable("$pref::Android::ScreenOrientation");
  136. // 0: iPhone
  137. // 1: iPad
  138. // 2: iPhone 5
  139. //TODO: android
  140. /*
  141. if (iDeviceType == 2)
  142. {
  143. resolutionWidth = 1136;
  144. resolutionHeight = 640;
  145. }
  146. else
  147. {
  148. U32 scaleFactor = retinaEnabled ? 2 : 1;
  149. resolutionWidth = iDeviceType ? (1024 * scaleFactor) : (480 * scaleFactor);
  150. resolutionHeight = iDeviceType ? (768 * scaleFactor) : (320 * scaleFactor);
  151. }
  152. Point2I startRes;
  153. if (!iDeviceOrientation)
  154. {
  155. startRes.x = resolutionWidth;
  156. startRes.y = resolutionHeight;
  157. }
  158. else
  159. {
  160. //portrait, swap width height.
  161. startRes.x = resolutionHeight;
  162. startRes.y = resolutionWidth;
  163. }
  164. dSprintf(platState.appWindowTitle, sizeof(platState.appWindowTitle), name);
  165. platState.windowSize.x = startRes.x;
  166. platState.windowSize.y = startRes.y;
  167. //Get screen orientation prefs //Based on 0 Landscape, 1 Portrait
  168. gScreenOrientation = iDeviceOrientation;
  169. gScreenUpsideDown = Con::getBoolVariable("$pref::Android::ScreenUpsideDown");
  170. //Default to landscape, and run into portrait if requested.
  171. platState.portrait = false;
  172. if (gScreenOrientation != 0) //fuzzytodo :add a constant
  173. {
  174. //Could handle other options here, later.
  175. platState.portrait = true;
  176. }
  177. //We should now have a good windowSize, it will be default if initial size was bad
  178. T2DView * glView;
  179. CGRect rect;
  180. rect.origin.x = 0;
  181. rect.origin.y = 0;
  182. rect.size.width = platState.windowSize.x;
  183. rect.size.height = platState.windowSize.y;
  184. glView = (T2DView *) platState.Window;
  185. //TODO: android
  186. //if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)
  187. // glView.contentScaleFactor = [[UIScreen mainScreen] scale];
  188. platState.ctx = glView;
  189. //get status bar pref // 0 Hidden , 1 BlackOpaque , 2 BlackTranslucent
  190. S32 tempType = Con::getIntVariable("$pref::Android::StatusBarType");
  191. setStatusBarType(tempType);
  192. //set screen orientation
  193. setScreenOrientation(platState.portrait, gScreenUpsideDown);
  194. bool fullScreen;
  195. U32 bpp = Con::getIntVariable("$pref::Android::ScreenDepth"); //ANDROID_DEFAULT_RESOLUTION_BIT_DEPTH;
  196. if (!bpp)
  197. {
  198. Con::printf("Default BPP Chosen , $pref::Android::ScreenDepth was not found.");
  199. bpp = ANDROID_DEFAULT_RESOLUTION_BIT_DEPTH;
  200. }
  201. fullScreen = true;
  202. //
  203. DisplayDevice::init();
  204. // this will create a rendering context & window
  205. bool ok = Video::setDevice("OpenGL", platState.windowSize.x, platState.windowSize.y, bpp, fullScreen);
  206. if (!ok)
  207. {
  208. AssertFatal( false, "Could not find a compatible display device!" );
  209. }
  210. //Luma: Clear frame buffer to BLACK to start with
  211. //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?
  212. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  213. glClear(GL_COLOR_BUFFER_BIT);
  214. */
  215. }
  216. //--------------------------------------
  217. // run app function: not applicable to Android
  218. //--------------------------------------
  219. // run other apps
  220. int runApp(const char *fileName, const char *batchArgs, bool blocking = false)
  221. {
  222. return 0;
  223. }
  224. bool appIsRunning(int batchId)
  225. {
  226. return false;
  227. }
  228. bool Platform::openWebBrowser(const char *webAddress)
  229. {
  230. //TODO: android
  231. /*NSString *string = [[NSString alloc] initWithUTF8String:webAddress];
  232. NSURL *url = [[NSURL alloc] initWithString:string];
  233. bool ret = [platState.application openURL:url];
  234. return ret;// this bails on the application, switching to Safari
  235. */
  236. return false;
  237. }
  238. bool setScreenOrientation(bool portrait, bool upsidedown)
  239. {
  240. bool success = false;
  241. //TODO: android
  242. /*
  243. CGPoint point;
  244. if (platState.portrait)
  245. {
  246. point.x = platState.windowSize.x / 2;
  247. point.y = platState.windowSize.y / 2;
  248. }
  249. else
  250. {
  251. point.x = platState.windowSize.y / 2;
  252. point.y = platState.windowSize.x / 2;
  253. }
  254. */
  255. //TODO: android
  256. /*[platState.ctx centerOnPoint:point];
  257. if (portrait)
  258. {//normal upright
  259. if (upsidedown)
  260. {//button on top
  261. [platState.ctx rotateToAngle:M_PI + (M_PI / 2.0)];//rotate to 90 degrees
  262. platState.application.statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
  263. success = true;
  264. } else
  265. {//button on bottom
  266. [platState.ctx rotateToAngle:(M_PI / 2.0)];//rotate to 270 degrees
  267. platState.application.statusBarOrientation = UIInterfaceOrientationPortrait;
  268. success = true;
  269. }
  270. } else
  271. {//landscape/ sideways
  272. if (upsidedown)
  273. {//button on left
  274. [platState.ctx rotateToAngle:0];//rotate to -180 (0) degrees
  275. platState.application.statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
  276. success = true;
  277. } else
  278. {//button on right
  279. [platState.ctx rotateToAngle:(M_PI)];//rotate to 180 degrees
  280. platState.application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
  281. success = true;
  282. }
  283. }
  284. */
  285. return success;
  286. }
  287. ConsoleFunction(setScreenOrientation, bool, 3, 3, "Sets the orientation of the screen ( portrait/landscape, upside down or right-side up )\n"
  288. "@(bool portrait, bool upside_down)"){
  289. return setScreenOrientation(dAtob(argv[1]), dAtob(argv[2]));
  290. }