AndroidWindow.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 getStatusBarHidden();
  35. bool setStatusBarHidden(bool);
  36. void setStatusBarType(S32);
  37. //------------------------------------------------------------------------------
  38. #pragma mark ---- PlatState ----
  39. AndroidPlatState platState;
  40. AndroidPlatState::AndroidPlatState()
  41. {
  42. captureDisplay = true;
  43. fadeWindows = true;
  44. backgrounded = false;
  45. minimized = false;
  46. quit = false;
  47. portrait = true;//-Mat Android is in portrait mode by default
  48. // start with something reasonable.
  49. desktopBitsPixel = ANDROID_DEFAULT_RESOLUTION_BIT_DEPTH;
  50. desktopWidth = ANDROID_DEFAULT_RESOLUTION_X;
  51. desktopHeight = ANDROID_DEFAULT_RESOLUTION_Y;
  52. fullscreen = true;
  53. osVersion = 0;
  54. dStrcpy(appWindowTitle, "Android Torque Game Engine");
  55. // Semaphore for alerts. We put the app in a modal state by blocking the main
  56. alertSemaphore = Semaphore::createSemaphore(0);
  57. // directory that contains main.cs . This will help us detect whether we are
  58. // running with the scripts in the bundle or not.
  59. mainDotCsDir = NULL;
  60. }
  61. //------------------------------------------------------------------------------
  62. // DGL, the Gui, and TS use this for various purposes.
  63. const Point2I &Platform::getWindowSize()
  64. {
  65. return platState.windowSize;
  66. }
  67. //------------------------------------------------------------------------------
  68. // save the window size, for DGL's use
  69. void Platform::setWindowSize(U32 newWidth, U32 newHeight)
  70. {
  71. platState.windowSize.set(newWidth, newHeight);
  72. }
  73. //------------------------------------------------------------------------------
  74. // Issue a minimize event. The standard handler will handle it.
  75. void Platform::minimizeWindow()
  76. {
  77. //no minimizing on Android
  78. }
  79. void Platform::restoreWindow()
  80. {
  81. //no minimizing on Android
  82. }
  83. //------------------------------------------------------------------------------
  84. void Platform::setWindowTitle(const char *title)
  85. {
  86. //no window titles on Android
  87. }
  88. #pragma mark ---- Init funcs ----
  89. //------------------------------------------------------------------------------
  90. void Platform::init()
  91. {
  92. Con::setVariable("$platform", "Android");
  93. AndroidConsole::create();
  94. //if ( !AndroidConsole::isEnabled() )
  95. Input::init();
  96. // allow users to specify whether to capture the display or not when going fullscreen
  97. Con::addVariable("pref::Android::captureDisplay", TypeBool, &platState.captureDisplay);
  98. Con::addVariable("pref::Android::fadeWindows", TypeBool, &platState.fadeWindows);
  99. // create the opengl display device
  100. DisplayDevice *dev = NULL;
  101. Con::printf("Video Init:");
  102. Video::init();
  103. dev = OpenGLDevice::create();
  104. if (dev)
  105. Con::printf(" Accelerated OpenGL display device detected.");
  106. else
  107. Con::printf(" Accelerated OpenGL display device not detected.");
  108. // and now we can install the device.
  109. Video::installDevice(dev);
  110. Con::printf("");
  111. }
  112. //------------------------------------------------------------------------------
  113. void Platform::shutdown()
  114. {
  115. setMouseLock(false);
  116. Video::destroy();
  117. Input::destroy();
  118. AndroidConsole::destroy();
  119. }
  120. //Hidden by Default. 1 Black Opaque, 2 Black Translucent
  121. S32 gScreenOrientation = 0;
  122. S32 gStatusBarType = 0;
  123. bool gStatusBarHidden = true;
  124. //------------------------------------------------------------------------------
  125. void Platform::initWindow(const Point2I &initialSize, const char *name)
  126. {
  127. S32 resolutionWidth = ANDROID_DEFAULT_RESOLUTION_X;
  128. S32 resolutionHeight = ANDROID_DEFAULT_RESOLUTION_Y;
  129. dSprintf(platState.appWindowTitle, sizeof(platState.appWindowTitle), name);
  130. platState.windowSize.x = _AndroidGetScreenWidth();
  131. platState.windowSize.y = _AndroidGetScreenHeight();
  132. //Default to landscape, and run into portrait if requested.
  133. S32 orientation = _AndroidGameGetOrientation();
  134. if (orientation == ACONFIGURATION_ORIENTATION_PORT)
  135. {
  136. gScreenOrientation = 1;
  137. platState.portrait = true;
  138. }
  139. else
  140. {
  141. gScreenOrientation = 0;
  142. platState.portrait = false;
  143. }
  144. bool fullScreen;
  145. U32 bpp = Con::getIntVariable("$pref::Android::ScreenDepth"); //ANDROID_DEFAULT_RESOLUTION_BIT_DEPTH;
  146. if (!bpp)
  147. {
  148. Con::printf("Default BPP Chosen , $pref::Android::ScreenDepth was not found.");
  149. bpp = ANDROID_DEFAULT_RESOLUTION_BIT_DEPTH;
  150. }
  151. fullScreen = true;
  152. //
  153. DisplayDevice::init();
  154. // this will create a rendering context & window
  155. bool ok = Video::setDevice("OpenGL", platState.windowSize.x, platState.windowSize.y, bpp, fullScreen);
  156. if (!ok)
  157. {
  158. AssertFatal( false, "Could not find a compatible display device!" );
  159. }
  160. //Luma: Clear frame buffer to BLACK to start with
  161. //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?
  162. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  163. glClear(GL_COLOR_BUFFER_BIT);
  164. }
  165. //--------------------------------------
  166. // run app function: not applicable to Android
  167. //--------------------------------------
  168. // run other apps
  169. int runApp(const char *fileName, const char *batchArgs, bool blocking = false)
  170. {
  171. return 0;
  172. }
  173. bool appIsRunning(int batchId)
  174. {
  175. return false;
  176. }
  177. ConsoleFunction(setScreenOrientation, bool, 3, 3, "Sets the orientation of the screen ( portrait/landscape, upside down or right-side up )\n"
  178. "@(bool portrait, bool upside_down)"){
  179. adprintf("screen orientation is set via the manifest file on android");
  180. return false;
  181. }