axmol/cocos2dx/platform/linux/CCEGLView.cpp

373 lines
8.9 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 "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"
#include "keyboard_dispatcher/CCKeyboardDispatcher.h"
2012-08-02 13:02:59 +08:00
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
EGLView::EGLView()
: bIsInit(false)
, _frameZoomFactor(1.0f)
2012-08-02 13:02:59 +08:00
{
}
EGLView::~EGLView()
2012-08-02 13:02:59 +08:00
{
}
void keyEventHandle(int iKeyID,int iKeyState) {
if (iKeyState ==GLFW_RELEASE) {
return;
}
if (iKeyID == GLFW_KEY_DEL) {
IMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
2012-08-02 13:02:59 +08:00
} else if (iKeyID == GLFW_KEY_ENTER) {
IMEDispatcher::sharedDispatcher()->dispatchInsertText("\n", 1);
2012-08-02 13:02:59 +08:00
} else if (iKeyID == GLFW_KEY_TAB) {
}
}
void charEventHandle(int iCharID,int iCharState) {
if (iCharState ==GLFW_RELEASE) {
return;
}
// ascii char
IMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&iCharID, 1);
2012-08-02 13:02:59 +08:00
}
void mouseButtonEventHandle(int iMouseID,int iMouseState) {
if (iMouseID == GLFW_MOUSE_BUTTON_LEFT) {
EGLView* pEGLView = EGLView::sharedOpenGLView();
2012-08-02 13:02:59 +08:00
//get current mouse pos
int x,y;
glfwGetMousePos(&x, &y);
Point oPoint((float)x,(float)y);
/*
if (!Rect::RectContainsPoint(s_pMainWindow->_rcViewPort,oPoint))
{
2012-08-02 13:02:59 +08:00
CCLOG("not in the viewport");
return;
}
*/
oPoint.x /= pEGLView->_frameZoomFactor;
oPoint.y /= pEGLView->_frameZoomFactor;
2012-08-02 13:02:59 +08:00
int id = 0;
if (iMouseState == GLFW_PRESS) {
pEGLView->handleTouchesBegin(1, &id, &oPoint.x, &oPoint.y);
2012-08-02 13:02:59 +08:00
} else if (iMouseState == GLFW_RELEASE) {
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) {
EGLView* pEGLView = EGLView::sharedOpenGLView();
int id = 0;
float x = (float)iPosX;
float y = (float)iPosY;
x /= pEGLView->_frameZoomFactor;
y /= pEGLView->_frameZoomFactor;
pEGLView->handleTouchesMove(1, &id, &x, &y);
2012-08-02 13:02:59 +08:00
}
}
int closeEventHandle() {
Director::sharedDirector()->end();
return GL_TRUE;
}
void GLFWCALL keyboardEventHandle(int keyCode, int action)
{
KeyboardDispatcher *kbDisp = Director::sharedDirector()->getKeyboardDispatcher();
switch (action)
{
case GLFW_PRESS:
kbDisp->dispatchKeyboardEvent(keyCode, true);
break;
case GLFW_RELEASE:
kbDisp->dispatchKeyboardEvent(keyCode, false);
break;
}
}
void EGLView::setFrameSize(float width, float height)
{
bool eResult = false;
2012-08-02 13:02:59 +08:00
int u32GLFWFlags = GLFW_WINDOW;
//create the window by glfw.
//check
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);
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 */
// glfwGetWindowSize(&width, &height);
2012-08-02 13:02:59 +08:00
EGLViewProtocol::setFrameSize(width, height);
2012-08-02 13:02:59 +08:00
/* Updates its title */
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);
#ifdef CC_KEYBOARD_SUPPORT
//register the glfw keyboard event
glfwSetKeyCallback(keyboardEventHandle);
#endif
2012-08-02 13:02:59 +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();
}
}
void EGLView::setFrameZoomFactor(float fZoomFactor)
{
_frameZoomFactor = fZoomFactor;
glfwSetWindowSize(_screenSize.width * fZoomFactor, _screenSize.height * fZoomFactor);
Director::sharedDirector()->setProjection(Director::sharedDirector()->getProjection());
}
float EGLView::getFrameZoomFactor()
{
return _frameZoomFactor;
}
void EGLView::setViewPortInPoints(float x , float y , float w , float h)
{
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));
}
void EGLView::setScissorInPoints(float x , float y , float w , float h)
{
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));
}
bool EGLView::isOpenGLReady()
2012-08-02 13:02:59 +08:00
{
return bIsInit;
}
void EGLView::end()
2012-08-02 13:02:59 +08:00
{
/* Exits from GLFW */
glfwTerminate();
delete this;
2012-08-02 13:02:59 +08:00
exit(0);
}
void EGLView::swapBuffers() {
2012-08-02 13:02:59 +08:00
if (bIsInit) {
/* Swap buffers */
glfwSwapBuffers();
}
}
void EGLView::setIMEKeyboardState(bool bOpen) {
2012-08-02 13:02:59 +08:00
}
bool EGLView::initGL()
2012-08-02 13:02:59 +08:00
{
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");
2012-08-02 13:02:59 +08:00
}
else
{
CCLog("Not totally ready :(");
2012-08-02 13:02:59 +08:00
}
if (glewIsSupported("GL_VERSION_2_0"))
{
CCLog("Ready for OpenGL 2.0");
2012-08-02 13:02:59 +08:00
}
else
{
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 EGLView::destroyGL()
2012-08-02 13:02:59 +08:00
{
/*
if (_DC != NULL && _RC != NULL)
2012-08-02 13:02:59 +08:00
{
// deselect rendering context and delete it
wglMakeCurrent(_DC, NULL);
wglDeleteContext(_RC);
2012-08-02 13:02:59 +08:00
}
*/
}
EGLView* EGLView::sharedOpenGLView()
2012-08-02 13:02:59 +08:00
{
static EGLView* s_pEglView = NULL;
if (s_pEglView == NULL)
{
s_pEglView = new EGLView();
}
return s_pEglView;
2012-08-02 13:02:59 +08:00
}
NS_CC_END