CCGLViewImpl-desktop.cpp 41 KB

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