2012-07-21 12:23:40 +08:00
/****************************************************************************
2014-01-07 11:25:07 +08:00
Copyright ( c ) 2010 - 2012 cocos2d - x . org
Copyright ( c ) 2013 - 2014 Chukong Technologies Inc .
http : //www.cocos2d-x.org
Permission is hereby granted , free of charge , to any person obtaining a copy
of this software and associated documentation files ( the " Software " ) , to deal
in the Software without restriction , including without limitation the rights
to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
copies of the Software , and to permit persons to whom the Software is
furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2013-11-22 05:43:59 +08:00
2014-09-10 07:50:02 +08:00
# include "CCGLViewImpl-desktop.h"
# include <unordered_map>
# include "platform/CCApplication.h"
2014-04-30 08:37:36 +08:00
# include "base/CCDirector.h"
# include "base/CCTouch.h"
# include "base/CCEventDispatcher.h"
# include "base/CCEventKeyboard.h"
# include "base/CCEventMouse.h"
2014-05-17 05:36:00 +08:00
# include "base/CCIMEDispatcher.h"
2014-07-14 20:45:24 +08:00
# include "base/ccUtils.h"
2014-08-10 00:42:21 +08:00
# include "base/ccUTF8.h"
2015-06-01 14:26:08 +08:00
# include "2d/CCCamera.h"
2015-08-06 04:06:16 +08:00
# include "deprecated/CCString.h"
2014-04-09 23:54:40 +08:00
2012-09-04 02:06:04 +08:00
NS_CC_BEGIN
2014-03-07 13:50:50 +08:00
// GLFWEventHandler
class GLFWEventHandler
{
public :
static void onGLFWError ( int errorID , const char * errorDesc )
{
if ( _view )
_view - > onGLFWError ( errorID , errorDesc ) ;
}
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
static void onGLFWMouseCallBack ( GLFWwindow * window , int button , int action , int modify )
{
if ( _view )
_view - > onGLFWMouseCallBack ( window , button , action , modify ) ;
}
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
static void onGLFWMouseMoveCallBack ( GLFWwindow * window , double x , double y )
{
if ( _view )
_view - > onGLFWMouseMoveCallBack ( window , x , y ) ;
}
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
static void onGLFWMouseScrollCallback ( GLFWwindow * window , double x , double y )
{
if ( _view )
_view - > onGLFWMouseScrollCallback ( window , x , y ) ;
}
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
static void onGLFWKeyCallback ( GLFWwindow * window , int key , int scancode , int action , int mods )
{
if ( _view )
_view - > onGLFWKeyCallback ( window , key , scancode , action , mods ) ;
}
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
static void onGLFWCharCallback ( GLFWwindow * window , unsigned int character )
{
if ( _view )
_view - > onGLFWCharCallback ( window , character ) ;
}
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
static void onGLFWWindowPosCallback ( GLFWwindow * windows , int x , int y )
{
if ( _view )
_view - > onGLFWWindowPosCallback ( windows , x , y ) ;
}
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
static void onGLFWframebuffersize ( GLFWwindow * window , int w , int h )
{
if ( _view )
_view - > onGLFWframebuffersize ( window , w , h ) ;
}
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
static void onGLFWWindowSizeFunCallback ( GLFWwindow * window , int width , int height )
{
if ( _view )
_view - > onGLFWWindowSizeFunCallback ( window , width , height ) ;
}
2014-03-07 13:59:56 +08:00
2014-07-31 00:53:04 +08:00
static void setGLViewImpl ( GLViewImpl * view )
2014-03-07 13:50:50 +08:00
{
_view = view ;
}
2014-03-07 13:59:56 +08:00
2014-08-14 15:48:09 +08:00
static void onGLFWWindowIconifyCallback ( GLFWwindow * window , int iconified )
{
if ( _view )
{
_view - > onGLFWWindowIconifyCallback ( window , iconified ) ;
}
}
2014-08-13 03:22:47 +08:00
2014-03-07 13:50:50 +08:00
private :
2014-07-31 00:53:04 +08:00
static GLViewImpl * _view ;
2014-03-07 13:50:50 +08:00
} ;
2014-07-31 00:53:04 +08:00
GLViewImpl * GLFWEventHandler : : _view = nullptr ;
2014-03-07 13:50:50 +08:00
////////////////////////////////////////////////////
2014-01-23 09:58:12 +08:00
struct keyCodeItem
{
int glfwKeyCode ;
EventKeyboard : : KeyCode keyCode ;
} ;
2014-01-23 10:27:42 +08:00
static std : : unordered_map < int , EventKeyboard : : KeyCode > g_keyCodeMap ;
2013-09-03 18:22:03 +08:00
2014-01-23 09:58:12 +08:00
static keyCodeItem g_keyCodeStructArray [ ] = {
2013-09-03 18:22:03 +08:00
/* The unknown key */
2013-09-20 19:19:31 +08:00
{ GLFW_KEY_UNKNOWN , EventKeyboard : : KeyCode : : KEY_NONE } ,
2014-03-07 13:59:56 +08:00
2013-09-03 18:22:03 +08:00
/* Printable keys */
2013-09-20 19:19:31 +08:00
{ GLFW_KEY_SPACE , EventKeyboard : : KeyCode : : KEY_SPACE } ,
{ GLFW_KEY_APOSTROPHE , EventKeyboard : : KeyCode : : KEY_APOSTROPHE } ,
{ GLFW_KEY_COMMA , EventKeyboard : : KeyCode : : KEY_COMMA } ,
{ GLFW_KEY_MINUS , EventKeyboard : : KeyCode : : KEY_MINUS } ,
{ GLFW_KEY_PERIOD , EventKeyboard : : KeyCode : : KEY_PERIOD } ,
{ GLFW_KEY_SLASH , EventKeyboard : : KeyCode : : KEY_SLASH } ,
{ GLFW_KEY_0 , EventKeyboard : : KeyCode : : KEY_0 } ,
{ GLFW_KEY_1 , EventKeyboard : : KeyCode : : KEY_1 } ,
{ GLFW_KEY_2 , EventKeyboard : : KeyCode : : KEY_2 } ,
{ GLFW_KEY_3 , EventKeyboard : : KeyCode : : KEY_3 } ,
{ GLFW_KEY_4 , EventKeyboard : : KeyCode : : KEY_4 } ,
{ GLFW_KEY_5 , EventKeyboard : : KeyCode : : KEY_5 } ,
{ GLFW_KEY_6 , EventKeyboard : : KeyCode : : KEY_6 } ,
{ GLFW_KEY_7 , EventKeyboard : : KeyCode : : KEY_7 } ,
{ GLFW_KEY_8 , EventKeyboard : : KeyCode : : KEY_8 } ,
{ GLFW_KEY_9 , EventKeyboard : : KeyCode : : KEY_9 } ,
{ GLFW_KEY_SEMICOLON , EventKeyboard : : KeyCode : : KEY_SEMICOLON } ,
{ GLFW_KEY_EQUAL , EventKeyboard : : KeyCode : : KEY_EQUAL } ,
{ GLFW_KEY_A , EventKeyboard : : KeyCode : : KEY_A } ,
{ GLFW_KEY_B , EventKeyboard : : KeyCode : : KEY_B } ,
{ GLFW_KEY_C , EventKeyboard : : KeyCode : : KEY_C } ,
{ GLFW_KEY_D , EventKeyboard : : KeyCode : : KEY_D } ,
{ GLFW_KEY_E , EventKeyboard : : KeyCode : : KEY_E } ,
{ GLFW_KEY_F , EventKeyboard : : KeyCode : : KEY_F } ,
{ GLFW_KEY_G , EventKeyboard : : KeyCode : : KEY_G } ,
{ GLFW_KEY_H , EventKeyboard : : KeyCode : : KEY_H } ,
{ GLFW_KEY_I , EventKeyboard : : KeyCode : : KEY_I } ,
{ GLFW_KEY_J , EventKeyboard : : KeyCode : : KEY_J } ,
{ GLFW_KEY_K , EventKeyboard : : KeyCode : : KEY_K } ,
{ GLFW_KEY_L , EventKeyboard : : KeyCode : : KEY_L } ,
{ GLFW_KEY_M , EventKeyboard : : KeyCode : : KEY_M } ,
{ GLFW_KEY_N , EventKeyboard : : KeyCode : : KEY_N } ,
{ GLFW_KEY_O , EventKeyboard : : KeyCode : : KEY_O } ,
{ GLFW_KEY_P , EventKeyboard : : KeyCode : : KEY_P } ,
{ GLFW_KEY_Q , EventKeyboard : : KeyCode : : KEY_Q } ,
{ GLFW_KEY_R , EventKeyboard : : KeyCode : : KEY_R } ,
{ GLFW_KEY_S , EventKeyboard : : KeyCode : : KEY_S } ,
{ GLFW_KEY_T , EventKeyboard : : KeyCode : : KEY_T } ,
{ GLFW_KEY_U , EventKeyboard : : KeyCode : : KEY_U } ,
{ GLFW_KEY_V , EventKeyboard : : KeyCode : : KEY_V } ,
{ GLFW_KEY_W , EventKeyboard : : KeyCode : : KEY_W } ,
{ GLFW_KEY_X , EventKeyboard : : KeyCode : : KEY_X } ,
{ GLFW_KEY_Y , EventKeyboard : : KeyCode : : KEY_Y } ,
{ GLFW_KEY_Z , EventKeyboard : : KeyCode : : KEY_Z } ,
{ GLFW_KEY_LEFT_BRACKET , EventKeyboard : : KeyCode : : KEY_LEFT_BRACKET } ,
{ GLFW_KEY_BACKSLASH , EventKeyboard : : KeyCode : : KEY_BACK_SLASH } ,
{ GLFW_KEY_RIGHT_BRACKET , EventKeyboard : : KeyCode : : KEY_RIGHT_BRACKET } ,
{ GLFW_KEY_GRAVE_ACCENT , EventKeyboard : : KeyCode : : KEY_GRAVE } ,
{ GLFW_KEY_WORLD_1 , EventKeyboard : : KeyCode : : KEY_GRAVE } ,
{ GLFW_KEY_WORLD_2 , EventKeyboard : : KeyCode : : KEY_NONE } ,
2014-03-07 13:59:56 +08:00
2013-09-03 18:22:03 +08:00
/* Function keys */
2013-09-20 19:19:31 +08:00
{ GLFW_KEY_ESCAPE , EventKeyboard : : KeyCode : : KEY_ESCAPE } ,
2015-08-28 04:46:24 +08:00
{ GLFW_KEY_ENTER , EventKeyboard : : KeyCode : : KEY_ENTER } ,
2013-09-20 19:19:31 +08:00
{ GLFW_KEY_TAB , EventKeyboard : : KeyCode : : KEY_TAB } ,
{ GLFW_KEY_BACKSPACE , EventKeyboard : : KeyCode : : KEY_BACKSPACE } ,
{ GLFW_KEY_INSERT , EventKeyboard : : KeyCode : : KEY_INSERT } ,
{ GLFW_KEY_DELETE , EventKeyboard : : KeyCode : : KEY_DELETE } ,
{ GLFW_KEY_RIGHT , EventKeyboard : : KeyCode : : KEY_RIGHT_ARROW } ,
{ GLFW_KEY_LEFT , EventKeyboard : : KeyCode : : KEY_LEFT_ARROW } ,
{ GLFW_KEY_DOWN , EventKeyboard : : KeyCode : : KEY_DOWN_ARROW } ,
{ GLFW_KEY_UP , EventKeyboard : : KeyCode : : KEY_UP_ARROW } ,
2015-08-28 04:46:24 +08:00
{ GLFW_KEY_PAGE_UP , EventKeyboard : : KeyCode : : KEY_PG_UP } ,
{ GLFW_KEY_PAGE_DOWN , EventKeyboard : : KeyCode : : KEY_PG_DOWN } ,
{ GLFW_KEY_HOME , EventKeyboard : : KeyCode : : KEY_HOME } ,
2013-09-20 19:19:31 +08:00
{ GLFW_KEY_END , EventKeyboard : : KeyCode : : KEY_END } ,
{ GLFW_KEY_CAPS_LOCK , EventKeyboard : : KeyCode : : KEY_CAPS_LOCK } ,
{ GLFW_KEY_SCROLL_LOCK , EventKeyboard : : KeyCode : : KEY_SCROLL_LOCK } ,
{ GLFW_KEY_NUM_LOCK , EventKeyboard : : KeyCode : : KEY_NUM_LOCK } ,
{ GLFW_KEY_PRINT_SCREEN , EventKeyboard : : KeyCode : : KEY_PRINT } ,
{ GLFW_KEY_PAUSE , EventKeyboard : : KeyCode : : KEY_PAUSE } ,
{ GLFW_KEY_F1 , EventKeyboard : : KeyCode : : KEY_F1 } ,
{ GLFW_KEY_F2 , EventKeyboard : : KeyCode : : KEY_F2 } ,
{ GLFW_KEY_F3 , EventKeyboard : : KeyCode : : KEY_F3 } ,
{ GLFW_KEY_F4 , EventKeyboard : : KeyCode : : KEY_F4 } ,
{ GLFW_KEY_F5 , EventKeyboard : : KeyCode : : KEY_F5 } ,
{ GLFW_KEY_F6 , EventKeyboard : : KeyCode : : KEY_F6 } ,
{ GLFW_KEY_F7 , EventKeyboard : : KeyCode : : KEY_F7 } ,
{ GLFW_KEY_F8 , EventKeyboard : : KeyCode : : KEY_F8 } ,
{ GLFW_KEY_F9 , EventKeyboard : : KeyCode : : KEY_F9 } ,
{ GLFW_KEY_F10 , EventKeyboard : : KeyCode : : KEY_F10 } ,
{ GLFW_KEY_F11 , EventKeyboard : : KeyCode : : KEY_F11 } ,
{ GLFW_KEY_F12 , EventKeyboard : : KeyCode : : KEY_F12 } ,
{ GLFW_KEY_F13 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F14 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F15 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F16 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F17 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F18 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F19 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F20 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F21 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F22 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F23 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F24 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_F25 , EventKeyboard : : KeyCode : : KEY_NONE } ,
{ GLFW_KEY_KP_0 , EventKeyboard : : KeyCode : : KEY_0 } ,
{ GLFW_KEY_KP_1 , EventKeyboard : : KeyCode : : KEY_1 } ,
{ GLFW_KEY_KP_2 , EventKeyboard : : KeyCode : : KEY_2 } ,
{ GLFW_KEY_KP_3 , EventKeyboard : : KeyCode : : KEY_3 } ,
{ GLFW_KEY_KP_4 , EventKeyboard : : KeyCode : : KEY_4 } ,
{ GLFW_KEY_KP_5 , EventKeyboard : : KeyCode : : KEY_5 } ,
{ GLFW_KEY_KP_6 , EventKeyboard : : KeyCode : : KEY_6 } ,
{ GLFW_KEY_KP_7 , EventKeyboard : : KeyCode : : KEY_7 } ,
{ GLFW_KEY_KP_8 , EventKeyboard : : KeyCode : : KEY_8 } ,
{ GLFW_KEY_KP_9 , EventKeyboard : : KeyCode : : KEY_9 } ,
{ GLFW_KEY_KP_DECIMAL , EventKeyboard : : KeyCode : : KEY_PERIOD } ,
{ GLFW_KEY_KP_DIVIDE , EventKeyboard : : KeyCode : : KEY_KP_DIVIDE } ,
{ GLFW_KEY_KP_MULTIPLY , EventKeyboard : : KeyCode : : KEY_KP_MULTIPLY } ,
{ GLFW_KEY_KP_SUBTRACT , EventKeyboard : : KeyCode : : KEY_KP_MINUS } ,
{ GLFW_KEY_KP_ADD , EventKeyboard : : KeyCode : : KEY_KP_PLUS } ,
{ GLFW_KEY_KP_ENTER , EventKeyboard : : KeyCode : : KEY_KP_ENTER } ,
{ GLFW_KEY_KP_EQUAL , EventKeyboard : : KeyCode : : KEY_EQUAL } ,
2014-11-26 10:40:43 +08:00
{ GLFW_KEY_LEFT_SHIFT , EventKeyboard : : KeyCode : : KEY_LEFT_SHIFT } ,
{ GLFW_KEY_LEFT_CONTROL , EventKeyboard : : KeyCode : : KEY_LEFT_CTRL } ,
{ GLFW_KEY_LEFT_ALT , EventKeyboard : : KeyCode : : KEY_LEFT_ALT } ,
2013-09-20 19:19:31 +08:00
{ GLFW_KEY_LEFT_SUPER , EventKeyboard : : KeyCode : : KEY_HYPER } ,
2014-11-26 10:40:43 +08:00
{ GLFW_KEY_RIGHT_SHIFT , EventKeyboard : : KeyCode : : KEY_RIGHT_SHIFT } ,
{ GLFW_KEY_RIGHT_CONTROL , EventKeyboard : : KeyCode : : KEY_RIGHT_CTRL } ,
{ GLFW_KEY_RIGHT_ALT , EventKeyboard : : KeyCode : : KEY_RIGHT_ALT } ,
2013-09-20 19:19:31 +08:00
{ GLFW_KEY_RIGHT_SUPER , EventKeyboard : : KeyCode : : KEY_HYPER } ,
{ GLFW_KEY_MENU , EventKeyboard : : KeyCode : : KEY_MENU } ,
{ GLFW_KEY_LAST , EventKeyboard : : KeyCode : : KEY_NONE }
2013-09-03 18:22:03 +08:00
} ;
2014-03-07 13:50:50 +08:00
//////////////////////////////////////////////////////////////////////////
2014-07-31 00:53:04 +08:00
// implement GLViewImpl
2014-03-07 13:50:50 +08:00
//////////////////////////////////////////////////////////////////////////
2013-09-03 18:22:03 +08:00
2012-09-04 02:06:04 +08:00
2014-07-31 00:53:04 +08:00
GLViewImpl : : GLViewImpl ( )
2014-03-07 13:50:50 +08:00
: _captured ( false )
, _supportTouch ( false )
, _isInRetinaMonitor ( false )
, _isRetinaEnabled ( false )
, _retinaFactor ( 1 )
, _frameZoomFactor ( 1.0f )
, _mainWindow ( nullptr )
2014-04-09 01:33:24 +08:00
, _monitor ( nullptr )
2014-03-07 13:50:50 +08:00
, _mouseX ( 0.0f )
, _mouseY ( 0.0f )
2013-07-12 13:11:21 +08:00
{
2014-03-07 13:50:50 +08:00
_viewName = " cocos2dx " ;
g_keyCodeMap . clear ( ) ;
for ( auto & item : g_keyCodeStructArray )
2013-08-21 12:01:56 +08:00
{
2014-03-07 13:50:50 +08:00
g_keyCodeMap [ item . glfwKeyCode ] = item . keyCode ;
2013-08-21 12:01:56 +08:00
}
2014-03-07 13:59:56 +08:00
2014-07-31 00:53:04 +08:00
GLFWEventHandler : : setGLViewImpl ( this ) ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
glfwSetErrorCallback ( GLFWEventHandler : : onGLFWError ) ;
glfwInit ( ) ;
2014-01-14 13:59:00 +08:00
}
2014-07-31 00:53:04 +08:00
GLViewImpl : : ~ GLViewImpl ( )
2014-01-14 13:59:00 +08:00
{
2014-07-31 00:53:04 +08:00
CCLOGINFO ( " deallocing GLViewImpl: %p " , this ) ;
GLFWEventHandler : : setGLViewImpl ( nullptr ) ;
2014-03-07 13:50:50 +08:00
glfwTerminate ( ) ;
2014-02-10 18:15:30 +08:00
}
2014-01-14 13:59:00 +08:00
2014-07-31 00:53:04 +08:00
GLViewImpl * GLViewImpl : : create ( const std : : string & viewName )
2014-01-24 07:36:55 +08:00
{
2014-08-28 07:31:57 +08:00
auto ret = new ( std : : nothrow ) GLViewImpl ;
2014-01-31 12:16:40 +08:00
if ( ret & & ret - > initWithRect ( viewName , Rect ( 0 , 0 , 960 , 640 ) , 1 ) ) {
2014-01-24 07:36:55 +08:00
ret - > autorelease ( ) ;
return ret ;
}
2014-03-07 13:59:56 +08:00
2014-01-24 07:36:55 +08:00
return nullptr ;
}
2014-07-31 00:53:04 +08:00
GLViewImpl * GLViewImpl : : createWithRect ( const std : : string & viewName , Rect rect , float frameZoomFactor )
2014-01-24 07:36:55 +08:00
{
2014-08-28 07:31:57 +08:00
auto ret = new ( std : : nothrow ) GLViewImpl ;
2014-01-31 12:16:40 +08:00
if ( ret & & ret - > initWithRect ( viewName , rect , frameZoomFactor ) ) {
2014-01-24 07:36:55 +08:00
ret - > autorelease ( ) ;
return ret ;
}
2014-03-07 13:59:56 +08:00
2014-01-24 07:36:55 +08:00
return nullptr ;
}
2014-07-31 00:53:04 +08:00
GLViewImpl * GLViewImpl : : createWithFullScreen ( const std : : string & viewName )
2014-01-24 07:36:55 +08:00
{
2014-08-28 07:31:57 +08:00
auto ret = new ( std : : nothrow ) GLViewImpl ( ) ;
2014-01-24 07:36:55 +08:00
if ( ret & & ret - > initWithFullScreen ( viewName ) ) {
ret - > autorelease ( ) ;
return ret ;
}
2014-03-07 13:59:56 +08:00
2014-01-24 07:36:55 +08:00
return nullptr ;
}
2013-08-21 12:01:56 +08:00
2014-07-31 00:53:04 +08:00
GLViewImpl * GLViewImpl : : createWithFullScreen ( const std : : string & viewName , const GLFWvidmode & videoMode , GLFWmonitor * monitor )
2014-04-09 01:33:24 +08:00
{
2014-08-28 07:31:57 +08:00
auto ret = new ( std : : nothrow ) GLViewImpl ( ) ;
2014-04-09 01:33:24 +08:00
if ( ret & & ret - > initWithFullscreen ( viewName , videoMode , monitor ) ) {
ret - > autorelease ( ) ;
return ret ;
}
return nullptr ;
}
2014-07-31 00:53:04 +08:00
bool GLViewImpl : : initWithRect ( const std : : string & viewName , Rect rect , float frameZoomFactor )
2012-07-21 12:23:40 +08:00
{
2013-08-29 11:36:16 +08:00
setViewName ( viewName ) ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
_frameZoomFactor = frameZoomFactor ;
2014-03-07 13:59:56 +08:00
2013-08-21 12:01:56 +08:00
glfwWindowHint ( GLFW_RESIZABLE , GL_FALSE ) ;
2014-08-22 16:22:16 +08:00
glfwWindowHint ( GLFW_RED_BITS , _glContextAttrs . redBits ) ;
glfwWindowHint ( GLFW_GREEN_BITS , _glContextAttrs . greenBits ) ;
glfwWindowHint ( GLFW_BLUE_BITS , _glContextAttrs . blueBits ) ;
glfwWindowHint ( GLFW_ALPHA_BITS , _glContextAttrs . alphaBits ) ;
glfwWindowHint ( GLFW_DEPTH_BITS , _glContextAttrs . depthBits ) ;
glfwWindowHint ( GLFW_STENCIL_BITS , _glContextAttrs . stencilBits ) ;
2014-03-07 13:59:56 +08:00
2015-04-06 19:24:22 +08:00
int needWidth = rect . size . width * _frameZoomFactor ;
int neeHeight = rect . size . height * _frameZoomFactor ;
_mainWindow = glfwCreateWindow ( needWidth , neeHeight , _viewName . c_str ( ) , _monitor , nullptr ) ;
2015-08-06 04:06:16 +08:00
if ( _mainWindow = = nullptr )
{
std : : string message = " Can't create window " ;
if ( ! _glfwError . empty ( ) )
{
message . append ( " \n More info: \n " ) ;
message . append ( _glfwError ) ;
}
MessageBox ( message . c_str ( ) , " Error launch application " ) ;
return false ;
}
2015-04-06 19:24:22 +08:00
/*
* Note that the created window and context may differ from what you requested ,
* as not all parameters and hints are
* [ hard constraints ] ( @ ref window_hints_hard ) . This includes the size of the
* window , especially for full screen windows . To retrieve the actual
* attributes of the created window and context , use queries like @ ref
* glfwGetWindowAttrib and @ ref glfwGetWindowSize .
*
* see declaration glfwCreateWindow
*/
int realW = 0 , realH = 0 ;
glfwGetWindowSize ( _mainWindow , & realW , & realH ) ;
if ( realW ! = needWidth )
{
rect . size . width = realW / _frameZoomFactor ;
}
if ( realH ! = neeHeight )
{
rect . size . height = realH / _frameZoomFactor ;
}
2013-08-21 12:01:56 +08:00
glfwMakeContextCurrent ( _mainWindow ) ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
glfwSetMouseButtonCallback ( _mainWindow , GLFWEventHandler : : onGLFWMouseCallBack ) ;
glfwSetCursorPosCallback ( _mainWindow , GLFWEventHandler : : onGLFWMouseMoveCallBack ) ;
glfwSetScrollCallback ( _mainWindow , GLFWEventHandler : : onGLFWMouseScrollCallback ) ;
glfwSetCharCallback ( _mainWindow , GLFWEventHandler : : onGLFWCharCallback ) ;
glfwSetKeyCallback ( _mainWindow , GLFWEventHandler : : onGLFWKeyCallback ) ;
glfwSetWindowPosCallback ( _mainWindow , GLFWEventHandler : : onGLFWWindowPosCallback ) ;
glfwSetFramebufferSizeCallback ( _mainWindow , GLFWEventHandler : : onGLFWframebuffersize ) ;
2014-03-07 13:59:56 +08:00
glfwSetWindowSizeCallback ( _mainWindow , GLFWEventHandler : : onGLFWWindowSizeFunCallback ) ;
2014-08-14 15:48:09 +08:00
glfwSetWindowIconifyCallback ( _mainWindow , GLFWEventHandler : : onGLFWWindowIconifyCallback ) ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
setFrameSize ( rect . size . width , rect . size . height ) ;
2014-03-07 13:59:56 +08:00
2013-08-21 12:01:56 +08:00
// check OpenGL version at first
const GLubyte * glVersion = glGetString ( GL_VERSION ) ;
2014-03-07 13:59:56 +08:00
2014-07-14 20:45:24 +08:00
if ( utils : : atof ( ( const char * ) glVersion ) < 1.5 )
2013-08-21 12:01:56 +08:00
{
char strComplain [ 256 ] = { 0 } ;
sprintf ( strComplain ,
" OpenGL 1.5 or higher is required (your version is %s). Please upgrade the driver of your video card. " ,
glVersion ) ;
MessageBox ( strComplain , " OpenGL version too old " ) ;
return false ;
}
2014-03-07 13:59:56 +08:00
2014-01-23 05:51:01 +08:00
initGlew ( ) ;
2014-01-23 05:05:37 +08:00
2014-01-23 15:56:12 +08:00
// Enable point size by default.
2013-08-21 12:01:56 +08:00
glEnable ( GL_VERTEX_PROGRAM_POINT_SIZE ) ;
2014-03-07 13:59:56 +08:00
2013-08-21 12:01:56 +08:00
return true ;
2012-07-21 12:23:40 +08:00
}
2014-07-31 00:53:04 +08:00
bool GLViewImpl : : initWithFullScreen ( const std : : string & viewName )
2014-01-23 15:56:12 +08:00
{
2014-04-09 01:33:24 +08:00
//Create fullscreen window on primary monitor at its current video mode.
_monitor = glfwGetPrimaryMonitor ( ) ;
if ( nullptr = = _monitor )
2014-01-23 15:56:12 +08:00
return false ;
2014-03-07 13:59:56 +08:00
2014-04-09 01:33:24 +08:00
const GLFWvidmode * videoMode = glfwGetVideoMode ( _monitor ) ;
2014-01-31 12:16:40 +08:00
return initWithRect ( viewName , Rect ( 0 , 0 , videoMode - > width , videoMode - > height ) , 1.0f ) ;
2014-01-23 15:56:12 +08:00
}
2014-07-31 00:53:04 +08:00
bool GLViewImpl : : initWithFullscreen ( const std : : string & viewname , const GLFWvidmode & videoMode , GLFWmonitor * monitor )
2014-04-09 01:33:24 +08:00
{
//Create fullscreen on specified monitor at the specified video mode.
_monitor = monitor ;
if ( nullptr = = _monitor )
return false ;
//These are soft contraints. 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.
glfwWindowHint ( GLFW_REFRESH_RATE , videoMode . refreshRate ) ;
glfwWindowHint ( GLFW_RED_BITS , videoMode . redBits ) ;
glfwWindowHint ( GLFW_BLUE_BITS , videoMode . blueBits ) ;
glfwWindowHint ( GLFW_GREEN_BITS , videoMode . greenBits ) ;
return initWithRect ( viewname , Rect ( 0 , 0 , videoMode . width , videoMode . height ) , 1.0f ) ;
}
2014-07-31 00:53:04 +08:00
bool GLViewImpl : : isOpenGLReady ( )
2012-07-21 12:23:40 +08:00
{
2013-08-21 12:01:56 +08:00
return nullptr ! = _mainWindow ;
2012-07-21 12:23:40 +08:00
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : end ( )
2012-07-21 12:23:40 +08:00
{
2013-08-21 12:01:56 +08:00
if ( _mainWindow )
2014-02-02 04:10:00 +08:00
{
2013-08-21 12:01:56 +08:00
glfwSetWindowShouldClose ( _mainWindow , 1 ) ;
2014-02-02 04:10:00 +08:00
_mainWindow = nullptr ;
}
2014-07-31 00:53:04 +08:00
// Release self. Otherwise, GLViewImpl could not be freed.
2014-03-12 21:45:57 +08:00
release ( ) ;
2012-07-21 12:23:40 +08:00
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : swapBuffers ( )
2012-07-21 12:23:40 +08:00
{
2013-08-21 12:01:56 +08:00
if ( _mainWindow )
glfwSwapBuffers ( _mainWindow ) ;
2012-07-21 12:23:40 +08:00
}
2014-07-31 00:53:04 +08:00
bool GLViewImpl : : windowShouldClose ( )
2012-07-21 12:23:40 +08:00
{
2013-08-21 12:01:56 +08:00
if ( _mainWindow )
2014-03-14 15:38:43 +08:00
return glfwWindowShouldClose ( _mainWindow ) ? true : false ;
2012-07-21 12:23:40 +08:00
else
2013-08-21 12:01:56 +08:00
return true ;
2012-07-21 12:23:40 +08:00
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : pollEvents ( )
2013-08-21 12:01:56 +08:00
{
glfwPollEvents ( ) ;
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : enableRetina ( bool enabled )
2014-03-07 13:50:50 +08:00
{
# if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
_isRetinaEnabled = enabled ;
if ( _isRetinaEnabled )
{
_retinaFactor = 1 ;
}
else
{
_retinaFactor = 2 ;
}
updateFrameSize ( ) ;
# endif
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : setIMEKeyboardState ( bool /*bOpen*/ )
2012-10-19 11:33:06 +08:00
{
2014-03-07 13:59:56 +08:00
2013-08-21 12:01:56 +08:00
}
2015-02-12 09:21:04 +08:00
void GLViewImpl : : setCursorVisible ( bool isVisible )
{
if ( _mainWindow = = NULL )
return ;
if ( isVisible )
glfwSetInputMode ( _mainWindow , GLFW_CURSOR , GLFW_CURSOR_NORMAL ) ;
else
glfwSetInputMode ( _mainWindow , GLFW_CURSOR , GLFW_CURSOR_HIDDEN ) ;
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : setFrameZoomFactor ( float zoomFactor )
2013-08-21 12:01:56 +08:00
{
2014-03-07 13:50:50 +08:00
CCASSERT ( zoomFactor > 0.0f , " zoomFactor must be larger than 0 " ) ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
if ( fabs ( _frameZoomFactor - zoomFactor ) < FLT_EPSILON )
{
return ;
}
2014-03-07 13:59:56 +08:00
2013-12-18 17:47:20 +08:00
_frameZoomFactor = zoomFactor ;
2014-03-07 13:50:50 +08:00
updateFrameSize ( ) ;
2013-08-21 12:01:56 +08:00
}
2014-07-31 00:53:04 +08:00
float GLViewImpl : : getFrameZoomFactor ( ) const
2013-08-21 12:01:56 +08:00
{
return _frameZoomFactor ;
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : updateFrameSize ( )
2014-03-07 13:50:50 +08:00
{
if ( _screenSize . width > 0 & & _screenSize . height > 0 )
{
int w = 0 , h = 0 ;
glfwGetWindowSize ( _mainWindow , & w , & h ) ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
int frameBufferW = 0 , frameBufferH = 0 ;
glfwGetFramebufferSize ( _mainWindow , & frameBufferW , & frameBufferH ) ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
if ( frameBufferW = = 2 * w & & frameBufferH = = 2 * h )
{
if ( _isRetinaEnabled )
{
_retinaFactor = 1 ;
}
else
{
_retinaFactor = 2 ;
}
glfwSetWindowSize ( _mainWindow , _screenSize . width / 2 * _retinaFactor * _frameZoomFactor , _screenSize . height / 2 * _retinaFactor * _frameZoomFactor ) ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
_isInRetinaMonitor = true ;
}
else
{
if ( _isInRetinaMonitor )
{
_retinaFactor = 1 ;
}
glfwSetWindowSize ( _mainWindow , _screenSize . width * _retinaFactor * _frameZoomFactor , _screenSize . height * _retinaFactor * _frameZoomFactor ) ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
_isInRetinaMonitor = false ;
}
}
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : setFrameSize ( float width , float height )
2013-08-21 12:01:56 +08:00
{
2014-07-31 00:53:04 +08:00
GLView : : setFrameSize ( width , height ) ;
2014-03-07 13:50:50 +08:00
updateFrameSize ( ) ;
2013-08-21 12:01:56 +08:00
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : setViewPortInPoints ( float x , float y , float w , float h )
2013-08-21 12:01:56 +08:00
{
2015-08-05 10:57:27 +08:00
experimental : : Viewport vp ( ( float ) ( x * _scaleX * _retinaFactor * _frameZoomFactor + _viewPortRect . origin . x * _retinaFactor * _frameZoomFactor ) ,
2015-06-01 14:26:08 +08:00
( float ) ( y * _scaleY * _retinaFactor * _frameZoomFactor + _viewPortRect . origin . y * _retinaFactor * _frameZoomFactor ) ,
( float ) ( w * _scaleX * _retinaFactor * _frameZoomFactor ) ,
2015-08-05 10:57:27 +08:00
( float ) ( h * _scaleY * _retinaFactor * _frameZoomFactor ) ) ;
2015-06-01 14:26:08 +08:00
Camera : : setDefaultViewport ( vp ) ;
2012-10-19 11:33:06 +08:00
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : setScissorInPoints ( float x , float y , float w , float h )
2012-10-24 10:03:19 +08:00
{
2014-03-07 13:50:50 +08:00
glScissor ( ( GLint ) ( x * _scaleX * _retinaFactor * _frameZoomFactor + _viewPortRect . origin . x * _retinaFactor * _frameZoomFactor ) ,
( GLint ) ( y * _scaleY * _retinaFactor * _frameZoomFactor + _viewPortRect . origin . y * _retinaFactor * _frameZoomFactor ) ,
( GLsizei ) ( w * _scaleX * _retinaFactor * _frameZoomFactor ) ,
( GLsizei ) ( h * _scaleY * _retinaFactor * _frameZoomFactor ) ) ;
}
2015-07-29 18:43:11 +08:00
Rect GLViewImpl : : getScissorRect ( ) const
{
GLfloat params [ 4 ] ;
glGetFloatv ( GL_SCISSOR_BOX , params ) ;
float x = ( params [ 0 ] - _viewPortRect . origin . x * _retinaFactor * _frameZoomFactor ) / ( _scaleX * _retinaFactor * _frameZoomFactor ) ;
float y = ( params [ 1 ] - _viewPortRect . origin . y * _retinaFactor * _frameZoomFactor ) / ( _scaleY * _retinaFactor * _frameZoomFactor ) ;
float w = params [ 2 ] / ( _scaleX * _retinaFactor * _frameZoomFactor ) ;
float h = params [ 3 ] / ( _scaleY * _retinaFactor * _frameZoomFactor ) ;
return Rect ( x , y , w , h ) ;
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : onGLFWError ( int errorID , const char * errorDesc )
2014-03-07 13:50:50 +08:00
{
2015-08-06 04:06:16 +08:00
_glfwError = StringUtils : : format ( " GLFWError #%d Happen, %s " , errorID , errorDesc ) ;
CCLOGERROR ( _glfwError . c_str ( ) ) ;
2014-03-07 13:50:50 +08:00
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : onGLFWMouseCallBack ( GLFWwindow * window , int button , int action , int modify )
2014-03-07 13:50:50 +08:00
{
if ( GLFW_MOUSE_BUTTON_LEFT = = button )
{
if ( GLFW_PRESS = = action )
{
_captured = true ;
2014-05-15 01:07:09 +08:00
if ( this - > getViewPortRect ( ) . equals ( Rect : : ZERO ) | | this - > getViewPortRect ( ) . containsPoint ( Vec2 ( _mouseX , _mouseY ) ) )
2014-03-07 13:50:50 +08:00
{
2014-03-26 15:44:38 +08:00
intptr_t id = 0 ;
2014-03-07 13:50:50 +08:00
this - > handleTouchesBegin ( 1 , & id , & _mouseX , & _mouseY ) ;
}
}
else if ( GLFW_RELEASE = = action )
{
2014-03-19 16:53:42 +08:00
if ( _captured )
2014-03-07 13:50:50 +08:00
{
2014-03-19 16:53:42 +08:00
_captured = false ;
2014-03-26 15:44:38 +08:00
intptr_t id = 0 ;
2014-03-07 13:50:50 +08:00
this - > handleTouchesEnd ( 1 , & id , & _mouseX , & _mouseY ) ;
}
}
}
2014-07-01 15:14:41 +08:00
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
float cursorX = ( _mouseX - _viewPortRect . origin . x ) / _scaleX ;
float cursorY = ( _viewPortRect . origin . y + _viewPortRect . size . height - _mouseY ) / _scaleY ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
if ( GLFW_PRESS = = action )
{
EventMouse event ( EventMouse : : MouseEventType : : MOUSE_DOWN ) ;
2014-07-01 15:14:41 +08:00
event . setCursorPosition ( cursorX , cursorY ) ;
2014-03-07 13:50:50 +08:00
event . setMouseButton ( button ) ;
Director : : getInstance ( ) - > getEventDispatcher ( ) - > dispatchEvent ( & event ) ;
}
else if ( GLFW_RELEASE = = action )
{
EventMouse event ( EventMouse : : MouseEventType : : MOUSE_UP ) ;
2014-07-01 15:14:41 +08:00
event . setCursorPosition ( cursorX , cursorY ) ;
2014-03-07 13:50:50 +08:00
event . setMouseButton ( button ) ;
Director : : getInstance ( ) - > getEventDispatcher ( ) - > dispatchEvent ( & event ) ;
}
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : onGLFWMouseMoveCallBack ( GLFWwindow * window , double x , double y )
2014-03-07 13:50:50 +08:00
{
_mouseX = ( float ) x ;
_mouseY = ( float ) y ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
_mouseX / = this - > getFrameZoomFactor ( ) ;
_mouseY / = this - > getFrameZoomFactor ( ) ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
if ( _isInRetinaMonitor )
{
if ( _retinaFactor = = 1 )
{
_mouseX * = 2 ;
_mouseY * = 2 ;
}
}
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
if ( _captured )
{
2014-03-26 15:44:38 +08:00
intptr_t id = 0 ;
2014-03-19 16:53:42 +08:00
this - > handleTouchesMove ( 1 , & id , & _mouseX , & _mouseY ) ;
2014-03-07 13:50:50 +08:00
}
2014-07-01 15:14:41 +08:00
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
float cursorX = ( _mouseX - _viewPortRect . origin . x ) / _scaleX ;
float cursorY = ( _viewPortRect . origin . y + _viewPortRect . size . height - _mouseY ) / _scaleY ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
EventMouse event ( EventMouse : : MouseEventType : : MOUSE_MOVE ) ;
2014-07-01 13:01:04 +08:00
// Set current button
if ( glfwGetMouseButton ( window , GLFW_MOUSE_BUTTON_LEFT ) = = GLFW_PRESS )
{
event . setMouseButton ( GLFW_MOUSE_BUTTON_LEFT ) ;
}
else if ( glfwGetMouseButton ( window , GLFW_MOUSE_BUTTON_RIGHT ) = = GLFW_PRESS )
{
event . setMouseButton ( GLFW_MOUSE_BUTTON_RIGHT ) ;
}
else if ( glfwGetMouseButton ( window , GLFW_MOUSE_BUTTON_MIDDLE ) = = GLFW_PRESS )
{
event . setMouseButton ( GLFW_MOUSE_BUTTON_MIDDLE ) ;
}
2014-07-01 15:14:41 +08:00
event . setCursorPosition ( cursorX , cursorY ) ;
2014-03-07 13:50:50 +08:00
Director : : getInstance ( ) - > getEventDispatcher ( ) - > dispatchEvent ( & event ) ;
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : onGLFWMouseScrollCallback ( GLFWwindow * window , double x , double y )
2014-03-07 13:50:50 +08:00
{
EventMouse event ( EventMouse : : MouseEventType : : MOUSE_SCROLL ) ;
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
2014-09-02 11:13:30 +08:00
float cursorX = ( _mouseX - _viewPortRect . origin . x ) / _scaleX ;
float cursorY = ( _viewPortRect . origin . y + _viewPortRect . size . height - _mouseY ) / _scaleY ;
2014-03-07 13:50:50 +08:00
event . setScrollData ( ( float ) x , - ( float ) y ) ;
2014-09-02 11:13:30 +08:00
event . setCursorPosition ( cursorX , cursorY ) ;
2014-03-07 13:50:50 +08:00
Director : : getInstance ( ) - > getEventDispatcher ( ) - > dispatchEvent ( & event ) ;
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : onGLFWKeyCallback ( GLFWwindow * window , int key , int scancode , int action , int mods )
2014-03-07 13:50:50 +08:00
{
if ( GLFW_REPEAT ! = action )
{
EventKeyboard event ( g_keyCodeMap [ key ] , GLFW_PRESS = = action ) ;
auto dispatcher = Director : : getInstance ( ) - > getEventDispatcher ( ) ;
dispatcher - > dispatchEvent ( & event ) ;
}
2014-11-26 10:40:43 +08:00
2014-08-10 00:42:21 +08:00
if ( GLFW_RELEASE ! = action & & g_keyCodeMap [ key ] = = EventKeyboard : : KeyCode : : KEY_BACKSPACE )
{
IMEDispatcher : : sharedDispatcher ( ) - > dispatchDeleteBackward ( ) ;
}
2014-03-07 13:50:50 +08:00
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : onGLFWCharCallback ( GLFWwindow * window , unsigned int character )
2014-03-07 13:50:50 +08:00
{
2014-08-10 00:42:21 +08:00
char16_t wcharString [ 2 ] = { ( char16_t ) character , 0 } ;
std : : string utf8String ;
StringUtils : : UTF16ToUTF8 ( wcharString , utf8String ) ;
IMEDispatcher : : sharedDispatcher ( ) - > dispatchInsertText ( utf8String . c_str ( ) , utf8String . size ( ) ) ;
2014-03-07 13:50:50 +08:00
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : onGLFWWindowPosCallback ( GLFWwindow * windows , int x , int y )
2014-03-07 13:50:50 +08:00
{
Director : : getInstance ( ) - > setViewport ( ) ;
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : onGLFWframebuffersize ( GLFWwindow * window , int w , int h )
2014-03-07 13:50:50 +08:00
{
float frameSizeW = _screenSize . width ;
float frameSizeH = _screenSize . height ;
float factorX = frameSizeW / w * _retinaFactor * _frameZoomFactor ;
float factorY = frameSizeH / h * _retinaFactor * _frameZoomFactor ;
2014-03-07 13:59:56 +08:00
2014-03-07 13:50:50 +08:00
if ( fabs ( factorX - 0.5f ) < FLT_EPSILON & & fabs ( factorY - 0.5f ) < FLT_EPSILON )
{
_isInRetinaMonitor = true ;
if ( _isRetinaEnabled )
{
_retinaFactor = 1 ;
}
else
{
_retinaFactor = 2 ;
}
glfwSetWindowSize ( window , static_cast < int > ( frameSizeW * 0.5f * _retinaFactor * _frameZoomFactor ) , static_cast < int > ( frameSizeH * 0.5f * _retinaFactor * _frameZoomFactor ) ) ;
}
else if ( fabs ( factorX - 2.0f ) < FLT_EPSILON & & fabs ( factorY - 2.0f ) < FLT_EPSILON )
{
_isInRetinaMonitor = false ;
_retinaFactor = 1 ;
glfwSetWindowSize ( window , static_cast < int > ( frameSizeW * _retinaFactor * _frameZoomFactor ) , static_cast < int > ( frameSizeH * _retinaFactor * _frameZoomFactor ) ) ;
}
}
2014-07-31 00:53:04 +08:00
void GLViewImpl : : onGLFWWindowSizeFunCallback ( GLFWwindow * window , int width , int height )
2014-03-07 13:50:50 +08:00
{
2014-03-07 13:59:56 +08:00
if ( _resolutionPolicy ! = ResolutionPolicy : : UNKNOWN )
{
2014-03-07 14:01:30 +08:00
updateDesignResolutionSize ( ) ;
2014-03-07 13:59:56 +08:00
Director : : getInstance ( ) - > setViewport ( ) ;
}
2012-10-24 10:03:19 +08:00
}
2014-08-13 03:22:47 +08:00
void GLViewImpl : : onGLFWWindowIconifyCallback ( GLFWwindow * window , int iconified )
{
2014-08-14 15:48:09 +08:00
if ( iconified = = GL_TRUE )
{
Application : : getInstance ( ) - > applicationDidEnterBackground ( ) ;
}
else
{
Application : : getInstance ( ) - > applicationWillEnterForeground ( ) ;
}
2014-08-13 03:22:47 +08:00
}
2014-01-23 10:27:42 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
static bool glew_dynamic_binding ( )
{
2014-03-07 13:59:56 +08:00
const char * gl_extensions = ( const char * ) glGetString ( GL_EXTENSIONS ) ;
// If the current opengl driver doesn't have framebuffers methods, check if an extension exists
2014-07-10 00:45:27 +08:00
if ( glGenFramebuffers = = nullptr )
2014-03-07 13:59:56 +08:00
{
2014-07-10 00:45:27 +08:00
log ( " OpenGL: glGenFramebuffers is nullptr, try to detect an extension " ) ;
2014-03-07 13:59:56 +08:00
if ( strstr ( gl_extensions , " ARB_framebuffer_object " ) )
{
log ( " OpenGL: ARB_framebuffer_object is supported " ) ;
glIsRenderbuffer = ( PFNGLISRENDERBUFFERPROC ) wglGetProcAddress ( " glIsRenderbuffer " ) ;
glBindRenderbuffer = ( PFNGLBINDRENDERBUFFERPROC ) wglGetProcAddress ( " glBindRenderbuffer " ) ;
glDeleteRenderbuffers = ( PFNGLDELETERENDERBUFFERSPROC ) wglGetProcAddress ( " glDeleteRenderbuffers " ) ;
glGenRenderbuffers = ( PFNGLGENRENDERBUFFERSPROC ) wglGetProcAddress ( " glGenRenderbuffers " ) ;
glRenderbufferStorage = ( PFNGLRENDERBUFFERSTORAGEPROC ) wglGetProcAddress ( " glRenderbufferStorage " ) ;
glGetRenderbufferParameteriv = ( PFNGLGETRENDERBUFFERPARAMETERIVPROC ) wglGetProcAddress ( " glGetRenderbufferParameteriv " ) ;
glIsFramebuffer = ( PFNGLISFRAMEBUFFERPROC ) wglGetProcAddress ( " glIsFramebuffer " ) ;
glBindFramebuffer = ( PFNGLBINDFRAMEBUFFERPROC ) wglGetProcAddress ( " glBindFramebuffer " ) ;
glDeleteFramebuffers = ( PFNGLDELETEFRAMEBUFFERSPROC ) wglGetProcAddress ( " glDeleteFramebuffers " ) ;
glGenFramebuffers = ( PFNGLGENFRAMEBUFFERSPROC ) wglGetProcAddress ( " glGenFramebuffers " ) ;
glCheckFramebufferStatus = ( PFNGLCHECKFRAMEBUFFERSTATUSPROC ) wglGetProcAddress ( " glCheckFramebufferStatus " ) ;
glFramebufferTexture1D = ( PFNGLFRAMEBUFFERTEXTURE1DPROC ) wglGetProcAddress ( " glFramebufferTexture1D " ) ;
glFramebufferTexture2D = ( PFNGLFRAMEBUFFERTEXTURE2DPROC ) wglGetProcAddress ( " glFramebufferTexture2D " ) ;
glFramebufferTexture3D = ( PFNGLFRAMEBUFFERTEXTURE3DPROC ) wglGetProcAddress ( " glFramebufferTexture3D " ) ;
glFramebufferRenderbuffer = ( PFNGLFRAMEBUFFERRENDERBUFFERPROC ) wglGetProcAddress ( " glFramebufferRenderbuffer " ) ;
glGetFramebufferAttachmentParameteriv = ( PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC ) wglGetProcAddress ( " glGetFramebufferAttachmentParameteriv " ) ;
glGenerateMipmap = ( PFNGLGENERATEMIPMAPPROC ) wglGetProcAddress ( " glGenerateMipmap " ) ;
}
else
if ( strstr ( gl_extensions , " EXT_framebuffer_object " ) )
{
log ( " OpenGL: EXT_framebuffer_object is supported " ) ;
glIsRenderbuffer = ( PFNGLISRENDERBUFFERPROC ) wglGetProcAddress ( " glIsRenderbufferEXT " ) ;
glBindRenderbuffer = ( PFNGLBINDRENDERBUFFERPROC ) wglGetProcAddress ( " glBindRenderbufferEXT " ) ;
glDeleteRenderbuffers = ( PFNGLDELETERENDERBUFFERSPROC ) wglGetProcAddress ( " glDeleteRenderbuffersEXT " ) ;
glGenRenderbuffers = ( PFNGLGENRENDERBUFFERSPROC ) wglGetProcAddress ( " glGenRenderbuffersEXT " ) ;
glRenderbufferStorage = ( PFNGLRENDERBUFFERSTORAGEPROC ) wglGetProcAddress ( " glRenderbufferStorageEXT " ) ;
glGetRenderbufferParameteriv = ( PFNGLGETRENDERBUFFERPARAMETERIVPROC ) wglGetProcAddress ( " glGetRenderbufferParameterivEXT " ) ;
glIsFramebuffer = ( PFNGLISFRAMEBUFFERPROC ) wglGetProcAddress ( " glIsFramebufferEXT " ) ;
glBindFramebuffer = ( PFNGLBINDFRAMEBUFFERPROC ) wglGetProcAddress ( " glBindFramebufferEXT " ) ;
glDeleteFramebuffers = ( PFNGLDELETEFRAMEBUFFERSPROC ) wglGetProcAddress ( " glDeleteFramebuffersEXT " ) ;
glGenFramebuffers = ( PFNGLGENFRAMEBUFFERSPROC ) wglGetProcAddress ( " glGenFramebuffersEXT " ) ;
glCheckFramebufferStatus = ( PFNGLCHECKFRAMEBUFFERSTATUSPROC ) wglGetProcAddress ( " glCheckFramebufferStatusEXT " ) ;
glFramebufferTexture1D = ( PFNGLFRAMEBUFFERTEXTURE1DPROC ) wglGetProcAddress ( " glFramebufferTexture1DEXT " ) ;
glFramebufferTexture2D = ( PFNGLFRAMEBUFFERTEXTURE2DPROC ) wglGetProcAddress ( " glFramebufferTexture2DEXT " ) ;
glFramebufferTexture3D = ( PFNGLFRAMEBUFFERTEXTURE3DPROC ) wglGetProcAddress ( " glFramebufferTexture3DEXT " ) ;
glFramebufferRenderbuffer = ( PFNGLFRAMEBUFFERRENDERBUFFERPROC ) wglGetProcAddress ( " glFramebufferRenderbufferEXT " ) ;
glGetFramebufferAttachmentParameteriv = ( PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC ) wglGetProcAddress ( " glGetFramebufferAttachmentParameterivEXT " ) ;
glGenerateMipmap = ( PFNGLGENERATEMIPMAPPROC ) wglGetProcAddress ( " glGenerateMipmapEXT " ) ;
}
else
{
log ( " OpenGL: No framebuffers extension is supported " ) ;
log ( " OpenGL: Any call to Fbo will crash! " ) ;
return false ;
}
}
return true ;
2014-01-23 10:27:42 +08:00
}
# endif
2014-01-23 05:51:01 +08:00
// helper
2014-07-31 00:53:04 +08:00
bool GLViewImpl : : initGlew ( )
2014-01-23 05:51:01 +08:00
{
2014-01-23 10:27:42 +08:00
# if (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
2014-01-23 05:51:01 +08:00
GLenum GlewInitResult = glewInit ( ) ;
if ( GLEW_OK ! = GlewInitResult )
{
MessageBox ( ( char * ) glewGetErrorString ( GlewInitResult ) , " OpenGL error " ) ;
return false ;
}
if ( GLEW_ARB_vertex_shader & & GLEW_ARB_fragment_shader )
{
log ( " Ready for GLSL " ) ;
}
else
{
log ( " Not totally ready :( " ) ;
}
if ( glewIsSupported ( " GL_VERSION_2_0 " ) )
{
log ( " Ready for OpenGL 2.0 " ) ;
}
else
{
log ( " OpenGL 2.0 not supported " ) ;
}
2014-01-23 10:27:42 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
if ( glew_dynamic_binding ( ) = = false )
{
MessageBox ( " No OpenGL framebuffer support. Please upgrade the driver of your video card. " , " OpenGL error " ) ;
return false ;
2014-03-07 13:59:56 +08:00
}
2014-01-23 10:27:42 +08:00
# endif
2014-01-23 05:51:01 +08:00
2014-01-23 10:27:42 +08:00
# endif // (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
2014-01-23 06:17:32 +08:00
return true ;
2014-01-23 05:51:01 +08:00
}
2012-09-04 02:06:04 +08:00
NS_CC_END // end of namespace cocos2d;