CCGLView.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  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 FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CCGLVIEW_H__
  23. #define __CCGLVIEW_H__
  24. #include "base/ccTypes.h"
  25. #include "base/CCEventTouch.h"
  26. #include <vector>
  27. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  28. #include <windows.h>
  29. #endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */
  30. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  31. typedef void* id;
  32. #endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) */
  33. /** There are some Resolution Policy for Adapt to the screen. */
  34. enum class ResolutionPolicy
  35. {
  36. /** The entire application is visible in the specified area without trying to preserve the original aspect ratio.
  37. * Distortion can occur, and the application may appear stretched or compressed.
  38. */
  39. EXACT_FIT,
  40. /** The entire application fills the specified area, without distortion but possibly with some cropping,
  41. * while maintaining the original aspect ratio of the application.
  42. */
  43. NO_BORDER,
  44. /** The entire application is visible in the specified area without distortion while maintaining the original
  45. * aspect ratio of the application. Borders can appear on two sides of the application.
  46. */
  47. SHOW_ALL,
  48. /** The application takes the height of the design resolution size and modifies the width of the internal
  49. * canvas so that it fits the aspect ratio of the device.
  50. * No distortion will occur however you must make sure your application works on different
  51. * aspect ratios.
  52. */
  53. FIXED_HEIGHT,
  54. /** The application takes the width of the design resolution size and modifies the height of the internal
  55. * canvas so that it fits the aspect ratio of the device.
  56. * No distortion will occur however you must make sure your application works on different
  57. * aspect ratios.
  58. */
  59. FIXED_WIDTH,
  60. UNKNOWN,
  61. };
  62. /** @struct GLContextAttrs
  63. *
  64. * There are six opengl Context Attrs.
  65. */
  66. struct GLContextAttrs
  67. {
  68. int redBits;
  69. int greenBits;
  70. int blueBits;
  71. int alphaBits;
  72. int depthBits;
  73. int stencilBits;
  74. int multisamplingCount;
  75. };
  76. NS_CC_BEGIN
  77. class Scene;
  78. class Renderer;
  79. class VRIRenderer;
  80. /**
  81. * @addtogroup platform
  82. * @{
  83. */
  84. /**
  85. * @brief By GLView you can operate the frame information of EGL view through some function.
  86. */
  87. class CC_DLL GLView : public Ref
  88. {
  89. public:
  90. /**
  91. * @js ctor
  92. */
  93. GLView();
  94. /**
  95. * @js NA
  96. * @lua NA
  97. */
  98. virtual ~GLView();
  99. /** Force destroying EGL view, subclass must implement this method.
  100. *
  101. * @lua endToLua
  102. */
  103. virtual void end() = 0;
  104. /** Get whether opengl render system is ready, subclass must implement this method. */
  105. virtual bool isOpenGLReady() = 0;
  106. /** Exchanges the front and back buffers, subclass must implement this method. */
  107. virtual void swapBuffers() = 0;
  108. /** Open or close IME keyboard , subclass must implement this method.
  109. *
  110. * @param open Open or close IME keyboard.
  111. */
  112. virtual void setIMEKeyboardState(bool open) = 0;
  113. /** When the window is closed, it will return false if the platforms is Ios or Android.
  114. * If the platforms is windows or Mac,it will return true.
  115. *
  116. * @return In ios and android it will return false,if in windows or Mac it will return true.
  117. */
  118. virtual bool windowShouldClose() { return false; };
  119. /** Static method and member so that we can modify it on all platforms before create OpenGL context.
  120. *
  121. * @param glContextAttrs The OpenGL context attrs.
  122. */
  123. static void setGLContextAttrs(GLContextAttrs& glContextAttrs);
  124. /** Return the OpenGL context attrs.
  125. *
  126. * @return Return the OpenGL context attrs.
  127. */
  128. static GLContextAttrs getGLContextAttrs();
  129. /** The OpenGL context attrs. */
  130. static GLContextAttrs _glContextAttrs;
  131. /** @deprecated
  132. * Polls input events. Subclass must implement methods if platform
  133. * does not provide event callbacks.
  134. */
  135. CC_DEPRECATED_ATTRIBUTE virtual void pollInputEvents();
  136. /** Polls the events. */
  137. virtual void pollEvents();
  138. /**
  139. * Get the frame size of EGL view.
  140. * In general, it returns the screen size since the EGL view is a fullscreen view.
  141. *
  142. * @return The frame size of EGL view.
  143. */
  144. virtual Size getFrameSize() const;
  145. /**
  146. * Set the frame size of EGL view.
  147. *
  148. * @param width The width of the fram size.
  149. * @param height The height of the fram size.
  150. */
  151. virtual void setFrameSize(float width, float height);
  152. /** Set zoom factor for frame. This methods are for
  153. * debugging big resolution (e.g.new ipad) app on desktop.
  154. *
  155. * @param zoomFactor The zoom factor for frame.
  156. */
  157. virtual void setFrameZoomFactor(float /*zoomFactor*/) {}
  158. /** Get zoom factor for frame. This methods are for
  159. * debugging big resolution (e.g.new ipad) app on desktop.
  160. *
  161. * @return The zoom factor for frame.
  162. */
  163. virtual float getFrameZoomFactor() const { return 1.0; }
  164. /**
  165. * Hide or Show the mouse cursor if there is one.
  166. *
  167. * @param isVisible Hide or Show the mouse cursor if there is one.
  168. */
  169. virtual void setCursorVisible(bool /*isVisible*/) {}
  170. virtual void getCursorPos(double* lastPositionX, double* lastPositionY) {}
  171. /** Get retina factor.
  172. *
  173. * @return The retina factor.
  174. */
  175. virtual int getRetinaFactor() const { return 1; }
  176. /** Only works on ios platform. Set Content Scale of the Factor. */
  177. virtual bool setContentScaleFactor(float /*scaleFactor*/) { return false; }
  178. /** Only works on ios platform. Get Content Scale of the Factor. */
  179. virtual float getContentScaleFactor() const { return 1.0; }
  180. /** Returns whether or not the view is in Retina Display mode.
  181. *
  182. * @return Returns whether or not the view is in Retina Display mode.
  183. */
  184. virtual bool isRetinaDisplay() const { return false; }
  185. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  186. virtual void* getEAGLView() const { return nullptr; }
  187. #endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) */
  188. /**
  189. * Get the visible area size of opengl viewport.
  190. *
  191. * @return The visible area size of opengl viewport.
  192. */
  193. virtual Size getVisibleSize() const;
  194. /**
  195. * Get the visible origin point of opengl viewport.
  196. *
  197. * @return The visible origin point of opengl viewport.
  198. */
  199. virtual Vec2 getVisibleOrigin() const;
  200. /**
  201. * Get the visible rectangle of opengl viewport.
  202. *
  203. * @return The visible rectangle of opengl viewport.
  204. */
  205. virtual Rect getVisibleRect() const;
  206. /**
  207. * Gets safe area rectangle
  208. */
  209. virtual Rect getSafeAreaRect() const;
  210. /**
  211. * Set the design resolution size.
  212. * @param width Design resolution width.
  213. * @param height Design resolution height.
  214. * @param resolutionPolicy The resolution policy desired, you may choose:
  215. * [1] EXACT_FIT Fill screen by stretch-to-fit: if the design resolution ratio of width to height is different from the screen resolution ratio, your game view will be stretched.
  216. * [2] NO_BORDER Full screen without black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two areas of your game view will be cut.
  217. * [3] SHOW_ALL Full screen with black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two black borders will be shown.
  218. */
  219. virtual void setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy);
  220. /** Get design resolution size.
  221. * Default resolution size is the same as 'getFrameSize'.
  222. *
  223. * @return The design resolution size.
  224. */
  225. virtual const Size& getDesignResolutionSize() const;
  226. /**
  227. * Set opengl view port rectangle with points.
  228. *
  229. * @param x Set the points of x.
  230. * @param y Set the points of y.
  231. * @param w Set the width of the view port
  232. * @param h Set the Height of the view port.
  233. */
  234. virtual void setViewPortInPoints(float x , float y , float w , float h);
  235. /**
  236. * Set Scissor rectangle with points.
  237. *
  238. * @param x Set the points of x.
  239. * @param y Set the points of y.
  240. * @param w Set the width of the view port
  241. * @param h Set the Height of the view port.
  242. */
  243. virtual void setScissorInPoints(float x , float y , float w , float h);
  244. /**
  245. * Get whether GL_SCISSOR_TEST is enable.
  246. *
  247. * @return Whether GL_SCISSOR_TEST is enable.
  248. */
  249. virtual bool isScissorEnabled();
  250. /**
  251. * Get the current scissor rectangle.
  252. *
  253. * @return The current scissor rectangle.
  254. */
  255. virtual Rect getScissorRect() const;
  256. /** Set the view name.
  257. *
  258. * @param viewname A string will be set to the view as name.
  259. */
  260. virtual void setViewName(const std::string& viewname);
  261. /** Get the view name.
  262. *
  263. * @return The view name.
  264. */
  265. const std::string& getViewName() const;
  266. /** Touch events are handled by default; if you want to customize your handlers, please override this function.
  267. *
  268. * @param num The number of touch.
  269. * @param ids The identity of the touch.
  270. * @param xs The points of x.
  271. * @param ys The points of y.
  272. */
  273. virtual void handleTouchesBegin(int num, intptr_t ids[], float xs[], float ys[]);
  274. /** Touch events are handled by default; if you want to customize your handlers, please override this function.
  275. *
  276. * @param num The number of touch.
  277. * @param ids The identity of the touch.
  278. * @param xs The points of x.
  279. * @param ys The points of y.
  280. */
  281. virtual void handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[]);
  282. /** Touch events are handled by default; if you want to customize your handlers, please override this function.
  283. *
  284. * @param num The number of touch.
  285. * @param ids The identity of the touch.
  286. * @param xs The points of x.
  287. * @param ys The points of y.
  288. * @param fs The force of 3d touches.
  289. # @param ms The maximum force of 3d touches
  290. */
  291. virtual void handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[], float fs[], float ms[]);
  292. /** Touch events are handled by default; if you want to customize your handlers, please override this function.
  293. *
  294. * @param num The number of touch.
  295. * @param ids The identity of the touch.
  296. * @param xs The points of x.
  297. * @param ys The points of y.
  298. */
  299. virtual void handleTouchesEnd(int num, intptr_t ids[], float xs[], float ys[]);
  300. /** Touch events are handled by default; if you want to customize your handlers, please override this function.
  301. *
  302. * @param num The number of touch.
  303. * @param ids The identity of the touch.
  304. * @param xs The points of x.
  305. * @param ys The points of y.
  306. */
  307. virtual void handleTouchesCancel(int num, intptr_t ids[], float xs[], float ys[]);
  308. /**
  309. * Get the opengl view port rectangle.
  310. *
  311. * @return Return the opengl view port rectangle.
  312. */
  313. const Rect& getViewPortRect() const;
  314. /**
  315. * Get list of all active touches.
  316. *
  317. * @return A list of all active touches.
  318. */
  319. std::vector<Touch*> getAllTouches() const;
  320. /**
  321. * Get scale factor of the horizontal direction.
  322. *
  323. * @return Scale factor of the horizontal direction.
  324. */
  325. float getScaleX() const;
  326. /**
  327. * Get scale factor of the vertical direction.
  328. *
  329. * @return Scale factor of the vertical direction.
  330. */
  331. float getScaleY() const;
  332. /** Returns the current Resolution policy.
  333. *
  334. * @return The current Resolution policy.
  335. */
  336. ResolutionPolicy getResolutionPolicy() const { return _resolutionPolicy; }
  337. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  338. virtual HWND getWin32Window() = 0;
  339. #endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */
  340. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  341. virtual id getCocoaWindow() = 0;
  342. #endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) */
  343. /**
  344. * Renders a Scene with a Renderer
  345. * This method is called directly by the Director
  346. */
  347. void renderScene(Scene* scene, Renderer* renderer);
  348. /**
  349. * Sets a VR renderer.
  350. * if `vrrenderer` is `nullptr` VR will be disabled
  351. */
  352. void setVR(VRIRenderer* vrrenderer);
  353. VRIRenderer* getVR() const;
  354. protected:
  355. void updateDesignResolutionSize();
  356. void handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, intptr_t ids[], float xs[], float ys[]);
  357. // real screen size
  358. Size _screenSize;
  359. // resolution size, it is the size appropriate for the app resources.
  360. Size _designResolutionSize;
  361. // the view port size
  362. Rect _viewPortRect;
  363. // the view name
  364. std::string _viewName;
  365. float _scaleX;
  366. float _scaleY;
  367. ResolutionPolicy _resolutionPolicy;
  368. // VR stuff
  369. VRIRenderer* _vrImpl;
  370. };
  371. // end of platform group
  372. /// @}
  373. NS_CC_END
  374. #endif /* __CCGLVIEW_H__ */