Cocos2dx 事件响应机制(1): GLView

CCGLView.cpp:

部分代码注释如下,由于作者是在windows下阅读的代码,因此这里的CCGLView的路径为【 cocos2d\cocos\platform\desktop\CCGLView.cpp 】:

// GLFWEventHandler
/*
定义了一个管理GL回调函数的结构体,里面所有的函数都是静态的,由于是结构体,因此也全部都是默认public的,
这样便于管理,即这里的所有的函数都是用来处理openGL的回调的。
同时,这里所有的函数都没有实际的处理逻辑,全部是调用的GLView的函数,通过这种方式就可以通过编译时的函数指针检查
*/
class GLFWEventHandler
{
public:
    static void onGLFWError(int errorID, const char* errorDesc)
    {
        if (_view)
            _view->onGLFWError(errorID, errorDesc);
    }
 
    static void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
    {
        if (_view)
            _view->onGLFWMouseCallBack(window, button, action, modify);
    }
 
    static void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
    {
        if (_view)
            _view->onGLFWMouseMoveCallBack(window, x, y);
    }
 
    static void onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y)
    {
        if (_view)
            _view->onGLFWMouseScrollCallback(window, x, y);
    }
 
    static void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
    {
        if (_view)
            _view->onGLFWKeyCallback(window, key, scancode, action, mods);
    }
 
    static void onGLFWCharCallback(GLFWwindow* window, unsigned int character)
    {
        if (_view)
            _view->onGLFWCharCallback(window, character);
    }
 
    static void onGLFWWindowPosCallback(GLFWwindow* windows, int x, int y)
    {
        if (_view)
            _view->onGLFWWindowPosCallback(windows, x, y);
    }
 
    static void onGLFWframebuffersize(GLFWwindow* window, int w, int h)
    {
        if (_view)
            _view->onGLFWframebuffersize(window, w, h);
    }
 
    static void onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height)
    {
        if (_view)
            _view->onGLFWWindowSizeFunCallback(window, width, height);
    }
 
/*
通过这个函数和下面的静态私有变量,来给结构体中的_view赋值。
*/
    static void setGLView(GLView* view)
    {
        _view = view;
    }
 
private:
    static GLView* _view;
};
 
//结构体静态变量初始化
GLView* GLFWEventHandler::_view = nullptr;
 
////////////////////////////////////////////////////
 
struct keyCodeItem
{
    int glfwKeyCode;
    EventKeyboard::KeyCode keyCode;
};
 
static std::unordered_map<int, EventKeyboard::KeyCode> g_keyCodeMap;
 
static keyCodeItem g_keyCodeStructArray[] = {
    /* The unknown key */
    { GLFW_KEY_UNKNOWN         , EventKeyboard::KeyCode::KEY_NONE          },
 
    /* Printable keys */
    { GLFW_KEY_SPACE           , EventKeyboard::KeyCode::KEY_SPACE         },
。。。。。
。。。。。
    { GLFW_KEY_MENU            , EventKeyboard::KeyCode::KEY_MENU          },
    { GLFW_KEY_LAST            , EventKeyboard::KeyCode::KEY_NONE          }
};
 
//////////////////////////////////////////////////////////////////////////
// implement GLView
//////////////////////////////////////////////////////////////////////////
 
/* 默认构造函数 */
GLView::GLView()
: _captured(false)
, _supportTouch(false)
, _isInRetinaMonitor(false)
, _isRetinaEnabled(false)
, _retinaFactor(1)
, _frameZoomFactor(1.0f)
, _mainWindow(nullptr)
, _monitor(nullptr) //monitor默认是nullptr
, _mouseX(0.0f)
, _mouseY(0.0f)
{
    _viewName = "cocos2dx";//先暂时命名GL窗口为cocos2dx
    g_keyCodeMap.clear();//将键盘码映射到map中
    for (auto& item : g_keyCodeStructArray)
    {
        g_keyCodeMap[item.glfwKeyCode] = item.keyCode;
    }
     
        //设置好回调函数的调用实例
    GLFWEventHandler::setGLView(this);
        //注册回调函数
    glfwSetErrorCallback(GLFWEventHandler::onGLFWError);
    //初始化GLFW
    glfwInit();
}
 
//析构函数
GLView::~GLView()
{
    CCLOGINFO("deallocing GLView: %p"this);//日志
    GLFWEventHandler::setGLView(nullptr);//干掉回调函数响应
    glfwTerminate();//关闭GLFW
}
 
//create函数,默认大小为960X640
GLView* GLView::create(const std::string& viewName)
{
    auto ret = new GLView;
    if(ret && ret->initWithRect(viewName, Rect(0, 0, 960, 640), 1)) {
        ret->autorelease();
        return ret;
    }
 
    return nullptr;
}
//create函数
GLView* GLView::createWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
{
    auto ret = new GLView;
    if(ret && ret->initWithRect(viewName, rect, frameZoomFactor)) {
        ret->autorelease();
        return ret;
    }
 
    return nullptr;
}
//create函数 全屏大小
GLView* GLView::createWithFullScreen(const std::string& viewName)
{
    auto ret = new GLView();
    if(ret && ret->initWithFullScreen(viewName)) {
        ret->autorelease();
        return ret;
    }
 
    return nullptr;
}
//create函数 全屏大小
GLView* GLView::createWithFullScreen(const std::string& viewName, const GLFWvidmode &videoMode, GLFWmonitor *monitor)
{
    auto ret = new GLView();
    if(ret && ret->initWithFullscreen(viewName, videoMode, monitor)) {
        ret->autorelease();
        return ret;
    }
     
    return nullptr;
}
 
//初始化函数
bool GLView::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
{
//更新下_viewName变量   
    setViewName(viewName);
//更新下缩放整体缩放系数
    _frameZoomFactor = frameZoomFactor;
 
//在调用 glfwCreateWindow 前更新glfw的属性
    glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
 
//调用 glfwCreateWindow,创建窗口
    _mainWindow = glfwCreateWindow(rect.size.width * _frameZoomFactor,
                                   rect.size.height * _frameZoomFactor,
                                   _viewName.c_str(),
                                   _monitor,
                                   nullptr);
//切换为当前上下文
    glfwMakeContextCurrent(_mainWindow);
 
//将当前上下文的回调函数全部注册好
    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);
    glfwSetWindowSizeCallback(_mainWindow, GLFWEventHandler::onGLFWWindowSizeFunCallback);
 
//更新FrameSize大小,这里的FrameSize其实就是windows下游戏窗口的大小
    setFrameSize(rect.size.width, rect.size.height);
 
//检查版本号码
    // check OpenGL version at first
    const GLubyte* glVersion = glGetString(GL_VERSION);
 
    if atof((const char*)glVersion) < 1.5 )
    {
        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;
    }
//初始化Glew
    initGlew();
 
//允许设置点的大小
    // Enable point size by default.
    glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
 
    return true;
}
 
bool GLView::initWithFullScreen(const std::string& viewName)
{
//首先获取当前的主显示器
    //Create fullscreen window on primary monitor at its current video mode.
    _monitor = glfwGetPrimaryMonitor();
    if (nullptr == _monitor)
        return false;
//然后根据当前的主显示器,获取当前显示器的宽 高 颜色位宽 等信息
    const GLFWvidmode* videoMode = glfwGetVideoMode(_monitor);
//根据显示器的宽高初始化   
    return initWithRect(viewName, Rect(0, 0, videoMode->width, videoMode->height), 1.0f);
}
 
bool GLView::initWithFullscreen(const std::string &viewname, const GLFWvidmode &videoMode, GLFWmonitor *monitor)
{
    //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);
}
 
//检查GLEW是否就绪,通过判断窗口是否创建好
bool GLView::isOpenGLReady()
{
    return nullptr != _mainWindow;
}
 
//结束GLEW
void GLView::end()
{
    if(_mainWindow)
    {
        //关闭GLEW的窗口
        glfwSetWindowShouldClose(_mainWindow,1);
        _mainWindow = nullptr;
    }
    // Release self. Otherwise, GLView could not be freed.
    release();//由于GLView继承自Ref,但是外部并不会在程序结束时释放GLView,因此需要自己掉用下Release,来减少引用计数
}
 
void GLView::swapBuffers()
{//交换前后buffer
    if(_mainWindow)
        glfwSwapBuffers(_mainWindow);
}
 
//判断是否需要关闭GLView
bool GLView::windowShouldClose()
{
    if(_mainWindow)
        return glfwWindowShouldClose(_mainWindow) ? true false;
    else
        return true;
}
//类似于消息泵,用来进行消息循环 该函数在Director::drawScene中被调用,而drawScene在DisplayLinkDirector::mainLoopzh中循环执行
void GLView::pollEvents()
{
    glfwPollEvents();
}
 
//支持Retina分辨率,主要用来在Mac平台上
void GLView::enableRetina(bool enabled)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
    _isRetinaEnabled = enabled;
    if (_isRetinaEnabled)
    {
        _retinaFactor = 1;
    }
    else
    {
        _retinaFactor = 2;
    }
    updateFrameSize();
#endif
}
 
 
void GLView::setIMEKeyboardState(bool /*bOpen*/)
{
 
}
 
//设置窗口的缩放大小,由于有时候电脑显示器的分辨率不够,因此为了能够整屏幕
//的显示下GLView,需要再次缩放下窗口的大小
void GLView::setFrameZoomFactor(float zoomFactor)
{
    CCASSERT(zoomFactor > 0.0f, "zoomFactor must be larger than 0");
 
    if (fabs(_frameZoomFactor - zoomFactor) < FLT_EPSILON)
    {
        return;
    }
 
    _frameZoomFactor = zoomFactor;
    updateFrameSize();
}
//获取窗口缩放大小
float GLView::getFrameZoomFactor()
{
    return _frameZoomFactor;
}
 
//更新游戏窗口大小,主要通过调用glfwSetWindowSize来实现
void GLView::updateFrameSize()
{
    if (_screenSize.width > 0 && _screenSize.height > 0)
    {
        int w = 0, h = 0;
        glfwGetWindowSize(_mainWindow, &w, &h);
 
        int frameBufferW = 0, frameBufferH = 0;
        glfwGetFramebufferSize(_mainWindow, &frameBufferW, &frameBufferH);
 
        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);
 
            _isInRetinaMonitor = true;
        }
        else
        {
            if (_isInRetinaMonitor)
            {
                _retinaFactor = 1;
            }
            glfwSetWindowSize(_mainWindow, _screenSize.width * _retinaFactor * _frameZoomFactor, _screenSize.height *_retinaFactor * _frameZoomFactor);
 
            _isInRetinaMonitor = false;
        }
    }
}
//设置游戏窗口大小
void GLView::setFrameSize(float width, float height)
{
    GLViewProtocol::setFrameSize(width, height);
    updateFrameSize();
}
 
//设置视点
void GLView::setViewPortInPoints(float x , float y , float w , float h)
{
    glViewport((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));
}
 
//设置裁剪
void GLView::setScissorInPoints(float x , float y , float w , float h)
{
    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));
}
 
//以下为GLView的内部回调函数,通过上面的结构体的二次封装
 
//错误时直接输出
void GLView::onGLFWError(int errorID, const char* errorDesc)
{
    CCLOGERROR("GLFWError #%d Happen, %s\n", errorID, errorDesc);
}
 
//鼠标按下时的相应函数
void GLView::onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
{
    //按下去的是左键
    if(GLFW_MOUSE_BUTTON_LEFT == button)
    {
        if(GLFW_PRESS == action)//是按下
        {
            _captured = true;
            if (this->getViewPortRect().equals(Rect::ZERO) || this->getViewPortRect().containsPoint(Vec2(_mouseX,_mouseY)))
            {
                intptr_t id = 0;
                this->handleTouchesBegin(1, &id, &_mouseX, &_mouseY);
            }
        }
        else if(GLFW_RELEASE == action)//是抬起
        {
            if (_captured)
            {
                _captured = false;
                intptr_t id = 0;
                this->handleTouchesEnd(1, &id, &_mouseX, &_mouseY);
            }
        }
    }
 
//二次处理,不管是左键还是右键,直接将鼠标按下事件传入cocos2dx的事件队列中
    if(GLFW_PRESS == action)
    {
        EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN);
        //Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
        event.setCursorPosition(_mouseX, this->getViewPortRect().size.height - _mouseY);
        event.setMouseButton(button);
        Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
    }
    else if(GLFW_RELEASE == action)
        //二次处理,不管是左键还是右键,直接将鼠标松开事件传入cocos2dx的事件队列中
    {
        EventMouse event(EventMouse::MouseEventType::MOUSE_UP);
        //Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
        event.setCursorPosition(_mouseX, this->getViewPortRect().size.height - _mouseY);
        event.setMouseButton(button);
        Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
    }
}
 
//鼠标移动回调函数
void GLView::onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
{
    _mouseX = (float)x;
    _mouseY = (float)y;
 
    _mouseX /= this->getFrameZoomFactor();
    _mouseY /= this->getFrameZoomFactor();
 
    if (_isInRetinaMonitor)//鼠标移动回调函数,判断是否是视网膜屏幕
    {
        if (_retinaFactor == 1)
        {
            _mouseX *= 2;
            _mouseY *= 2;
        }
    }
 
    if (_captured)
    {
        intptr_t id = 0;
        this->handleTouchesMove(1, &id, &_mouseX, &_mouseY);
    }
 
//将事件传入cocos2dx事件队列中
    EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE);
    //Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
    event.setCursorPosition(_mouseX, this->getViewPortRect().size.height - _mouseY);
    Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
}
 
//鼠标滚动回调函数
void GLView::onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y)
{
    EventMouse event(EventMouse::MouseEventType::MOUSE_SCROLL);
    //Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
    event.setScrollData((float)x, -(float)y);
    event.setCursorPosition(_mouseX, this->getViewPortRect().size.height - _mouseY);
    Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
}
 
void GLView::onGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
    if (GLFW_REPEAT != action)
    {
        EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action);
        auto dispatcher = Director::getInstance()->getEventDispatcher();
        dispatcher->dispatchEvent(&event);
    }
}
 
void GLView::onGLFWCharCallback(GLFWwindow *window, unsigned int character)
{
    IMEDispatcher::sharedDispatcher()->dispatchInsertText((const char*) &character, 1);
}
 
void GLView::onGLFWWindowPosCallback(GLFWwindow *windows, int x, int y)
{
    Director::getInstance()->setViewport();
}
 
//设置FrameBufferSize回调函数,通过返回的宽和高设置游戏窗口大小
void GLView::onGLFWframebuffersize(GLFWwindow* window, int w, int h)
{
    float frameSizeW = _screenSize.width;
    float frameSizeH = _screenSize.height;
    float factorX = frameSizeW / w * _retinaFactor * _frameZoomFactor;
    float factorY = frameSizeH / h * _retinaFactor * _frameZoomFactor;
 
    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));
    }
}
 
//设置WindowSize大小回调函数,当WindowSize大小确定之后,会在回调函数中自动更新下设计大小。
void GLView::onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height)
{
    if (_resolutionPolicy != ResolutionPolicy::UNKNOWN)
    {
        updateDesignResolutionSize();
        Director::getInstance()->setViewport();
    }
}
 
//动态绑定内存操作函数
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
static bool glew_dynamic_binding()
{
    const char *gl_extensions = (const char*)glGetString(GL_EXTENSIONS);
 
    // If the current opengl driver doesn't have framebuffers methods, check if an extension exists
    if (glGenFramebuffers == NULL)
    {
        log("OpenGL: glGenFramebuffers is NULL, try to detect an extension");
        if (strstr(gl_extensions, "ARB_framebuffer_object"))
        {
            log("OpenGL: ARB_framebuffer_object is supported");
 
            glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbuffer");
。。。。。
。。。。。
            glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmap");
        }
        else
        if (strstr(gl_extensions, "EXT_framebuffer_object"))
        {
            log("OpenGL: EXT_framebuffer_object is supported");
            glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbufferEXT");
。。。。。
。。。。。
            glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmapEXT");
        }
        else
        {
            log("OpenGL: No framebuffers extension is supported");
            log("OpenGL: Any call to Fbo will crash!");
            return false;
        }
    }
    return true;
}
#endif
 
// helper
bool GLView::initGlew()
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
    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");
    }
 
#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;
    }
#endif
 
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
 
    return true;
}
 
NS_CC_END // end of namespace cocos2d;

这里我们再看一下GLView的继承关系:

class CC_DLL GLView : public GLViewProtocol, public Ref

这里【 Ref 】是用来管理引用计数的。
这里【 GLViewProtocol 】是一个基类,各个平台下会对其进行继承,继承之后的名称都为GLView。

2 鼠标事件响应

 glfwSetMouseButtonCallback(_mainWindow, GLFWEventHandler::onGLFWMouseCallBack);

GLView中注册了相关鼠标事件,当发生鼠标事件时,首先执行:GLView::pollEvents() ,然后调用注册后的GLFWEventHandler::onGLFWMouseCallBack ,接着基于鼠标点击状态调用 this->handleTouchesBegin(1, &id, &_mouseX, &_mouseY); 或者this->handleTouchesEnd(1, &id, &_mouseX, &_mouseY);

猜你喜欢

转载自blog.csdn.net/XIANG__jiangsu/article/details/72731408
今日推荐