axmol/cocos2dx/platform/android/CCEGLView_android.cpp

218 lines
5.6 KiB
C++
Raw Normal View History

2010-11-03 11:01:17 +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.
****************************************************************************/
2011-03-08 13:49:58 +08:00
#include "CCEGLView_android.h"
#include "CCSet.h"
2010-11-03 11:01:17 +08:00
#include "CCDirector.h"
#include "ccMacros.h"
2010-12-29 10:30:42 +08:00
#include "CCTouchDispatcher.h"
#include "jni/IMEJni.h"
#include "jni/MessageJni.h"
2010-11-03 11:01:17 +08:00
2010-11-04 17:33:31 +08:00
#include <stdlib.h>
2012-03-20 16:10:12 +08:00
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <EGL/egl.h>
#include <android/log.h>
PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT = 0;
PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT = 0;
PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT = 0;
void initExtensions() {
glGenVertexArraysOESEXT = (PFNGLGENVERTEXARRAYSOESPROC)eglGetProcAddress("glGenVertexArraysOES");
glBindVertexArrayOESEXT = (PFNGLBINDVERTEXARRAYOESPROC)eglGetProcAddress("glBindVertexArrayOES");
glDeleteVertexArraysOESEXT = (PFNGLDELETEVERTEXARRAYSOESPROC)eglGetProcAddress("glDeleteVertexArraysOES");
}
2010-11-04 17:33:31 +08:00
2010-11-03 11:01:17 +08:00
namespace cocos2d {
2011-03-08 13:49:58 +08:00
CCEGLView::CCEGLView()
2011-06-10 17:51:37 +08:00
: m_bNotHVGA(false),
m_pDelegate(NULL),
m_fScreenScaleFactor(1.0)
2010-11-03 11:01:17 +08:00
{
2012-03-20 16:10:12 +08:00
initExtensions();
2010-11-03 11:01:17 +08:00
}
void CCEGLView::setFrameWidthAndHeight(int width, int height)
2010-11-03 11:01:17 +08:00
{
m_sSizeInPixel.width = width;
m_sSizeInPixel.height = height;
}
bool CCEGLView::isIpad()
{
return false;
}
2011-03-08 13:49:58 +08:00
void CCEGLView::create(int width, int height)
{
if (width == 0 || height == 0)
{
return;
}
m_sSizeInPoint.width = width;
m_sSizeInPoint.height = height;
// calculate the factor and the rect of viewport
m_fScreenScaleFactor = MIN((float)m_sSizeInPixel.width / m_sSizeInPoint.width,
(float)m_sSizeInPixel.height / m_sSizeInPoint.height);
int viewPortW = (int)(m_sSizeInPoint.width * m_fScreenScaleFactor);
int viewPortH = (int)(m_sSizeInPoint.height * m_fScreenScaleFactor);
m_rcViewPort.origin.x = (m_sSizeInPixel.width - viewPortW) / 2;
m_rcViewPort.origin.y = (m_sSizeInPixel.height - viewPortH) / 2;
m_rcViewPort.size.width = viewPortW;
m_rcViewPort.size.height = viewPortH;
m_bNotHVGA = true;
2010-11-03 11:01:17 +08:00
}
2011-03-08 13:49:58 +08:00
CCEGLView::~CCEGLView()
2010-11-03 11:01:17 +08:00
{
2010-11-03 11:01:17 +08:00
}
2011-03-08 13:49:58 +08:00
CCSize CCEGLView::getSize()
2010-11-03 11:01:17 +08:00
{
if (m_bNotHVGA)
{
2011-03-08 13:49:58 +08:00
CCSize size(m_sSizeInPoint.width, m_sSizeInPoint.height);
return size;
}
else
{
2011-03-08 13:49:58 +08:00
CCSize size(m_sSizeInPixel.width, m_sSizeInPixel.height);
return size;
}
2010-11-03 11:01:17 +08:00
}
2011-03-08 13:49:58 +08:00
bool CCEGLView::isOpenGLReady()
2010-11-03 11:01:17 +08:00
{
return (m_sSizeInPixel.width != 0 && m_sSizeInPixel.height != 0);
2010-11-03 11:01:17 +08:00
}
2011-03-08 13:49:58 +08:00
void CCEGLView::release()
2010-11-03 11:01:17 +08:00
{
terminateProcessJNI();
2010-11-03 11:01:17 +08:00
}
2011-03-08 13:49:58 +08:00
void CCEGLView::setTouchDelegate(EGLTouchDelegate * pDelegate)
2010-11-03 11:01:17 +08:00
{
2010-12-09 10:50:02 +08:00
m_pDelegate = pDelegate;
}
2011-03-08 13:49:58 +08:00
EGLTouchDelegate* CCEGLView::getDelegate(void)
2010-12-09 10:50:02 +08:00
{
return m_pDelegate;
2010-11-03 11:01:17 +08:00
}
2011-03-08 13:49:58 +08:00
void CCEGLView::swapBuffers()
2010-11-03 11:01:17 +08:00
{
}
2011-03-08 13:49:58 +08:00
bool CCEGLView::canSetContentScaleFactor()
{
// can scale content?
return false;
}
2011-03-08 13:49:58 +08:00
void CCEGLView::setContentScaleFactor(float contentScaleFactor)
{
m_fScreenScaleFactor = contentScaleFactor;
}
2011-03-08 13:49:58 +08:00
void CCEGLView::setViewPortInPoints(float x, float y, float w, float h)
{
2011-04-22 16:33:41 +08:00
if (m_bNotHVGA)
{
float factor = m_fScreenScaleFactor / CC_CONTENT_SCALE_FACTOR();
glViewport((GLint)(x * factor) + m_rcViewPort.origin.x,
(GLint)(y * factor) + m_rcViewPort.origin.y,
(GLint)(w * factor),
(GLint)(h * factor));
}
else
{
glViewport((GLint)x,
(GLint)y,
(GLint)w,
(GLint)h);
}
}
void CCEGLView::setScissorInPoints(float x, float y, float w, float h)
{
if (m_bNotHVGA)
{
float factor = m_fScreenScaleFactor / CC_CONTENT_SCALE_FACTOR();
glScissor((GLint)(x * factor) + m_rcViewPort.origin.x,
(GLint)(y * factor) + m_rcViewPort.origin.y,
(GLint)(w * factor),
(GLint)(h * factor));
}
else
{
glScissor((GLint)x,
(GLint)y,
(GLint)w,
(GLint)h);
}
}
2011-03-08 13:49:58 +08:00
CCEGLView& CCEGLView::sharedOpenGLView()
{
2011-03-08 13:49:58 +08:00
static CCEGLView instance;
return instance;
}
2011-03-08 13:49:58 +08:00
float CCEGLView::getScreenScaleFactor()
{
return m_fScreenScaleFactor;
}
2011-03-08 13:49:58 +08:00
CCRect CCEGLView::getViewPort()
{
2011-04-22 16:33:41 +08:00
if (m_bNotHVGA)
{
return m_rcViewPort;
}
else
{
CCRect rect(0, 0, 0, 0);
return rect;
}
}
void CCEGLView::setIMEKeyboardState(bool bOpen)
{
setKeyboardStateJNI((int)bOpen);
}
2010-11-03 11:01:17 +08:00
} // end of namespace cocos2d
2010-11-04 17:33:31 +08:00