2012-08-02 13:02:59 +08:00
|
|
|
/*
|
|
|
|
* CCEGLViewlinux.cpp
|
|
|
|
*
|
|
|
|
* Created on: Aug 8, 2011
|
|
|
|
* Author: laschweinski
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CCEGLView.h"
|
|
|
|
#include "CCGL.h"
|
|
|
|
#include "GL/glfw.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "touch_dispatcher/CCTouch.h"
|
|
|
|
#include "touch_dispatcher/CCTouchDispatcher.h"
|
|
|
|
#include "text_input_node/CCIMEDispatcher.h"
|
|
|
|
|
|
|
|
PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT = NULL;
|
|
|
|
PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT = NULL;
|
|
|
|
PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT = NULL;
|
|
|
|
PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT = NULL;
|
|
|
|
PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT = NULL;
|
|
|
|
PFNGLGENERATEMIPMAPEXTPROC glGenerateMipmapEXT = NULL;
|
|
|
|
|
|
|
|
PFNGLGENBUFFERSARBPROC glGenBuffersARB = NULL;
|
|
|
|
PFNGLBINDBUFFERARBPROC glBindBufferARB = NULL;
|
|
|
|
PFNGLBUFFERDATAARBPROC glBufferDataARB = NULL;
|
|
|
|
PFNGLBUFFERSUBDATAARBPROC glBufferSubDataARB = NULL;
|
|
|
|
PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = NULL;
|
|
|
|
|
|
|
|
bool initExtensions() {
|
|
|
|
#define LOAD_EXTENSION_FUNCTION(TYPE, FN) FN = (TYPE)glfwGetProcAddress(#FN);
|
|
|
|
bool bRet = false;
|
|
|
|
do {
|
|
|
|
|
|
|
|
// char* p = (char*) glGetString(GL_EXTENSIONS);
|
|
|
|
// printf(p);
|
|
|
|
|
|
|
|
/* Supports frame buffer? */
|
|
|
|
if (glfwExtensionSupported("GL_EXT_framebuffer_object") != GL_FALSE)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Loads frame buffer extension functions */
|
|
|
|
LOAD_EXTENSION_FUNCTION(PFNGLGENERATEMIPMAPEXTPROC,
|
|
|
|
glGenerateMipmapEXT);
|
|
|
|
LOAD_EXTENSION_FUNCTION(PFNGLGENFRAMEBUFFERSEXTPROC,
|
|
|
|
glGenFramebuffersEXT);
|
|
|
|
LOAD_EXTENSION_FUNCTION(PFNGLDELETEFRAMEBUFFERSEXTPROC,
|
|
|
|
glDeleteFramebuffersEXT);
|
|
|
|
LOAD_EXTENSION_FUNCTION(PFNGLBINDFRAMEBUFFEREXTPROC,
|
|
|
|
glBindFramebufferEXT);
|
|
|
|
LOAD_EXTENSION_FUNCTION(PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC,
|
|
|
|
glCheckFramebufferStatusEXT);
|
|
|
|
LOAD_EXTENSION_FUNCTION(PFNGLFRAMEBUFFERTEXTURE2DEXTPROC,
|
|
|
|
glFramebufferTexture2DEXT);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (glfwExtensionSupported("GL_ARB_vertex_buffer_object") != GL_FALSE) {
|
|
|
|
LOAD_EXTENSION_FUNCTION(PFNGLGENBUFFERSARBPROC, glGenBuffersARB);
|
|
|
|
LOAD_EXTENSION_FUNCTION(PFNGLBINDBUFFERARBPROC, glBindBufferARB);
|
|
|
|
LOAD_EXTENSION_FUNCTION(PFNGLBUFFERDATAARBPROC, glBufferDataARB);
|
|
|
|
LOAD_EXTENSION_FUNCTION(PFNGLBUFFERSUBDATAARBPROC,
|
|
|
|
glBufferSubDataARB);
|
|
|
|
LOAD_EXTENSION_FUNCTION(PFNGLDELETEBUFFERSARBPROC,
|
|
|
|
glDeleteBuffersARB);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
CCEGLView::CCEGLView()
|
2012-08-03 13:56:18 +08:00
|
|
|
: bIsInit(false)
|
2012-10-18 11:51:02 +08:00
|
|
|
, m_fFrameZoomFactor(1.0f)
|
2012-08-02 13:02:59 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCEGLView::~CCEGLView()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void keyEventHandle(int iKeyID,int iKeyState) {
|
|
|
|
if (iKeyState ==GLFW_RELEASE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iKeyID == GLFW_KEY_DEL) {
|
|
|
|
CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
|
|
|
|
} else if (iKeyID == GLFW_KEY_ENTER) {
|
|
|
|
CCIMEDispatcher::sharedDispatcher()->dispatchInsertText("\n", 1);
|
|
|
|
} else if (iKeyID == GLFW_KEY_TAB) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void charEventHandle(int iCharID,int iCharState) {
|
|
|
|
if (iCharState ==GLFW_RELEASE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ascii char
|
|
|
|
CCIMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&iCharID, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mouseButtonEventHandle(int iMouseID,int iMouseState) {
|
|
|
|
if (iMouseID == GLFW_MOUSE_BUTTON_LEFT) {
|
2012-10-18 11:51:02 +08:00
|
|
|
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
|
2012-08-02 13:02:59 +08:00
|
|
|
//get current mouse pos
|
|
|
|
int x,y;
|
|
|
|
glfwGetMousePos(&x, &y);
|
|
|
|
CCPoint oPoint((float)x,(float)y);
|
2012-08-03 13:56:18 +08:00
|
|
|
/*
|
|
|
|
if (!CCRect::CCRectContainsPoint(s_pMainWindow->m_rcViewPort,oPoint))
|
|
|
|
{
|
2012-08-02 13:02:59 +08:00
|
|
|
CCLOG("not in the viewport");
|
|
|
|
return;
|
|
|
|
}
|
2012-08-03 13:56:18 +08:00
|
|
|
*/
|
2012-10-18 11:51:02 +08:00
|
|
|
oPoint.x /= pEGLView->m_fFrameZoomFactor;
|
|
|
|
oPoint.y /= pEGLView->m_fFrameZoomFactor;
|
2012-08-02 13:02:59 +08:00
|
|
|
int id = 0;
|
|
|
|
if (iMouseState == GLFW_PRESS) {
|
2012-10-18 11:51:02 +08:00
|
|
|
pEGLView->handleTouchesBegin(1, &id, &oPoint.x, &oPoint.y);
|
2012-08-02 13:02:59 +08:00
|
|
|
|
|
|
|
} else if (iMouseState == GLFW_RELEASE) {
|
2012-10-18 11:51:02 +08:00
|
|
|
pEGLView->handleTouchesEnd(1, &id, &oPoint.x, &oPoint.y);
|
2012-08-02 13:02:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mousePosEventHandle(int iPosX,int iPosY) {
|
|
|
|
int iButtonState = glfwGetMouseButton(GLFW_MOUSE_BUTTON_LEFT);
|
|
|
|
|
|
|
|
//to test move
|
|
|
|
if (iButtonState == GLFW_PRESS) {
|
2012-10-18 11:51:02 +08:00
|
|
|
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
|
|
|
|
int id = 0;
|
|
|
|
float x = (float)iPosX;
|
|
|
|
float y = (float)iPosY;
|
|
|
|
x /= pEGLView->m_fFrameZoomFactor;
|
|
|
|
y /= pEGLView->m_fFrameZoomFactor;
|
|
|
|
pEGLView->handleTouchesMove(1, &id, &x, &y);
|
2012-08-02 13:02:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:49:42 +08:00
|
|
|
int closeEventHandle() {
|
|
|
|
CCDirector::sharedDirector()->end();
|
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-08-16 11:30:25 +08:00
|
|
|
void CCEGLView::setFrameSize(float width, float height)
|
2012-08-03 13:56:18 +08:00
|
|
|
{
|
|
|
|
bool eResult = false;
|
2012-08-02 13:02:59 +08:00
|
|
|
int u32GLFWFlags = GLFW_WINDOW;
|
|
|
|
//create the window by glfw.
|
|
|
|
|
|
|
|
//check
|
2012-08-03 13:56:18 +08:00
|
|
|
CCAssert(width!=0&&height!=0, "invalid window's size equal 0");
|
2012-08-02 13:02:59 +08:00
|
|
|
|
|
|
|
//Inits GLFW
|
|
|
|
eResult = glfwInit() != GL_FALSE;
|
|
|
|
|
|
|
|
if (!eResult) {
|
|
|
|
CCAssert(0, "fail to init the glfw");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Updates window hint */
|
|
|
|
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
|
|
|
|
|
2012-08-03 13:56:18 +08:00
|
|
|
int iDepth = 16; // set default value
|
2012-08-02 13:02:59 +08:00
|
|
|
/* Depending on video depth */
|
|
|
|
switch(iDepth)
|
|
|
|
{
|
|
|
|
/* 16-bit */
|
|
|
|
case 16:
|
|
|
|
{
|
|
|
|
/* Updates video mode */
|
2012-12-04 16:44:33 +08:00
|
|
|
eResult = (glfwOpenWindow(width, height, 5, 6, 5, 0, 16, 8, (int)u32GLFWFlags) != false) ? true : false;
|
2012-08-02 13:02:59 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 24-bit */
|
|
|
|
case 24:
|
|
|
|
{
|
|
|
|
/* Updates video mode */
|
2012-12-04 16:44:33 +08:00
|
|
|
eResult = (glfwOpenWindow(width, height, 8, 8, 8, 0, 16, 8, (int)u32GLFWFlags) != false) ? true : false;
|
2012-08-02 13:02:59 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 32-bit */
|
|
|
|
default:
|
|
|
|
case 32:
|
|
|
|
{
|
|
|
|
/* Updates video mode */
|
2012-12-04 16:44:33 +08:00
|
|
|
eResult = (glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 8, (int)u32GLFWFlags) != GL_FALSE) ? true :false;
|
2012-08-02 13:02:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Success? */
|
|
|
|
if(eResult)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Updates actual size */
|
2012-08-03 13:56:18 +08:00
|
|
|
// glfwGetWindowSize(&width, &height);
|
2012-08-02 13:02:59 +08:00
|
|
|
|
2012-08-16 11:30:25 +08:00
|
|
|
CCEGLViewProtocol::setFrameSize(width, height);
|
2012-08-02 13:02:59 +08:00
|
|
|
|
|
|
|
/* Updates its title */
|
2012-08-03 13:56:18 +08:00
|
|
|
glfwSetWindowTitle("Cocos2dx-Linux");
|
2012-08-02 13:02:59 +08:00
|
|
|
|
|
|
|
//set the init flag
|
|
|
|
bIsInit = true;
|
|
|
|
|
|
|
|
//register the glfw key event
|
|
|
|
glfwSetKeyCallback(keyEventHandle);
|
|
|
|
//register the glfw char event
|
|
|
|
glfwSetCharCallback(charEventHandle);
|
|
|
|
//register the glfw mouse event
|
|
|
|
glfwSetMouseButtonCallback(mouseButtonEventHandle);
|
|
|
|
//register the glfw mouse pos event
|
|
|
|
glfwSetMousePosCallback(mousePosEventHandle);
|
|
|
|
|
2012-09-10 21:49:42 +08:00
|
|
|
glfwSetWindowCloseCallback(closeEventHandle);
|
|
|
|
|
2012-08-02 13:02:59 +08:00
|
|
|
//Inits extensions
|
|
|
|
eResult = initExtensions();
|
|
|
|
|
|
|
|
if (!eResult) {
|
|
|
|
CCAssert(0, "fail to init the extensions of opengl");
|
|
|
|
}
|
|
|
|
initGL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 17:39:55 +08:00
|
|
|
void CCEGLView::setFrameZoomFactor(float fZoomFactor)
|
2012-10-18 11:51:02 +08:00
|
|
|
{
|
|
|
|
m_fFrameZoomFactor = fZoomFactor;
|
|
|
|
glfwSetWindowSize(m_obScreenSize.width * fZoomFactor, m_obScreenSize.height * fZoomFactor);
|
|
|
|
CCDirector::sharedDirector()->setProjection(CCDirector::sharedDirector()->getProjection());
|
|
|
|
}
|
|
|
|
|
2012-10-19 17:39:55 +08:00
|
|
|
float CCEGLView::getFrameZoomFactor()
|
|
|
|
{
|
|
|
|
return m_fFrameZoomFactor;
|
|
|
|
}
|
|
|
|
|
2012-10-18 11:51:02 +08:00
|
|
|
void CCEGLView::setViewPortInPoints(float x , float y , float w , float h)
|
|
|
|
{
|
|
|
|
glViewport((GLint)(x * m_fScaleX * m_fFrameZoomFactor+ m_obViewPortRect.origin.x * m_fFrameZoomFactor),
|
|
|
|
(GLint)(y * m_fScaleY * m_fFrameZoomFactor + m_obViewPortRect.origin.y * m_fFrameZoomFactor),
|
|
|
|
(GLsizei)(w * m_fScaleX * m_fFrameZoomFactor),
|
|
|
|
(GLsizei)(h * m_fScaleY * m_fFrameZoomFactor));
|
|
|
|
}
|
|
|
|
|
2012-10-24 10:03:19 +08:00
|
|
|
void CCEGLView::setScissorInPoints(float x , float y , float w , float h)
|
|
|
|
{
|
|
|
|
glScissor((GLint)(x * m_fScaleX * m_fFrameZoomFactor + m_obViewPortRect.origin.x * m_fFrameZoomFactor),
|
|
|
|
(GLint)(y * m_fScaleY * m_fFrameZoomFactor + m_obViewPortRect.origin.y * m_fFrameZoomFactor),
|
|
|
|
(GLsizei)(w * m_fScaleX * m_fFrameZoomFactor),
|
|
|
|
(GLsizei)(h * m_fScaleY * m_fFrameZoomFactor));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-02 13:02:59 +08:00
|
|
|
bool CCEGLView::isOpenGLReady()
|
|
|
|
{
|
|
|
|
return bIsInit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLView::end()
|
|
|
|
{
|
|
|
|
/* Exits from GLFW */
|
|
|
|
glfwTerminate();
|
2012-08-03 13:56:18 +08:00
|
|
|
delete this;
|
2012-08-02 13:02:59 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
2012-08-03 13:56:18 +08:00
|
|
|
|
2012-08-02 13:02:59 +08:00
|
|
|
void CCEGLView::swapBuffers() {
|
|
|
|
if (bIsInit) {
|
|
|
|
/* Swap buffers */
|
|
|
|
glfwSwapBuffers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLView::setIMEKeyboardState(bool bOpen) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCEGLView::initGL()
|
|
|
|
{
|
|
|
|
GLenum GlewInitResult = glewInit();
|
|
|
|
if (GLEW_OK != GlewInitResult)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"ERROR: %s\n",glewGetErrorString(GlewInitResult));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
|
|
|
|
{
|
2013-03-16 23:56:03 +08:00
|
|
|
CCLog("Ready for GLSL");
|
2012-08-02 13:02:59 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-16 23:56:03 +08:00
|
|
|
CCLog("Not totally ready :(");
|
2012-08-02 13:02:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (glewIsSupported("GL_VERSION_2_0"))
|
|
|
|
{
|
2013-03-16 23:56:03 +08:00
|
|
|
CCLog("Ready for OpenGL 2.0");
|
2012-08-02 13:02:59 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-16 23:56:03 +08:00
|
|
|
CCLog("OpenGL 2.0 not supported");
|
2012-08-02 13:02:59 +08:00
|
|
|
}
|
2012-12-04 16:44:33 +08:00
|
|
|
|
|
|
|
// Enable point size by default on linux.
|
|
|
|
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
|
|
|
|
|
2012-08-02 13:02:59 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLView::destroyGL()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
if (m_hDC != NULL && m_hRC != NULL)
|
|
|
|
{
|
|
|
|
// deselect rendering context and delete it
|
|
|
|
wglMakeCurrent(m_hDC, NULL);
|
|
|
|
wglDeleteContext(m_hRC);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2012-08-21 14:58:31 +08:00
|
|
|
CCEGLView* CCEGLView::sharedOpenGLView()
|
2012-08-02 13:02:59 +08:00
|
|
|
{
|
2012-08-03 13:56:18 +08:00
|
|
|
static CCEGLView* s_pEglView = NULL;
|
|
|
|
if (s_pEglView == NULL)
|
|
|
|
{
|
|
|
|
s_pEglView = new CCEGLView();
|
|
|
|
}
|
2012-08-21 14:58:31 +08:00
|
|
|
return s_pEglView;
|
2012-08-02 13:02:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|