2013-04-26 17:48:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
Copyright (c) 2013 Lee, Jae-Hong
|
|
|
|
|
|
|
|
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"
|
2013-04-30 00:14:11 +08:00
|
|
|
#include "CCApplication.h"
|
2013-04-26 17:48:26 +08:00
|
|
|
#include "CCGL.h"
|
2013-04-30 00:14:11 +08:00
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "CCOspApplication.h"
|
2013-05-29 21:46:52 +08:00
|
|
|
#include "CCOspForm.h"
|
2013-04-26 17:48:26 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2013-04-30 23:52:34 +08:00
|
|
|
using namespace Tizen::App;
|
2013-04-30 00:14:11 +08:00
|
|
|
using namespace Tizen::Base::Runtime;
|
2013-04-26 17:48:26 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLView::EGLView()
|
2013-04-30 00:14:11 +08:00
|
|
|
: __eglDisplay(EGL_DEFAULT_DISPLAY)
|
|
|
|
, __eglSurface(EGL_NO_SURFACE)
|
|
|
|
, __eglConfig(null)
|
|
|
|
, __eglContext(EGL_NO_DISPLAY)
|
|
|
|
, __pTimer(null)
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLView::~EGLView()
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool EGLView::isOpenGLReady()
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return (_screenSize.width != 0 && _screenSize.height != 0);
|
2013-04-26 17:48:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 18:20:27 +08:00
|
|
|
Tizen::Base::Runtime::Timer*
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLView::getTimer()
|
2013-04-30 00:14:11 +08:00
|
|
|
{
|
|
|
|
return __pTimer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLView::cleanup()
|
2013-04-30 00:14:11 +08:00
|
|
|
{
|
|
|
|
if (__pTimer != null)
|
|
|
|
{
|
|
|
|
__pTimer->Cancel();
|
|
|
|
delete __pTimer;
|
|
|
|
__pTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroyGL();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::end()
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
2013-04-30 23:52:34 +08:00
|
|
|
cleanup();
|
|
|
|
|
|
|
|
UiApp* pApp = UiApp::GetInstance();
|
|
|
|
AppAssert(pApp);
|
|
|
|
pApp->Terminate();
|
2013-04-26 17:48:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::swapBuffers()
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
2013-04-30 00:14:11 +08:00
|
|
|
eglSwapBuffers(__eglDisplay, __eglSurface);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLView::setIMEKeyboardState(bool bOpen)
|
2013-04-30 00:14:11 +08:00
|
|
|
{
|
2013-05-29 21:46:52 +08:00
|
|
|
if (bOpen)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
((OspForm *)OspApplication::GetInstance()->getOspForm())->ShowKeypad();
|
2013-05-29 21:46:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
((OspForm *)OspApplication::GetInstance()->getOspForm())->CloseKeypad();
|
2013-05-29 21:46:52 +08:00
|
|
|
}
|
2013-04-26 17:48:26 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 13:11:21 +08:00
|
|
|
EGLView* EGLView::getInstance()
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
static EGLView* s_pEglView = NULL;
|
2013-04-30 00:14:11 +08:00
|
|
|
if (s_pEglView == NULL)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
s_pEglView = new EGLView();
|
2013-04-30 00:14:11 +08:00
|
|
|
if(!s_pEglView->create())
|
|
|
|
{
|
|
|
|
delete s_pEglView;
|
|
|
|
s_pEglView = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s_pEglView;
|
2013-04-26 17:48:26 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 13:11:21 +08:00
|
|
|
// XXX: deprecated
|
|
|
|
EGLView* EGLView::sharedOpenGLView()
|
|
|
|
{
|
|
|
|
return EGLView::getInstance();
|
|
|
|
}
|
|
|
|
|
2013-04-30 00:14:11 +08:00
|
|
|
void
|
2013-06-20 18:20:27 +08:00
|
|
|
EGLView::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
|
2013-04-30 00:14:11 +08:00
|
|
|
{
|
|
|
|
if (__pTimer == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-07-15 16:24:42 +08:00
|
|
|
__pTimer->Start(Application::getInstance()->getAnimationInterval());
|
2013-04-30 00:14:11 +08:00
|
|
|
|
2013-07-12 11:50:36 +08:00
|
|
|
Director::getInstance()->mainLoop();
|
2013-04-30 00:14:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLView::create()
|
2013-04-30 00:14:11 +08:00
|
|
|
{
|
|
|
|
result r = E_SUCCESS;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TryCatch(initEGL(), , "[EGLView] EGLView::create() failed.");
|
2013-04-30 00:14:11 +08:00
|
|
|
|
2013-06-20 18:20:27 +08:00
|
|
|
__pTimer = new (std::nothrow) Tizen::Base::Runtime::Timer;
|
2013-06-20 14:13:12 +08:00
|
|
|
TryCatch(__pTimer != null, , "[EGLView] Failed to allocate memory.");
|
2013-04-30 00:14:11 +08:00
|
|
|
|
|
|
|
r = __pTimer->Construct(*this);
|
2013-06-20 14:13:12 +08:00
|
|
|
TryCatch(!IsFailed(r), , "[EGLView] __pTimer->Construct(*this) failed.");
|
2013-04-30 00:14:11 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
CATCH:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLView::initEGL()
|
2013-04-30 00:14:11 +08:00
|
|
|
{
|
|
|
|
EGLint numConfigs = 1;
|
|
|
|
|
|
|
|
EGLint eglConfigList[] =
|
|
|
|
{
|
|
|
|
EGL_RED_SIZE, 5,
|
|
|
|
EGL_GREEN_SIZE, 6,
|
|
|
|
EGL_BLUE_SIZE, 5,
|
|
|
|
EGL_ALPHA_SIZE, 0,
|
|
|
|
EGL_DEPTH_SIZE, 8,
|
|
|
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint eglContextList[] =
|
|
|
|
{
|
|
|
|
EGL_CONTEXT_CLIENT_VERSION, 2,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
eglBindAPI(EGL_OPENGL_ES_API);
|
|
|
|
|
|
|
|
if (__eglDisplay)
|
|
|
|
{
|
|
|
|
destroyGL();
|
|
|
|
}
|
|
|
|
|
|
|
|
__eglDisplay = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY);
|
2013-06-20 14:13:12 +08:00
|
|
|
TryCatch(__eglDisplay != EGL_NO_DISPLAY, , "[EGLView] eglGetDisplay() failed.");
|
2013-04-30 00:14:11 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TryCatch(!(eglInitialize(__eglDisplay, null, null) == EGL_FALSE || eglGetError() != EGL_SUCCESS), , "[EGLView] eglInitialize() failed.");
|
2013-04-30 00:14:11 +08:00
|
|
|
|
|
|
|
TryCatch(!(eglChooseConfig(__eglDisplay, eglConfigList, &__eglConfig, 1, &numConfigs) == EGL_FALSE ||
|
2013-06-20 14:13:12 +08:00
|
|
|
eglGetError() != EGL_SUCCESS), , "[EGLView] eglChooseConfig() failed.");
|
2013-04-30 00:14:11 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TryCatch(numConfigs, , "[EGLView] eglChooseConfig() failed. because of matching config doesn't exist");
|
2013-04-30 00:14:11 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
__eglSurface = eglCreateWindowSurface(__eglDisplay, __eglConfig, (EGLNativeWindowType)OspApplication::GetInstance()->getOspForm(), null);
|
|
|
|
TryCatch(!(__eglSurface == EGL_NO_SURFACE || eglGetError() != EGL_SUCCESS), , "[EGLView] eglCreateWindowSurface() failed.");
|
2013-04-30 00:14:11 +08:00
|
|
|
|
|
|
|
__eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, eglContextList);
|
2013-06-20 14:13:12 +08:00
|
|
|
TryCatch(!(__eglContext == EGL_NO_CONTEXT || eglGetError() != EGL_SUCCESS), , "[EGLView] eglCreateContext() failed.");
|
2013-04-30 00:14:11 +08:00
|
|
|
|
|
|
|
TryCatch(!(eglMakeCurrent(__eglDisplay, __eglSurface, __eglSurface, __eglContext) == EGL_FALSE ||
|
2013-06-20 14:13:12 +08:00
|
|
|
eglGetError() != EGL_SUCCESS), , "[EGLView] eglMakeCurrent() failed.");
|
2013-04-30 00:14:11 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
CATCH:
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
AppLog("[EGLView] EGLView can run on systems which supports OpenGL ES(R) 2.0.");
|
|
|
|
AppLog("[EGLView] When EGLView does not correctly execute, there are a few reasons.");
|
|
|
|
AppLog("[EGLView] 1. The current device(real-target or emulator) does not support OpenGL ES(R) 2.0.\n"
|
2013-04-30 00:14:11 +08:00
|
|
|
" Check the Release Notes.");
|
2013-06-20 14:13:12 +08:00
|
|
|
AppLog("[EGLView] 2. The system running on emulator cannot support OpenGL(R) 2.1 or later.\n"
|
2013-04-30 00:14:11 +08:00
|
|
|
" Try with other system.");
|
2013-06-20 14:13:12 +08:00
|
|
|
AppLog("[EGLView] 3. The system running on emulator does not maintain the latest graphics driver.\n"
|
2013-04-30 00:14:11 +08:00
|
|
|
" Update the graphics driver.");
|
|
|
|
}
|
|
|
|
|
|
|
|
destroyGL();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLView::destroyGL(void)
|
2013-04-26 17:48:26 +08:00
|
|
|
{
|
2013-04-30 00:14:11 +08:00
|
|
|
if (__eglDisplay)
|
|
|
|
{
|
|
|
|
eglMakeCurrent(__eglDisplay, null, null, null);
|
|
|
|
|
|
|
|
if (__eglContext)
|
|
|
|
{
|
|
|
|
eglDestroyContext(__eglDisplay, __eglContext);
|
|
|
|
__eglContext = EGL_NO_CONTEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (__eglSurface)
|
|
|
|
{
|
|
|
|
eglDestroySurface(__eglDisplay, __eglSurface);
|
|
|
|
__eglSurface = EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
eglTerminate(__eglDisplay);
|
|
|
|
__eglDisplay = EGL_NO_DISPLAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2013-04-26 17:48:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|