CCGLViewImpl-desktop.cpp 39 KB

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