CCGLView-desktop.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #pragma once
  23. #include "platform/CCGL.h"
  24. #include "base/ccMacros.h"
  25. #include "platform/CCApplication.h"
  26. #include "glfw3/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 "glfw3/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 "glfw3/glfw3native.h"
  44. #endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  45. NS_CC_BEGIN
  46. class CC_DLL GLView final
  47. {
  48. public:
  49. GLView(Application *application, const std::string& name, int x, int y, int width, int height,
  50. Application::PixelFormat pixelformat, Application::DepthFormat depthFormat, int multiSampleCount);
  51. ~GLView();
  52. bool windowShouldClose() const;
  53. void pollEvents();
  54. void swapBuffers();
  55. float getScale() const;
  56. GLint getMainFBO() const;
  57. void setIsEditboxEditing(bool value);
  58. inline GLFWwindow* getGLFWWindow() const {return _mainWindow;};
  59. private:
  60. bool initGlew();
  61. void computeScale();
  62. // GLFW callbacks
  63. void onGLFWError(int errorID, const char* errorDesc);
  64. void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify);
  65. void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y);
  66. void onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y);
  67. void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
  68. void onGLFWCharCallback(GLFWwindow* window, unsigned int character);
  69. void onGLFWWindowIconifyCallback(GLFWwindow* window, int iconified);
  70. void onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height);
  71. GLFWwindow* _mainWindow = nullptr;
  72. GLFWmonitor* _monitor = nullptr;
  73. GLint _mainFBO = -1;
  74. std::string _glfwError;
  75. Application *_application = nullptr;
  76. float _mouseX = 0;
  77. float _mouseY = 0;
  78. // (framebuffer size) / (window size)
  79. float _scale = 1.0f;
  80. bool _isEditboxEditing = false;
  81. friend class GLFWEventHandler;
  82. };
  83. class CC_DLL GLFWEventHandler final
  84. {
  85. public:
  86. static void onGLFWError(int errorID, const char* errorDesc)
  87. {
  88. _view->onGLFWError(errorID, errorDesc);
  89. }
  90. static void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
  91. {
  92. _view->onGLFWMouseCallBack(window, button, action, modify);
  93. }
  94. static void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
  95. {
  96. _view->onGLFWMouseMoveCallBack(window, x, y);
  97. }
  98. static void onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y)
  99. {
  100. _view->onGLFWMouseScrollCallback(window, x, y);
  101. }
  102. static void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
  103. {
  104. _view->onGLFWKeyCallback(window, key, scancode, action, mods);
  105. }
  106. static void onGLFWCharCallback(GLFWwindow* window, unsigned int character)
  107. {
  108. _view->onGLFWCharCallback(window, character);
  109. }
  110. static void setGLView(GLView* view)
  111. {
  112. _view = view;
  113. }
  114. static void onGLFWWindowIconifyCallback(GLFWwindow* window, int iconified)
  115. {
  116. _view->onGLFWWindowIconifyCallback(window, iconified);
  117. }
  118. static void onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height)
  119. {
  120. _view->onGLFWWindowSizeFunCallback(window, width, height);
  121. }
  122. private:
  123. static GLView* _view;
  124. };
  125. NS_CC_END // end of namespace cocos2d