CCGLView-desktop.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. #include "platform/desktop/CCGLView-desktop.h"
  23. #include "scripting/js-bindings/event/EventDispatcher.h"
  24. #include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
  25. #include "base/ccMacros.h"
  26. #include "base/ccUtils.h"
  27. #include "platform/CCApplication.h"
  28. #include "cocos/ui/edit-box/EditBox.h"
  29. #include <cmath>
  30. #include <unordered_map>
  31. NS_CC_BEGIN
  32. GLView* GLFWEventHandler::_view = nullptr;
  33. namespace
  34. {
  35. struct RGBA
  36. {
  37. int r = 0;
  38. int g = 0;
  39. int b = 0;
  40. int a = 0;
  41. };
  42. struct DepthInfo
  43. {
  44. int depth = 0;
  45. int stencil = 0;
  46. };
  47. struct RGBA pixelformat2RGBA(Application::PixelFormat pixelformat)
  48. {
  49. struct RGBA ret;
  50. if (Application::PixelFormat::RGBA8 == pixelformat)
  51. ret.r = ret.g = ret.b = ret. a = 8;
  52. if (Application::PixelFormat::RGB565 == pixelformat)
  53. {
  54. ret.r = 5;
  55. ret.g = 6;
  56. ret.b = 5;
  57. }
  58. if (Application::PixelFormat::RGB8 == pixelformat)
  59. ret.r = ret.g = ret.b = 8;
  60. return ret;
  61. }
  62. struct DepthInfo depthformat2DepthInfo(Application::DepthFormat depthFormat)
  63. {
  64. struct DepthInfo ret;
  65. switch (depthFormat)
  66. {
  67. case Application::DepthFormat::NONE:
  68. break;
  69. case Application::DepthFormat::DEPTH_COMPONENT16:
  70. ret.depth = 16;
  71. break;
  72. case Application::DepthFormat::DEPTH_COMPONENT24:
  73. ret.depth = 24;
  74. break;
  75. case Application::DepthFormat::DEPTH_COMPONENT32F:
  76. ret.depth = 32;
  77. break;
  78. case Application::DepthFormat::DEPTH24_STENCIL8:
  79. ret.depth = 24;
  80. ret.stencil = 8;
  81. break;
  82. case Application::DepthFormat::DEPTH32F_STENCIL8:
  83. ret.depth = 32;
  84. ret.stencil = 8;
  85. break;
  86. case Application::DepthFormat::STENCIL_INDEX8:
  87. ret.stencil = 8;
  88. break;
  89. default:
  90. break;
  91. }
  92. return ret;
  93. }
  94. }
  95. //////////////////////////////////////////////////////////////////////////
  96. // implement GLView
  97. //////////////////////////////////////////////////////////////////////////
  98. GLView::GLView(Application* application, const std::string& name, int x, int y, int width, int height,
  99. Application::PixelFormat pixelformat, Application::DepthFormat depthFormat, int multisamplingCount)
  100. : _application(application)
  101. , _mainWindow(nullptr)
  102. , _monitor(nullptr)
  103. , _mouseX(0.0f)
  104. , _mouseY(0.0f)
  105. {
  106. GLFWEventHandler::setGLView(this);
  107. glfwSetErrorCallback(GLFWEventHandler::onGLFWError);
  108. glfwInit();
  109. struct RGBA rgba = pixelformat2RGBA(pixelformat);
  110. struct DepthInfo depthInfo = depthformat2DepthInfo(depthFormat);
  111. glfwWindowHint(GLFW_RED_BITS, rgba.r);
  112. glfwWindowHint(GLFW_GREEN_BITS, rgba.g);
  113. glfwWindowHint(GLFW_BLUE_BITS, rgba.b);
  114. glfwWindowHint(GLFW_ALPHA_BITS, rgba.a);
  115. glfwWindowHint(GLFW_DEPTH_BITS, depthInfo.depth);
  116. glfwWindowHint(GLFW_STENCIL_BITS, depthInfo.stencil);
  117. glfwWindowHint(GLFW_SAMPLES, multisamplingCount);
  118. _mainWindow = glfwCreateWindow(width, height, name.c_str(), _monitor, nullptr);
  119. if (_mainWindow == nullptr)
  120. {
  121. std::string message = "Can't create window";
  122. if (!_glfwError.empty())
  123. {
  124. message.append("\nMore info: \n");
  125. message.append(_glfwError);
  126. }
  127. printf("%s\n", message.c_str());
  128. return;
  129. }
  130. glfwMakeContextCurrent(_mainWindow);
  131. glfwSetMouseButtonCallback(_mainWindow, GLFWEventHandler::onGLFWMouseCallBack);
  132. glfwSetCursorPosCallback(_mainWindow, GLFWEventHandler::onGLFWMouseMoveCallBack);
  133. glfwSetScrollCallback(_mainWindow, GLFWEventHandler::onGLFWMouseScrollCallback);
  134. glfwSetCharCallback(_mainWindow, GLFWEventHandler::onGLFWCharCallback);
  135. glfwSetKeyCallback(_mainWindow, GLFWEventHandler::onGLFWKeyCallback);
  136. glfwSetWindowIconifyCallback(_mainWindow, GLFWEventHandler::onGLFWWindowIconifyCallback);
  137. glfwSetWindowSizeCallback(_mainWindow, GLFWEventHandler::onGLFWWindowSizeFunCallback);
  138. // check OpenGL version at first
  139. const GLubyte* glVersion = glGetString(GL_VERSION);
  140. if (utils::atof((const char*)glVersion) < 1.5 )
  141. {
  142. char strComplain[256] = {0};
  143. sprintf(strComplain,
  144. "OpenGL 1.5 or higher is required (your version is %s). Please upgrade the driver of your video card.",
  145. glVersion);
  146. printf("%s\n", strComplain);
  147. return;
  148. }
  149. initGlew();
  150. // Enable point size by default.
  151. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
  152. if(multisamplingCount > 0)
  153. glEnable(GL_MULTISAMPLE);
  154. computeScale();
  155. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_mainFBO);
  156. Application::getInstance()->updateViewSize(width * _scale, height * _scale);
  157. }
  158. GLView::~GLView()
  159. {
  160. GLFWEventHandler::setGLView(nullptr);
  161. glfwTerminate();
  162. }
  163. bool GLView::windowShouldClose() const
  164. {
  165. if(_mainWindow)
  166. return glfwWindowShouldClose(_mainWindow) ? true : false;
  167. else
  168. return true;
  169. }
  170. void GLView::pollEvents()
  171. {
  172. glfwPollEvents();
  173. }
  174. void GLView::swapBuffers()
  175. {
  176. glfwSwapBuffers(_mainWindow);
  177. }
  178. float GLView::getScale() const
  179. {
  180. return _scale;
  181. }
  182. GLint GLView::getMainFBO() const
  183. {
  184. return _mainFBO;
  185. }
  186. void GLView::setIsEditboxEditing(bool value)
  187. {
  188. _isEditboxEditing = value;
  189. }
  190. void GLView::onGLFWError(int errorID, const char* errorDesc)
  191. {
  192. if (_mainWindow)
  193. printf("GLFWError #%d Happen, %s\n", errorID, errorDesc);
  194. else
  195. printf("GLFWError #%d Happen, %s\n", errorID, errorDesc);
  196. }
  197. namespace
  198. {
  199. void dispatchMouseEvent(double x, double y, unsigned short button, cocos2d::MouseEvent::Type type)
  200. {
  201. cocos2d::MouseEvent mouseEvent;
  202. mouseEvent.x = x;
  203. mouseEvent.y = y;
  204. mouseEvent.button = button;
  205. mouseEvent.type = type;
  206. cocos2d::EventDispatcher::dispatchMouseEvent(mouseEvent);
  207. }
  208. }
  209. void GLView::onGLFWMouseCallBack(GLFWwindow* /*window*/, int button, int action, int /*modify*/)
  210. {
  211. if (_isEditboxEditing)
  212. EditBox::complete();
  213. unsigned short jsButton;
  214. if (GLFW_MOUSE_BUTTON_LEFT == button)
  215. jsButton = 0;
  216. else if (GLFW_MOUSE_BUTTON_MIDDLE == button)
  217. jsButton = 1;
  218. else if (GLFW_MOUSE_BUTTON_RIGHT == button)
  219. jsButton = 2;
  220. if (GLFW_PRESS == action)
  221. {
  222. dispatchMouseEvent(_mouseX, _mouseY, jsButton, MouseEvent::Type::DOWN);
  223. }
  224. else if (GLFW_RELEASE == action)
  225. {
  226. dispatchMouseEvent(_mouseX, _mouseY, jsButton, MouseEvent::Type::UP);
  227. }
  228. }
  229. void GLView::onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y)
  230. {
  231. if (_isEditboxEditing)
  232. return;
  233. dispatchMouseEvent(x, y, 0, MouseEvent::Type::WHEEL);
  234. }
  235. void GLView::onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
  236. {
  237. _mouseX = (float)x;
  238. _mouseY = (float)y;
  239. if (_isEditboxEditing)
  240. return;
  241. dispatchMouseEvent(_mouseX, _mouseY, 0, MouseEvent::Type::MOVE);
  242. }
  243. void GLView::onGLFWKeyCallback(GLFWwindow* /*window*/, int key, int /*scancode*/, int action, int mods)
  244. {
  245. // printf("key: %d, action: %d, mods: %d\n", key, action, mods);
  246. int keyInWeb = -1;
  247. if (key >= GLFW_KEY_0 && key <= GLFW_KEY_9)
  248. keyInWeb = key;
  249. else if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z)
  250. keyInWeb = key;
  251. else if (key >= GLFW_KEY_F1 && key <= GLFW_KEY_F12)
  252. keyInWeb = key - 178;
  253. else if (key >= GLFW_KEY_KP_0 && key <= GLFW_KEY_KP_9)
  254. keyInWeb = key - 272 + 10000; // For indicating number in Numberpad, needs to be converted in JS.
  255. else if (key == GLFW_KEY_ESCAPE)
  256. keyInWeb = 27;
  257. else if (key == GLFW_KEY_MINUS)
  258. keyInWeb = 189;
  259. else if (key == GLFW_KEY_EQUAL)
  260. keyInWeb = 187;
  261. else if (key == GLFW_KEY_BACKSLASH)
  262. keyInWeb = 220;
  263. else if (key == GLFW_KEY_GRAVE_ACCENT)
  264. keyInWeb = 192;
  265. else if (key == GLFW_KEY_BACKSPACE)
  266. keyInWeb = 8;
  267. else if (key == GLFW_KEY_ENTER)
  268. keyInWeb = 13;
  269. else if (key == GLFW_KEY_LEFT_BRACKET)
  270. keyInWeb = 219;
  271. else if (key == GLFW_KEY_RIGHT_BRACKET)
  272. keyInWeb = 221;
  273. else if (key == GLFW_KEY_SEMICOLON)
  274. keyInWeb = 186;
  275. else if (key == GLFW_KEY_APOSTROPHE)
  276. keyInWeb = 222;
  277. else if (key == GLFW_KEY_TAB)
  278. keyInWeb = 9;
  279. else if (key == GLFW_KEY_LEFT_CONTROL)
  280. keyInWeb = 17;
  281. else if (key == GLFW_KEY_RIGHT_CONTROL)
  282. keyInWeb = 17 + 20000; // For indicating Left/Right control, needs to be converted in JS.
  283. else if (key == GLFW_KEY_LEFT_SHIFT)
  284. keyInWeb = 16;
  285. else if (key == GLFW_KEY_RIGHT_SHIFT)
  286. keyInWeb = 16 + 20000; // For indicating Left/Right shift, needs to be converted in JS.
  287. else if (key == GLFW_KEY_LEFT_ALT)
  288. keyInWeb = 18;
  289. else if (key == GLFW_KEY_RIGHT_ALT)
  290. keyInWeb = 18 + 20000; // For indicating Left/Right alt, needs to be converted in JS.
  291. else if (key == GLFW_KEY_LEFT_SUPER)
  292. keyInWeb = 91;
  293. else if (key == GLFW_KEY_RIGHT_SUPER)
  294. keyInWeb = 93;
  295. else if (key == GLFW_KEY_UP)
  296. keyInWeb = 38;
  297. else if (key == GLFW_KEY_DOWN)
  298. keyInWeb = 40;
  299. else if (key == GLFW_KEY_LEFT)
  300. keyInWeb = 37;
  301. else if (key == GLFW_KEY_RIGHT)
  302. keyInWeb = 39;
  303. else if (key == GLFW_KEY_MENU)
  304. keyInWeb = 93 + 20000;
  305. else if (key == GLFW_KEY_KP_ENTER)
  306. keyInWeb = 13 + 20000; // For indicating numpad enter, needs to be converted in JS.
  307. else if (key == GLFW_KEY_KP_ADD)
  308. keyInWeb = 107;
  309. else if (key == GLFW_KEY_KP_SUBTRACT)
  310. keyInWeb = 109;
  311. else if (key == GLFW_KEY_KP_MULTIPLY)
  312. keyInWeb = 106;
  313. else if (key == GLFW_KEY_KP_DIVIDE)
  314. keyInWeb = 111;
  315. else if (key == GLFW_KEY_NUM_LOCK)
  316. keyInWeb = 12;
  317. else if (key == GLFW_KEY_F13)
  318. keyInWeb = 124;
  319. else if (key == GLFW_KEY_BACKSPACE)
  320. keyInWeb = 8;
  321. else if (key == GLFW_KEY_HOME)
  322. keyInWeb = 36;
  323. else if (key == GLFW_KEY_PAGE_UP)
  324. keyInWeb = 33;
  325. else if (key == GLFW_KEY_PAGE_DOWN)
  326. keyInWeb = 34;
  327. else if (key == GLFW_KEY_END)
  328. keyInWeb = 35;
  329. else if (key == GLFW_KEY_COMMA)
  330. keyInWeb = 188;
  331. else if (key == GLFW_KEY_PERIOD)
  332. keyInWeb = 190;
  333. else if (key == GLFW_KEY_SLASH)
  334. keyInWeb = 191;
  335. else if (key == GLFW_KEY_SPACE)
  336. keyInWeb = 32;
  337. else if (key == GLFW_KEY_DELETE)
  338. keyInWeb = 46;
  339. else if (key == GLFW_KEY_KP_DECIMAL)
  340. keyInWeb = 110;
  341. else if (key == GLFW_KEY_CAPS_LOCK)
  342. keyInWeb = 20;
  343. KeyboardEvent event;
  344. event.key = keyInWeb;
  345. if (action == GLFW_PRESS)
  346. event.action = KeyboardEvent::Action::PRESS;
  347. else if (action == GLFW_RELEASE)
  348. event.action = KeyboardEvent::Action::RELEASE;
  349. else if (action == GLFW_REPEAT)
  350. event.action = KeyboardEvent::Action::REPEAT;
  351. if (mods & GLFW_MOD_SHIFT)
  352. event.shiftKeyActive = true;
  353. if (mods & GLFW_MOD_CONTROL)
  354. event.ctrlKeyActive = true;
  355. if (mods & GLFW_MOD_ALT)
  356. event.altKeyActive = true;
  357. if (mods & GLFW_MOD_SUPER)
  358. event.metaKeyActive = true;
  359. EventDispatcher::dispatchKeyboardEvent(event);
  360. }
  361. void GLView::onGLFWCharCallback(GLFWwindow* /*window*/, unsigned int character)
  362. {
  363. // REFINE
  364. // char16_t wcharString[2] = { (char16_t) character, 0 };
  365. // std::string utf8String;
  366. // StringUtils::UTF16ToUTF8( wcharString, utf8String );
  367. // static std::set<std::string> controlUnicode = {
  368. // "\xEF\x9C\x80", // up
  369. // "\xEF\x9C\x81", // down
  370. // "\xEF\x9C\x82", // left
  371. // "\xEF\x9C\x83", // right
  372. // "\xEF\x9C\xA8", // delete
  373. // "\xEF\x9C\xA9", // home
  374. // "\xEF\x9C\xAB", // end
  375. // "\xEF\x9C\xAC", // pageup
  376. // "\xEF\x9C\xAD", // pagedown
  377. // "\xEF\x9C\xB9" // clear
  378. // };
  379. }
  380. void GLView::onGLFWWindowIconifyCallback(GLFWwindow* /*window*/, int iconified)
  381. {
  382. if (iconified == GL_TRUE)
  383. _application->onPause();
  384. else
  385. _application->onResume();
  386. }
  387. void GLView::onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height)
  388. {
  389. int targetWidth = width * _scale, targetHeight = height * _scale;
  390. Application::getInstance()->updateViewSize(targetWidth, targetHeight);
  391. EventDispatcher::dispatchResizeEvent(targetWidth, targetHeight);
  392. }
  393. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  394. static bool glew_dynamic_binding()
  395. {
  396. const char *gl_extensions = (const char*)glGetString(GL_EXTENSIONS);
  397. // If the current opengl driver doesn't have framebuffers methods, check if an extension exists
  398. if (glGenFramebuffers == nullptr)
  399. {
  400. log("OpenGL: glGenFramebuffers is nullptr, try to detect an extension");
  401. if (strstr(gl_extensions, "ARB_framebuffer_object"))
  402. {
  403. log("OpenGL: ARB_framebuffer_object is supported");
  404. glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbuffer");
  405. glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC) wglGetProcAddress("glBindRenderbuffer");
  406. glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC) wglGetProcAddress("glDeleteRenderbuffers");
  407. glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC) wglGetProcAddress("glGenRenderbuffers");
  408. glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC) wglGetProcAddress("glRenderbufferStorage");
  409. glGetRenderbufferParameteriv = (PFNGLGETRENDERBUFFERPARAMETERIVPROC) wglGetProcAddress("glGetRenderbufferParameteriv");
  410. glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC) wglGetProcAddress("glIsFramebuffer");
  411. glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC) wglGetProcAddress("glBindFramebuffer");
  412. glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC) wglGetProcAddress("glDeleteFramebuffers");
  413. glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC) wglGetProcAddress("glGenFramebuffers");
  414. glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC) wglGetProcAddress("glCheckFramebufferStatus");
  415. glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DPROC) wglGetProcAddress("glFramebufferTexture1D");
  416. glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) wglGetProcAddress("glFramebufferTexture2D");
  417. glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC) wglGetProcAddress("glFramebufferTexture3D");
  418. glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC) wglGetProcAddress("glFramebufferRenderbuffer");
  419. glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC) wglGetProcAddress("glGetFramebufferAttachmentParameteriv");
  420. glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmap");
  421. }
  422. else
  423. if (strstr(gl_extensions, "EXT_framebuffer_object"))
  424. {
  425. log("OpenGL: EXT_framebuffer_object is supported");
  426. glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbufferEXT");
  427. glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC) wglGetProcAddress("glBindRenderbufferEXT");
  428. glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC) wglGetProcAddress("glDeleteRenderbuffersEXT");
  429. glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC) wglGetProcAddress("glGenRenderbuffersEXT");
  430. glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC) wglGetProcAddress("glRenderbufferStorageEXT");
  431. glGetRenderbufferParameteriv = (PFNGLGETRENDERBUFFERPARAMETERIVPROC) wglGetProcAddress("glGetRenderbufferParameterivEXT");
  432. glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC) wglGetProcAddress("glIsFramebufferEXT");
  433. glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC) wglGetProcAddress("glBindFramebufferEXT");
  434. glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC) wglGetProcAddress("glDeleteFramebuffersEXT");
  435. glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC) wglGetProcAddress("glGenFramebuffersEXT");
  436. glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC) wglGetProcAddress("glCheckFramebufferStatusEXT");
  437. glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DPROC) wglGetProcAddress("glFramebufferTexture1DEXT");
  438. glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) wglGetProcAddress("glFramebufferTexture2DEXT");
  439. glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC) wglGetProcAddress("glFramebufferTexture3DEXT");
  440. glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC) wglGetProcAddress("glFramebufferRenderbufferEXT");
  441. glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC) wglGetProcAddress("glGetFramebufferAttachmentParameterivEXT");
  442. glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmapEXT");
  443. }
  444. else
  445. {
  446. log("OpenGL: No framebuffers extension is supported");
  447. log("OpenGL: Any call to Fbo will crash!");
  448. return false;
  449. }
  450. }
  451. return true;
  452. }
  453. #endif
  454. void GLView::computeScale()
  455. {
  456. int widthInPixel = 0;
  457. glfwGetFramebufferSize(_mainWindow, &widthInPixel, nullptr);
  458. int width = 0;
  459. glfwGetWindowSize(_mainWindow, &width, nullptr);
  460. _scale = float(widthInPixel) / width;
  461. }
  462. // helper
  463. bool GLView::initGlew()
  464. {
  465. #if (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
  466. GLenum GlewInitResult = glewInit();
  467. if (GLEW_OK != GlewInitResult)
  468. {
  469. log((char *)glewGetErrorString(GlewInitResult), "OpenGL error");
  470. return false;
  471. }
  472. if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
  473. {
  474. log("Ready for GLSL");
  475. }
  476. else
  477. {
  478. log("Not totally ready :(");
  479. }
  480. if (glewIsSupported("GL_VERSION_2_0"))
  481. {
  482. log("Ready for OpenGL 2.0");
  483. }
  484. else
  485. {
  486. log("OpenGL 2.0 not supported");
  487. }
  488. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  489. if(glew_dynamic_binding() == false)
  490. {
  491. log("No OpenGL framebuffer support. Please upgrade the driver of your video card.", "OpenGL error");
  492. return false;
  493. }
  494. #endif
  495. #endif // (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
  496. return true;
  497. }
  498. NS_CC_END // end of namespace cocos2d;