axmol/core/platform/GLView.cpp

506 lines
14 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +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.
****************************************************************************/
#include "platform/GLView.h"
2019-11-23 20:27:39 +08:00
#include "base/Touch.h"
#include "base/Director.h"
#include "base/EventDispatcher.h"
#include "2d/Camera.h"
#include "2d/Scene.h"
#include "renderer/Renderer.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
namespace
{
static Touch* g_touches[EventTouch::MAX_TOUCHES] = {nullptr};
static unsigned int g_indexBitsUsed = 0;
// System touch pointer ID (It may not be ascending order number) <-> Ascending order number from 0
static std::map<intptr_t, int> g_touchIdReorderMap;
static int getUnUsedIndex()
{
int i;
int temp = g_indexBitsUsed;
for (i = 0; i < EventTouch::MAX_TOUCHES; i++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (!(temp & 0x00000001))
{
g_indexBitsUsed |= (1 << i);
return i;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
temp >>= 1;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
// all bits are used
return -1;
}
static std::vector<Touch*> getAllTouchesVector()
{
std::vector<Touch*> ret;
int i;
int temp = g_indexBitsUsed;
ret.reserve(EventTouch::MAX_TOUCHES);
2021-12-25 10:04:45 +08:00
for (i = 0; i < EventTouch::MAX_TOUCHES; i++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (temp & 0x00000001)
{
ret.emplace_back(g_touches[i]);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
temp >>= 1;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
return ret;
}
static void removeUsedIndexBit(int index)
{
if (index < 0 || index >= EventTouch::MAX_TOUCHES)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
return;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
unsigned int temp = 1 << index;
temp = ~temp;
g_indexBitsUsed &= temp;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
} // namespace
// default context attributions are set as follows
2019-11-23 20:27:39 +08:00
GLContextAttrs GLView::_glContextAttrs = {8, 8, 8, 8, 24, 8, 0};
void GLView::setGLContextAttrs(GLContextAttrs& glContextAttrs)
{
_glContextAttrs = glContextAttrs;
}
GLContextAttrs GLView::getGLContextAttrs()
{
return _glContextAttrs;
}
GLView::GLView()
2021-12-25 10:04:45 +08:00
: _screenSize(0, 0)
, _designResolutionSize(0, 0)
, _scaleX(1.0f)
, _scaleY(1.0f)
, _resolutionPolicy(ResolutionPolicy::UNKNOWN)
{}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
GLView::~GLView() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void GLView::pollEvents() {}
2019-11-23 20:27:39 +08:00
void GLView::updateDesignResolutionSize()
{
2021-12-25 10:04:45 +08:00
if (_screenSize.width > 0 && _screenSize.height > 0 && _designResolutionSize.width > 0 &&
_designResolutionSize.height > 0)
2019-11-23 20:27:39 +08:00
{
_scaleX = (float)_screenSize.width / _designResolutionSize.width;
_scaleY = (float)_screenSize.height / _designResolutionSize.height;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
{
_scaleX = _scaleY = MAX(_scaleX, _scaleY);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
else if (_resolutionPolicy == ResolutionPolicy::SHOW_ALL)
{
_scaleX = _scaleY = MIN(_scaleX, _scaleY);
}
2021-12-25 10:04:45 +08:00
else if (_resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT)
{
_scaleX = _scaleY;
_designResolutionSize.width = ceilf(_screenSize.width / _scaleX);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (_resolutionPolicy == ResolutionPolicy::FIXED_WIDTH)
{
_scaleY = _scaleX;
_designResolutionSize.height = ceilf(_screenSize.height / _scaleY);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// calculate the rect of viewport
float viewPortW = _designResolutionSize.width * _scaleX;
float viewPortH = _designResolutionSize.height * _scaleY;
2021-12-25 10:04:45 +08:00
_viewPortRect.setRect((_screenSize.width - viewPortW) / 2, (_screenSize.height - viewPortH) / 2, viewPortW,
viewPortH);
2019-11-23 20:27:39 +08:00
// reset director's member variables to fit visible rect
2021-12-25 10:04:45 +08:00
auto director = Director::getInstance();
director->_winSizeInPoints = getDesignResolutionSize();
2019-11-23 20:27:39 +08:00
director->_isStatusLabelUpdated = true;
director->setProjection(director->getProjection());
// Github issue #16139
// A default viewport is needed in order to display the FPS,
// since the FPS are rendered in the Director, and there is no viewport there.
// Everything, including the FPS should renderer in the Scene.
2021-12-25 10:04:45 +08:00
// TODO: minggo
// glViewport(0, 0, _screenSize.width, _screenSize.height);
2019-11-23 20:27:39 +08:00
}
}
void GLView::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy)
{
2022-07-16 10:43:05 +08:00
AXASSERT(resolutionPolicy != ResolutionPolicy::UNKNOWN, "should set resolutionPolicy");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (width == 0.0f || height == 0.0f)
{
return;
}
2021-10-23 23:27:14 +08:00
_designResolutionSize.set(width, height);
2019-11-23 20:27:39 +08:00
_resolutionPolicy = resolutionPolicy;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
updateDesignResolutionSize();
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
const Vec2& GLView::getDesignResolutionSize() const
2019-11-23 20:27:39 +08:00
{
return _designResolutionSize;
}
2021-10-23 23:27:14 +08:00
Vec2 GLView::getFrameSize() const
2019-11-23 20:27:39 +08:00
{
return _screenSize;
}
void GLView::setFrameSize(float width, float height)
{
2021-10-23 23:27:14 +08:00
_screenSize = Vec2(width, height);
2019-11-23 20:27:39 +08:00
// Github issue #16003 and #16485
// only update the designResolution if it wasn't previously set
2021-10-23 23:27:14 +08:00
if (_designResolutionSize.equals(Vec2::ZERO))
2019-11-23 20:27:39 +08:00
_designResolutionSize = _screenSize;
}
Rect GLView::getVisibleRect() const
{
Rect ret;
2021-12-25 10:04:45 +08:00
ret.size = getVisibleSize();
2019-11-23 20:27:39 +08:00
ret.origin = getVisibleOrigin();
return ret;
}
Rect GLView::getSafeAreaRect() const
{
return getVisibleRect();
}
2021-10-23 23:27:14 +08:00
Vec2 GLView::getVisibleSize() const
2019-11-23 20:27:39 +08:00
{
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
{
2021-12-25 10:04:45 +08:00
return Vec2(_screenSize.width / _scaleX, _screenSize.height / _scaleY);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else
2019-11-23 20:27:39 +08:00
{
return _designResolutionSize;
}
}
Vec2 GLView::getVisibleOrigin() const
{
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
{
2021-12-25 10:04:45 +08:00
return Vec2((_designResolutionSize.width - _screenSize.width / _scaleX) / 2,
(_designResolutionSize.height - _screenSize.height / _scaleY) / 2);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else
2019-11-23 20:27:39 +08:00
{
return Vec2::ZERO;
}
}
2021-12-25 10:04:45 +08:00
void GLView::setViewPortInPoints(float x, float y, float w, float h)
2019-11-23 20:27:39 +08:00
{
Viewport vp;
vp.x = (int)(x * _scaleX + _viewPortRect.origin.x);
vp.y = (int)(y * _scaleY + _viewPortRect.origin.y);
2023-08-25 16:57:38 +08:00
vp.w = (unsigned int)(w * _scaleX);
vp.h = (unsigned int)(h * _scaleY);
2019-11-23 20:27:39 +08:00
Camera::setDefaultViewport(vp);
}
2021-12-25 10:04:45 +08:00
void GLView::setScissorInPoints(float x, float y, float w, float h)
2019-11-23 20:27:39 +08:00
{
auto renderer = Director::getInstance()->getRenderer();
2021-12-25 10:04:45 +08:00
renderer->setScissorRect((int)(x * _scaleX + _viewPortRect.origin.x), (int)(y * _scaleY + _viewPortRect.origin.y),
(unsigned int)(w * _scaleX), (unsigned int)(h * _scaleY));
2019-11-23 20:27:39 +08:00
}
bool GLView::isScissorEnabled()
{
auto renderer = Director::getInstance()->getRenderer();
return renderer->getScissorTest();
}
Rect GLView::getScissorRect() const
{
auto renderer = Director::getInstance()->getRenderer();
2021-12-25 10:04:45 +08:00
auto& rect = renderer->getScissorRect();
2019-11-23 20:27:39 +08:00
float x = (rect.x - _viewPortRect.origin.x) / _scaleX;
2021-12-25 10:04:45 +08:00
float y = (rect.y - _viewPortRect.origin.y) / _scaleY;
float w = rect.width / _scaleX;
2019-11-23 20:27:39 +08:00
float h = rect.height / _scaleY;
return Rect(x, y, w, h);
}
void GLView::setViewName(std::string_view viewname)
2019-11-23 20:27:39 +08:00
{
_viewName = viewname;
}
std::string_view GLView::getViewName() const
2019-11-23 20:27:39 +08:00
{
return _viewName;
}
void GLView::handleTouchesBegin(int num, intptr_t ids[], float xs[], float ys[])
{
2021-12-25 10:04:45 +08:00
intptr_t id = 0;
float x = 0.0f;
float y = 0.0f;
2019-11-23 20:27:39 +08:00
int unusedIndex = 0;
EventTouch touchEvent;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (int i = 0; i < num; ++i)
{
id = ids[i];
2021-12-25 10:04:45 +08:00
x = xs[i];
y = ys[i];
2019-11-23 20:27:39 +08:00
auto iter = g_touchIdReorderMap.find(id);
// it is a new touch
if (iter == g_touchIdReorderMap.end())
{
unusedIndex = getUnUsedIndex();
// The touches is more than MAX_TOUCHES ?
2021-12-25 10:04:45 +08:00
if (unusedIndex == -1)
{
2022-07-16 10:43:05 +08:00
AXLOG("The touches is more than MAX_TOUCHES, unusedIndex = %d", unusedIndex);
2019-11-23 20:27:39 +08:00
continue;
}
2021-12-08 00:11:53 +08:00
Touch* touch = g_touches[unusedIndex] = new Touch();
2019-11-23 20:27:39 +08:00
touch->setTouchInfo(unusedIndex, (x - _viewPortRect.origin.x) / _scaleX,
2021-12-25 10:04:45 +08:00
(y - _viewPortRect.origin.y) / _scaleY);
2022-07-16 10:43:05 +08:00
AXLOGINFO("x = %f y = %f", touch->getLocationInView().x, touch->getLocationInView().y);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
g_touchIdReorderMap.emplace(id, unusedIndex);
touchEvent._touches.emplace_back(touch);
2019-11-23 20:27:39 +08:00
}
}
2020-08-18 14:29:09 +08:00
if (touchEvent._touches.empty())
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("touchesBegan: size = 0");
2019-11-23 20:27:39 +08:00
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
touchEvent._eventCode = EventTouch::EventCode::BEGAN;
2021-12-25 10:04:45 +08:00
auto dispatcher = Director::getInstance()->getEventDispatcher();
2019-11-23 20:27:39 +08:00
dispatcher->dispatchEvent(&touchEvent);
}
void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[])
{
handleTouchesMove(num, ids, xs, ys, nullptr, nullptr);
}
void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[], float fs[], float ms[])
{
2021-12-25 10:04:45 +08:00
intptr_t id = 0;
float x = 0.0f;
float y = 0.0f;
float force = 0.0f;
2019-11-23 20:27:39 +08:00
float maxForce = 0.0f;
EventTouch touchEvent;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (int i = 0; i < num; ++i)
{
2021-12-25 10:04:45 +08:00
id = ids[i];
x = xs[i];
y = ys[i];
force = fs ? fs[i] : 0.0f;
2019-11-23 20:27:39 +08:00
maxForce = ms ? ms[i] : 0.0f;
auto iter = g_touchIdReorderMap.find(id);
if (iter == g_touchIdReorderMap.end())
{
2022-07-16 10:43:05 +08:00
AXLOG("if the index doesn't exist, it is an error");
2019-11-23 20:27:39 +08:00
continue;
}
2022-07-16 10:43:05 +08:00
AXLOGINFO("Moving touches with id: %d, x=%f, y=%f, force=%f, maxFource=%f", (int)id, x, y, force, maxForce);
2019-11-23 20:27:39 +08:00
Touch* touch = g_touches[iter->second];
if (touch)
{
touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
(y - _viewPortRect.origin.y) / _scaleY, force, maxForce);
2021-12-25 10:04:45 +08:00
touchEvent._touches.emplace_back(touch);
2019-11-23 20:27:39 +08:00
}
else
{
// It is error, should return.
2022-07-16 10:43:05 +08:00
AXLOG("Moving touches with id: %d error", static_cast<int32_t>(id));
2019-11-23 20:27:39 +08:00
return;
}
}
2020-08-18 14:29:09 +08:00
if (touchEvent._touches.empty())
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("touchesMoved: size = 0");
2019-11-23 20:27:39 +08:00
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
touchEvent._eventCode = EventTouch::EventCode::MOVED;
2021-12-25 10:04:45 +08:00
auto dispatcher = Director::getInstance()->getEventDispatcher();
2019-11-23 20:27:39 +08:00
dispatcher->dispatchEvent(&touchEvent);
}
2021-12-25 10:04:45 +08:00
void GLView::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode,
int num,
intptr_t ids[],
float xs[],
float ys[])
2019-11-23 20:27:39 +08:00
{
intptr_t id = 0;
2021-12-25 10:04:45 +08:00
float x = 0.0f;
float y = 0.0f;
2019-11-23 20:27:39 +08:00
EventTouch touchEvent;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (int i = 0; i < num; ++i)
{
id = ids[i];
2021-12-25 10:04:45 +08:00
x = xs[i];
y = ys[i];
2019-11-23 20:27:39 +08:00
auto iter = g_touchIdReorderMap.find(id);
if (iter == g_touchIdReorderMap.end())
{
2022-07-16 10:43:05 +08:00
AXLOG("if the index doesn't exist, it is an error");
2019-11-23 20:27:39 +08:00
continue;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/* Add to the set to send to the director */
Touch* touch = g_touches[iter->second];
if (touch)
{
2022-07-16 10:43:05 +08:00
AXLOGINFO("Ending touches with id: %d, x=%f, y=%f", (int)id, x, y);
2019-11-23 20:27:39 +08:00
touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
(y - _viewPortRect.origin.y) / _scaleY);
touchEvent._touches.emplace_back(touch);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
g_touches[iter->second] = nullptr;
removeUsedIndexBit(iter->second);
g_touchIdReorderMap.erase(id);
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
else
{
2022-07-16 10:43:05 +08:00
AXLOG("Ending touches with id: %d error", static_cast<int32_t>(id));
2019-11-23 20:27:39 +08:00
return;
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
}
2020-08-18 14:29:09 +08:00
if (touchEvent._touches.empty())
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("touchesEnded or touchesCancel: size = 0");
2019-11-23 20:27:39 +08:00
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
touchEvent._eventCode = eventCode;
2021-12-25 10:04:45 +08:00
auto dispatcher = Director::getInstance()->getEventDispatcher();
2019-11-23 20:27:39 +08:00
dispatcher->dispatchEvent(&touchEvent);
2021-12-25 10:04:45 +08:00
for (auto&& touch : touchEvent._touches)
2019-11-23 20:27:39 +08:00
{
// release the touch object.
touch->release();
}
}
void GLView::handleTouchesEnd(int num, intptr_t ids[], float xs[], float ys[])
{
handleTouchesOfEndOrCancel(EventTouch::EventCode::ENDED, num, ids, xs, ys);
}
void GLView::handleTouchesCancel(int num, intptr_t ids[], float xs[], float ys[])
{
handleTouchesOfEndOrCancel(EventTouch::EventCode::CANCELLED, num, ids, xs, ys);
}
const Rect& GLView::getViewPortRect() const
{
return _viewPortRect;
}
std::vector<Touch*> GLView::getAllTouches() const
{
return getAllTouchesVector();
}
float GLView::getScaleX() const
{
return _scaleX;
}
float GLView::getScaleY() const
{
return _scaleY;
}
void GLView::renderScene(Scene* scene, Renderer* renderer)
{
2022-07-16 10:43:05 +08:00
AXASSERT(scene, "Invalid Scene");
AXASSERT(renderer, "Invalid Renderer");
2019-11-23 20:27:39 +08:00
scene->render(renderer, Mat4::IDENTITY, nullptr);
}
void GLView::queueOperation(AsyncOperation /*op*/, void* /*param*/)
{
}
NS_AX_END