2013-04-09 09:21:53 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCEGLView.h"
|
|
|
|
#include "cocoa/CCSet.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "ccMacros.h"
|
2013-09-03 18:22:03 +08:00
|
|
|
#include "event_dispatcher/CCTouch.h"
|
2013-09-12 20:56:57 +08:00
|
|
|
#include "event_dispatcher/CCEventDispatcher.h"
|
|
|
|
#include "event_dispatcher/CCKeyboardEvent.h"
|
2013-04-09 09:21:53 +08:00
|
|
|
#include "text_input_node/CCIMEDispatcher.h"
|
|
|
|
#include "CCGL.h"
|
|
|
|
#include "CCAccelerometer.h"
|
|
|
|
#include "CCApplication.h"
|
|
|
|
|
2013-06-13 05:46:32 +08:00
|
|
|
#include <emscripten/emscripten.h>
|
2013-05-15 12:38:56 +08:00
|
|
|
#include <SDL/SDL.h>
|
|
|
|
#include <SDL/SDL_ttf.h>
|
2013-05-21 11:09:56 +08:00
|
|
|
#include <SDL/SDL_mixer.h>
|
2013-05-15 12:38:56 +08:00
|
|
|
|
2013-04-09 09:21:53 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
void glutInit(int *argcp, char **argv);
|
|
|
|
void glutMouseFunc(void (*func)(int button, int state, int x, int y));
|
|
|
|
void glutMotionFunc(void (*func)(int x, int y));
|
|
|
|
void glutPassiveMotionFunc(void (*func)(int x, int y));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constants for mouse events (inferred from experiment)
|
|
|
|
static const int glutLeftButton = 0;
|
|
|
|
static const int glutMouseDown = 0;
|
|
|
|
static const int glutMouseUp = 1;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool EGLView::_initializedFunctions = false;
|
|
|
|
const GLubyte *EGLView::_extensions = 0;
|
2013-04-09 09:21:53 +08:00
|
|
|
|
|
|
|
enum Orientation
|
|
|
|
{
|
|
|
|
PORTRAIT,
|
|
|
|
LANDSCAPE,
|
|
|
|
AUTO
|
|
|
|
};
|
|
|
|
|
|
|
|
static Orientation orientation = LANDSCAPE;
|
|
|
|
|
|
|
|
#define MAX_TOUCHES 4
|
2013-06-20 14:13:12 +08:00
|
|
|
static EGLView* s_pInstance = NULL;
|
2013-04-09 09:21:53 +08:00
|
|
|
|
|
|
|
static bool buttonDepressed = false;
|
|
|
|
extern "C" void mouseCB(int button, int state, int x, int y)
|
|
|
|
{
|
|
|
|
float fx = x;
|
|
|
|
float fy = y;
|
2013-07-12 13:11:21 +08:00
|
|
|
EGLView* pEGLView = EGLView::getInstance();
|
2013-04-09 09:21:53 +08:00
|
|
|
int id = 0;
|
|
|
|
|
|
|
|
if(button != glutLeftButton) return;
|
|
|
|
|
|
|
|
if(state == glutMouseDown)
|
|
|
|
{
|
|
|
|
pEGLView->handleTouchesBegin(1, &id, &fx, &fy);
|
|
|
|
buttonDepressed = true;
|
|
|
|
}
|
|
|
|
else if(state == glutMouseUp)
|
|
|
|
{
|
|
|
|
pEGLView->handleTouchesEnd(1, &id, &fx, &fy);
|
|
|
|
buttonDepressed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void motionCB(int x, int y)
|
|
|
|
{
|
|
|
|
float fx = x;
|
|
|
|
float fy = y;
|
2013-07-12 13:11:21 +08:00
|
|
|
EGLView* pEGLView = EGLView::getInstance();
|
2013-04-09 09:21:53 +08:00
|
|
|
int id = 0;
|
|
|
|
|
|
|
|
if(buttonDepressed)
|
|
|
|
{
|
|
|
|
pEGLView->handleTouchesMove(1, &id, &fx, &fy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLView::EGLView()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_eglDisplay = EGL_NO_DISPLAY;
|
|
|
|
_eglContext = EGL_NO_CONTEXT;
|
|
|
|
_eglSurface = EGL_NO_SURFACE;
|
2013-04-09 09:21:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
strcpy(_windowGroupID, "");
|
|
|
|
snprintf(_windowGroupID, sizeof(_windowGroupID), "%d", 1);
|
2013-04-09 09:21:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_isGLInitialized = initGL();
|
2013-04-09 09:21:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_isGLInitialized)
|
2013-04-09 09:21:53 +08:00
|
|
|
initEGLFunctions();
|
|
|
|
|
2013-05-21 11:09:56 +08:00
|
|
|
// Initialize SDL: used for font rendering, sprite loading and audio
|
|
|
|
// playing.
|
|
|
|
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
|
|
|
|
|
|
|
|
// Emscripten ignores all these values.
|
|
|
|
Mix_OpenAudio(0, 0, 0, 0);
|
2013-05-15 12:38:56 +08:00
|
|
|
TTF_Init();
|
2013-05-16 11:21:02 +08:00
|
|
|
|
2013-04-09 09:21:53 +08:00
|
|
|
char *arg1 = (char*)malloc(1);
|
|
|
|
char **dummyArgv = (char**)malloc(sizeof(char*));
|
|
|
|
dummyArgv[0] = arg1;
|
|
|
|
glutInit(0, dummyArgv);
|
|
|
|
free(dummyArgv[0]);
|
|
|
|
free(dummyArgv);
|
|
|
|
|
|
|
|
glutMouseFunc(&mouseCB);
|
|
|
|
glutMotionFunc(&motionCB);
|
|
|
|
glutPassiveMotionFunc(&motionCB);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLView::~EGLView()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const char* EGLView::getWindowGroupId() const
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _windowGroupID;
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::release()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_eglDisplay != EGL_NO_DISPLAY)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
eglMakeCurrent(_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_eglSurface != EGL_NO_SURFACE)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
eglDestroySurface(_eglDisplay, _eglSurface);
|
|
|
|
_eglSurface = EGL_NO_SURFACE;
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_eglContext != EGL_NO_CONTEXT)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
eglDestroyContext(_eglDisplay, _eglContext);
|
|
|
|
_eglContext = EGL_NO_CONTEXT;
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_eglDisplay != EGL_NO_DISPLAY)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
eglTerminate(_eglDisplay);
|
|
|
|
_eglDisplay = EGL_NO_DISPLAY;
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_isGLInitialized = false;
|
2013-04-09 09:21:53 +08:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::initEGLFunctions()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_extensions = glGetString(GL_EXTENSIONS);
|
|
|
|
_initializedFunctions = true;
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool EGLView::isOpenGLReady()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return (_isGLInitialized && _screenSize.height != 0 && _screenSize.width != 0);
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::end()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::swapBuffers()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
eglSwapBuffers(_eglDisplay, _eglSurface);
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 13:11:21 +08:00
|
|
|
EGLView* EGLView::getInstance()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
if (!s_pInstance)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
s_pInstance = new EGLView();
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCAssert(s_pInstance != NULL, "CCEGLView wasn't constructed yet");
|
|
|
|
return s_pInstance;
|
|
|
|
}
|
|
|
|
|
2013-07-12 13:11:21 +08:00
|
|
|
// XXX: deprecated
|
|
|
|
EGLView* EGLView::sharedOpenGLView()
|
|
|
|
{
|
|
|
|
return EGLView::getInstance();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::showKeyboard()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::hideKeyboard()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::setIMEKeyboardState(bool bOpen)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool EGLView::isGLExtension(const char *searchName) const
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
const GLubyte *start;
|
|
|
|
GLubyte *where, *terminator;
|
|
|
|
|
|
|
|
/* It takes a bit of care to be fool-proof about parsing the
|
|
|
|
OpenGL extensions string. Don't be fooled by sub-strings,
|
|
|
|
etc. */
|
2013-06-15 14:03:30 +08:00
|
|
|
start = _extensions;
|
2013-04-09 09:21:53 +08:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
where = (GLubyte *) strstr((const char *) start, searchName);
|
|
|
|
if (!where)
|
|
|
|
break;
|
|
|
|
|
|
|
|
terminator = where + strlen(searchName);
|
|
|
|
if (where == start || *(where - 1) == ' ')
|
|
|
|
if (*terminator == ' ' || *terminator == '\0')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
start = terminator;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static EGLenum checkErrorEGL(const char* msg)
|
|
|
|
{
|
|
|
|
assert(msg);
|
|
|
|
static const char* errmsg[] =
|
|
|
|
{
|
|
|
|
"EGL function succeeded",
|
|
|
|
"EGL is not initialized, or could not be initialized, for the specified display",
|
|
|
|
"EGL cannot access a requested resource",
|
|
|
|
"EGL failed to allocate resources for the requested operation",
|
|
|
|
"EGL fail to access an unrecognized attribute or attribute value was passed in an attribute list",
|
|
|
|
"EGLConfig argument does not name a valid EGLConfig",
|
|
|
|
"EGLContext argument does not name a valid EGLContext",
|
|
|
|
"EGL current surface of the calling thread is no longer valid",
|
|
|
|
"EGLDisplay argument does not name a valid EGLDisplay",
|
|
|
|
"EGL arguments are inconsistent",
|
|
|
|
"EGLNativePixmapType argument does not refer to a valid native pixmap",
|
|
|
|
"EGLNativeWindowType argument does not refer to a valid native window",
|
|
|
|
"EGL one or more argument values are invalid",
|
|
|
|
"EGLSurface argument does not name a valid surface configured for rendering",
|
|
|
|
"EGL power management event has occurred",
|
|
|
|
};
|
|
|
|
EGLenum error = eglGetError();
|
|
|
|
fprintf(stderr, "%s: %s\n", msg, errmsg[error - EGL_SUCCESS]);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool EGLView::initGL()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
EGLint eglConfigCount;
|
|
|
|
EGLConfig config;
|
|
|
|
|
|
|
|
// Hard-coded to 32-bit/OpenGL ES 2.0.
|
|
|
|
const EGLint eglConfigAttrs[] =
|
|
|
|
{
|
|
|
|
EGL_RED_SIZE, 8,
|
|
|
|
EGL_GREEN_SIZE, 8,
|
|
|
|
EGL_BLUE_SIZE, 8,
|
|
|
|
EGL_ALPHA_SIZE, 8,
|
|
|
|
EGL_DEPTH_SIZE, 24,
|
|
|
|
EGL_STENCIL_SIZE, 8,
|
|
|
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
const EGLint eglContextAttrs[] =
|
|
|
|
{
|
|
|
|
EGL_CONTEXT_CLIENT_VERSION, 2,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
const EGLint eglSurfaceAttrs[] =
|
|
|
|
{
|
|
|
|
EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get the EGL display and initialize.
|
2013-06-15 14:03:30 +08:00
|
|
|
_eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
|
|
|
if (_eglDisplay == EGL_NO_DISPLAY)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
perror("eglGetDisplay");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (eglInitialize(_eglDisplay, NULL, NULL) != EGL_TRUE)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
perror("eglInitialize");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (eglChooseConfig(_eglDisplay, eglConfigAttrs, &config, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
checkErrorEGL("eglChooseConfig");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_eglContext = eglCreateContext(_eglDisplay, config, EGL_NO_CONTEXT, eglContextAttrs);
|
|
|
|
if (_eglContext == EGL_NO_CONTEXT)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
checkErrorEGL("eglCreateContext");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_eglSurface = eglCreateWindowSurface(_eglDisplay, config, NULL, eglSurfaceAttrs);
|
|
|
|
if (_eglSurface == EGL_NO_SURFACE)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
checkErrorEGL("eglCreateWindowSurface");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (eglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext) != EGL_TRUE)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
checkErrorEGL("eglMakeCurrent");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Get the actual canvas size somehow.
|
2013-05-18 03:39:18 +08:00
|
|
|
EGLint width;
|
|
|
|
EGLint height;
|
2013-04-09 09:21:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if ((_eglDisplay == EGL_NO_DISPLAY) || (_eglSurface == EGL_NO_SURFACE) )
|
2013-04-09 09:21:53 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
eglQuerySurface(_eglDisplay, _eglSurface, EGL_WIDTH, &width);
|
|
|
|
eglQuerySurface(_eglDisplay, _eglSurface, EGL_HEIGHT, &height);
|
2013-04-09 09:21:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_screenSize.width = width;
|
|
|
|
_screenSize.height = height;
|
2013-04-09 09:21:53 +08:00
|
|
|
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
|
2013-05-18 03:39:18 +08:00
|
|
|
// Default the frame size to be the whole canvas. In general we want to be
|
|
|
|
// setting the size of the viewport by adjusting the canvas size (so
|
|
|
|
// there's no weird letterboxing).
|
|
|
|
setFrameSize(width, height);
|
2013-04-09 09:21:53 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long time2millis(struct timespec *times)
|
|
|
|
{
|
|
|
|
return times->tv_sec*1000 + times->tv_nsec/1000000;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool EGLView::handleEvents()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|