CCGLViewImpl-desktop.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CC_EGLViewIMPL_DESKTOP_H__
  22. #define __CC_EGLViewIMPL_DESKTOP_H__
  23. #include "base/CCRef.h"
  24. #include "platform/CCCommon.h"
  25. #include "platform/CCGLView.h"
  26. #include "glfw3.h"
  27. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  28. #ifndef GLFW_EXPOSE_NATIVE_WIN32
  29. #define GLFW_EXPOSE_NATIVE_WIN32
  30. #endif
  31. #ifndef GLFW_EXPOSE_NATIVE_WGL
  32. #define GLFW_EXPOSE_NATIVE_WGL
  33. #endif
  34. #include "glfw3native.h"
  35. #endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */
  36. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  37. #ifndef GLFW_EXPOSE_NATIVE_NSGL
  38. #define GLFW_EXPOSE_NATIVE_NSGL
  39. #endif
  40. #ifndef GLFW_EXPOSE_NATIVE_COCOA
  41. #define GLFW_EXPOSE_NATIVE_COCOA
  42. #endif
  43. #include "glfw3native.h"
  44. #endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  45. NS_CC_BEGIN
  46. class CC_DLL GLViewImpl : public GLView
  47. {
  48. public:
  49. static GLViewImpl* create(const std::string& viewName);
  50. static GLViewImpl* create(const std::string& viewName, bool resizable);
  51. static GLViewImpl* createWithRect(const std::string& viewName, Rect size, float frameZoomFactor = 1.0f, bool resizable = false);
  52. static GLViewImpl* createWithFullScreen(const std::string& viewName);
  53. static GLViewImpl* createWithFullScreen(const std::string& viewName, const GLFWvidmode &videoMode, GLFWmonitor *monitor);
  54. /*
  55. *frameZoomFactor for frame. This method is for debugging big resolution (e.g.new ipad) app on desktop.
  56. */
  57. //void resize(int width, int height);
  58. float getFrameZoomFactor() const override;
  59. //void centerWindow();
  60. virtual void setViewPortInPoints(float x , float y , float w , float h) override;
  61. virtual void setScissorInPoints(float x , float y , float w , float h) override;
  62. virtual Rect getScissorRect() const override;
  63. bool windowShouldClose() override;
  64. void pollEvents() override;
  65. GLFWwindow* getWindow() const { return _mainWindow; }
  66. bool isFullscreen() const;
  67. void setFullscreen();
  68. void setFullscreen(int monitorIndex);
  69. void setFullscreen(const GLFWvidmode &videoMode, GLFWmonitor *monitor);
  70. void setWindowed(int width, int height);
  71. int getMonitorCount() const;
  72. Size getMonitorSize() const;
  73. /* override functions */
  74. virtual bool isOpenGLReady() override;
  75. virtual void end() override;
  76. virtual void swapBuffers() override;
  77. virtual void setFrameSize(float width, float height) override;
  78. virtual void setIMEKeyboardState(bool bOpen) override;
  79. /*
  80. * Set zoom factor for frame. This method is for debugging big resolution (e.g.new ipad) app on desktop.
  81. */
  82. void setFrameZoomFactor(float zoomFactor) override;
  83. /**
  84. * Hide or Show the mouse cursor if there is one.
  85. */
  86. virtual void setCursorVisible(bool isVisible) override;
  87. /** Retina support is disabled by default
  88. * @note This method is only available on Mac.
  89. */
  90. void enableRetina(bool enabled);
  91. /** Check whether retina display is enabled. */
  92. bool isRetinaEnabled() const { return _isRetinaEnabled; };
  93. /** Get retina factor */
  94. int getRetinaFactor() const override { return _retinaFactor; }
  95. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  96. HWND getWin32Window() { return glfwGetWin32Window(_mainWindow); }
  97. #endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */
  98. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  99. id getCocoaWindow() override { return glfwGetCocoaWindow(_mainWindow); }
  100. #endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  101. protected:
  102. GLViewImpl(bool initglfw = true);
  103. virtual ~GLViewImpl();
  104. bool initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor, bool resizable);
  105. bool initWithFullScreen(const std::string& viewName);
  106. bool initWithFullscreen(const std::string& viewname, const GLFWvidmode &videoMode, GLFWmonitor *monitor);
  107. bool initGlew();
  108. void updateFrameSize();
  109. // GLFW callbacks
  110. void onGLFWError(int errorID, const char* errorDesc);
  111. void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify);
  112. void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y);
  113. void onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y);
  114. void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
  115. void onGLFWCharCallback(GLFWwindow* window, unsigned int character);
  116. void onGLFWWindowPosCallback(GLFWwindow* windows, int x, int y);
  117. void onGLFWframebuffersize(GLFWwindow* window, int w, int h);
  118. void onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height);
  119. void onGLFWWindowIconifyCallback(GLFWwindow* window, int iconified);
  120. void onGLFWWindowFocusCallback(GLFWwindow* window, int focused);
  121. bool _captured;
  122. bool _supportTouch;
  123. bool _isInRetinaMonitor;
  124. bool _isRetinaEnabled;
  125. int _retinaFactor; // Should be 1 or 2
  126. float _frameZoomFactor;
  127. GLFWwindow* _mainWindow;
  128. GLFWmonitor* _monitor;
  129. std::string _glfwError;
  130. float _mouseX;
  131. float _mouseY;
  132. friend class GLFWEventHandler;
  133. public:
  134. // View will trigger an event when window is resized, gains or loses focus
  135. static const std::string EVENT_WINDOW_RESIZED;
  136. static const std::string EVENT_WINDOW_FOCUSED;
  137. static const std::string EVENT_WINDOW_UNFOCUSED;
  138. private:
  139. CC_DISALLOW_COPY_AND_ASSIGN(GLViewImpl);
  140. };
  141. NS_CC_END // end of namespace cocos2d
  142. #endif // end of __CC_EGLViewImpl_DESKTOP_H__