axmol/cocos2dx/platform/linux/CCEGLView.cpp

256 lines
6.8 KiB
C++
Raw Normal View History

2012-08-02 13:02:59 +08:00
/*
* EGLViewlinux.cpp
2012-08-02 13:02:59 +08:00
*
* Created on: Aug 8, 2011
* Author: laschweinski
*/
#include "CCEGLView.h"
#include "CCGL.h"
#include "ccMacros.h"
#include "CCDirector.h"
#include "touch_dispatcher/CCTouch.h"
#include "touch_dispatcher/CCTouchDispatcher.h"
#include "text_input_node/CCIMEDispatcher.h"
#include "keyboard_dispatcher/CCKeyboardDispatcher.h"
#include <unistd.h>
2012-08-02 13:02:59 +08:00
NS_CC_BEGIN
2013-08-28 12:00:06 +08:00
//begin EGLViewEventHandler
class EGLViewEventHandler
2012-08-02 13:02:59 +08:00
{
2013-08-28 12:00:06 +08:00
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);
};
2012-08-02 13:02:59 +08:00
2013-08-28 12:00:06 +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-08-02 13:02:59 +08:00
{
2013-08-28 12:00:06 +08:00
CCLOGERROR("GLFWError #%d Happen, %s\n", errorID, errorDesc);
2012-08-02 13:02:59 +08:00
}
2013-08-28 12:00:06 +08:00
void EGLViewEventHandler::OnGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
{
2013-08-28 12:00:06 +08:00
EGLView* eglView = EGLView::getInstance();
if(nullptr == eglView) return;
if(GLFW_MOUSE_BUTTON_LEFT == button)
{
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);
}
}
}
}
2013-08-28 12:00:06 +08:00
void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
{
2013-08-28 12:00:06 +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)
{
2013-08-28 12:00:06 +08:00
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-08-28 12:00:06 +08:00
//end EGLViewEventHandler
2013-08-28 12:00:06 +08:00
//////////////////////////////////////////////////////////////////////////
// impliment EGLView
//////////////////////////////////////////////////////////////////////////
2013-08-28 12:00:06 +08:00
EGLView* EGLView::s_pEglView = nullptr;
2013-08-28 12:00:06 +08:00
EGLView::EGLView()
: _captured(false)
, _frameZoomFactor(1.0f)
, _supportTouch(false)
, _mainWindow(nullptr)
{
CCASSERT(nullptr == s_pEglView, "EGLView is singleton, Should be inited only one time\n");
s_pEglView = this;
strcpy(_viewName, "Cocos2dxWin32");
glfwSetErrorCallback(EGLViewEventHandler::OnGLFWError);
glfwInit();
}
2013-08-28 12:00:06 +08:00
EGLView::~EGLView()
{
glfwTerminate();
s_pEglView = nullptr;
}
2013-08-28 12:00:06 +08:00
bool EGLView::create()
{
if(nullptr != _mainWindow) return true;
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
_mainWindow = glfwCreateWindow(_screenSize.width, _screenSize.height, _viewName, nullptr, nullptr);
glfwMakeContextCurrent(_mainWindow);
glfwSetMouseButtonCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseCallBack);
glfwSetCursorPosCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseMoveCallBack);
// 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");
}
2013-08-28 12:00:06 +08:00
// 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-08-02 13:02:59 +08:00
}
2013-08-28 12:00:06 +08:00
bool EGLView::isOpenGLReady()
{
2013-08-28 12:00:06 +08:00
return nullptr != _mainWindow;
}
2013-08-28 12:00:06 +08:00
void EGLView::end()
{
2013-08-28 12:00:06 +08:00
if(_mainWindow)
glfwSetWindowShouldClose(_mainWindow,1);
}
2013-08-28 12:00:06 +08:00
void EGLView::swapBuffers()
{
2013-08-28 12:00:06 +08:00
if(_mainWindow)
glfwSwapBuffers(_mainWindow);
}
2013-08-28 12:00:06 +08:00
bool EGLView::windowShouldClose()
{
2013-08-28 12:00:06 +08:00
if(_mainWindow)
return glfwWindowShouldClose(_mainWindow);
else
return true;
}
2013-08-28 12:00:06 +08:00
void EGLView::pollEvents()
2012-08-02 13:02:59 +08:00
{
2013-08-28 12:00:06 +08:00
glfwPollEvents();
2012-08-02 13:02:59 +08:00
}
2013-08-28 12:00:06 +08:00
void EGLView::setIMEKeyboardState(bool /*bOpen*/)
2012-08-02 13:02:59 +08:00
{
2013-08-28 12:00:06 +08:00
2012-08-02 13:02:59 +08:00
}
2013-08-28 12:00:06 +08:00
void EGLView::setFrameZoomFactor(float fZoomFactor)
{
_frameZoomFactor = fZoomFactor;
Director::getInstance()->setProjection(Director::getInstance()->getProjection());
2012-08-02 13:02:59 +08:00
}
2013-08-28 12:00:06 +08:00
float EGLView::getFrameZoomFactor()
{
2013-08-28 12:00:06 +08:00
return _frameZoomFactor;
}
2012-08-02 13:02:59 +08:00
2013-08-28 12:00:06 +08:00
void EGLView::setFrameSize(float width, float height)
{
2013-08-28 12:00:06 +08:00
EGLViewProtocol::setFrameSize(width, height);
2012-08-02 13:02:59 +08:00
}
2013-08-28 12:00:06 +08:00
void EGLView::setViewPortInPoints(float x , float y , float w , float h)
2012-08-02 13:02:59 +08:00
{
2013-08-28 12:00:06 +08:00
glViewport((GLint)(x * _scaleX * _frameZoomFactor + _viewPortRect.origin.x * _frameZoomFactor),
(GLint)(y * _scaleY * _frameZoomFactor + _viewPortRect.origin.y * _frameZoomFactor),
(GLsizei)(w * _scaleX * _frameZoomFactor),
(GLsizei)(h * _scaleY * _frameZoomFactor));
2012-08-02 13:02:59 +08:00
}
2013-08-28 12:00:06 +08:00
void EGLView::setScissorInPoints(float x , float y , float w , float h)
2012-08-02 13:02:59 +08:00
{
2013-08-28 12:00:06 +08:00
glScissor((GLint)(x * _scaleX * _frameZoomFactor + _viewPortRect.origin.x * _frameZoomFactor),
(GLint)(y * _scaleY * _frameZoomFactor + _viewPortRect.origin.y * _frameZoomFactor),
(GLsizei)(w * _scaleX * _frameZoomFactor),
(GLsizei)(h * _scaleY * _frameZoomFactor));
2012-08-02 13:02:59 +08:00
}
EGLView* EGLView::getInstance()
2012-08-02 13:02:59 +08:00
{
2013-08-28 12:00:06 +08:00
CCASSERT(nullptr != s_pEglView, "EGL singleton should not be null");
return s_pEglView;
2012-08-02 13:02:59 +08:00
}
// XXX: deprecated
EGLView* EGLView::sharedOpenGLView()
{
return EGLView::getInstance();
}
2012-08-02 13:02:59 +08:00
NS_CC_END