2010-11-12 17:16:15 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2010-11-12 17:16:15 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "platform/android/CCGLViewImpl-android.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/ccMacros.h"
|
2015-11-27 18:19:10 +08:00
|
|
|
#include "platform/android/jni/JniHelper.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "CCGL.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <android/log.h>
|
2012-06-19 16:20:46 +08:00
|
|
|
|
2012-11-16 14:23:14 +08:00
|
|
|
// <EGL/egl.h> exists since android 2.3
|
|
|
|
#include <EGL/egl.h>
|
2021-12-25 10:04:45 +08:00
|
|
|
PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT = 0;
|
|
|
|
PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT = 0;
|
2012-11-16 14:23:14 +08:00
|
|
|
PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT = 0;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#define DEFAULT_MARGIN_ANDROID 30.0f
|
|
|
|
#define WIDE_SCREEN_ASPECT_RATIO_ANDROID 2.0f
|
2018-09-15 16:45:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void initExtensions()
|
|
|
|
{
|
|
|
|
glGenVertexArraysOESEXT = (PFNGLGENVERTEXARRAYSOESPROC)eglGetProcAddress("glGenVertexArraysOES");
|
|
|
|
glBindVertexArrayOESEXT = (PFNGLBINDVERTEXARRAYOESPROC)eglGetProcAddress("glBindVertexArrayOES");
|
|
|
|
glDeleteVertexArraysOESEXT = (PFNGLDELETEVERTEXARRAYSOESPROC)eglGetProcAddress("glDeleteVertexArraysOES");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
GLViewImpl* GLViewImpl::createWithRect(std::string_view viewName, Rect rect, float frameZoomFactor)
|
2014-01-24 07:36:55 +08:00
|
|
|
{
|
2014-07-31 00:53:04 +08:00
|
|
|
auto ret = new GLViewImpl;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ret && ret->initWithRect(viewName, rect, frameZoomFactor))
|
|
|
|
{
|
2014-01-24 07:36:55 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2014-01-24 07:36:55 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
GLViewImpl* GLViewImpl::create(std::string_view viewName)
|
2014-01-24 07:36:55 +08:00
|
|
|
{
|
2014-07-31 00:53:04 +08:00
|
|
|
auto ret = new GLViewImpl;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ret && ret->initWithFullScreen(viewName))
|
|
|
|
{
|
2014-01-24 07:36:55 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2014-01-24 07:36:55 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
GLViewImpl* GLViewImpl::createWithFullScreen(std::string_view viewName)
|
2014-01-24 07:36:55 +08:00
|
|
|
{
|
2014-07-31 00:53:04 +08:00
|
|
|
auto ret = new GLViewImpl();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ret && ret->initWithFullScreen(viewName))
|
|
|
|
{
|
2014-01-24 07:36:55 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2014-01-24 07:36:55 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
GLViewImpl::GLViewImpl()
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
initExtensions();
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
GLViewImpl::~GLViewImpl() {}
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool GLViewImpl::initWithRect(std::string_view viewName, Rect rect, float frameZoomFactor)
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2014-01-24 07:36:55 +08:00
|
|
|
return true;
|
2012-04-23 15:40:46 +08:00
|
|
|
}
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool GLViewImpl::initWithFullScreen(std::string_view viewName)
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2014-01-24 07:36:55 +08:00
|
|
|
return true;
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
bool GLViewImpl::isOpenGLReady()
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2014-01-24 07:36:55 +08:00
|
|
|
return (_screenSize.width != 0 && _screenSize.height != 0);
|
2011-02-23 18:22:05 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLViewImpl::end()
|
2011-02-23 18:22:05 +08:00
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
JniHelper::callStaticVoidMethod("org.axys1.lib.AxysHelper", "terminateProcess");
|
2011-02-23 18:22:05 +08:00
|
|
|
}
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void GLViewImpl::swapBuffers() {}
|
2013-07-12 06:24:23 +08:00
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLViewImpl::setIMEKeyboardState(bool bOpen)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (bOpen)
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
JniHelper::callStaticVoidMethod("org.axys1.lib.AxysGLSurfaceView", "openIMEKeyboard");
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
JniHelper::callStaticVoidMethod("org.axys1.lib.AxysGLSurfaceView", "closeIMEKeyboard");
|
2015-11-27 17:00:33 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect GLViewImpl::getSafeAreaRect() const
|
|
|
|
{
|
|
|
|
Rect safeAreaRect = GLView::getSafeAreaRect();
|
2018-09-15 16:45:22 +08:00
|
|
|
float deviceAspectRatio = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (safeAreaRect.size.height > safeAreaRect.size.width)
|
|
|
|
{
|
2018-09-15 16:45:22 +08:00
|
|
|
deviceAspectRatio = safeAreaRect.size.height / safeAreaRect.size.width;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-15 16:45:22 +08:00
|
|
|
deviceAspectRatio = safeAreaRect.size.width / safeAreaRect.size.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
float marginX = DEFAULT_MARGIN_ANDROID / _scaleX;
|
|
|
|
float marginY = DEFAULT_MARGIN_ANDROID / _scaleY;
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
bool isScreenRound = JniHelper::callStaticBooleanMethod("org/axys1/lib/AxysHelper", "isScreenRound");
|
|
|
|
bool hasSoftKeys = JniHelper::callStaticBooleanMethod("org/axys1/lib/AxysHelper", "hasSoftKeys");
|
|
|
|
bool isCutoutEnabled = JniHelper::callStaticBooleanMethod("org/axys1/lib/AxysHelper", "isCutoutEnabled");
|
2019-04-11 09:32:27 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isScreenRound)
|
|
|
|
{
|
2018-09-15 16:45:22 +08:00
|
|
|
// edge screen (ex. Samsung Galaxy s7, s9, s9+, Note 9, Nokia 8 Sirocco, Sony Xperia XZ3, Oppo Find X...)
|
2021-12-25 10:04:45 +08:00
|
|
|
if (safeAreaRect.size.width < safeAreaRect.size.height)
|
|
|
|
{
|
2018-09-15 16:45:22 +08:00
|
|
|
safeAreaRect.origin.y += marginY * 2.f;
|
|
|
|
safeAreaRect.size.height -= (marginY * 2.f);
|
|
|
|
|
|
|
|
safeAreaRect.origin.x += marginX;
|
|
|
|
safeAreaRect.size.width -= (marginX * 2.f);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-15 16:45:22 +08:00
|
|
|
safeAreaRect.origin.y += marginY;
|
|
|
|
safeAreaRect.size.height -= (marginY * 2.f);
|
|
|
|
|
|
|
|
// landscape: no changes with X-coords
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (deviceAspectRatio >= WIDE_SCREEN_ASPECT_RATIO_ANDROID)
|
|
|
|
{
|
2018-09-15 16:45:22 +08:00
|
|
|
// almost all devices on the market have round corners if
|
|
|
|
// deviceAspectRatio more than 2 (@see "android.max_aspect" parameter in AndroidManifest.xml)
|
|
|
|
float bottomMarginIfPortrait = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (hasSoftKeys)
|
|
|
|
{
|
2018-09-15 16:45:22 +08:00
|
|
|
bottomMarginIfPortrait = marginY * 2.f;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (safeAreaRect.size.width < safeAreaRect.size.height)
|
|
|
|
{
|
2018-09-15 16:45:22 +08:00
|
|
|
// portrait: double margin space if device has soft menu
|
|
|
|
safeAreaRect.origin.y += bottomMarginIfPortrait;
|
|
|
|
safeAreaRect.size.height -= (bottomMarginIfPortrait + marginY);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-15 16:45:22 +08:00
|
|
|
// landscape: ignore double margin at the bottom in any cases
|
|
|
|
// prepare signle margin for round corners
|
|
|
|
safeAreaRect.origin.y += marginY;
|
|
|
|
safeAreaRect.size.height -= (marginY * 2.f);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (hasSoftKeys && (safeAreaRect.size.width < safeAreaRect.size.height))
|
|
|
|
{
|
2018-09-15 16:45:22 +08:00
|
|
|
// portrait: preserve only for soft system menu
|
|
|
|
safeAreaRect.origin.y += marginY * 2.f;
|
|
|
|
safeAreaRect.size.height -= (marginY * 2.f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isCutoutEnabled)
|
|
|
|
{
|
2019-04-11 09:32:27 +08:00
|
|
|
// screen with enabled cutout area (ex. Google Pixel 3 XL, Huawei P20, Asus ZenFone 5, etc)
|
2021-12-25 10:04:45 +08:00
|
|
|
static int* safeInsets =
|
2022-08-08 18:02:17 +08:00
|
|
|
JniHelper::callStaticIntArrayMethod("org/axys1/lib/AxysHelper", "getSafeInsets");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (safeInsets != nullptr)
|
|
|
|
{
|
2019-04-11 09:32:27 +08:00
|
|
|
float safeInsetBottom = safeInsets[0] / _scaleY;
|
2021-12-25 10:04:45 +08:00
|
|
|
float safeInsetLeft = safeInsets[1] / _scaleX;
|
|
|
|
float safeInsetRight = safeInsets[2] / _scaleX;
|
|
|
|
float safeInsetTop = safeInsets[3] / _scaleY;
|
2019-04-11 09:32:27 +08:00
|
|
|
|
|
|
|
// fit safe area rect with safe insets
|
2021-12-25 10:04:45 +08:00
|
|
|
if (safeInsetBottom > 0)
|
|
|
|
{
|
2019-04-11 09:32:27 +08:00
|
|
|
safeAreaRect.origin.y += safeInsetBottom;
|
|
|
|
safeAreaRect.size.height -= safeInsetBottom;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (safeInsetLeft > 0)
|
|
|
|
{
|
2019-04-11 09:32:27 +08:00
|
|
|
safeAreaRect.origin.x += safeInsetLeft;
|
|
|
|
safeAreaRect.size.width -= safeInsetLeft;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (safeInsetRight > 0)
|
|
|
|
{
|
2019-04-11 09:32:27 +08:00
|
|
|
safeAreaRect.size.width -= safeInsetRight;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (safeInsetTop > 0)
|
|
|
|
{
|
2019-04-11 09:32:27 +08:00
|
|
|
safeAreaRect.size.height -= safeInsetTop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 16:45:22 +08:00
|
|
|
return safeAreaRect;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|