2012-07-21 12:23:40 +08:00
|
|
|
/****************************************************************************
|
2012-09-04 02:06:04 +08:00
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2012-07-21 12:23:40 +08:00
|
|
|
#include "CCEGLView.h"
|
|
|
|
#include "EAGLView.h"
|
2013-08-21 12:01:56 +08:00
|
|
|
#include "CCDirector.h"
|
2012-07-21 12:23:40 +08:00
|
|
|
#include "CCSet.h"
|
|
|
|
#include "CCTouch.h"
|
|
|
|
#include "CCTouchDispatcher.h"
|
2013-08-28 16:25:03 +08:00
|
|
|
#include "CCKeyboardDispatcher.h"
|
|
|
|
#include "CCIMEDispatcher.h"
|
2012-07-21 12:23:40 +08:00
|
|
|
|
2012-09-04 02:06:04 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
//begin EGLViewEventHandler
|
|
|
|
class EGLViewEventHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static bool s_captured;
|
|
|
|
static float s_mouseX;
|
|
|
|
static float s_mouseY;
|
|
|
|
|
|
|
|
static void OnGLFWError(int errorID, const char* errorDesc);
|
|
|
|
static void OnGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify);
|
|
|
|
static void OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y);
|
2013-08-28 16:25:03 +08:00
|
|
|
static void OnGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
|
|
|
static void OnGLFWCharCallback(GLFWwindow* window, unsigned int character);
|
2013-09-13 17:11:35 +08:00
|
|
|
static void OnGLFWWindowPosCallback(GLFWwindow* windows, int x, int y);
|
2013-08-21 12:01:56 +08:00
|
|
|
};
|
2012-09-04 02:06:04 +08:00
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
bool EGLViewEventHandler::s_captured = false;
|
|
|
|
float EGLViewEventHandler::s_mouseX = 0;
|
|
|
|
float EGLViewEventHandler::s_mouseY = 0;
|
|
|
|
|
|
|
|
void EGLViewEventHandler::OnGLFWError(int errorID, const char* errorDesc)
|
2012-09-04 02:06:04 +08:00
|
|
|
{
|
2013-08-21 12:01:56 +08:00
|
|
|
CCLOGERROR("GLFWError #%d Happen, %s\n", errorID, errorDesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EGLViewEventHandler::OnGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
|
|
|
|
{
|
|
|
|
EGLView* eglView = EGLView::getInstance();
|
|
|
|
if(nullptr == eglView) return;
|
|
|
|
if(GLFW_MOUSE_BUTTON_LEFT == button)
|
2012-09-04 02:06:04 +08:00
|
|
|
{
|
2013-08-21 12:01:56 +08:00
|
|
|
if(GLFW_PRESS == action)
|
|
|
|
{
|
|
|
|
s_captured = true;
|
|
|
|
if (eglView->getViewPortRect().equals(Rect::ZERO) || eglView->getViewPortRect().containsPoint(Point(s_mouseX,s_mouseY)))
|
|
|
|
{
|
|
|
|
int id = 0;
|
|
|
|
eglView->handleTouchesBegin(1, &id, &s_mouseX, &s_mouseY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(GLFW_RELEASE == action)
|
|
|
|
{
|
|
|
|
s_captured = false;
|
|
|
|
if (eglView->getViewPortRect().equals(Rect::ZERO) || eglView->getViewPortRect().containsPoint(Point(s_mouseX,s_mouseY)))
|
|
|
|
{
|
|
|
|
int id = 0;
|
|
|
|
eglView->handleTouchesEnd(1, &id, &s_mouseX, &s_mouseY);
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 02:06:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
|
2013-07-12 13:11:21 +08:00
|
|
|
{
|
2013-08-21 12:01:56 +08:00
|
|
|
s_mouseX = (float)x;
|
|
|
|
s_mouseY = (float)y;
|
|
|
|
EGLView* eglView = EGLView::getInstance();
|
|
|
|
if(nullptr == eglView) return;
|
|
|
|
|
|
|
|
s_mouseX /= eglView->getFrameZoomFactor();
|
|
|
|
s_mouseY /= eglView->getFrameZoomFactor();
|
|
|
|
|
|
|
|
if(s_captured)
|
|
|
|
{
|
|
|
|
if (eglView->getViewPortRect().equals(Rect::ZERO) || eglView->getViewPortRect().containsPoint(Point(s_mouseX,eglView->getFrameSize().height - s_mouseY)))
|
|
|
|
{
|
|
|
|
int id = 0;
|
|
|
|
eglView->handleTouchesMove(1, &id, &s_mouseX, &s_mouseY);
|
|
|
|
}
|
|
|
|
}
|
2013-07-12 13:11:21 +08:00
|
|
|
}
|
|
|
|
|
2013-08-28 16:25:03 +08:00
|
|
|
void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
|
|
|
{
|
|
|
|
if(GLFW_PRESS == action)
|
|
|
|
{
|
|
|
|
Director::getInstance()->getKeyboardDispatcher()->dispatchKeyboardEvent(key, true);
|
|
|
|
}
|
|
|
|
else if(GLFW_RELEASE == action)
|
|
|
|
{
|
|
|
|
Director::getInstance()->getKeyboardDispatcher()->dispatchKeyboardEvent(key,false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EGLViewEventHandler::OnGLFWCharCallback(GLFWwindow *window, unsigned int character)
|
|
|
|
{
|
|
|
|
IMEDispatcher::sharedDispatcher()->dispatchInsertText((const char*) &character, 1);
|
|
|
|
}
|
|
|
|
|
2013-09-13 17:11:35 +08:00
|
|
|
void EGLViewEventHandler::OnGLFWWindowPosCallback(GLFWwindow *windows, int x, int y)
|
|
|
|
{
|
|
|
|
if(Director::getInstance())
|
|
|
|
{
|
|
|
|
Director::getInstance()->setViewport();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
//end EGLViewEventHandler
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// impliment EGLView
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
EGLView* EGLView::s_pEglView = nullptr;
|
|
|
|
|
|
|
|
EGLView::EGLView()
|
|
|
|
: _captured(false)
|
|
|
|
, _frameZoomFactor(1.0f)
|
|
|
|
, _supportTouch(false)
|
|
|
|
, _mainWindow(nullptr)
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-08-21 12:01:56 +08:00
|
|
|
CCASSERT(nullptr == s_pEglView, "EGLView is singleton, Should be inited only one time\n");
|
|
|
|
s_pEglView = this;
|
|
|
|
strcpy(_viewName, "Cocos2dxWin32");
|
|
|
|
glfwSetErrorCallback(EGLViewEventHandler::OnGLFWError);
|
|
|
|
glfwInit();
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
EGLView::~EGLView()
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-08-22 11:05:06 +08:00
|
|
|
CCLOGINFO("deallocing EGLView: %p", this);
|
2013-08-21 12:01:56 +08:00
|
|
|
glfwTerminate();
|
|
|
|
s_pEglView = nullptr;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
2013-08-29 11:36:16 +08:00
|
|
|
bool EGLView::init(const char *viewName, float width, float height)
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-08-21 12:01:56 +08:00
|
|
|
if(nullptr != _mainWindow) return true;
|
2013-08-29 11:36:16 +08:00
|
|
|
|
|
|
|
setViewName(viewName);
|
|
|
|
setFrameSize(width, height);
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
|
2013-09-13 18:09:11 +08:00
|
|
|
_mainWindow = glfwCreateWindow(_screenSize.width * _frameZoomFactor, _screenSize.height * _frameZoomFactor, _viewName, nullptr, nullptr);
|
2013-08-21 12:01:56 +08:00
|
|
|
glfwMakeContextCurrent(_mainWindow);
|
2013-09-13 18:09:11 +08:00
|
|
|
|
|
|
|
glfwGetFramebufferSize(_mainWindow, &_frameBufferSize[0], &_frameBufferSize[1]);
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
glfwSetMouseButtonCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseCallBack);
|
|
|
|
glfwSetCursorPosCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseMoveCallBack);
|
2013-08-28 16:25:03 +08:00
|
|
|
glfwSetCharCallback(_mainWindow, EGLViewEventHandler::OnGLFWCharCallback);
|
|
|
|
glfwSetKeyCallback(_mainWindow, EGLViewEventHandler::OnGLFWKeyCallback);
|
2013-09-13 17:11:35 +08:00
|
|
|
glfwSetWindowPosCallback(_mainWindow, EGLViewEventHandler::OnGLFWWindowPosCallback);
|
2013-08-21 12:01:56 +08:00
|
|
|
|
|
|
|
// check OpenGL version at first
|
|
|
|
const GLubyte* glVersion = glGetString(GL_VERSION);
|
|
|
|
CCLOG("OpenGL version = %s", glVersion);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// 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(glew_dynamic_binding() == false)
|
|
|
|
// {
|
|
|
|
// MessageBox("No OpenGL framebuffer support. Please upgrade the driver of your video card.", "OpenGL error");
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Enable point size by default on windows.
|
|
|
|
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
|
|
|
|
|
|
|
|
return true;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
bool EGLView::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
|
|
|
}
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
void EGLView::end()
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-08-21 12:01:56 +08:00
|
|
|
if(_mainWindow)
|
|
|
|
glfwSetWindowShouldClose(_mainWindow,1);
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::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
|
|
|
}
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
bool EGLView::windowShouldClose()
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-08-21 12:01:56 +08:00
|
|
|
if(_mainWindow)
|
|
|
|
return glfwWindowShouldClose(_mainWindow);
|
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
|
|
|
}
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
void EGLView::pollEvents()
|
|
|
|
{
|
|
|
|
glfwPollEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EGLView::setIMEKeyboardState(bool /*bOpen*/)
|
2012-10-19 11:33:06 +08:00
|
|
|
{
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EGLView::setFrameZoomFactor(float fZoomFactor)
|
|
|
|
{
|
|
|
|
_frameZoomFactor = fZoomFactor;
|
|
|
|
Director::getInstance()->setProjection(Director::getInstance()->getProjection());
|
|
|
|
}
|
|
|
|
|
|
|
|
float EGLView::getFrameZoomFactor()
|
|
|
|
{
|
|
|
|
return _frameZoomFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EGLView::setFrameSize(float width, float height)
|
|
|
|
{
|
|
|
|
EGLViewProtocol::setFrameSize(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EGLView::setViewPortInPoints(float x , float y , float w , float h)
|
|
|
|
{
|
2013-09-13 18:09:11 +08:00
|
|
|
float frameZoomFactorX = _frameBufferSize[0]/_screenSize.width;
|
|
|
|
float frameZoomFactorY = _frameBufferSize[1]/_screenSize.height;
|
|
|
|
glViewport((GLint)(x * _scaleX * frameZoomFactorX + _viewPortRect.origin.x * frameZoomFactorX),
|
|
|
|
(GLint)(y * _scaleY * frameZoomFactorY + _viewPortRect.origin.y * frameZoomFactorY),
|
|
|
|
(GLsizei)(w * _scaleX * frameZoomFactorX),
|
|
|
|
(GLsizei)(h * _scaleY * frameZoomFactorY));
|
2012-10-19 11:33:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::setScissorInPoints(float x , float y , float w , float h)
|
2012-10-24 10:03:19 +08:00
|
|
|
{
|
2013-09-13 18:09:11 +08:00
|
|
|
float frameZoomFactorX = _frameBufferSize[0]/_screenSize.width;
|
|
|
|
float frameZoomFactorY = _frameBufferSize[1]/_screenSize.height;
|
|
|
|
glScissor((GLint)(x * _scaleX * frameZoomFactorX + _viewPortRect.origin.x * frameZoomFactorX),
|
|
|
|
(GLint)(y * _scaleY * frameZoomFactorY + _viewPortRect.origin.y * frameZoomFactorY),
|
|
|
|
(GLsizei)(w * _scaleX * frameZoomFactorX),
|
|
|
|
(GLsizei)(h * _scaleY * frameZoomFactorY));
|
2012-10-24 10:03:19 +08:00
|
|
|
}
|
|
|
|
|
2013-08-21 12:01:56 +08:00
|
|
|
EGLView* EGLView::getInstance()
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-08-21 12:01:56 +08:00
|
|
|
CCASSERT(nullptr != s_pEglView, "EGL singleton should not be null");
|
|
|
|
return s_pEglView;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: deprecated
|
|
|
|
EGLView* EGLView::sharedOpenGLView()
|
|
|
|
{
|
|
|
|
return EGLView::getInstance();
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
2012-09-04 02:06:04 +08:00
|
|
|
NS_CC_END // end of namespace cocos2d;
|