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-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) {
|
|
|
|
//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-08-02 13:02:59 +08:00
|
|
|
int id = 0;
|
|
|
|
if (iMouseState == GLFW_PRESS) {
|
2012-08-21 14:58:31 +08:00
|
|
|
CCEGLView::sharedOpenGLView()->handleTouchesBegin(1, &id, &oPoint.x, &oPoint.y);
|
2012-08-02 13:02:59 +08:00
|
|
|
|
|
|
|
} else if (iMouseState == GLFW_RELEASE) {
|
2012-08-21 14:58:31 +08:00
|
|
|
CCEGLView::sharedOpenGLView()->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-08-03 13:56:18 +08:00
|
|
|
int id = 0;
|
|
|
|
float x = (float)iPosX;
|
|
|
|
float y = (float)iPosY;
|
2012-08-21 14:58:31 +08:00
|
|
|
CCEGLView::sharedOpenGLView()->handleTouchesMove(1, &id, &x, &y);
|
2012-08-02 13:02:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-03 13:56:18 +08:00
|
|
|
eResult = (glfwOpenWindow(width, height, 5, 6, 5, 0, 16, 0, (int)u32GLFWFlags) != false) ? true : false;
|
2012-08-02 13:02:59 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 24-bit */
|
|
|
|
case 24:
|
|
|
|
{
|
|
|
|
/* Updates video mode */
|
2012-08-03 13:56:18 +08:00
|
|
|
eResult = (glfwOpenWindow(width, height, 8, 8, 8, 0, 16, 0, (int)u32GLFWFlags) != false) ? true : false;
|
2012-08-02 13:02:59 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 32-bit */
|
|
|
|
default:
|
|
|
|
case 32:
|
|
|
|
{
|
|
|
|
/* Updates video mode */
|
2012-08-03 13:56:18 +08:00
|
|
|
eResult = (glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 0, (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);
|
|
|
|
|
|
|
|
//Inits extensions
|
|
|
|
eResult = initExtensions();
|
|
|
|
|
|
|
|
if (!eResult) {
|
|
|
|
CCAssert(0, "fail to init the extensions of opengl");
|
|
|
|
}
|
|
|
|
initGL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
CCLog("Ready for GLSL\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLog("Not totally ready :( \n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (glewIsSupported("GL_VERSION_2_0"))
|
|
|
|
{
|
|
|
|
CCLog("Ready for OpenGL 2.0\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLog("OpenGL 2.0 not supported\n");
|
|
|
|
}
|
|
|
|
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
|