axmol/core/platform/desktop/CCGLViewImpl-desktop.cpp

1231 lines
44 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.
Copyright (c) 2020 C4games Ltd.
Copyright (c) 2021-2022 Bytedance Inc.
2019-11-23 20:27:39 +08:00
https://axis-project.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/desktop/CCGLViewImpl-desktop.h"
#include <cmath>
#include <unordered_map>
#include "platform/CCApplication.h"
#include "base/CCDirector.h"
#include "base/CCTouch.h"
#include "base/CCEventDispatcher.h"
#include "base/CCEventKeyboard.h"
#include "base/CCEventMouse.h"
#include "base/CCIMEDispatcher.h"
#include "base/ccUtils.h"
#include "base/ccUTF8.h"
#include "2d/CCCamera.h"
2020-10-17 16:32:16 +08:00
2021-07-07 14:09:11 +08:00
#include "renderer/backend/opengl/MacrosGL.h"
2020-10-17 16:32:16 +08:00
#if defined(_WIN32)
2021-12-25 10:04:45 +08:00
# include "glfw3ext.h"
2020-10-17 16:32:16 +08:00
#endif
2022-07-16 10:43:05 +08:00
#if AX_ICON_SET_SUPPORT
2021-12-25 10:04:45 +08:00
# include "platform/CCImage.h"
2022-07-16 10:43:05 +08:00
#endif /* AX_ICON_SET_SUPPORT */
2019-11-23 20:27:39 +08:00
#include "renderer/CCRenderer.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2020-09-04 17:58:34 +08:00
class GLFWEventHandler
{
public:
static void onGLFWError(int errorID, const char* errorDesc)
{
if (_view)
_view->onGLFWError(errorID, errorDesc);
}
static void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
{
if (_view)
_view->onGLFWMouseCallBack(window, button, action, modify);
}
static void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
{
if (_view)
_view->onGLFWMouseMoveCallBack(window, x, y);
}
static void onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y)
{
if (_view)
_view->onGLFWMouseScrollCallback(window, x, y);
}
static void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (_view)
_view->onGLFWKeyCallback(window, key, scancode, action, mods);
}
static void onGLFWCharCallback(GLFWwindow* window, unsigned int character)
{
if (_view)
_view->onGLFWCharCallback(window, character);
}
static void onGLFWWindowPosCallback(GLFWwindow* windows, int x, int y)
{
if (_view)
_view->onGLFWWindowPosCallback(windows, x, y);
}
// Notes: Unused on windows or macos Metal renderer backend
// static void onGLFWframebufferSize(GLFWwindow* window, int w, int h)
// {
// if (_view)
// _view->onGLFWframebufferSize(window, w, h);
// }
2021-12-25 10:04:45 +08:00
static void onGLFWWindowSizeCallback(GLFWwindow* window, int width, int height)
2020-09-04 17:58:34 +08:00
{
if (_view)
_view->onGLFWWindowSizeCallback(window, width, height);
}
2021-12-25 10:04:45 +08:00
static void setGLViewImpl(GLViewImpl* view) { _view = view; }
2020-09-04 17:58:34 +08:00
static void onGLFWWindowIconifyCallback(GLFWwindow* window, int iconified)
{
if (_view)
{
_view->onGLFWWindowIconifyCallback(window, iconified);
}
}
static void onGLFWWindowFocusCallback(GLFWwindow* window, int focused)
{
if (_view)
{
_view->onGLFWWindowFocusCallback(window, focused);
}
}
private:
static GLViewImpl* _view;
};
GLViewImpl* GLFWEventHandler::_view = nullptr;
2021-12-25 10:04:45 +08:00
const std::string GLViewImpl::EVENT_WINDOW_RESIZED = "glview_window_resized";
const std::string GLViewImpl::EVENT_WINDOW_FOCUSED = "glview_window_focused";
2019-11-23 20:27:39 +08:00
const std::string GLViewImpl::EVENT_WINDOW_UNFOCUSED = "glview_window_unfocused";
////////////////////////////////////////////////////
struct keyCodeItem
{
int glfwKeyCode;
EventKeyboard::KeyCode keyCode;
};
static std::unordered_map<int, EventKeyboard::KeyCode> g_keyCodeMap;
static keyCodeItem g_keyCodeStructArray[] = {
/* The unknown key */
2021-12-25 10:04:45 +08:00
{GLFW_KEY_UNKNOWN, EventKeyboard::KeyCode::KEY_NONE},
2019-11-23 20:27:39 +08:00
/* Printable keys */
2021-12-25 10:04:45 +08:00
{GLFW_KEY_SPACE, EventKeyboard::KeyCode::KEY_SPACE},
{GLFW_KEY_APOSTROPHE, EventKeyboard::KeyCode::KEY_APOSTROPHE},
{GLFW_KEY_COMMA, EventKeyboard::KeyCode::KEY_COMMA},
{GLFW_KEY_MINUS, EventKeyboard::KeyCode::KEY_MINUS},
{GLFW_KEY_PERIOD, EventKeyboard::KeyCode::KEY_PERIOD},
{GLFW_KEY_SLASH, EventKeyboard::KeyCode::KEY_SLASH},
{GLFW_KEY_0, EventKeyboard::KeyCode::KEY_0},
{GLFW_KEY_1, EventKeyboard::KeyCode::KEY_1},
{GLFW_KEY_2, EventKeyboard::KeyCode::KEY_2},
{GLFW_KEY_3, EventKeyboard::KeyCode::KEY_3},
{GLFW_KEY_4, EventKeyboard::KeyCode::KEY_4},
{GLFW_KEY_5, EventKeyboard::KeyCode::KEY_5},
{GLFW_KEY_6, EventKeyboard::KeyCode::KEY_6},
{GLFW_KEY_7, EventKeyboard::KeyCode::KEY_7},
{GLFW_KEY_8, EventKeyboard::KeyCode::KEY_8},
{GLFW_KEY_9, EventKeyboard::KeyCode::KEY_9},
{GLFW_KEY_SEMICOLON, EventKeyboard::KeyCode::KEY_SEMICOLON},
{GLFW_KEY_EQUAL, EventKeyboard::KeyCode::KEY_EQUAL},
{GLFW_KEY_A, EventKeyboard::KeyCode::KEY_A},
{GLFW_KEY_B, EventKeyboard::KeyCode::KEY_B},
{GLFW_KEY_C, EventKeyboard::KeyCode::KEY_C},
{GLFW_KEY_D, EventKeyboard::KeyCode::KEY_D},
{GLFW_KEY_E, EventKeyboard::KeyCode::KEY_E},
{GLFW_KEY_F, EventKeyboard::KeyCode::KEY_F},
{GLFW_KEY_G, EventKeyboard::KeyCode::KEY_G},
{GLFW_KEY_H, EventKeyboard::KeyCode::KEY_H},
{GLFW_KEY_I, EventKeyboard::KeyCode::KEY_I},
{GLFW_KEY_J, EventKeyboard::KeyCode::KEY_J},
{GLFW_KEY_K, EventKeyboard::KeyCode::KEY_K},
{GLFW_KEY_L, EventKeyboard::KeyCode::KEY_L},
{GLFW_KEY_M, EventKeyboard::KeyCode::KEY_M},
{GLFW_KEY_N, EventKeyboard::KeyCode::KEY_N},
{GLFW_KEY_O, EventKeyboard::KeyCode::KEY_O},
{GLFW_KEY_P, EventKeyboard::KeyCode::KEY_P},
{GLFW_KEY_Q, EventKeyboard::KeyCode::KEY_Q},
{GLFW_KEY_R, EventKeyboard::KeyCode::KEY_R},
{GLFW_KEY_S, EventKeyboard::KeyCode::KEY_S},
{GLFW_KEY_T, EventKeyboard::KeyCode::KEY_T},
{GLFW_KEY_U, EventKeyboard::KeyCode::KEY_U},
{GLFW_KEY_V, EventKeyboard::KeyCode::KEY_V},
{GLFW_KEY_W, EventKeyboard::KeyCode::KEY_W},
{GLFW_KEY_X, EventKeyboard::KeyCode::KEY_X},
{GLFW_KEY_Y, EventKeyboard::KeyCode::KEY_Y},
{GLFW_KEY_Z, EventKeyboard::KeyCode::KEY_Z},
{GLFW_KEY_LEFT_BRACKET, EventKeyboard::KeyCode::KEY_LEFT_BRACKET},
{GLFW_KEY_BACKSLASH, EventKeyboard::KeyCode::KEY_BACK_SLASH},
{GLFW_KEY_RIGHT_BRACKET, EventKeyboard::KeyCode::KEY_RIGHT_BRACKET},
{GLFW_KEY_GRAVE_ACCENT, EventKeyboard::KeyCode::KEY_GRAVE},
{GLFW_KEY_WORLD_1, EventKeyboard::KeyCode::KEY_GRAVE},
{GLFW_KEY_WORLD_2, EventKeyboard::KeyCode::KEY_NONE},
2019-11-23 20:27:39 +08:00
/* Function keys */
2021-12-25 10:04:45 +08:00
{GLFW_KEY_ESCAPE, EventKeyboard::KeyCode::KEY_ESCAPE},
{GLFW_KEY_ENTER, EventKeyboard::KeyCode::KEY_ENTER},
{GLFW_KEY_TAB, EventKeyboard::KeyCode::KEY_TAB},
{GLFW_KEY_BACKSPACE, EventKeyboard::KeyCode::KEY_BACKSPACE},
{GLFW_KEY_INSERT, EventKeyboard::KeyCode::KEY_INSERT},
{GLFW_KEY_DELETE, EventKeyboard::KeyCode::KEY_DELETE},
{GLFW_KEY_RIGHT, EventKeyboard::KeyCode::KEY_RIGHT_ARROW},
{GLFW_KEY_LEFT, EventKeyboard::KeyCode::KEY_LEFT_ARROW},
{GLFW_KEY_DOWN, EventKeyboard::KeyCode::KEY_DOWN_ARROW},
{GLFW_KEY_UP, EventKeyboard::KeyCode::KEY_UP_ARROW},
{GLFW_KEY_PAGE_UP, EventKeyboard::KeyCode::KEY_PG_UP},
{GLFW_KEY_PAGE_DOWN, EventKeyboard::KeyCode::KEY_PG_DOWN},
{GLFW_KEY_HOME, EventKeyboard::KeyCode::KEY_HOME},
{GLFW_KEY_END, EventKeyboard::KeyCode::KEY_END},
{GLFW_KEY_CAPS_LOCK, EventKeyboard::KeyCode::KEY_CAPS_LOCK},
{GLFW_KEY_SCROLL_LOCK, EventKeyboard::KeyCode::KEY_SCROLL_LOCK},
{GLFW_KEY_NUM_LOCK, EventKeyboard::KeyCode::KEY_NUM_LOCK},
{GLFW_KEY_PRINT_SCREEN, EventKeyboard::KeyCode::KEY_PRINT},
{GLFW_KEY_PAUSE, EventKeyboard::KeyCode::KEY_PAUSE},
{GLFW_KEY_F1, EventKeyboard::KeyCode::KEY_F1},
{GLFW_KEY_F2, EventKeyboard::KeyCode::KEY_F2},
{GLFW_KEY_F3, EventKeyboard::KeyCode::KEY_F3},
{GLFW_KEY_F4, EventKeyboard::KeyCode::KEY_F4},
{GLFW_KEY_F5, EventKeyboard::KeyCode::KEY_F5},
{GLFW_KEY_F6, EventKeyboard::KeyCode::KEY_F6},
{GLFW_KEY_F7, EventKeyboard::KeyCode::KEY_F7},
{GLFW_KEY_F8, EventKeyboard::KeyCode::KEY_F8},
{GLFW_KEY_F9, EventKeyboard::KeyCode::KEY_F9},
{GLFW_KEY_F10, EventKeyboard::KeyCode::KEY_F10},
{GLFW_KEY_F11, EventKeyboard::KeyCode::KEY_F11},
{GLFW_KEY_F12, EventKeyboard::KeyCode::KEY_F12},
{GLFW_KEY_F13, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F14, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F15, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F16, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F17, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F18, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F19, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F20, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F21, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F22, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F23, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F24, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_F25, EventKeyboard::KeyCode::KEY_NONE},
{GLFW_KEY_KP_0, EventKeyboard::KeyCode::KEY_0},
{GLFW_KEY_KP_1, EventKeyboard::KeyCode::KEY_1},
{GLFW_KEY_KP_2, EventKeyboard::KeyCode::KEY_2},
{GLFW_KEY_KP_3, EventKeyboard::KeyCode::KEY_3},
{GLFW_KEY_KP_4, EventKeyboard::KeyCode::KEY_4},
{GLFW_KEY_KP_5, EventKeyboard::KeyCode::KEY_5},
{GLFW_KEY_KP_6, EventKeyboard::KeyCode::KEY_6},
{GLFW_KEY_KP_7, EventKeyboard::KeyCode::KEY_7},
{GLFW_KEY_KP_8, EventKeyboard::KeyCode::KEY_8},
{GLFW_KEY_KP_9, EventKeyboard::KeyCode::KEY_9},
{GLFW_KEY_KP_DECIMAL, EventKeyboard::KeyCode::KEY_PERIOD},
{GLFW_KEY_KP_DIVIDE, EventKeyboard::KeyCode::KEY_KP_DIVIDE},
{GLFW_KEY_KP_MULTIPLY, EventKeyboard::KeyCode::KEY_KP_MULTIPLY},
{GLFW_KEY_KP_SUBTRACT, EventKeyboard::KeyCode::KEY_KP_MINUS},
{GLFW_KEY_KP_ADD, EventKeyboard::KeyCode::KEY_KP_PLUS},
{GLFW_KEY_KP_ENTER, EventKeyboard::KeyCode::KEY_KP_ENTER},
{GLFW_KEY_KP_EQUAL, EventKeyboard::KeyCode::KEY_EQUAL},
{GLFW_KEY_LEFT_SHIFT, EventKeyboard::KeyCode::KEY_LEFT_SHIFT},
{GLFW_KEY_LEFT_CONTROL, EventKeyboard::KeyCode::KEY_LEFT_CTRL},
{GLFW_KEY_LEFT_ALT, EventKeyboard::KeyCode::KEY_LEFT_ALT},
{GLFW_KEY_LEFT_SUPER, EventKeyboard::KeyCode::KEY_HYPER},
{GLFW_KEY_RIGHT_SHIFT, EventKeyboard::KeyCode::KEY_RIGHT_SHIFT},
{GLFW_KEY_RIGHT_CONTROL, EventKeyboard::KeyCode::KEY_RIGHT_CTRL},
{GLFW_KEY_RIGHT_ALT, EventKeyboard::KeyCode::KEY_RIGHT_ALT},
{GLFW_KEY_RIGHT_SUPER, EventKeyboard::KeyCode::KEY_HYPER},
{GLFW_KEY_MENU, EventKeyboard::KeyCode::KEY_MENU},
{GLFW_KEY_LAST, EventKeyboard::KeyCode::KEY_NONE}};
2019-11-23 20:27:39 +08:00
//////////////////////////////////////////////////////////////////////////
// implement GLViewImpl
//////////////////////////////////////////////////////////////////////////
GLViewImpl::GLViewImpl(bool initglfw)
2021-12-25 10:04:45 +08:00
: _captured(false)
, _isInRetinaMonitor(false)
, _isRetinaEnabled(false)
, _retinaFactor(1)
, _frameZoomFactor(1.0f)
, _mainWindow(nullptr)
, _monitor(nullptr)
, _mouseX(0.0f)
, _mouseY(0.0f)
2019-11-23 20:27:39 +08:00
{
_viewName = "AX_10";
2019-11-23 20:27:39 +08:00
g_keyCodeMap.clear();
for (auto& item : g_keyCodeStructArray)
{
g_keyCodeMap[item.glfwKeyCode] = item.keyCode;
}
GLFWEventHandler::setGLViewImpl(this);
if (initglfw)
{
glfwSetErrorCallback(GLFWEventHandler::onGLFWError);
2022-07-16 10:43:05 +08:00
#if defined(AX_USE_GLES) && GLFW_VERSION_MAJOR >= 3 && GLFW_VERSION_MINOR >= 4
2021-12-25 10:04:45 +08:00
glfwInitHint(GLFW_ANGLE_PLATFORM_TYPE, GLFW_ANGLE_PLATFORM_TYPE_D3D11); // since glfw-3.4
2020-07-03 11:08:39 +08:00
#endif
2020-10-17 16:32:16 +08:00
#if defined(_WIN32)
glfwxInit();
#else
2019-11-23 20:27:39 +08:00
glfwInit();
2020-10-17 16:32:16 +08:00
#endif
2019-11-23 20:27:39 +08:00
}
}
GLViewImpl::~GLViewImpl()
{
2022-07-16 10:43:05 +08:00
AXLOGINFO("deallocing GLViewImpl: %p", this);
2019-11-23 20:27:39 +08:00
GLFWEventHandler::setGLViewImpl(nullptr);
2020-10-17 16:32:16 +08:00
#if defined(_WIN32)
glfwxTerminate();
#else
2019-11-23 20:27:39 +08:00
glfwTerminate();
2020-10-17 16:32:16 +08:00
#endif
2019-11-23 20:27:39 +08:00
}
GLViewImpl* GLViewImpl::create(std::string_view viewName)
2019-11-23 20:27:39 +08:00
{
return GLViewImpl::create(viewName, false);
}
GLViewImpl* GLViewImpl::create(std::string_view viewName, bool resizable)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new GLViewImpl;
2021-12-25 10:04:45 +08:00
if (ret->initWithRect(viewName, Rect(0, 0, 960, 640), 1.0f, resizable))
{
2019-11-23 20:27:39 +08:00
ret->autorelease();
return ret;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
return nullptr;
}
GLViewImpl* GLViewImpl::createWithRect(std::string_view viewName, Rect rect, float frameZoomFactor, bool resizable)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new GLViewImpl;
2021-12-25 10:04:45 +08:00
if (ret->initWithRect(viewName, rect, frameZoomFactor, resizable))
{
2019-11-23 20:27:39 +08:00
ret->autorelease();
return ret;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
return nullptr;
}
GLViewImpl* GLViewImpl::createWithFullScreen(std::string_view viewName)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new GLViewImpl();
2021-12-25 10:04:45 +08:00
if (ret->initWithFullScreen(viewName))
{
2019-11-23 20:27:39 +08:00
ret->autorelease();
return ret;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
return nullptr;
}
GLViewImpl* GLViewImpl::createWithFullScreen(std::string_view viewName,
2021-12-25 10:04:45 +08:00
const GLFWvidmode& videoMode,
GLFWmonitor* monitor)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new GLViewImpl();
2021-12-25 10:04:45 +08:00
if (ret->initWithFullscreen(viewName, videoMode, monitor))
{
2019-11-23 20:27:39 +08:00
ret->autorelease();
return ret;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
return nullptr;
}
bool GLViewImpl::initWithRect(std::string_view viewName, Rect rect, float frameZoomFactor, bool resizable)
2019-11-23 20:27:39 +08:00
{
setViewName(viewName);
_frameZoomFactor = frameZoomFactor;
2022-07-16 10:43:05 +08:00
#if defined(AX_USE_GLES)
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
#endif
2021-12-25 10:04:45 +08:00
glfwWindowHint(GLFW_RESIZABLE, resizable ? GL_TRUE : GL_FALSE);
glfwWindowHint(GLFW_RED_BITS, _glContextAttrs.redBits);
glfwWindowHint(GLFW_GREEN_BITS, _glContextAttrs.greenBits);
glfwWindowHint(GLFW_BLUE_BITS, _glContextAttrs.blueBits);
glfwWindowHint(GLFW_ALPHA_BITS, _glContextAttrs.alphaBits);
glfwWindowHint(GLFW_DEPTH_BITS, _glContextAttrs.depthBits);
glfwWindowHint(GLFW_STENCIL_BITS, _glContextAttrs.stencilBits);
2019-11-23 20:27:39 +08:00
glfwWindowHint(GLFW_SAMPLES, _glContextAttrs.multisamplingCount);
2020-10-17 16:32:16 +08:00
glfwWindowHint(GLFW_VISIBLE, _glContextAttrs.visible);
glfwWindowHint(GLFW_DECORATED, _glContextAttrs.decorated);
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_MAC)
2019-11-23 20:27:39 +08:00
// Don't create gl context.
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
#endif
2021-12-25 10:04:45 +08:00
int neededWidth = (int)(rect.size.width * _frameZoomFactor);
2019-11-23 20:27:39 +08:00
int neededHeight = (int)(rect.size.height * _frameZoomFactor);
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_WIN32)
2020-10-17 16:32:16 +08:00
glfwxSetParent((HWND)_glContextAttrs.viewParent);
#endif
2019-11-23 20:27:39 +08:00
_mainWindow = glfwCreateWindow(neededWidth, neededHeight, _viewName.c_str(), _monitor, nullptr);
if (_mainWindow == nullptr)
{
std::string message = "Can't create window";
if (!_glfwError.empty())
{
message.append("\nMore info: \n");
message.append(_glfwError);
}
ccMessageBox(message.c_str(), "Error launch application");
2021-12-25 10:04:45 +08:00
utils::killCurrentProcess(); // kill current process, don't cause crash when driver issue.
2019-11-23 20:27:39 +08:00
return false;
}
/*
2021-12-25 10:04:45 +08:00
* Note that the created window and context may differ from what you requested,
* as not all parameters and hints are
* [hard constraints](@ref window_hints_hard). This includes the size of the
* window, especially for full screen windows. To retrieve the actual
* attributes of the created window and context, use queries like @ref
* glfwGetWindowAttrib and @ref glfwGetWindowSize.
*
* see declaration glfwCreateWindow
*/
2019-11-23 20:27:39 +08:00
int realW = 0, realH = 0;
glfwGetWindowSize(_mainWindow, &realW, &realH);
if (realW != neededWidth)
{
rect.size.width = realW / _frameZoomFactor;
}
if (realH != neededHeight)
{
rect.size.height = realH / _frameZoomFactor;
}
glfwMakeContextCurrent(_mainWindow);
glfwSetMouseButtonCallback(_mainWindow, GLFWEventHandler::onGLFWMouseCallBack);
glfwSetCursorPosCallback(_mainWindow, GLFWEventHandler::onGLFWMouseMoveCallBack);
glfwSetScrollCallback(_mainWindow, GLFWEventHandler::onGLFWMouseScrollCallback);
glfwSetCharCallback(_mainWindow, GLFWEventHandler::onGLFWCharCallback);
glfwSetKeyCallback(_mainWindow, GLFWEventHandler::onGLFWKeyCallback);
glfwSetWindowPosCallback(_mainWindow, GLFWEventHandler::onGLFWWindowPosCallback);
glfwSetWindowSizeCallback(_mainWindow, GLFWEventHandler::onGLFWWindowSizeCallback);
2019-11-23 20:27:39 +08:00
glfwSetWindowIconifyCallback(_mainWindow, GLFWEventHandler::onGLFWWindowIconifyCallback);
glfwSetWindowFocusCallback(_mainWindow, GLFWEventHandler::onGLFWWindowFocusCallback);
setFrameSize(rect.size.width, rect.size.height);
loadGL();
2019-11-23 20:27:39 +08:00
// check OpenGL version at first
const GLubyte* glVersion = glGetString(GL_VERSION);
2021-12-25 10:04:45 +08:00
if (utils::atof((const char*)glVersion) < 1.5 && nullptr == strstr((const char*)glVersion, "ANGLE"))
2019-11-23 20:27:39 +08:00
{
char strComplain[256] = {0};
sprintf(strComplain,
"OpenGL 1.5 or higher is required (your version is %s). Please upgrade the driver of your video card.",
glVersion);
ccMessageBox(strComplain, "OpenGL version too old");
2021-12-25 10:04:45 +08:00
utils::killCurrentProcess(); // kill current process, don't cause crash when driver issue.
return false;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
// Will cause OpenGL error 0x0500 when use ANGLE-GLES on desktop
2022-07-16 10:43:05 +08:00
#if defined(AX_USE_GL)
2019-11-23 20:27:39 +08:00
// Enable point size by default.
2021-12-25 10:04:45 +08:00
# if defined(GL_VERSION_2_0)
2019-11-23 20:27:39 +08:00
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
2021-12-25 10:04:45 +08:00
# else
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
2021-12-25 10:04:45 +08:00
# endif
if (_glContextAttrs.multisamplingCount > 0)
2019-11-23 20:27:39 +08:00
glEnable(GL_MULTISAMPLE);
#endif
CHECK_GL_ERROR_DEBUG();
2022-07-16 10:43:05 +08:00
#if AX_TARGET_PLATFORM == AX_PLATFORM_WIN32 || AX_TARGET_PLATFORM == AX_PLATFORM_LINUX
2022-07-01 17:55:54 +08:00
glfwSwapInterval(_glContextAttrs.vsync ? 1 : 0);
2020-10-17 16:32:16 +08:00
#endif
2021-12-25 10:04:45 +08:00
// // GLFW v3.2 no longer emits "onGLFWWindowSizeFunCallback" at creation time. Force default viewport:
// setViewPortInPoints(0, 0, neededWidth, neededHeight);
//
2019-11-23 20:27:39 +08:00
return true;
}
bool GLViewImpl::initWithFullScreen(std::string_view viewName)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// Create fullscreen window on primary monitor at its current video mode.
2019-11-23 20:27:39 +08:00
_monitor = glfwGetPrimaryMonitor();
if (nullptr == _monitor)
return false;
const GLFWvidmode* videoMode = glfwGetVideoMode(_monitor);
return initWithRect(viewName, Rect(0, 0, (float)videoMode->width, (float)videoMode->height), 1.0f, false);
}
bool GLViewImpl::initWithFullscreen(std::string_view viewname, const GLFWvidmode& videoMode, GLFWmonitor* monitor)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// Create fullscreen on specified monitor at the specified video mode.
2019-11-23 20:27:39 +08:00
_monitor = monitor;
if (nullptr == _monitor)
return false;
2021-12-25 10:04:45 +08:00
// These are soft constraints. If the video mode is retrieved at runtime, the resulting window and context should
// match these exactly. If invalid attribs are passed (eg. from an outdated cache), window creation will NOT fail
// but the actual window/context may differ.
2019-11-23 20:27:39 +08:00
glfwWindowHint(GLFW_REFRESH_RATE, videoMode.refreshRate);
glfwWindowHint(GLFW_RED_BITS, videoMode.redBits);
glfwWindowHint(GLFW_BLUE_BITS, videoMode.blueBits);
glfwWindowHint(GLFW_GREEN_BITS, videoMode.greenBits);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return initWithRect(viewname, Rect(0, 0, (float)videoMode.width, (float)videoMode.height), 1.0f, false);
}
bool GLViewImpl::isOpenGLReady()
{
return nullptr != _mainWindow;
}
void GLViewImpl::end()
{
2021-12-25 10:04:45 +08:00
if (_mainWindow)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
glfwSetWindowShouldClose(_mainWindow, 1);
2019-11-23 20:27:39 +08:00
_mainWindow = nullptr;
}
// Release self. Otherwise, GLViewImpl could not be freed.
release();
}
void GLViewImpl::swapBuffers()
{
2021-12-25 10:04:45 +08:00
if (_mainWindow)
2019-11-23 20:27:39 +08:00
glfwSwapBuffers(_mainWindow);
}
bool GLViewImpl::windowShouldClose()
{
2021-12-25 10:04:45 +08:00
if (_mainWindow)
2019-11-23 20:27:39 +08:00
return glfwWindowShouldClose(_mainWindow) ? true : false;
else
return true;
}
void GLViewImpl::pollEvents()
{
glfwPollEvents();
}
void GLViewImpl::enableRetina(bool enabled)
2021-12-25 10:04:45 +08:00
{ // official v4 comment follow sources
2022-07-16 10:43:05 +08:00
// #if (AX_TARGET_PLATFORM == AX_PLATFORM_MAC)
// _isRetinaEnabled = enabled;
// if (_isRetinaEnabled)
// {
// _retinaFactor = 1;
// }
// else
// {
// _retinaFactor = 2;
// }
// updateFrameSize();
// #endif
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void GLViewImpl::setIMEKeyboardState(bool /*bOpen*/) {}
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#if AX_ICON_SET_SUPPORT
void GLViewImpl::setIcon(std::string_view filename) const
2021-12-25 10:04:45 +08:00
{
this->setIcon(std::vector<std::string_view>{filename});
2019-11-23 20:27:39 +08:00
}
void GLViewImpl::setIcon(const std::vector<std::string_view>& filelist) const
2021-12-25 10:04:45 +08:00
{
if (filelist.empty())
return;
2019-11-23 20:27:39 +08:00
std::vector<Image*> icons;
2021-12-25 10:04:45 +08:00
for (auto const& filename : filelist)
{
2021-12-08 00:11:53 +08:00
Image* icon = new Image();
2021-12-25 10:04:45 +08:00
if (icon->initWithImageFile(filename))
{
2019-11-23 20:27:39 +08:00
icons.push_back(icon);
2021-12-25 10:04:45 +08:00
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(icon);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
if (icons.empty())
return; // No valid images
2019-11-23 20:27:39 +08:00
size_t iconsCount = icons.size();
2021-12-25 10:04:45 +08:00
auto images = new GLFWimage[iconsCount];
for (size_t i = 0; i < iconsCount; i++)
{
auto& image = images[i];
auto& icon = icons[i];
image.width = icon->getWidth();
2019-11-23 20:27:39 +08:00
image.height = icon->getHeight();
image.pixels = icon->getData();
};
GLFWwindow* window = this->getWindow();
glfwSetWindowIcon(window, iconsCount, images);
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE_ARRAY(images);
2021-12-25 10:04:45 +08:00
for (auto& icon : icons)
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(icon);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
void GLViewImpl::setDefaultIcon() const
{
2019-11-23 20:27:39 +08:00
GLFWwindow* window = this->getWindow();
glfwSetWindowIcon(window, 0, nullptr);
}
2022-07-16 10:43:05 +08:00
#endif /* AX_ICON_SET_SUPPORT */
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void GLViewImpl::setCursorVisible(bool isVisible)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (_mainWindow == NULL)
2019-11-23 20:27:39 +08:00
return;
2021-12-25 10:04:45 +08:00
if (isVisible)
2019-11-23 20:27:39 +08:00
glfwSetInputMode(_mainWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
else
glfwSetInputMode(_mainWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
void GLViewImpl::setFrameZoomFactor(float zoomFactor)
{
2022-07-16 10:43:05 +08:00
AXASSERT(zoomFactor > 0.0f, "zoomFactor must be larger than 0");
2019-11-23 20:27:39 +08:00
if (std::abs(_frameZoomFactor - zoomFactor) < FLT_EPSILON)
{
return;
}
_frameZoomFactor = zoomFactor;
updateFrameSize();
}
float GLViewImpl::getFrameZoomFactor() const
{
return _frameZoomFactor;
}
2021-12-25 10:04:45 +08:00
bool GLViewImpl::isFullscreen() const
{
2019-11-23 20:27:39 +08:00
return (_monitor != nullptr);
}
void GLViewImpl::setFullscreen()
{
setFullscreen(-1, -1, -1);
}
2021-12-25 10:04:45 +08:00
void GLViewImpl::setFullscreen(int w, int h, int refreshRate)
{
auto monitor = glfwGetPrimaryMonitor();
2021-12-25 10:04:45 +08:00
if (nullptr == monitor || monitor == _monitor)
{
return;
}
this->setFullscreen(monitor, w, h, refreshRate);
2019-11-23 20:27:39 +08:00
}
void GLViewImpl::setFullscreen(int monitorIndex)
{
setFullscreen(monitorIndex, -1, -1, -1);
}
2021-12-25 10:04:45 +08:00
void GLViewImpl::setFullscreen(int monitorIndex, int w, int h, int refreshRate)
{
int count = 0;
2019-11-23 20:27:39 +08:00
GLFWmonitor** monitors = glfwGetMonitors(&count);
2021-12-25 10:04:45 +08:00
if (monitorIndex < 0 || monitorIndex >= count)
{
2019-11-23 20:27:39 +08:00
return;
}
GLFWmonitor* monitor = monitors[monitorIndex];
2021-12-25 10:04:45 +08:00
if (nullptr == monitor || _monitor == monitor)
{
2019-11-23 20:27:39 +08:00
return;
}
this->setFullscreen(monitor, w, h, refreshRate);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void GLViewImpl::setFullscreen(GLFWmonitor* monitor, int w, int h, int refreshRate)
{
2019-11-23 20:27:39 +08:00
_monitor = monitor;
const GLFWvidmode* videoMode = glfwGetVideoMode(_monitor);
if (w == -1)
w = videoMode->width;
if (h == -1)
h = videoMode->height;
if (refreshRate == -1)
refreshRate = videoMode->refreshRate;
glfwSetWindowMonitor(_mainWindow, _monitor, 0, 0, w, h, refreshRate);
updateWindowSize();
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void GLViewImpl::setWindowed(int width, int height)
{
if (!this->isFullscreen())
{
2019-11-23 20:27:39 +08:00
this->setFrameSize((float)width, (float)height);
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
const GLFWvidmode* videoMode = glfwGetVideoMode(_monitor);
int xpos = 0, ypos = 0;
glfwGetMonitorPos(_monitor, &xpos, &ypos);
xpos += (int)((videoMode->width - width) * 0.5f);
ypos += (int)((videoMode->height - height) * 0.5f);
_monitor = nullptr;
glfwSetWindowMonitor(_mainWindow, nullptr, xpos, ypos, width, height, GLFW_DONT_CARE);
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_MAC)
2019-11-23 20:27:39 +08:00
// on mac window will sometimes lose title when windowed
glfwSetWindowTitle(_mainWindow, _viewName.c_str());
#endif
updateWindowSize();
2019-11-23 20:27:39 +08:00
}
}
void GLViewImpl::updateWindowSize()
{
int w = 0, h = 0;
glfwGetFramebufferSize(_mainWindow, &w, &h);
2021-12-25 10:04:45 +08:00
int frameWidth = w / _frameZoomFactor;
int frameHeight = h / _frameZoomFactor;
setFrameSize(frameWidth, frameHeight);
updateDesignResolutionSize();
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_RESIZED, nullptr);
}
2021-12-25 10:04:45 +08:00
int GLViewImpl::getMonitorCount() const
{
2019-11-23 20:27:39 +08:00
int count = 0;
glfwGetMonitors(&count);
return count;
}
2021-12-25 10:04:45 +08:00
Vec2 GLViewImpl::getMonitorSize() const
{
2019-11-23 20:27:39 +08:00
GLFWmonitor* monitor = _monitor;
2021-12-25 10:04:45 +08:00
if (nullptr == monitor)
{
2019-11-23 20:27:39 +08:00
GLFWwindow* window = this->getWindow();
2021-12-25 10:04:45 +08:00
monitor = glfwGetWindowMonitor(window);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
if (nullptr == monitor)
{
2019-11-23 20:27:39 +08:00
monitor = glfwGetPrimaryMonitor();
}
2021-12-25 10:04:45 +08:00
if (nullptr != monitor)
{
2019-11-23 20:27:39 +08:00
const GLFWvidmode* videoMode = glfwGetVideoMode(monitor);
2021-12-25 10:04:45 +08:00
Vec2 size = Vec2((float)videoMode->width, (float)videoMode->height);
2019-11-23 20:27:39 +08:00
return size;
}
2021-10-23 23:27:14 +08:00
return Vec2::ZERO;
2019-11-23 20:27:39 +08:00
}
void GLViewImpl::updateFrameSize()
{
if (_screenSize.width > 0 && _screenSize.height > 0)
{
int w = 0, h = 0;
glfwGetWindowSize(_mainWindow, &w, &h);
int frameBufferW = 0, frameBufferH = 0;
glfwGetFramebufferSize(_mainWindow, &frameBufferW, &frameBufferH);
if (frameBufferW == 2 * w && frameBufferH == 2 * h)
{
if (_isRetinaEnabled)
{
_retinaFactor = 1;
}
else
{
_retinaFactor = 2;
}
2021-12-25 10:04:45 +08:00
glfwSetWindowSize(_mainWindow, _screenSize.width / 2 * _retinaFactor * _frameZoomFactor,
_screenSize.height / 2 * _retinaFactor * _frameZoomFactor);
_isInRetinaMonitor = true;
}
else
2019-11-23 20:27:39 +08:00
{
if (_isInRetinaMonitor)
{
_retinaFactor = 1;
}
2021-12-25 10:04:45 +08:00
glfwSetWindowSize(_mainWindow, (int)(_screenSize.width * _retinaFactor * _frameZoomFactor),
(int)(_screenSize.height * _retinaFactor * _frameZoomFactor));
2019-11-23 20:27:39 +08:00
_isInRetinaMonitor = false;
}
}
}
void GLViewImpl::setFrameSize(float width, float height)
{
GLView::setFrameSize(width, height);
updateFrameSize();
}
2021-12-25 10:04:45 +08:00
void GLViewImpl::setViewPortInPoints(float x, float y, float w, float h)
2019-11-23 20:27:39 +08:00
{
Viewport vp;
2021-12-25 10:04:45 +08:00
vp.x = (int)(x * _scaleX * _retinaFactor * _frameZoomFactor +
_viewPortRect.origin.x * _retinaFactor * _frameZoomFactor);
vp.y = (int)(y * _scaleY * _retinaFactor * _frameZoomFactor +
_viewPortRect.origin.y * _retinaFactor * _frameZoomFactor);
2019-11-23 20:27:39 +08:00
vp.w = (unsigned int)(w * _scaleX * _retinaFactor * _frameZoomFactor);
vp.h = (unsigned int)(h * _scaleY * _retinaFactor * _frameZoomFactor);
Camera::setDefaultViewport(vp);
}
2021-12-25 10:04:45 +08:00
void GLViewImpl::setScissorInPoints(float x, float y, float w, float h)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto x1 = (int)(x * _scaleX * _retinaFactor * _frameZoomFactor +
_viewPortRect.origin.x * _retinaFactor * _frameZoomFactor);
auto y1 = (int)(y * _scaleY * _retinaFactor * _frameZoomFactor +
_viewPortRect.origin.y * _retinaFactor * _frameZoomFactor);
auto width1 = (unsigned int)(w * _scaleX * _retinaFactor * _frameZoomFactor);
auto height1 = (unsigned int)(h * _scaleY * _retinaFactor * _frameZoomFactor);
2019-11-23 20:27:39 +08:00
auto renderer = Director::getInstance()->getRenderer();
renderer->setScissorRect(x1, y1, width1, height1);
}
Rect GLViewImpl::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
2021-12-25 10:04:45 +08:00
float x = (rect.x - _viewPortRect.origin.x * _retinaFactor * _frameZoomFactor) /
(_scaleX * _retinaFactor * _frameZoomFactor);
float y = (rect.y - _viewPortRect.origin.y * _retinaFactor * _frameZoomFactor) /
(_scaleY * _retinaFactor * _frameZoomFactor);
2019-11-23 20:27:39 +08:00
float w = rect.width / (_scaleX * _retinaFactor * _frameZoomFactor);
2021-12-25 10:04:45 +08:00
float h = rect.height / (_scaleY * _retinaFactor * _frameZoomFactor);
2019-11-23 20:27:39 +08:00
return Rect(x, y, w, h);
}
void GLViewImpl::onGLFWError(int errorID, const char* errorDesc)
{
if (_mainWindow)
{
_glfwError = StringUtils::format("GLFWError #%d Happen, %s", errorID, errorDesc);
}
else
{
_glfwError.append(StringUtils::format("GLFWError #%d Happen, %s\n", errorID, errorDesc));
}
2022-07-16 10:43:05 +08:00
AXLOGERROR("%s", _glfwError.c_str());
2019-11-23 20:27:39 +08:00
}
void GLViewImpl::onGLFWMouseCallBack(GLFWwindow* /*window*/, int button, int action, int /*modify*/)
{
2021-12-25 10:04:45 +08:00
if (GLFW_MOUSE_BUTTON_LEFT == button)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (GLFW_PRESS == action)
2019-11-23 20:27:39 +08:00
{
_captured = true;
2021-12-25 10:04:45 +08:00
if (this->getViewPortRect().equals(Rect::ZERO) ||
this->getViewPortRect().containsPoint(Vec2(_mouseX, _mouseY)))
2019-11-23 20:27:39 +08:00
{
intptr_t id = 0;
this->handleTouchesBegin(1, &id, &_mouseX, &_mouseY);
}
}
2021-12-25 10:04:45 +08:00
else if (GLFW_RELEASE == action)
2019-11-23 20:27:39 +08:00
{
if (_captured)
{
2021-12-25 10:04:45 +08:00
_captured = false;
2019-11-23 20:27:39 +08:00
intptr_t id = 0;
this->handleTouchesEnd(1, &id, &_mouseX, &_mouseY);
}
}
}
2021-12-25 10:04:45 +08:00
// Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
2019-11-23 20:27:39 +08:00
float cursorX = (_mouseX - _viewPortRect.origin.x) / _scaleX;
float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - _mouseY) / _scaleY;
2021-12-25 10:04:45 +08:00
if (GLFW_PRESS == action)
2019-11-23 20:27:39 +08:00
{
EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN);
event.setCursorPosition(cursorX, cursorY);
event.setMouseButton(static_cast<axis::EventMouse::MouseButton>(button));
2019-11-23 20:27:39 +08:00
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
}
2021-12-25 10:04:45 +08:00
else if (GLFW_RELEASE == action)
2019-11-23 20:27:39 +08:00
{
EventMouse event(EventMouse::MouseEventType::MOUSE_UP);
event.setCursorPosition(cursorX, cursorY);
event.setMouseButton(static_cast<axis::EventMouse::MouseButton>(button));
2019-11-23 20:27:39 +08:00
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
}
}
void GLViewImpl::onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
{
_mouseX = (float)x;
_mouseY = (float)y;
_mouseX /= this->getFrameZoomFactor();
_mouseY /= this->getFrameZoomFactor();
if (_isInRetinaMonitor)
{
if (_retinaFactor == 1)
{
_mouseX *= 2;
_mouseY *= 2;
}
}
if (_captured)
{
intptr_t id = 0;
this->handleTouchesMove(1, &id, &_mouseX, &_mouseY);
}
2021-12-25 10:04:45 +08:00
// Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
2019-11-23 20:27:39 +08:00
float cursorX = (_mouseX - _viewPortRect.origin.x) / _scaleX;
float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - _mouseY) / _scaleY;
EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE);
// Set current button
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
event.setMouseButton(static_cast<axis::EventMouse::MouseButton>(GLFW_MOUSE_BUTTON_LEFT));
2019-11-23 20:27:39 +08:00
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
{
event.setMouseButton(static_cast<axis::EventMouse::MouseButton>(GLFW_MOUSE_BUTTON_RIGHT));
2019-11-23 20:27:39 +08:00
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS)
{
event.setMouseButton(static_cast<axis::EventMouse::MouseButton>(GLFW_MOUSE_BUTTON_MIDDLE));
2019-11-23 20:27:39 +08:00
}
event.setCursorPosition(cursorX, cursorY);
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
}
void GLViewImpl::onGLFWMouseScrollCallback(GLFWwindow* /*window*/, double x, double y)
{
EventMouse event(EventMouse::MouseEventType::MOUSE_SCROLL);
2021-12-25 10:04:45 +08:00
// Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
2019-11-23 20:27:39 +08:00
float cursorX = (_mouseX - _viewPortRect.origin.x) / _scaleX;
float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - _mouseY) / _scaleY;
event.setScrollData((float)x, -(float)y);
event.setCursorPosition(cursorX, cursorY);
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
}
void GLViewImpl::onGLFWKeyCallback(GLFWwindow* /*window*/, int key, int /*scancode*/, int action, int /*mods*/)
{
// x-studio spec, for repeat press key support.
EventKeyboard event(g_keyCodeMap[key], action);
auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->dispatchEvent(&event);
2019-11-23 20:27:39 +08:00
if (GLFW_RELEASE != action)
{
switch (g_keyCodeMap[key])
{
case EventKeyboard::KeyCode::KEY_BACKSPACE:
IMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
break;
case EventKeyboard::KeyCode::KEY_HOME:
case EventKeyboard::KeyCode::KEY_KP_HOME:
case EventKeyboard::KeyCode::KEY_DELETE:
case EventKeyboard::KeyCode::KEY_KP_DELETE:
case EventKeyboard::KeyCode::KEY_END:
case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
case EventKeyboard::KeyCode::KEY_ESCAPE:
IMEDispatcher::sharedDispatcher()->dispatchControlKey(g_keyCodeMap[key]);
break;
default:
break;
}
}
}
void GLViewImpl::onGLFWCharCallback(GLFWwindow* /*window*/, unsigned int character)
{
2021-12-25 10:04:45 +08:00
char16_t wcharString[2] = {(char16_t)character, 0};
2019-11-23 20:27:39 +08:00
std::string utf8String;
2021-12-25 10:04:45 +08:00
StringUtils::UTF16ToUTF8(wcharString, utf8String);
2019-11-23 20:27:39 +08:00
static std::set<std::string> controlUnicode = {
2021-12-25 10:04:45 +08:00
"\xEF\x9C\x80", // up
"\xEF\x9C\x81", // down
"\xEF\x9C\x82", // left
"\xEF\x9C\x83", // right
"\xEF\x9C\xA8", // delete
"\xEF\x9C\xA9", // home
"\xEF\x9C\xAB", // end
"\xEF\x9C\xAC", // pageup
"\xEF\x9C\xAD", // pagedown
"\xEF\x9C\xB9" // clear
2019-11-23 20:27:39 +08:00
};
// Check for send control key
if (controlUnicode.find(utf8String) == controlUnicode.end())
{
2021-12-25 10:04:45 +08:00
IMEDispatcher::sharedDispatcher()->dispatchInsertText(utf8String.c_str(), utf8String.size());
2019-11-23 20:27:39 +08:00
}
}
void GLViewImpl::onGLFWWindowPosCallback(GLFWwindow* /*window*/, int /*x*/, int /*y*/)
{
Director::getInstance()->setViewport();
}
void GLViewImpl::onGLFWWindowSizeCallback(GLFWwindow* /*window*/, int w, int h)
2019-11-23 20:27:39 +08:00
{
if (w && h && _resolutionPolicy != ResolutionPolicy::UNKNOWN)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
/*
x-studio spec, fix view size incorrect when window size changed.
The original code behavior:
1. first time enter full screen: w,h=1920,1080
2. second or later enter full screen: will trigger 2 times WindowSizeCallback
1). w,h=976,679
2). w,h=1024,768
2021-12-25 10:04:45 +08:00
@remark: we should use glfwSetWindowMonitor to control the window size in full screen mode
@see also: updateWindowSize (call after enter/exit full screen mode)
*/
updateDesignResolutionSize();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_RESIZED, nullptr);
}
}
void GLViewImpl::onGLFWWindowIconifyCallback(GLFWwindow* /*window*/, int iconified)
{
if (iconified == GL_TRUE)
{
Application::getInstance()->applicationDidEnterBackground();
}
else
{
Application::getInstance()->applicationWillEnterForeground();
}
}
void GLViewImpl::onGLFWWindowFocusCallback(GLFWwindow* /*window*/, int focused)
{
if (focused == GL_TRUE)
{
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_FOCUSED, nullptr);
}
else
{
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_UNFOCUSED, nullptr);
}
}
2021-12-25 10:04:45 +08:00
static bool loadFboExtensions()
{
const char* gl_extensions = (const char*)glGetString(GL_EXTENSIONS);
2019-11-23 20:27:39 +08:00
// If the current opengl driver doesn't have framebuffers methods, check if an extension exists
if (glGenFramebuffers == nullptr)
{
log("OpenGL: glGenFramebuffers is nullptr, try to detect an extension");
if (strstr(gl_extensions, "ARB_framebuffer_object"))
{
log("OpenGL: ARB_framebuffer_object is supported");
2021-12-25 10:04:45 +08:00
glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC)glfwGetProcAddress("glIsRenderbuffer");
glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC)glfwGetProcAddress("glBindRenderbuffer");
glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC)glfwGetProcAddress("glDeleteRenderbuffers");
2021-12-25 10:04:45 +08:00
glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC)glfwGetProcAddress("glGenRenderbuffers");
glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC)glfwGetProcAddress("glRenderbufferStorage");
2021-12-25 10:04:45 +08:00
glGetRenderbufferParameteriv =
(PFNGLGETRENDERBUFFERPARAMETERIVPROC)glfwGetProcAddress("glGetRenderbufferParameteriv");
glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC)glfwGetProcAddress("glIsFramebuffer");
glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC)glfwGetProcAddress("glBindFramebuffer");
glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)glfwGetProcAddress("glDeleteFramebuffers");
glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)glfwGetProcAddress("glGenFramebuffers");
glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC)glfwGetProcAddress("glCheckFramebufferStatus");
2021-12-25 10:04:45 +08:00
glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DPROC)glfwGetProcAddress("glFramebufferTexture1D");
glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)glfwGetProcAddress("glFramebufferTexture2D");
glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC)glfwGetProcAddress("glFramebufferTexture3D");
glFramebufferRenderbuffer =
(PFNGLFRAMEBUFFERRENDERBUFFERPROC)glfwGetProcAddress("glFramebufferRenderbuffer");
glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC)glfwGetProcAddress(
"glGetFramebufferAttachmentParameteriv");
glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC)glfwGetProcAddress("glGenerateMipmap");
2019-11-23 20:27:39 +08:00
}
else if (strstr(gl_extensions, "EXT_framebuffer_object"))
2019-11-23 20:27:39 +08:00
{
log("OpenGL: EXT_framebuffer_object is supported");
2021-12-25 10:04:45 +08:00
glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC)glfwGetProcAddress("glIsRenderbufferEXT");
glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC)glfwGetProcAddress("glBindRenderbufferEXT");
glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC)glfwGetProcAddress("glDeleteRenderbuffersEXT");
2021-12-25 10:04:45 +08:00
glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC)glfwGetProcAddress("glGenRenderbuffersEXT");
glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC)glfwGetProcAddress("glRenderbufferStorageEXT");
2021-12-25 10:04:45 +08:00
glGetRenderbufferParameteriv =
(PFNGLGETRENDERBUFFERPARAMETERIVPROC)glfwGetProcAddress("glGetRenderbufferParameterivEXT");
glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC)glfwGetProcAddress("glIsFramebufferEXT");
glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC)glfwGetProcAddress("glBindFramebufferEXT");
glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)glfwGetProcAddress("glDeleteFramebuffersEXT");
2021-12-25 10:04:45 +08:00
glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)glfwGetProcAddress("glGenFramebuffersEXT");
glCheckFramebufferStatus =
(PFNGLCHECKFRAMEBUFFERSTATUSPROC)glfwGetProcAddress("glCheckFramebufferStatusEXT");
glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DPROC)glfwGetProcAddress("glFramebufferTexture1DEXT");
glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)glfwGetProcAddress("glFramebufferTexture2DEXT");
glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC)glfwGetProcAddress("glFramebufferTexture3DEXT");
2021-12-25 10:04:45 +08:00
glFramebufferRenderbuffer =
(PFNGLFRAMEBUFFERRENDERBUFFERPROC)glfwGetProcAddress("glFramebufferRenderbufferEXT");
glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC)glfwGetProcAddress(
"glGetFramebufferAttachmentParameterivEXT");
glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC)glfwGetProcAddress("glGenerateMipmapEXT");
}
2021-12-25 10:04:45 +08:00
else if (strstr(gl_extensions, "GL_ANGLE_framebuffer_blit"))
{
log("OpenGL: GL_ANGLE_framebuffer_object is supported");
2020-02-16 16:44:14 +08:00
2021-12-25 10:04:45 +08:00
glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC)glfwGetProcAddress("glIsRenderbufferOES");
glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC)glfwGetProcAddress("glBindRenderbufferOES");
2020-02-16 16:44:14 +08:00
glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC)glfwGetProcAddress("glDeleteRenderbuffersOES");
2021-12-25 10:04:45 +08:00
glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC)glfwGetProcAddress("glGenRenderbuffersOES");
2020-02-16 16:44:14 +08:00
glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC)glfwGetProcAddress("glRenderbufferStorageOES");
2021-12-25 10:04:45 +08:00
// glGetRenderbufferParameteriv =
// (PFNGLGETRENDERBUFFERPARAMETERIVPROC)glfwGetProcAddress("glGetRenderbufferParameterivOES");
glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC)glfwGetProcAddress("glIsFramebufferOES");
glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC)glfwGetProcAddress("glBindFramebufferOES");
2020-02-16 16:44:14 +08:00
glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)glfwGetProcAddress("glDeleteFramebuffersOES");
2021-12-25 10:04:45 +08:00
glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)glfwGetProcAddress("glGenFramebuffersOES");
glCheckFramebufferStatus =
(PFNGLCHECKFRAMEBUFFERSTATUSPROC)glfwGetProcAddress("glCheckFramebufferStatusOES");
glFramebufferRenderbuffer =
(PFNGLFRAMEBUFFERRENDERBUFFERPROC)glfwGetProcAddress("glFramebufferRenderbufferOES");
2020-02-16 16:44:14 +08:00
glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)glfwGetProcAddress("glFramebufferTexture2DOES");
2021-12-25 10:04:45 +08:00
glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC)glfwGetProcAddress(
"glGetFramebufferAttachmentParameterivOES");
2020-02-16 16:44:14 +08:00
glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC)glfwGetProcAddress("glGenerateMipmapOES");
2019-11-23 20:27:39 +08:00
}
else
{
log("OpenGL: No framebuffers extension is supported");
log("OpenGL: Any call to Fbo will crash!");
return false;
}
}
return true;
}
// helper
bool GLViewImpl::loadGL()
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM != AX_PLATFORM_MAC)
// glad: load all OpenGL function pointers
// ---------------------------------------
2022-07-16 10:43:05 +08:00
# if defined(AX_USE_GL)
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
2019-11-23 20:27:39 +08:00
{
log("glad: Failed to Load GL");
2019-11-23 20:27:39 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
if (GL_ARB_vertex_shader && GL_ARB_fragment_shader)
{
2019-11-23 20:27:39 +08:00
log("Ready for GLSL");
}
else
{
log("Not totally ready :(");
}
2021-12-25 10:04:45 +08:00
# else
if (!gladLoadGLES2Loader((GLADloadproc)glfwGetProcAddress))
{
log("glad: Failed to Load GLES2");
2019-11-23 20:27:39 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
# endif
2019-11-23 20:27:39 +08:00
loadFboExtensions();
2022-07-16 10:43:05 +08:00
#endif // (AX_TARGET_PLATFORM != AX_PLATFORM_MAC)
2019-11-23 20:27:39 +08:00
return true;
}
NS_AX_END // end of namespace axis;