CCGLViewImpl-desktop.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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/CCGLViewImpl-desktop.h"
  23. #include <cmath>
  24. #include <unordered_map>
  25. #include "platform/CCApplication.h"
  26. #include "base/CCDirector.h"
  27. #include "base/CCTouch.h"
  28. #include "base/CCEventDispatcher.h"
  29. #include "base/CCEventKeyboard.h"
  30. #include "base/CCEventMouse.h"
  31. #include "base/CCIMEDispatcher.h"
  32. #include "base/ccUtils.h"
  33. #include "base/ccUTF8.h"
  34. #include "2d/CCCamera.h"
  35. NS_CC_BEGIN
  36. GLViewImpl* GLFWEventHandler::_view = nullptr;
  37. const std::string GLViewImpl::EVENT_WINDOW_RESIZED = "glview_window_resized";
  38. const std::string GLViewImpl::EVENT_WINDOW_FOCUSED = "glview_window_focused";
  39. const std::string GLViewImpl::EVENT_WINDOW_UNFOCUSED = "glview_window_unfocused";
  40. ////////////////////////////////////////////////////
  41. struct keyCodeItem
  42. {
  43. int glfwKeyCode;
  44. EventKeyboard::KeyCode keyCode;
  45. };
  46. static std::unordered_map<int, EventKeyboard::KeyCode> g_keyCodeMap;
  47. static keyCodeItem g_keyCodeStructArray[] = {
  48. /* The unknown key */
  49. { GLFW_KEY_UNKNOWN , EventKeyboard::KeyCode::KEY_NONE },
  50. /* Printable keys */
  51. { GLFW_KEY_SPACE , EventKeyboard::KeyCode::KEY_SPACE },
  52. { GLFW_KEY_APOSTROPHE , EventKeyboard::KeyCode::KEY_APOSTROPHE },
  53. { GLFW_KEY_COMMA , EventKeyboard::KeyCode::KEY_COMMA },
  54. { GLFW_KEY_MINUS , EventKeyboard::KeyCode::KEY_MINUS },
  55. { GLFW_KEY_PERIOD , EventKeyboard::KeyCode::KEY_PERIOD },
  56. { GLFW_KEY_SLASH , EventKeyboard::KeyCode::KEY_SLASH },
  57. { GLFW_KEY_0 , EventKeyboard::KeyCode::KEY_0 },
  58. { GLFW_KEY_1 , EventKeyboard::KeyCode::KEY_1 },
  59. { GLFW_KEY_2 , EventKeyboard::KeyCode::KEY_2 },
  60. { GLFW_KEY_3 , EventKeyboard::KeyCode::KEY_3 },
  61. { GLFW_KEY_4 , EventKeyboard::KeyCode::KEY_4 },
  62. { GLFW_KEY_5 , EventKeyboard::KeyCode::KEY_5 },
  63. { GLFW_KEY_6 , EventKeyboard::KeyCode::KEY_6 },
  64. { GLFW_KEY_7 , EventKeyboard::KeyCode::KEY_7 },
  65. { GLFW_KEY_8 , EventKeyboard::KeyCode::KEY_8 },
  66. { GLFW_KEY_9 , EventKeyboard::KeyCode::KEY_9 },
  67. { GLFW_KEY_SEMICOLON , EventKeyboard::KeyCode::KEY_SEMICOLON },
  68. { GLFW_KEY_EQUAL , EventKeyboard::KeyCode::KEY_EQUAL },
  69. { GLFW_KEY_A , EventKeyboard::KeyCode::KEY_A },
  70. { GLFW_KEY_B , EventKeyboard::KeyCode::KEY_B },
  71. { GLFW_KEY_C , EventKeyboard::KeyCode::KEY_C },
  72. { GLFW_KEY_D , EventKeyboard::KeyCode::KEY_D },
  73. { GLFW_KEY_E , EventKeyboard::KeyCode::KEY_E },
  74. { GLFW_KEY_F , EventKeyboard::KeyCode::KEY_F },
  75. { GLFW_KEY_G , EventKeyboard::KeyCode::KEY_G },
  76. { GLFW_KEY_H , EventKeyboard::KeyCode::KEY_H },
  77. { GLFW_KEY_I , EventKeyboard::KeyCode::KEY_I },
  78. { GLFW_KEY_J , EventKeyboard::KeyCode::KEY_J },
  79. { GLFW_KEY_K , EventKeyboard::KeyCode::KEY_K },
  80. { GLFW_KEY_L , EventKeyboard::KeyCode::KEY_L },
  81. { GLFW_KEY_M , EventKeyboard::KeyCode::KEY_M },
  82. { GLFW_KEY_N , EventKeyboard::KeyCode::KEY_N },
  83. { GLFW_KEY_O , EventKeyboard::KeyCode::KEY_O },
  84. { GLFW_KEY_P , EventKeyboard::KeyCode::KEY_P },
  85. { GLFW_KEY_Q , EventKeyboard::KeyCode::KEY_Q },
  86. { GLFW_KEY_R , EventKeyboard::KeyCode::KEY_R },
  87. { GLFW_KEY_S , EventKeyboard::KeyCode::KEY_S },
  88. { GLFW_KEY_T , EventKeyboard::KeyCode::KEY_T },
  89. { GLFW_KEY_U , EventKeyboard::KeyCode::KEY_U },
  90. { GLFW_KEY_V , EventKeyboard::KeyCode::KEY_V },
  91. { GLFW_KEY_W , EventKeyboard::KeyCode::KEY_W },
  92. { GLFW_KEY_X , EventKeyboard::KeyCode::KEY_X },
  93. { GLFW_KEY_Y , EventKeyboard::KeyCode::KEY_Y },
  94. { GLFW_KEY_Z , EventKeyboard::KeyCode::KEY_Z },
  95. { GLFW_KEY_LEFT_BRACKET , EventKeyboard::KeyCode::KEY_LEFT_BRACKET },
  96. { GLFW_KEY_BACKSLASH , EventKeyboard::KeyCode::KEY_BACK_SLASH },
  97. { GLFW_KEY_RIGHT_BRACKET , EventKeyboard::KeyCode::KEY_RIGHT_BRACKET },
  98. { GLFW_KEY_GRAVE_ACCENT , EventKeyboard::KeyCode::KEY_GRAVE },
  99. { GLFW_KEY_WORLD_1 , EventKeyboard::KeyCode::KEY_GRAVE },
  100. { GLFW_KEY_WORLD_2 , EventKeyboard::KeyCode::KEY_NONE },
  101. /* Function keys */
  102. { GLFW_KEY_ESCAPE , EventKeyboard::KeyCode::KEY_ESCAPE },
  103. { GLFW_KEY_ENTER , EventKeyboard::KeyCode::KEY_ENTER },
  104. { GLFW_KEY_TAB , EventKeyboard::KeyCode::KEY_TAB },
  105. { GLFW_KEY_BACKSPACE , EventKeyboard::KeyCode::KEY_BACKSPACE },
  106. { GLFW_KEY_INSERT , EventKeyboard::KeyCode::KEY_INSERT },
  107. { GLFW_KEY_DELETE , EventKeyboard::KeyCode::KEY_DELETE },
  108. { GLFW_KEY_RIGHT , EventKeyboard::KeyCode::KEY_RIGHT_ARROW },
  109. { GLFW_KEY_LEFT , EventKeyboard::KeyCode::KEY_LEFT_ARROW },
  110. { GLFW_KEY_DOWN , EventKeyboard::KeyCode::KEY_DOWN_ARROW },
  111. { GLFW_KEY_UP , EventKeyboard::KeyCode::KEY_UP_ARROW },
  112. { GLFW_KEY_PAGE_UP , EventKeyboard::KeyCode::KEY_PG_UP },
  113. { GLFW_KEY_PAGE_DOWN , EventKeyboard::KeyCode::KEY_PG_DOWN },
  114. { GLFW_KEY_HOME , EventKeyboard::KeyCode::KEY_HOME },
  115. { GLFW_KEY_END , EventKeyboard::KeyCode::KEY_END },
  116. { GLFW_KEY_CAPS_LOCK , EventKeyboard::KeyCode::KEY_CAPS_LOCK },
  117. { GLFW_KEY_SCROLL_LOCK , EventKeyboard::KeyCode::KEY_SCROLL_LOCK },
  118. { GLFW_KEY_NUM_LOCK , EventKeyboard::KeyCode::KEY_NUM_LOCK },
  119. { GLFW_KEY_PRINT_SCREEN , EventKeyboard::KeyCode::KEY_PRINT },
  120. { GLFW_KEY_PAUSE , EventKeyboard::KeyCode::KEY_PAUSE },
  121. { GLFW_KEY_F1 , EventKeyboard::KeyCode::KEY_F1 },
  122. { GLFW_KEY_F2 , EventKeyboard::KeyCode::KEY_F2 },
  123. { GLFW_KEY_F3 , EventKeyboard::KeyCode::KEY_F3 },
  124. { GLFW_KEY_F4 , EventKeyboard::KeyCode::KEY_F4 },
  125. { GLFW_KEY_F5 , EventKeyboard::KeyCode::KEY_F5 },
  126. { GLFW_KEY_F6 , EventKeyboard::KeyCode::KEY_F6 },
  127. { GLFW_KEY_F7 , EventKeyboard::KeyCode::KEY_F7 },
  128. { GLFW_KEY_F8 , EventKeyboard::KeyCode::KEY_F8 },
  129. { GLFW_KEY_F9 , EventKeyboard::KeyCode::KEY_F9 },
  130. { GLFW_KEY_F10 , EventKeyboard::KeyCode::KEY_F10 },
  131. { GLFW_KEY_F11 , EventKeyboard::KeyCode::KEY_F11 },
  132. { GLFW_KEY_F12 , EventKeyboard::KeyCode::KEY_F12 },
  133. { GLFW_KEY_F13 , EventKeyboard::KeyCode::KEY_NONE },
  134. { GLFW_KEY_F14 , EventKeyboard::KeyCode::KEY_NONE },
  135. { GLFW_KEY_F15 , EventKeyboard::KeyCode::KEY_NONE },
  136. { GLFW_KEY_F16 , EventKeyboard::KeyCode::KEY_NONE },
  137. { GLFW_KEY_F17 , EventKeyboard::KeyCode::KEY_NONE },
  138. { GLFW_KEY_F18 , EventKeyboard::KeyCode::KEY_NONE },
  139. { GLFW_KEY_F19 , EventKeyboard::KeyCode::KEY_NONE },
  140. { GLFW_KEY_F20 , EventKeyboard::KeyCode::KEY_NONE },
  141. { GLFW_KEY_F21 , EventKeyboard::KeyCode::KEY_NONE },
  142. { GLFW_KEY_F22 , EventKeyboard::KeyCode::KEY_NONE },
  143. { GLFW_KEY_F23 , EventKeyboard::KeyCode::KEY_NONE },
  144. { GLFW_KEY_F24 , EventKeyboard::KeyCode::KEY_NONE },
  145. { GLFW_KEY_F25 , EventKeyboard::KeyCode::KEY_NONE },
  146. { GLFW_KEY_KP_0 , EventKeyboard::KeyCode::KEY_0 },
  147. { GLFW_KEY_KP_1 , EventKeyboard::KeyCode::KEY_1 },
  148. { GLFW_KEY_KP_2 , EventKeyboard::KeyCode::KEY_2 },
  149. { GLFW_KEY_KP_3 , EventKeyboard::KeyCode::KEY_3 },
  150. { GLFW_KEY_KP_4 , EventKeyboard::KeyCode::KEY_4 },
  151. { GLFW_KEY_KP_5 , EventKeyboard::KeyCode::KEY_5 },
  152. { GLFW_KEY_KP_6 , EventKeyboard::KeyCode::KEY_6 },
  153. { GLFW_KEY_KP_7 , EventKeyboard::KeyCode::KEY_7 },
  154. { GLFW_KEY_KP_8 , EventKeyboard::KeyCode::KEY_8 },
  155. { GLFW_KEY_KP_9 , EventKeyboard::KeyCode::KEY_9 },
  156. { GLFW_KEY_KP_DECIMAL , EventKeyboard::KeyCode::KEY_PERIOD },
  157. { GLFW_KEY_KP_DIVIDE , EventKeyboard::KeyCode::KEY_KP_DIVIDE },
  158. { GLFW_KEY_KP_MULTIPLY , EventKeyboard::KeyCode::KEY_KP_MULTIPLY },
  159. { GLFW_KEY_KP_SUBTRACT , EventKeyboard::KeyCode::KEY_KP_MINUS },
  160. { GLFW_KEY_KP_ADD , EventKeyboard::KeyCode::KEY_KP_PLUS },
  161. { GLFW_KEY_KP_ENTER , EventKeyboard::KeyCode::KEY_KP_ENTER },
  162. { GLFW_KEY_KP_EQUAL , EventKeyboard::KeyCode::KEY_EQUAL },
  163. { GLFW_KEY_LEFT_SHIFT , EventKeyboard::KeyCode::KEY_LEFT_SHIFT },
  164. { GLFW_KEY_LEFT_CONTROL , EventKeyboard::KeyCode::KEY_LEFT_CTRL },
  165. { GLFW_KEY_LEFT_ALT , EventKeyboard::KeyCode::KEY_LEFT_ALT },
  166. { GLFW_KEY_LEFT_SUPER , EventKeyboard::KeyCode::KEY_HYPER },
  167. { GLFW_KEY_RIGHT_SHIFT , EventKeyboard::KeyCode::KEY_RIGHT_SHIFT },
  168. { GLFW_KEY_RIGHT_CONTROL , EventKeyboard::KeyCode::KEY_RIGHT_CTRL },
  169. { GLFW_KEY_RIGHT_ALT , EventKeyboard::KeyCode::KEY_RIGHT_ALT },
  170. { GLFW_KEY_RIGHT_SUPER , EventKeyboard::KeyCode::KEY_HYPER },
  171. { GLFW_KEY_MENU , EventKeyboard::KeyCode::KEY_MENU },
  172. { GLFW_KEY_LAST , EventKeyboard::KeyCode::KEY_NONE }
  173. };
  174. //////////////////////////////////////////////////////////////////////////
  175. // implement GLViewImpl
  176. //////////////////////////////////////////////////////////////////////////
  177. GLViewImpl::GLViewImpl(bool initglfw)
  178. : _captured(false)
  179. , _supportTouch(false)
  180. , _isInRetinaMonitor(false)
  181. , _isRetinaEnabled(false)
  182. , _retinaFactor(1)
  183. , _frameZoomFactor(1.0f)
  184. , _mainWindow(nullptr)
  185. , _monitor(nullptr)
  186. , _mouseX(0.0f)
  187. , _mouseY(0.0f)
  188. {
  189. _viewName = "cocos2dx";
  190. g_keyCodeMap.clear();
  191. for (auto& item : g_keyCodeStructArray)
  192. {
  193. g_keyCodeMap[item.glfwKeyCode] = item.keyCode;
  194. }
  195. GLFWEventHandler::setGLViewImpl(this);
  196. if (initglfw)
  197. {
  198. glfwSetErrorCallback(GLFWEventHandler::onGLFWError);
  199. glfwInit();
  200. }
  201. }
  202. GLViewImpl::~GLViewImpl()
  203. {
  204. CCLOGINFO("deallocing GLViewImpl: %p", this);
  205. GLFWEventHandler::setGLViewImpl(nullptr);
  206. glfwTerminate();
  207. }
  208. GLViewImpl* GLViewImpl::create(const std::string& viewName)
  209. {
  210. return GLViewImpl::create(viewName, false);
  211. }
  212. GLViewImpl* GLViewImpl::create(const std::string& viewName, bool resizable)
  213. {
  214. auto ret = new (std::nothrow) GLViewImpl;
  215. if(ret && ret->initWithRect(viewName, Rect(0, 0, 960, 640), 1.0f, resizable)) {
  216. ret->autorelease();
  217. return ret;
  218. }
  219. CC_SAFE_DELETE(ret);
  220. return nullptr;
  221. }
  222. GLViewImpl* GLViewImpl::createWithRect(const std::string& viewName, Rect rect, float frameZoomFactor, bool resizable)
  223. {
  224. auto ret = new (std::nothrow) GLViewImpl;
  225. if(ret && ret->initWithRect(viewName, rect, frameZoomFactor, resizable)) {
  226. ret->autorelease();
  227. return ret;
  228. }
  229. CC_SAFE_DELETE(ret);
  230. return nullptr;
  231. }
  232. GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName)
  233. {
  234. auto ret = new (std::nothrow) GLViewImpl();
  235. if(ret && ret->initWithFullScreen(viewName)) {
  236. ret->autorelease();
  237. return ret;
  238. }
  239. CC_SAFE_DELETE(ret);
  240. return nullptr;
  241. }
  242. GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName, const GLFWvidmode &videoMode, GLFWmonitor *monitor)
  243. {
  244. auto ret = new (std::nothrow) GLViewImpl();
  245. if(ret && ret->initWithFullscreen(viewName, videoMode, monitor)) {
  246. ret->autorelease();
  247. return ret;
  248. }
  249. CC_SAFE_DELETE(ret);
  250. return nullptr;
  251. }
  252. bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor, bool resizable)
  253. {
  254. setViewName(viewName);
  255. _frameZoomFactor = frameZoomFactor;
  256. glfwWindowHint(GLFW_RESIZABLE,resizable?GL_TRUE:GL_FALSE);
  257. glfwWindowHint(GLFW_RED_BITS,_glContextAttrs.redBits);
  258. glfwWindowHint(GLFW_GREEN_BITS,_glContextAttrs.greenBits);
  259. glfwWindowHint(GLFW_BLUE_BITS,_glContextAttrs.blueBits);
  260. glfwWindowHint(GLFW_ALPHA_BITS,_glContextAttrs.alphaBits);
  261. glfwWindowHint(GLFW_DEPTH_BITS,_glContextAttrs.depthBits);
  262. glfwWindowHint(GLFW_STENCIL_BITS,_glContextAttrs.stencilBits);
  263. glfwWindowHint(GLFW_SAMPLES, _glContextAttrs.multisamplingCount);
  264. int neededWidth = rect.size.width * _frameZoomFactor;
  265. int neededHeight = rect.size.height * _frameZoomFactor;
  266. _mainWindow = glfwCreateWindow(neededWidth, neededHeight, _viewName.c_str(), _monitor, nullptr);
  267. if (_mainWindow == nullptr)
  268. {
  269. std::string message = "Can't create window";
  270. if (!_glfwError.empty())
  271. {
  272. message.append("\nMore info: \n");
  273. message.append(_glfwError);
  274. }
  275. MessageBox(message.c_str(), "Error launch application");
  276. return false;
  277. }
  278. /*
  279. * Note that the created window and context may differ from what you requested,
  280. * as not all parameters and hints are
  281. * [hard constraints](@ref window_hints_hard). This includes the size of the
  282. * window, especially for full screen windows. To retrieve the actual
  283. * attributes of the created window and context, use queries like @ref
  284. * glfwGetWindowAttrib and @ref glfwGetWindowSize.
  285. *
  286. * see declaration glfwCreateWindow
  287. */
  288. int realW = 0, realH = 0;
  289. glfwGetWindowSize(_mainWindow, &realW, &realH);
  290. if (realW != neededWidth)
  291. {
  292. rect.size.width = realW / _frameZoomFactor;
  293. }
  294. if (realH != neededHeight)
  295. {
  296. rect.size.height = realH / _frameZoomFactor;
  297. }
  298. glfwMakeContextCurrent(_mainWindow);
  299. glfwSetMouseButtonCallback(_mainWindow, GLFWEventHandler::onGLFWMouseCallBack);
  300. glfwSetCursorPosCallback(_mainWindow, GLFWEventHandler::onGLFWMouseMoveCallBack);
  301. glfwSetScrollCallback(_mainWindow, GLFWEventHandler::onGLFWMouseScrollCallback);
  302. glfwSetCharCallback(_mainWindow, GLFWEventHandler::onGLFWCharCallback);
  303. glfwSetKeyCallback(_mainWindow, GLFWEventHandler::onGLFWKeyCallback);
  304. glfwSetWindowPosCallback(_mainWindow, GLFWEventHandler::onGLFWWindowPosCallback);
  305. glfwSetFramebufferSizeCallback(_mainWindow, GLFWEventHandler::onGLFWframebuffersize);
  306. glfwSetWindowSizeCallback(_mainWindow, GLFWEventHandler::onGLFWWindowSizeFunCallback);
  307. glfwSetWindowIconifyCallback(_mainWindow, GLFWEventHandler::onGLFWWindowIconifyCallback);
  308. glfwSetWindowFocusCallback(_mainWindow, GLFWEventHandler::onGLFWWindowFocusCallback);
  309. setFrameSize(rect.size.width, rect.size.height);
  310. // check OpenGL version at first
  311. const GLubyte* glVersion = glGetString(GL_VERSION);
  312. if ( utils::atof((const char*)glVersion) < 1.5 )
  313. {
  314. char strComplain[256] = {0};
  315. sprintf(strComplain,
  316. "OpenGL 1.5 or higher is required (your version is %s). Please upgrade the driver of your video card.",
  317. glVersion);
  318. MessageBox(strComplain, "OpenGL version too old");
  319. return false;
  320. }
  321. initGlew();
  322. // Enable point size by default.
  323. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
  324. if(_glContextAttrs.multisamplingCount > 0)
  325. glEnable(GL_MULTISAMPLE);
  326. // // GLFW v3.2 no longer emits "onGLFWWindowSizeFunCallback" at creation time. Force default viewport:
  327. // setViewPortInPoints(0, 0, neededWidth, neededHeight);
  328. //
  329. return true;
  330. }
  331. bool GLViewImpl::initWithFullScreen(const std::string& viewName)
  332. {
  333. //Create fullscreen window on primary monitor at its current video mode.
  334. _monitor = glfwGetPrimaryMonitor();
  335. if (nullptr == _monitor)
  336. return false;
  337. const GLFWvidmode* videoMode = glfwGetVideoMode(_monitor);
  338. return initWithRect(viewName, Rect(0, 0, videoMode->width, videoMode->height), 1.0f, false);
  339. }
  340. bool GLViewImpl::initWithFullscreen(const std::string &viewname, const GLFWvidmode &videoMode, GLFWmonitor *monitor)
  341. {
  342. //Create fullscreen on specified monitor at the specified video mode.
  343. _monitor = monitor;
  344. if (nullptr == _monitor)
  345. return false;
  346. //These are soft constraints. If the video mode is retrieved at runtime, the resulting window and context should match these exactly. If invalid attribs are passed (eg. from an outdated cache), window creation will NOT fail but the actual window/context may differ.
  347. glfwWindowHint(GLFW_REFRESH_RATE, videoMode.refreshRate);
  348. glfwWindowHint(GLFW_RED_BITS, videoMode.redBits);
  349. glfwWindowHint(GLFW_BLUE_BITS, videoMode.blueBits);
  350. glfwWindowHint(GLFW_GREEN_BITS, videoMode.greenBits);
  351. return initWithRect(viewname, Rect(0, 0, videoMode.width, videoMode.height), 1.0f, false);
  352. }
  353. bool GLViewImpl::isOpenGLReady()
  354. {
  355. return nullptr != _mainWindow;
  356. }
  357. void GLViewImpl::end()
  358. {
  359. if(_mainWindow)
  360. {
  361. glfwSetWindowShouldClose(_mainWindow,1);
  362. _mainWindow = nullptr;
  363. }
  364. // Release self. Otherwise, GLViewImpl could not be freed.
  365. release();
  366. }
  367. void GLViewImpl::swapBuffers()
  368. {
  369. if(_mainWindow)
  370. glfwSwapBuffers(_mainWindow);
  371. }
  372. bool GLViewImpl::windowShouldClose()
  373. {
  374. if(_mainWindow)
  375. return glfwWindowShouldClose(_mainWindow) ? true : false;
  376. else
  377. return true;
  378. }
  379. void GLViewImpl::pollEvents()
  380. {
  381. glfwPollEvents();
  382. }
  383. void GLViewImpl::enableRetina(bool enabled)
  384. {
  385. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  386. _isRetinaEnabled = enabled;
  387. if (_isRetinaEnabled)
  388. {
  389. _retinaFactor = 1;
  390. }
  391. else
  392. {
  393. _retinaFactor = 2;
  394. }
  395. updateFrameSize();
  396. #endif
  397. }
  398. void GLViewImpl::setIMEKeyboardState(bool /*bOpen*/)
  399. {
  400. }
  401. void GLViewImpl::setCursorVisible( bool isVisible )
  402. {
  403. if( _mainWindow == NULL )
  404. return;
  405. if( isVisible )
  406. glfwSetInputMode(_mainWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  407. else
  408. glfwSetInputMode(_mainWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  409. }
  410. void GLViewImpl::setFrameZoomFactor(float zoomFactor)
  411. {
  412. CCASSERT(zoomFactor > 0.0f, "zoomFactor must be larger than 0");
  413. if (std::abs(_frameZoomFactor - zoomFactor) < FLT_EPSILON)
  414. {
  415. return;
  416. }
  417. _frameZoomFactor = zoomFactor;
  418. updateFrameSize();
  419. }
  420. float GLViewImpl::getFrameZoomFactor() const
  421. {
  422. return _frameZoomFactor;
  423. }
  424. bool GLViewImpl::isFullscreen() const {
  425. return (_monitor != nullptr);
  426. }
  427. void GLViewImpl::setFullscreen() {
  428. if (this->isFullscreen()) {
  429. return;
  430. }
  431. _monitor = glfwGetPrimaryMonitor();
  432. if (nullptr == _monitor) {
  433. return;
  434. }
  435. const GLFWvidmode* videoMode = glfwGetVideoMode(_monitor);
  436. this->setFullscreen(*videoMode, _monitor);
  437. }
  438. void GLViewImpl::setFullscreen(int monitorIndex) {
  439. // set fullscreen on specific monitor
  440. int count = 0;
  441. GLFWmonitor** monitors = glfwGetMonitors(&count);
  442. if (monitorIndex < 0 || monitorIndex >= count) {
  443. return;
  444. }
  445. GLFWmonitor* monitor = monitors[monitorIndex];
  446. if (nullptr == monitor) {
  447. return;
  448. }
  449. const GLFWvidmode* videoMode = glfwGetVideoMode(monitor);
  450. this->setFullscreen(*videoMode, monitor);
  451. }
  452. void GLViewImpl::setFullscreen(const GLFWvidmode &videoMode, GLFWmonitor *monitor) {
  453. _monitor = monitor;
  454. glfwSetWindowMonitor(_mainWindow, _monitor, 0, 0, videoMode.width, videoMode.height, videoMode.refreshRate);
  455. }
  456. void GLViewImpl::setWindowed(int width, int height) {
  457. if (!this->isFullscreen()) {
  458. this->setFrameSize(width, height);
  459. } else {
  460. const GLFWvidmode* videoMode = glfwGetVideoMode(_monitor);
  461. int xpos = 0, ypos = 0;
  462. glfwGetMonitorPos(_monitor, &xpos, &ypos);
  463. xpos += (videoMode->width - width) * 0.5;
  464. ypos += (videoMode->height - height) * 0.5;
  465. _monitor = nullptr;
  466. glfwSetWindowMonitor(_mainWindow, nullptr, xpos, ypos, width, height, GLFW_DONT_CARE);
  467. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  468. // on mac window will sometimes lose title when windowed
  469. glfwSetWindowTitle(_mainWindow, _viewName.c_str());
  470. #endif
  471. }
  472. }
  473. int GLViewImpl::getMonitorCount() const {
  474. int count = 0;
  475. glfwGetMonitors(&count);
  476. return count;
  477. }
  478. Size GLViewImpl::getMonitorSize() const {
  479. GLFWmonitor* monitor = _monitor;
  480. if (nullptr == monitor) {
  481. GLFWwindow* window = this->getWindow();
  482. monitor = glfwGetWindowMonitor(window);
  483. }
  484. if (nullptr == monitor) {
  485. monitor = glfwGetPrimaryMonitor();
  486. }
  487. if (nullptr != monitor) {
  488. const GLFWvidmode* videoMode = glfwGetVideoMode(monitor);
  489. Size size = Size(videoMode->width, videoMode->height);
  490. return size;
  491. }
  492. return Size::ZERO;
  493. }
  494. void GLViewImpl::updateFrameSize()
  495. {
  496. if (_screenSize.width > 0 && _screenSize.height > 0)
  497. {
  498. int w = 0, h = 0;
  499. glfwGetWindowSize(_mainWindow, &w, &h);
  500. int frameBufferW = 0, frameBufferH = 0;
  501. glfwGetFramebufferSize(_mainWindow, &frameBufferW, &frameBufferH);
  502. if (frameBufferW == 2 * w && frameBufferH == 2 * h)
  503. {
  504. if (_isRetinaEnabled)
  505. {
  506. _retinaFactor = 1;
  507. }
  508. else
  509. {
  510. _retinaFactor = 2;
  511. }
  512. glfwSetWindowSize(_mainWindow, _screenSize.width/2 * _retinaFactor * _frameZoomFactor, _screenSize.height/2 * _retinaFactor * _frameZoomFactor);
  513. _isInRetinaMonitor = true;
  514. }
  515. else
  516. {
  517. if (_isInRetinaMonitor)
  518. {
  519. _retinaFactor = 1;
  520. }
  521. glfwSetWindowSize(_mainWindow, _screenSize.width * _retinaFactor * _frameZoomFactor, _screenSize.height *_retinaFactor * _frameZoomFactor);
  522. _isInRetinaMonitor = false;
  523. }
  524. }
  525. }
  526. void GLViewImpl::setFrameSize(float width, float height)
  527. {
  528. GLView::setFrameSize(width, height);
  529. updateFrameSize();
  530. }
  531. void GLViewImpl::setViewPortInPoints(float x , float y , float w , float h)
  532. {
  533. experimental::Viewport vp((float)(x * _scaleX * _retinaFactor * _frameZoomFactor + _viewPortRect.origin.x * _retinaFactor * _frameZoomFactor),
  534. (float)(y * _scaleY * _retinaFactor * _frameZoomFactor + _viewPortRect.origin.y * _retinaFactor * _frameZoomFactor),
  535. (float)(w * _scaleX * _retinaFactor * _frameZoomFactor),
  536. (float)(h * _scaleY * _retinaFactor * _frameZoomFactor));
  537. Camera::setDefaultViewport(vp);
  538. }
  539. void GLViewImpl::setScissorInPoints(float x , float y , float w , float h)
  540. {
  541. glScissor((GLint)(x * _scaleX * _retinaFactor * _frameZoomFactor + _viewPortRect.origin.x * _retinaFactor * _frameZoomFactor),
  542. (GLint)(y * _scaleY * _retinaFactor * _frameZoomFactor + _viewPortRect.origin.y * _retinaFactor * _frameZoomFactor),
  543. (GLsizei)(w * _scaleX * _retinaFactor * _frameZoomFactor),
  544. (GLsizei)(h * _scaleY * _retinaFactor * _frameZoomFactor));
  545. }
  546. Rect GLViewImpl::getScissorRect() const
  547. {
  548. GLfloat params[4];
  549. glGetFloatv(GL_SCISSOR_BOX, params);
  550. float x = (params[0] - _viewPortRect.origin.x * _retinaFactor * _frameZoomFactor) / (_scaleX * _retinaFactor * _frameZoomFactor);
  551. float y = (params[1] - _viewPortRect.origin.y * _retinaFactor * _frameZoomFactor) / (_scaleY * _retinaFactor * _frameZoomFactor);
  552. float w = params[2] / (_scaleX * _retinaFactor * _frameZoomFactor);
  553. float h = params[3] / (_scaleY * _retinaFactor * _frameZoomFactor);
  554. return Rect(x, y, w, h);
  555. }
  556. void GLViewImpl::getCursorPos(double* lastPositionX, double* lastPositionY)
  557. {
  558. if (_mainWindow == NULL)
  559. return;
  560. glfwGetCursorPos(_mainWindow, lastPositionX, lastPositionY);
  561. }
  562. void GLViewImpl::onGLFWError(int errorID, const char* errorDesc)
  563. {
  564. if (_mainWindow)
  565. {
  566. _glfwError = StringUtils::format("GLFWError #%d Happen, %s", errorID, errorDesc);
  567. }
  568. else
  569. {
  570. _glfwError.append(StringUtils::format("GLFWError #%d Happen, %s\n", errorID, errorDesc));
  571. }
  572. CCLOGERROR("%s", _glfwError.c_str());
  573. }
  574. void GLViewImpl::onGLFWMouseCallBack(GLFWwindow* /*window*/, int button, int action, int /*modify*/)
  575. {
  576. if(GLFW_MOUSE_BUTTON_LEFT == button)
  577. {
  578. if(GLFW_PRESS == action)
  579. {
  580. _captured = true;
  581. if (this->getViewPortRect().equals(Rect::ZERO) || this->getViewPortRect().containsPoint(Vec2(_mouseX,_mouseY)))
  582. {
  583. intptr_t id = 0;
  584. this->handleTouchesBegin(1, &id, &_mouseX, &_mouseY);
  585. }
  586. }
  587. else if(GLFW_RELEASE == action)
  588. {
  589. if (_captured)
  590. {
  591. _captured = false;
  592. intptr_t id = 0;
  593. this->handleTouchesEnd(1, &id, &_mouseX, &_mouseY);
  594. }
  595. }
  596. }
  597. //Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
  598. float cursorX = (_mouseX - _viewPortRect.origin.x) / _scaleX;
  599. float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - _mouseY) / _scaleY;
  600. if(GLFW_PRESS == action)
  601. {
  602. EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN);
  603. event.setCursorPosition(cursorX, cursorY);
  604. event.setMouseButton(static_cast<cocos2d::EventMouse::MouseButton>(button));
  605. Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
  606. }
  607. else if(GLFW_RELEASE == action)
  608. {
  609. EventMouse event(EventMouse::MouseEventType::MOUSE_UP);
  610. event.setCursorPosition(cursorX, cursorY);
  611. event.setMouseButton(static_cast<cocos2d::EventMouse::MouseButton>(button));
  612. Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
  613. }
  614. }
  615. void GLViewImpl::onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
  616. {
  617. _mouseX = (float)x;
  618. _mouseY = (float)y;
  619. _mouseX /= this->getFrameZoomFactor();
  620. _mouseY /= this->getFrameZoomFactor();
  621. if (_isInRetinaMonitor)
  622. {
  623. if (_retinaFactor == 1)
  624. {
  625. _mouseX *= 2;
  626. _mouseY *= 2;
  627. }
  628. }
  629. if (_captured)
  630. {
  631. intptr_t id = 0;
  632. this->handleTouchesMove(1, &id, &_mouseX, &_mouseY);
  633. }
  634. //Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
  635. float cursorX = (_mouseX - _viewPortRect.origin.x) / _scaleX;
  636. float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - _mouseY) / _scaleY;
  637. EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE);
  638. // Set current button
  639. if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
  640. {
  641. event.setMouseButton(static_cast<cocos2d::EventMouse::MouseButton>(GLFW_MOUSE_BUTTON_LEFT));
  642. }
  643. else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
  644. {
  645. event.setMouseButton(static_cast<cocos2d::EventMouse::MouseButton>(GLFW_MOUSE_BUTTON_RIGHT));
  646. }
  647. else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS)
  648. {
  649. event.setMouseButton(static_cast<cocos2d::EventMouse::MouseButton>(GLFW_MOUSE_BUTTON_MIDDLE));
  650. }
  651. event.setCursorPosition(cursorX, cursorY);
  652. Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
  653. }
  654. void GLViewImpl::onGLFWMouseScrollCallback(GLFWwindow* /*window*/, double x, double y)
  655. {
  656. EventMouse event(EventMouse::MouseEventType::MOUSE_SCROLL);
  657. //Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
  658. float cursorX = (_mouseX - _viewPortRect.origin.x) / _scaleX;
  659. float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - _mouseY) / _scaleY;
  660. event.setScrollData((float)x, -(float)y);
  661. event.setCursorPosition(cursorX, cursorY);
  662. Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
  663. }
  664. void GLViewImpl::onGLFWKeyCallback(GLFWwindow* /*window*/, int key, int /*scancode*/, int action, int /*mods*/)
  665. {
  666. if (GLFW_REPEAT != action)
  667. {
  668. EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action);
  669. auto dispatcher = Director::getInstance()->getEventDispatcher();
  670. dispatcher->dispatchEvent(&event);
  671. }
  672. if (GLFW_RELEASE != action)
  673. {
  674. switch (g_keyCodeMap[key])
  675. {
  676. case EventKeyboard::KeyCode::KEY_BACKSPACE:
  677. IMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
  678. break;
  679. case EventKeyboard::KeyCode::KEY_HOME:
  680. case EventKeyboard::KeyCode::KEY_KP_HOME:
  681. case EventKeyboard::KeyCode::KEY_DELETE:
  682. case EventKeyboard::KeyCode::KEY_KP_DELETE:
  683. case EventKeyboard::KeyCode::KEY_END:
  684. case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
  685. case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
  686. case EventKeyboard::KeyCode::KEY_ESCAPE:
  687. IMEDispatcher::sharedDispatcher()->dispatchControlKey(g_keyCodeMap[key]);
  688. break;
  689. default:
  690. break;
  691. }
  692. }
  693. }
  694. void GLViewImpl::onGLFWCharCallback(GLFWwindow* /*window*/, unsigned int character)
  695. {
  696. char16_t wcharString[2] = { (char16_t) character, 0 };
  697. std::string utf8String;
  698. StringUtils::UTF16ToUTF8( wcharString, utf8String );
  699. static std::set<std::string> controlUnicode = {
  700. "\xEF\x9C\x80", // up
  701. "\xEF\x9C\x81", // down
  702. "\xEF\x9C\x82", // left
  703. "\xEF\x9C\x83", // right
  704. "\xEF\x9C\xA8", // delete
  705. "\xEF\x9C\xA9", // home
  706. "\xEF\x9C\xAB", // end
  707. "\xEF\x9C\xAC", // pageup
  708. "\xEF\x9C\xAD", // pagedown
  709. "\xEF\x9C\xB9" // clear
  710. };
  711. // Check for send control key
  712. if (controlUnicode.find(utf8String) == controlUnicode.end())
  713. {
  714. IMEDispatcher::sharedDispatcher()->dispatchInsertText( utf8String.c_str(), utf8String.size() );
  715. }
  716. }
  717. void GLViewImpl::onGLFWWindowPosCallback(GLFWwindow* /*window*/, int /*x*/, int /*y*/)
  718. {
  719. Director::getInstance()->setViewport();
  720. }
  721. void GLViewImpl::onGLFWframebuffersize(GLFWwindow* window, int w, int h)
  722. {
  723. float frameSizeW = _screenSize.width;
  724. float frameSizeH = _screenSize.height;
  725. float factorX = frameSizeW / w * _retinaFactor * _frameZoomFactor;
  726. float factorY = frameSizeH / h * _retinaFactor * _frameZoomFactor;
  727. if (std::abs(factorX - 0.5f) < FLT_EPSILON && std::abs(factorY - 0.5f) < FLT_EPSILON)
  728. {
  729. _isInRetinaMonitor = true;
  730. if (_isRetinaEnabled)
  731. {
  732. _retinaFactor = 1;
  733. }
  734. else
  735. {
  736. _retinaFactor = 2;
  737. }
  738. glfwSetWindowSize(window, static_cast<int>(frameSizeW * 0.5f * _retinaFactor * _frameZoomFactor) , static_cast<int>(frameSizeH * 0.5f * _retinaFactor * _frameZoomFactor));
  739. }
  740. else if (std::abs(factorX - 2.0f) < FLT_EPSILON && std::abs(factorY - 2.0f) < FLT_EPSILON)
  741. {
  742. _isInRetinaMonitor = false;
  743. _retinaFactor = 1;
  744. glfwSetWindowSize(window, static_cast<int>(frameSizeW * _retinaFactor * _frameZoomFactor), static_cast<int>(frameSizeH * _retinaFactor * _frameZoomFactor));
  745. }
  746. }
  747. void GLViewImpl::onGLFWWindowSizeFunCallback(GLFWwindow* /*window*/, int width, int height)
  748. {
  749. if (width && height && _resolutionPolicy != ResolutionPolicy::UNKNOWN)
  750. {
  751. Size baseDesignSize = _designResolutionSize;
  752. ResolutionPolicy baseResolutionPolicy = _resolutionPolicy;
  753. int frameWidth = width / _frameZoomFactor;
  754. int frameHeight = height / _frameZoomFactor;
  755. setFrameSize(frameWidth, frameHeight);
  756. setDesignResolutionSize(baseDesignSize.width, baseDesignSize.height, baseResolutionPolicy);
  757. Director::getInstance()->setViewport();
  758. Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_RESIZED, nullptr);
  759. }
  760. }
  761. void GLViewImpl::onGLFWWindowIconifyCallback(GLFWwindow* /*window*/, int iconified)
  762. {
  763. if (iconified == GL_TRUE)
  764. {
  765. Application::getInstance()->applicationDidEnterBackground();
  766. }
  767. else
  768. {
  769. Application::getInstance()->applicationWillEnterForeground();
  770. }
  771. }
  772. void GLViewImpl::onGLFWWindowFocusCallback(GLFWwindow* /*window*/, int focused)
  773. {
  774. if (focused == GL_TRUE)
  775. {
  776. Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_FOCUSED, nullptr);
  777. }
  778. else
  779. {
  780. Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_UNFOCUSED, nullptr);
  781. }
  782. }
  783. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  784. static bool glew_dynamic_binding()
  785. {
  786. const char *gl_extensions = (const char*)glGetString(GL_EXTENSIONS);
  787. // If the current opengl driver doesn't have framebuffers methods, check if an extension exists
  788. if (glGenFramebuffers == nullptr)
  789. {
  790. log("OpenGL: glGenFramebuffers is nullptr, try to detect an extension");
  791. if (strstr(gl_extensions, "ARB_framebuffer_object"))
  792. {
  793. log("OpenGL: ARB_framebuffer_object is supported");
  794. glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbuffer");
  795. glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC) wglGetProcAddress("glBindRenderbuffer");
  796. glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC) wglGetProcAddress("glDeleteRenderbuffers");
  797. glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC) wglGetProcAddress("glGenRenderbuffers");
  798. glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC) wglGetProcAddress("glRenderbufferStorage");
  799. glGetRenderbufferParameteriv = (PFNGLGETRENDERBUFFERPARAMETERIVPROC) wglGetProcAddress("glGetRenderbufferParameteriv");
  800. glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC) wglGetProcAddress("glIsFramebuffer");
  801. glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC) wglGetProcAddress("glBindFramebuffer");
  802. glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC) wglGetProcAddress("glDeleteFramebuffers");
  803. glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC) wglGetProcAddress("glGenFramebuffers");
  804. glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC) wglGetProcAddress("glCheckFramebufferStatus");
  805. glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DPROC) wglGetProcAddress("glFramebufferTexture1D");
  806. glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) wglGetProcAddress("glFramebufferTexture2D");
  807. glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC) wglGetProcAddress("glFramebufferTexture3D");
  808. glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC) wglGetProcAddress("glFramebufferRenderbuffer");
  809. glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC) wglGetProcAddress("glGetFramebufferAttachmentParameteriv");
  810. glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmap");
  811. }
  812. else
  813. if (strstr(gl_extensions, "EXT_framebuffer_object"))
  814. {
  815. log("OpenGL: EXT_framebuffer_object is supported");
  816. glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbufferEXT");
  817. glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC) wglGetProcAddress("glBindRenderbufferEXT");
  818. glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC) wglGetProcAddress("glDeleteRenderbuffersEXT");
  819. glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC) wglGetProcAddress("glGenRenderbuffersEXT");
  820. glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC) wglGetProcAddress("glRenderbufferStorageEXT");
  821. glGetRenderbufferParameteriv = (PFNGLGETRENDERBUFFERPARAMETERIVPROC) wglGetProcAddress("glGetRenderbufferParameterivEXT");
  822. glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC) wglGetProcAddress("glIsFramebufferEXT");
  823. glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC) wglGetProcAddress("glBindFramebufferEXT");
  824. glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC) wglGetProcAddress("glDeleteFramebuffersEXT");
  825. glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC) wglGetProcAddress("glGenFramebuffersEXT");
  826. glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC) wglGetProcAddress("glCheckFramebufferStatusEXT");
  827. glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DPROC) wglGetProcAddress("glFramebufferTexture1DEXT");
  828. glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) wglGetProcAddress("glFramebufferTexture2DEXT");
  829. glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC) wglGetProcAddress("glFramebufferTexture3DEXT");
  830. glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC) wglGetProcAddress("glFramebufferRenderbufferEXT");
  831. glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC) wglGetProcAddress("glGetFramebufferAttachmentParameterivEXT");
  832. glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmapEXT");
  833. }
  834. else
  835. {
  836. log("OpenGL: No framebuffers extension is supported");
  837. log("OpenGL: Any call to Fbo will crash!");
  838. return false;
  839. }
  840. }
  841. return true;
  842. }
  843. #endif
  844. // helper
  845. bool GLViewImpl::initGlew()
  846. {
  847. #if (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
  848. GLenum GlewInitResult = glewInit();
  849. if (GLEW_OK != GlewInitResult)
  850. {
  851. MessageBox((char *)glewGetErrorString(GlewInitResult), "OpenGL error");
  852. return false;
  853. }
  854. if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
  855. {
  856. log("Ready for GLSL");
  857. }
  858. else
  859. {
  860. log("Not totally ready :(");
  861. }
  862. if (glewIsSupported("GL_VERSION_2_0"))
  863. {
  864. log("Ready for OpenGL 2.0");
  865. }
  866. else
  867. {
  868. log("OpenGL 2.0 not supported");
  869. }
  870. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  871. if(glew_dynamic_binding() == false)
  872. {
  873. MessageBox("No OpenGL framebuffer support. Please upgrade the driver of your video card.", "OpenGL error");
  874. return false;
  875. }
  876. #endif
  877. #endif // (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
  878. return true;
  879. }
  880. NS_CC_END // end of namespace cocos2d;