SDL related interface encapsulation (#10385)

* SDL related interface encapsulation
This commit is contained in:
qiuguohua
2022-03-18 14:43:01 +08:00
committed by GitHub
parent 09352240ce
commit 6a16077bc5
10 changed files with 492 additions and 935 deletions
+8 -1
View File
@@ -372,7 +372,14 @@ elseif(IOS)
)
endif()
############ platformex
if(WINDOWS OR LINUX)
cocos_source_files(
cocos/platform/SDLHelper.h
cocos/platform/SDLHelper.cpp
)
endif()
############ platform
cocos_source_files(
cocos/platform/IEventDispatch.h
cocos/platform/BasePlatform.cpp
+377
View File
@@ -0,0 +1,377 @@
/****************************************************************************
Copyright (c) 2022 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
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/SDLHelper.h"
#include "base/Log.h"
#include "platform/IEventDispatch.h"
#include "platform/interfaces/modules/ISystemWindow.h"
#include "SDL2/SDL.h"
#include "SDL2/SDL_main.h"
#include "SDL2/SDL_syswm.h"
namespace {
std::unordered_map<int, cc::KeyCode> gKeyMap = {
{SDLK_APPLICATION, cc::KeyCode::CONTEXT_MENU},
{SDLK_SCROLLLOCK, cc::KeyCode::SCROLLLOCK},
{SDLK_PAUSE, cc::KeyCode::PAUSE},
{SDLK_PRINTSCREEN, cc::KeyCode::PRINT_SCREEN},
{SDLK_INSERT, cc::KeyCode::INSERT},
{SDLK_ESCAPE, cc::KeyCode::ESCAPE},
{SDLK_MINUS, cc::KeyCode::MINUS},
{SDLK_LSHIFT, cc::KeyCode::SHIFT_LEFT},
{SDLK_RSHIFT, cc::KeyCode::SHIFT_RIGHT},
{SDLK_EQUALS, cc::KeyCode::EQUAL},
{SDLK_BACKSLASH, cc::KeyCode::BACKSLASH},
{SDLK_BACKQUOTE, cc::KeyCode::BACKQUOTE},
{SDLK_BACKSPACE, cc::KeyCode::BACKSPACE},
{SDLK_RETURN, cc::KeyCode::ENTER},
{SDLK_RETURN2, cc::KeyCode::ENTER},
{SDLK_LEFTBRACKET, cc::KeyCode::BRACKET_LEFT},
{SDLK_RIGHTBRACKET, cc::KeyCode::BRACKET_RIGHT},
{SDLK_SEMICOLON, cc::KeyCode::SEMICOLON},
{SDLK_QUOTE, cc::KeyCode::QUOTE},
{SDLK_TAB, cc::KeyCode::TAB},
{SDLK_LCTRL, cc::KeyCode::CONTROL_LEFT},
{SDLK_RCTRL, cc::KeyCode::CONTROL_RIGHT},
{SDLK_LALT, cc::KeyCode::ALT_LEFT},
{SDLK_RALT, cc::KeyCode::ALT_RIGHT},
{SDLK_LEFT, cc::KeyCode::ARROW_LEFT},
{SDLK_RIGHT, cc::KeyCode::ARROW_RIGHT},
{SDLK_UP, cc::KeyCode::ARROW_UP},
{SDLK_DOWN, cc::KeyCode::ARROW_DOWN},
{SDLK_KP_ENTER, cc::KeyCode::NUMPAD_ENTER},
{SDLK_KP_PLUS, cc::KeyCode::NUMPAD_PLUS},
{SDLK_KP_MULTIPLY, cc::KeyCode::NUMPAD_MULTIPLY},
{SDLK_KP_DIVIDE, cc::KeyCode::NUMPAD_DIVIDE},
{SDLK_KP_MINUS, cc::KeyCode::NUMPAD_MINUS},
{SDLK_KP_PERIOD, cc::KeyCode::NUMPAD_DECIMAL},
{SDLK_KP_BACKSPACE, cc::KeyCode::BACKSPACE},
{SDLK_NUMLOCKCLEAR, cc::KeyCode::NUM_LOCK},
{SDLK_HOME, cc::KeyCode::HOME},
{SDLK_PAGEUP, cc::KeyCode::PAGE_UP},
{SDLK_PAGEDOWN, cc::KeyCode::PAGE_DOWN},
{SDLK_END, cc::KeyCode::END},
{SDLK_COMMA, cc::KeyCode::COMMA},
{SDLK_PERIOD, cc::KeyCode::PERIOD},
{SDLK_SLASH, cc::KeyCode::SLASH},
{SDLK_SPACE, cc::KeyCode::SPACE},
{SDLK_DELETE, cc::KeyCode::DELETE_KEY},
{SDLK_CAPSLOCK, cc::KeyCode::CAPS_LOCK},
{SDLK_KP_0, cc::KeyCode::NUMPAD_0},
{SDLK_KP_1, cc::KeyCode::NUMPAD_1},
{SDLK_KP_2, cc::KeyCode::NUMPAD_2},
{SDLK_KP_3, cc::KeyCode::NUMPAD_3},
{SDLK_KP_4, cc::KeyCode::NUMPAD_4},
{SDLK_KP_5, cc::KeyCode::NUMPAD_5},
{SDLK_KP_6, cc::KeyCode::NUMPAD_6},
{SDLK_KP_7, cc::KeyCode::NUMPAD_7},
{SDLK_KP_8, cc::KeyCode::NUMPAD_8},
{SDLK_KP_9, cc::KeyCode::NUMPAD_9}};
int sdlKeycodeToCocosCode(int sdlCode, int mode) {
auto it = gKeyMap.find(sdlCode);
if (it != gKeyMap.end()) {
return static_cast<int>(it->second);
}
if (sdlCode >= SDLK_F1 && sdlCode <= SDLK_F12) {
// F1 ~ F12
return 112 + (sdlCode - SDLK_F1);
} else {
int code = sdlCode & (~(1 << 30));
if (code >= SDLK_a && code <= SDLK_z) {
return 'A' + (code - SDLK_a);
} else {
return code;
}
}
}
std::unordered_map<cc::ISystemWindow::WindowFlags, SDL_WindowFlags> gWindowFlagMap = {
{cc::ISystemWindow::CC_WINDOW_FULLSCREEN, SDL_WINDOW_FULLSCREEN},
{cc::ISystemWindow::CC_WINDOW_OPENGL, SDL_WINDOW_OPENGL},
{cc::ISystemWindow::CC_WINDOW_SHOWN, SDL_WINDOW_SHOWN},
{cc::ISystemWindow::CC_WINDOW_HIDDEN, SDL_WINDOW_HIDDEN},
{cc::ISystemWindow::CC_WINDOW_BORDERLESS, SDL_WINDOW_BORDERLESS},
{cc::ISystemWindow::CC_WINDOW_RESIZABLE, SDL_WINDOW_RESIZABLE},
{cc::ISystemWindow::CC_WINDOW_MINIMIZED, SDL_WINDOW_MINIMIZED},
{cc::ISystemWindow::CC_WINDOW_MAXIMIZED, SDL_WINDOW_MAXIMIZED},
{cc::ISystemWindow::CC_WINDOW_INPUT_GRABBED, SDL_WINDOW_INPUT_GRABBED},
{cc::ISystemWindow::CC_WINDOW_INPUT_FOCUS, SDL_WINDOW_INPUT_FOCUS},
{cc::ISystemWindow::CC_WINDOW_MOUSE_FOCUS, SDL_WINDOW_MOUSE_FOCUS},
{cc::ISystemWindow::CC_WINDOW_FULLSCREEN_DESKTOP, SDL_WINDOW_FULLSCREEN_DESKTOP},
{cc::ISystemWindow::CC_WINDOW_FOREIGN, SDL_WINDOW_FOREIGN},
{cc::ISystemWindow::CC_WINDOW_ALLOW_HIGHDPI, SDL_WINDOW_ALLOW_HIGHDPI},
{cc::ISystemWindow::CC_WINDOW_MOUSE_CAPTURE, SDL_WINDOW_MOUSE_CAPTURE},
{cc::ISystemWindow::CC_WINDOW_ALWAYS_ON_TOP, SDL_WINDOW_ALWAYS_ON_TOP},
{cc::ISystemWindow::CC_WINDOW_SKIP_TASKBAR, SDL_WINDOW_SKIP_TASKBAR},
{cc::ISystemWindow::CC_WINDOW_UTILITY, SDL_WINDOW_UTILITY},
{cc::ISystemWindow::CC_WINDOW_TOOLTIP, SDL_WINDOW_TOOLTIP},
{cc::ISystemWindow::CC_WINDOW_POPUP_MENU, SDL_WINDOW_POPUP_MENU},
{cc::ISystemWindow::CC_WINDOW_VULKAN, SDL_WINDOW_VULKAN}};
int windowFlagsToSDLWindowFlag(int flags) {
int sdlFlags = 0;
for (auto it : gWindowFlagMap) {
if ((it.first & flags) == it.first) {
sdlFlags |= it.second;
}
}
return sdlFlags;
}
} // namespace
namespace cc {
SDLHelper::SDLHelper(IEventDispatch *delegate)
: _delegate(delegate) {
}
SDLHelper::~SDLHelper() {
if (_handle) {
SDL_DestroyWindow(_handle);
_handle = nullptr;
}
if (_windowCreated) {
SDL_Quit();
}
}
bool SDLHelper::init() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
// Display error message
CC_LOG_ERROR("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return false;
}
return true;
}
void SDLHelper::dispatchWindowEvent(const SDL_WindowEvent &wevent) {
WindowEvent ev;
switch (wevent.event) {
case SDL_WINDOWEVENT_SHOWN: {
ev.type = WindowEvent::Type::SHOW;
_delegate->dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_RESTORED: {
ev.type = WindowEvent::Type::RESTORED;
_delegate->dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_SIZE_CHANGED: {
ev.type = WindowEvent::Type::SIZE_CHANGED;
ev.width = wevent.data1;
ev.height = wevent.data2;
_delegate->dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_RESIZED: {
ev.type = WindowEvent::Type::RESIZED;
ev.width = wevent.data1;
ev.height = wevent.data2;
_delegate->dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_HIDDEN: {
ev.type = WindowEvent::Type::HIDDEN;
_delegate->dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_MINIMIZED: {
ev.type = WindowEvent::Type::MINIMIZED;
_delegate->dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_ENTER: {
SDL_CaptureMouse(SDL_TRUE);
break;
}
case SDL_WINDOWEVENT_CLOSE: {
ev.type = WindowEvent::Type::CLOSE;
_delegate->dispatchEvent(ev);
break;
}
}
}
void SDLHelper::dispatchSDLEvent(const SDL_Event &sdlEvent, bool *quit) {
cc::TouchEvent touch;
cc::MouseEvent mouse;
cc::KeyboardEvent keyboard;
switch (sdlEvent.type) {
case SDL_QUIT: {
if (quit) {
*quit = true;
}
WindowEvent ev;
ev.type = WindowEvent::Type::QUIT;
_delegate->dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT: {
dispatchWindowEvent(sdlEvent.window);
break;
}
case SDL_MOUSEBUTTONDOWN: {
int width = 0;
int height = 0;
SDL_GetWindowSize(_handle, &width, &height);
const SDL_MouseButtonEvent &event = sdlEvent.button;
if (0 > event.x || event.x > width || 0 > event.y || event.y > height) {
break;
}
mouse.type = MouseEvent::Type::DOWN;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = event.button - 1;
_delegate->dispatchEvent(mouse);
break;
}
case SDL_MOUSEBUTTONUP: {
const SDL_MouseButtonEvent &event = sdlEvent.button;
mouse.type = MouseEvent::Type::UP;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = event.button - 1;
_delegate->dispatchEvent(mouse);
break;
}
case SDL_MOUSEMOTION: {
const SDL_MouseMotionEvent &event = sdlEvent.motion;
mouse.type = MouseEvent::Type::MOVE;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = 0;
_delegate->dispatchEvent(mouse);
break;
}
case SDL_MOUSEWHEEL: {
const SDL_MouseWheelEvent &event = sdlEvent.wheel;
mouse.type = MouseEvent::Type::WHEEL;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = 0; //TODO: direction
_delegate->dispatchEvent(mouse);
break;
}
case SDL_FINGERUP: {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::ENDED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
_delegate->dispatchEvent(touch);
break;
}
case SDL_FINGERDOWN: {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::BEGAN;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
_delegate->dispatchEvent(touch);
break;
}
case SDL_FINGERMOTION: {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::MOVED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
_delegate->dispatchEvent(touch);
break;
}
case SDL_KEYDOWN: {
const SDL_KeyboardEvent &event = sdlEvent.key;
auto mode = event.keysym.mod;
keyboard.action = KeyboardEvent::Action::PRESS;
keyboard.key = sdlKeycodeToCocosCode(event.keysym.sym, mode);
keyboard.altKeyActive = mode & KMOD_ALT;
keyboard.ctrlKeyActive = mode & KMOD_CTRL;
keyboard.shiftKeyActive = mode & KMOD_SHIFT;
//CC_LOG_DEBUG("==> key %d -> code %d", event.keysym.sym, keyboard.key);
_delegate->dispatchEvent(keyboard);
break;
}
case SDL_KEYUP: {
const SDL_KeyboardEvent &event = sdlEvent.key;
auto mode = event.keysym.mod;
keyboard.action = KeyboardEvent::Action::RELEASE;
keyboard.key = sdlKeycodeToCocosCode(event.keysym.sym, mode);
keyboard.altKeyActive = mode & KMOD_ALT;
keyboard.ctrlKeyActive = mode & KMOD_CTRL;
keyboard.shiftKeyActive = mode & KMOD_SHIFT;
_delegate->dispatchEvent(keyboard);
break;
}
default:
break;
}
}
void SDLHelper::pollEvent(bool *quit) {
SDL_Event sdlEvent;
int cnt = 0;
while ((cnt = SDL_PollEvent(&sdlEvent)) != 0) {
dispatchSDLEvent(sdlEvent, quit);
}
}
bool SDLHelper::createWindow(const char *title,
int x, int y, int w,
int h, int flags) {
// Create window
int sdlFlags = windowFlagsToSDLWindowFlag(flags);
_handle = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, sdlFlags);
if (_handle == nullptr) {
// Display error message
CC_LOG_ERROR("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return false;
}
_windowCreated = true;
return true;
}
void SDLHelper::swapWindow() {
SDL_GL_SwapWindow(_handle);
}
#if (CC_PLATFORM == CC_PLATFORM_LINUX)
uintptr_t SDLHelper::getDisplay() const {
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(_handle, &wmInfo);
return reinterpret_cast<uintptr_t>(wmInfo.info.x11.display);
}
#endif
uintptr_t SDLHelper::getWindowHandler() const {
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(_handle, &wmInfo);
#if CC_PLATFORM == CC_PLATFORM_WINDOWS
return reinterpret_cast<uintptr_t>(wmInfo.info.win.window);
#elif (CC_PLATFORM == CC_PLATFORM_LINUX)
return reinterpret_cast<uintptr_t>(wmInfo.info.x11.window);
#endif
CCASSERT(false, "Only valid in windows, linux");
return 0;
}
} // namespace cc
+64
View File
@@ -0,0 +1,64 @@
/****************************************************************************
Copyright (c) 2022 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
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.
****************************************************************************/
#pragma once
#include <iostream>
#include "bindings/event/EventDispatcher.h"
struct SDL_Window;
class OSEvent;
union SDL_Event;
struct SDL_WindowEvent;
namespace cc {
class IEventDispatch;
class SDLHelper {
public:
SDLHelper(IEventDispatch* delegate);
~SDLHelper();
bool init();
void swapWindow();
void createWindow();
int getEvent(OSEvent* ev);
bool createWindow(const char* title,
int x, int y, int w,
int h, int flags);
void pollEvent(bool* quit);
uintptr_t getWindowHandler() const;
#if (CC_PLATFORM == CC_PLATFORM_LINUX)
uintptr_t getDisplay() const;
#endif
private:
void dispatchSDLEvent(const SDL_Event& sdlEvent, bool* quit);
void dispatchWindowEvent(const SDL_WindowEvent& wevent);
bool _windowCreated{false};
IEventDispatch* _delegate{nullptr};
struct SDL_Window* _handle{nullptr};
};
} // namespace cc
+13 -305
View File
@@ -28,148 +28,24 @@
#include <sys/time.h>
#include <unistd.h>
#include "SDL2/SDL.h"
#include "SDL2/SDL_main.h"
#include "SDL2/SDL_syswm.h"
#include "platform/interfaces/OSInterface.h"
#include "platform/interfaces/modules/ISystemWindow.h"
#include "platform/SDLHelper.h"
namespace {
std::unordered_map<int, cc::KeyCode> gKeyMap = {
{SDLK_APPLICATION, cc::KeyCode::CONTEXT_MENU},
{SDLK_SCROLLLOCK, cc::KeyCode::SCROLLLOCK},
{SDLK_PAUSE, cc::KeyCode::PAUSE},
{SDLK_PRINTSCREEN, cc::KeyCode::PRINT_SCREEN},
{SDLK_INSERT, cc::KeyCode::INSERT},
{SDLK_ESCAPE, cc::KeyCode::ESCAPE},
{SDLK_MINUS, cc::KeyCode::MINUS},
{SDLK_LSHIFT, cc::KeyCode::SHIFT_LEFT},
{SDLK_RSHIFT, cc::KeyCode::SHIFT_RIGHT},
{SDLK_EQUALS, cc::KeyCode::EQUAL},
{SDLK_BACKSLASH, cc::KeyCode::BACKSLASH},
{SDLK_BACKQUOTE, cc::KeyCode::BACKQUOTE},
{SDLK_BACKSPACE, cc::KeyCode::BACKSPACE},
{SDLK_RETURN, cc::KeyCode::ENTER},
{SDLK_RETURN2, cc::KeyCode::ENTER},
{SDLK_LEFTBRACKET, cc::KeyCode::BRACKET_LEFT},
{SDLK_RIGHTBRACKET, cc::KeyCode::BRACKET_RIGHT},
{SDLK_SEMICOLON, cc::KeyCode::SEMICOLON},
{SDLK_QUOTE, cc::KeyCode::QUOTE},
{SDLK_TAB, cc::KeyCode::TAB},
{SDLK_LCTRL, cc::KeyCode::CONTROL_LEFT},
{SDLK_RCTRL, cc::KeyCode::CONTROL_RIGHT},
{SDLK_LALT, cc::KeyCode::ALT_LEFT},
{SDLK_RALT, cc::KeyCode::ALT_RIGHT},
{SDLK_LEFT, cc::KeyCode::ARROW_LEFT},
{SDLK_RIGHT, cc::KeyCode::ARROW_RIGHT},
{SDLK_UP, cc::KeyCode::ARROW_UP},
{SDLK_DOWN, cc::KeyCode::ARROW_DOWN},
{SDLK_KP_ENTER, cc::KeyCode::NUMPAD_ENTER},
{SDLK_KP_PLUS, cc::KeyCode::NUMPAD_PLUS},
{SDLK_KP_MULTIPLY, cc::KeyCode::NUMPAD_MULTIPLY},
{SDLK_KP_DIVIDE, cc::KeyCode::NUMPAD_DIVIDE},
{SDLK_KP_MINUS, cc::KeyCode::NUMPAD_MINUS},
{SDLK_KP_PERIOD, cc::KeyCode::NUMPAD_DECIMAL},
{SDLK_KP_BACKSPACE, cc::KeyCode::BACKSPACE},
{SDLK_NUMLOCKCLEAR, cc::KeyCode::NUM_LOCK},
{SDLK_HOME, cc::KeyCode::HOME},
{SDLK_PAGEUP, cc::KeyCode::PAGE_UP},
{SDLK_PAGEDOWN, cc::KeyCode::PAGE_DOWN},
{SDLK_END, cc::KeyCode::END},
{SDLK_COMMA, cc::KeyCode::COMMA},
{SDLK_PERIOD, cc::KeyCode::PERIOD},
{SDLK_SLASH, cc::KeyCode::SLASH},
{SDLK_SPACE, cc::KeyCode::SPACE},
{SDLK_DELETE, cc::KeyCode::DELETE_KEY},
{SDLK_CAPSLOCK, cc::KeyCode::CAPS_LOCK},
{SDLK_KP_0, cc::KeyCode::NUMPAD_0},
{SDLK_KP_1, cc::KeyCode::NUMPAD_1},
{SDLK_KP_2, cc::KeyCode::NUMPAD_2},
{SDLK_KP_3, cc::KeyCode::NUMPAD_3},
{SDLK_KP_4, cc::KeyCode::NUMPAD_4},
{SDLK_KP_5, cc::KeyCode::NUMPAD_5},
{SDLK_KP_6, cc::KeyCode::NUMPAD_6},
{SDLK_KP_7, cc::KeyCode::NUMPAD_7},
{SDLK_KP_8, cc::KeyCode::NUMPAD_8},
{SDLK_KP_9, cc::KeyCode::NUMPAD_9}};
int sdlKeycodeToCocosCode(int sdlCode, int mode) {
auto it = gKeyMap.find(sdlCode);
if (it != gKeyMap.end()) {
return static_cast<int>(it->second);
}
if (sdlCode >= SDLK_F1 && sdlCode <= SDLK_F12) {
// F1 ~ F12
return 112 + (sdlCode - SDLK_F1);
} else {
int code = sdlCode & (~(1 << 30));
if (code >= SDLK_a && code <= SDLK_z) {
return 'A' + (code - SDLK_a);
} else {
return code;
}
}
}
std::unordered_map<cc::ISystemWindow::WindowFlags, SDL_WindowFlags> gWindowFlagMap = {
{cc::ISystemWindow::CC_WINDOW_FULLSCREEN, SDL_WINDOW_FULLSCREEN},
{cc::ISystemWindow::CC_WINDOW_OPENGL, SDL_WINDOW_OPENGL},
{cc::ISystemWindow::CC_WINDOW_SHOWN, SDL_WINDOW_SHOWN},
{cc::ISystemWindow::CC_WINDOW_HIDDEN, SDL_WINDOW_HIDDEN},
{cc::ISystemWindow::CC_WINDOW_BORDERLESS, SDL_WINDOW_BORDERLESS},
{cc::ISystemWindow::CC_WINDOW_RESIZABLE, SDL_WINDOW_RESIZABLE},
{cc::ISystemWindow::CC_WINDOW_MINIMIZED, SDL_WINDOW_MINIMIZED},
{cc::ISystemWindow::CC_WINDOW_MAXIMIZED, SDL_WINDOW_MAXIMIZED},
{cc::ISystemWindow::CC_WINDOW_INPUT_GRABBED, SDL_WINDOW_INPUT_GRABBED},
{cc::ISystemWindow::CC_WINDOW_INPUT_FOCUS, SDL_WINDOW_INPUT_FOCUS},
{cc::ISystemWindow::CC_WINDOW_MOUSE_FOCUS, SDL_WINDOW_MOUSE_FOCUS},
{cc::ISystemWindow::CC_WINDOW_FULLSCREEN_DESKTOP, SDL_WINDOW_FULLSCREEN_DESKTOP},
{cc::ISystemWindow::CC_WINDOW_FOREIGN, SDL_WINDOW_FOREIGN},
{cc::ISystemWindow::CC_WINDOW_ALLOW_HIGHDPI, SDL_WINDOW_ALLOW_HIGHDPI},
{cc::ISystemWindow::CC_WINDOW_MOUSE_CAPTURE, SDL_WINDOW_MOUSE_CAPTURE},
{cc::ISystemWindow::CC_WINDOW_ALWAYS_ON_TOP, SDL_WINDOW_ALWAYS_ON_TOP},
{cc::ISystemWindow::CC_WINDOW_SKIP_TASKBAR, SDL_WINDOW_SKIP_TASKBAR},
{cc::ISystemWindow::CC_WINDOW_UTILITY, SDL_WINDOW_UTILITY},
{cc::ISystemWindow::CC_WINDOW_TOOLTIP, SDL_WINDOW_TOOLTIP},
{cc::ISystemWindow::CC_WINDOW_POPUP_MENU, SDL_WINDOW_POPUP_MENU},
{cc::ISystemWindow::CC_WINDOW_VULKAN, SDL_WINDOW_VULKAN}};
int windowFlagsToSDLWindowFlag(int flags) {
int sdlFlags = 0;
for (auto it : gWindowFlagMap) {
if ((it.first & flags) == it.first) {
sdlFlags |= it.second;
}
}
return sdlFlags;
}
} // namespace
namespace cc {
LinuxPlatform::LinuxPlatform() = default;
LinuxPlatform::LinuxPlatform() {
_sdl = std::make_unique<SDLHelper>(this);
}
LinuxPlatform::~LinuxPlatform() {
if (_handle) {
SDL_DestroyWindow(_handle);
_handle = nullptr;
}
if (_inited) {
SDL_Quit();
}
}
int32_t LinuxPlatform::init() {
UniversalPlatform::init();
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
// Display error message
CC_LOG_ERROR("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return false;
}
return 0;
return _sdl->init() ? 0 : -1;
}
static long getCurrentMillSecond() {
@@ -195,7 +71,7 @@ int32_t LinuxPlatform::loop() {
if (actualInterval >= desiredInterval) {
lastTime = getCurrentMillSecond();
runTask();
SDL_GL_SwapWindow(_handle);
_sdl->swapWindow();
} else {
usleep((desiredInterval - curTime + lastTime) * 1000);
}
@@ -205,190 +81,22 @@ int32_t LinuxPlatform::loop() {
return 0;
}
void LinuxPlatform::handleWindowEvent(SDL_WindowEvent &wevent) {
WindowEvent ev;
switch (wevent.event) {
case SDL_WINDOWEVENT_SHOWN: {
ev.type = WindowEvent::Type::SHOW;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_RESTORED: {
ev.type = WindowEvent::Type::RESTORED;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_SIZE_CHANGED: {
ev.type = WindowEvent::Type::SIZE_CHANGED;
ev.width = wevent.data1;
ev.height = wevent.data2;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_RESIZED: {
ev.type = WindowEvent::Type::RESIZED;
ev.width = wevent.data1;
ev.height = wevent.data2;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_HIDDEN: {
ev.type = WindowEvent::Type::HIDDEN;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_MINIMIZED: {
ev.type = WindowEvent::Type::MINIMIZED;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_ENTER: {
SDL_CaptureMouse(SDL_TRUE);
break;
}
case SDL_WINDOWEVENT_CLOSE: {
ev.type = WindowEvent::Type::CLOSE;
dispatchEvent(ev);
break;
}
}
}
void LinuxPlatform::pollEvent() {
SDL_Event sdlEvent;
int cnt = SDL_PollEvent(&sdlEvent);
if (cnt == 0) {
return;
}
cc::TouchEvent touch;
cc::MouseEvent mouse;
cc::KeyboardEvent keyboard;
switch (sdlEvent.type) {
case SDL_QUIT: {
_quit = true;
WindowEvent ev;
ev.type = WindowEvent::Type::QUIT;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT: {
handleWindowEvent(sdlEvent.window);
break;
}
case SDL_MOUSEBUTTONDOWN: {
SDL_MouseButtonEvent &event = sdlEvent.button;
mouse.type = MouseEvent::Type::DOWN;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
/*if (0 > mouse.x || mouse.x > this->_width || 0 > mouse.y || mouse.y > this->_height) {
break;
}*/
mouse.button = event.button - 1;
dispatchEvent(mouse);
break;
}
case SDL_MOUSEBUTTONUP: {
SDL_MouseButtonEvent &event = sdlEvent.button;
mouse.type = MouseEvent::Type::UP;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = event.button - 1;
dispatchEvent(mouse);
break;
}
case SDL_MOUSEMOTION: {
SDL_MouseMotionEvent &event = sdlEvent.motion;
mouse.type = MouseEvent::Type::MOVE;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = 0;
dispatchEvent(mouse);
break;
}
case SDL_MOUSEWHEEL: {
SDL_MouseWheelEvent &event = sdlEvent.wheel;
mouse.type = MouseEvent::Type::WHEEL;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = 0; //TODO: direction
dispatchEvent(mouse);
break;
}
case SDL_FINGERUP: {
SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::ENDED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
dispatchEvent(touch);
break;
}
case SDL_FINGERDOWN: {
SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::BEGAN;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
dispatchEvent(touch);
break;
}
case SDL_FINGERMOTION: {
SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::MOVED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
dispatchEvent(touch);
break;
}
case SDL_KEYDOWN: {
SDL_KeyboardEvent &event = sdlEvent.key;
auto mode = event.keysym.mod;
keyboard.action = KeyboardEvent::Action::PRESS;
keyboard.key = sdlKeycodeToCocosCode(event.keysym.sym, mode);
keyboard.altKeyActive = mode & KMOD_ALT;
keyboard.ctrlKeyActive = mode & KMOD_CTRL;
keyboard.shiftKeyActive = mode & KMOD_SHIFT;
//CC_LOG_DEBUG("==> key %d -> code %d", event.keysym.sym, keyboard.key);
//cc::EventDispatcher::dispatchKeyboardEvent(keyboard);
dispatchEvent(keyboard);
break;
}
case SDL_KEYUP: {
SDL_KeyboardEvent &event = sdlEvent.key;
auto mode = event.keysym.mod;
keyboard.action = KeyboardEvent::Action::RELEASE;
keyboard.key = sdlKeycodeToCocosCode(event.keysym.sym, mode);
keyboard.altKeyActive = mode & KMOD_ALT;
keyboard.ctrlKeyActive = mode & KMOD_CTRL;
keyboard.shiftKeyActive = mode & KMOD_SHIFT;
dispatchEvent(keyboard);
//cc::EventDispatcher::dispatchKeyboardEvent(keyboard);
break;
}
default:
break;
}
_sdl->pollEvent(&_quit);
}
bool LinuxPlatform::createWindow(const char *title,
int x, int y, int w,
int h, int flags) {
if (_inited) {
return true;
}
// Create window
int sdlFlags = windowFlagsToSDLWindowFlag(flags);
_handle = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, sdlFlags);
if (_handle == nullptr) {
// Display error message
CC_LOG_ERROR("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return false;
}
_inited = true;
return true;
return _sdl->createWindow(title, x, y, w, h, flags);
}
uintptr_t LinuxPlatform::getWindowHandler() const {
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(_handle, &wmInfo);
return reinterpret_cast<uintptr_t>(wmInfo.info.x11.window);
return _sdl->getWindowHandler();
}
uintptr_t LinuxPlatform::getDisplay() const {
return _sdl->getDisplay();
}
} // namespace cc
+3 -9
View File
@@ -28,11 +28,8 @@
#include "platform/UniversalPlatform.h"
#include "platform/linux/modules/SystemWindow.h"
struct SDL_WindowEvent;
struct SDL_Window;
namespace cc {
class SDLHelper;
class LinuxPlatform : public UniversalPlatform,
public SystemWindow::Delegate {
public:
@@ -53,16 +50,13 @@ public:
int x, int y, int w,
int h, int flags) override;
uintptr_t getWindowHandler() const override;
struct SDL_Window* getWindow() {
return _handle;
}
uintptr_t getDisplay() const;
private:
void pollEvent() override;
void handleWindowEvent(SDL_WindowEvent& wevent);
bool _inited{false};
bool _quit{false};
struct SDL_Window* _handle{nullptr};
std::unique_ptr<SDLHelper> _sdl;
};
} // namespace cc
@@ -24,8 +24,6 @@
****************************************************************************/
#include "platform/linux/modules/CanvasRenderingContext2DDelegate.h"
#include "SDL2/SDL.h"
#include "SDL2/SDL_syswm.h"
#include "platform/linux/LinuxPlatform.h"
namespace {
@@ -39,13 +37,10 @@ namespace cc {
static const char gdefaultFontName[] = "lucidasans-24";
CanvasRenderingContext2DDelegate::CanvasRenderingContext2DDelegate() {
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
LinuxPlatform *platform = dynamic_cast<LinuxPlatform *>(BasePlatform::getPlatform());
CCASSERT(platform != nullptr, "Platform pointer can't be null");
SDL_GetWindowWMInfo(reinterpret_cast<SDL_Window *>(platform->getWindow()), &wmInfo);
_dis = wmInfo.info.x11.display;
_win = wmInfo.info.x11.window;
_dis = reinterpret_cast<Display*>(platform->getDisplay());
_win = reinterpret_cast<Drawable>(platform->getWindowHandler());
}
CanvasRenderingContext2DDelegate::~CanvasRenderingContext2DDelegate() {
+9 -293
View File
@@ -28,130 +28,17 @@
#include <sys/time.h>
#include <unistd.h>
#include "SDL2/SDL.h"
#include "SDL2/SDL_main.h"
#include "SDL2/SDL_sysvideo.h"
#include "SDL2/SDL_syswm.h"
#include "platform/interfaces/OSInterface.h"
#include "platform/interfaces/modules/ISystemWindow.h"
namespace {
std::unordered_map<int, cc::KeyCode> gKeyMap = {
{SDLK_APPLICATION, cc::KeyCode::CONTEXT_MENU},
{SDLK_SCROLLLOCK, cc::KeyCode::SCROLLLOCK},
{SDLK_PAUSE, cc::KeyCode::PAUSE},
{SDLK_PRINTSCREEN, cc::KeyCode::PRINT_SCREEN},
{SDLK_INSERT, cc::KeyCode::INSERT},
{SDLK_ESCAPE, cc::KeyCode::ESCAPE},
{SDLK_MINUS, cc::KeyCode::MINUS},
{SDLK_LSHIFT, cc::KeyCode::SHIFT_LEFT},
{SDLK_RSHIFT, cc::KeyCode::SHIFT_RIGHT},
{SDLK_EQUALS, cc::KeyCode::EQUAL},
{SDLK_BACKSLASH, cc::KeyCode::BACKSLASH},
{SDLK_BACKQUOTE, cc::KeyCode::BACKQUOTE},
{SDLK_BACKSPACE, cc::KeyCode::BACKSPACE},
{SDLK_RETURN, cc::KeyCode::ENTER},
{SDLK_RETURN2, cc::KeyCode::ENTER},
{SDLK_LEFTBRACKET, cc::KeyCode::BRACKET_LEFT},
{SDLK_RIGHTBRACKET, cc::KeyCode::BRACKET_RIGHT},
{SDLK_SEMICOLON, cc::KeyCode::SEMICOLON},
{SDLK_QUOTE, cc::KeyCode::QUOTE},
{SDLK_TAB, cc::KeyCode::TAB},
{SDLK_LCTRL, cc::KeyCode::CONTROL_LEFT},
{SDLK_RCTRL, cc::KeyCode::CONTROL_RIGHT},
{SDLK_LALT, cc::KeyCode::ALT_LEFT},
{SDLK_RALT, cc::KeyCode::ALT_RIGHT},
{SDLK_LEFT, cc::KeyCode::ARROW_LEFT},
{SDLK_RIGHT, cc::KeyCode::ARROW_RIGHT},
{SDLK_UP, cc::KeyCode::ARROW_UP},
{SDLK_DOWN, cc::KeyCode::ARROW_DOWN},
{SDLK_KP_ENTER, cc::KeyCode::NUMPAD_ENTER},
{SDLK_KP_PLUS, cc::KeyCode::NUMPAD_PLUS},
{SDLK_KP_MULTIPLY, cc::KeyCode::NUMPAD_MULTIPLY},
{SDLK_KP_DIVIDE, cc::KeyCode::NUMPAD_DIVIDE},
{SDLK_KP_MINUS, cc::KeyCode::NUMPAD_MINUS},
{SDLK_KP_PERIOD, cc::KeyCode::NUMPAD_DECIMAL},
{SDLK_KP_BACKSPACE, cc::KeyCode::BACKSPACE},
{SDLK_NUMLOCKCLEAR, cc::KeyCode::NUM_LOCK},
{SDLK_HOME, cc::KeyCode::HOME},
{SDLK_PAGEUP, cc::KeyCode::PAGE_UP},
{SDLK_PAGEDOWN, cc::KeyCode::PAGE_DOWN},
{SDLK_END, cc::KeyCode::END},
{SDLK_COMMA, cc::KeyCode::COMMA},
{SDLK_PERIOD, cc::KeyCode::PERIOD},
{SDLK_SLASH, cc::KeyCode::SLASH},
{SDLK_SPACE, cc::KeyCode::SPACE},
{SDLK_DELETE, cc::KeyCode::DELETE_KEY},
{SDLK_CAPSLOCK, cc::KeyCode::CAPS_LOCK},
{SDLK_KP_0, cc::KeyCode::NUMPAD_0},
{SDLK_KP_1, cc::KeyCode::NUMPAD_1},
{SDLK_KP_2, cc::KeyCode::NUMPAD_2},
{SDLK_KP_3, cc::KeyCode::NUMPAD_3},
{SDLK_KP_4, cc::KeyCode::NUMPAD_4},
{SDLK_KP_5, cc::KeyCode::NUMPAD_5},
{SDLK_KP_6, cc::KeyCode::NUMPAD_6},
{SDLK_KP_7, cc::KeyCode::NUMPAD_7},
{SDLK_KP_8, cc::KeyCode::NUMPAD_8},
{SDLK_KP_9, cc::KeyCode::NUMPAD_9}};
int sdlKeycodeToCocosCode(int sdlCode, int mode) {
auto it = gKeyMap.find(sdlCode);
if (it != gKeyMap.end()) {
return static_cast<int>(it->second);
}
if (sdlCode >= SDLK_F1 && sdlCode <= SDLK_F12) {
// F1 ~ F12
return 112 + (sdlCode - SDLK_F1);
} else {
int code = sdlCode & (~(1 << 30));
if (code >= SDLK_a && code <= SDLK_z) {
return 'A' + (code - SDLK_a);
} else {
return code;
}
}
}
std::unordered_map<cc::ISystemWindow::WindowFlags, SDL_WindowFlags> gWindowFlagMap = {
{cc::ISystemWindow::CC_WINDOW_FULLSCREEN, SDL_WINDOW_FULLSCREEN},
{cc::ISystemWindow::CC_WINDOW_OPENGL, SDL_WINDOW_OPENGL},
{cc::ISystemWindow::CC_WINDOW_SHOWN, SDL_WINDOW_SHOWN},
{cc::ISystemWindow::CC_WINDOW_HIDDEN, SDL_WINDOW_HIDDEN},
{cc::ISystemWindow::CC_WINDOW_BORDERLESS, SDL_WINDOW_BORDERLESS},
{cc::ISystemWindow::CC_WINDOW_RESIZABLE, SDL_WINDOW_RESIZABLE},
{cc::ISystemWindow::CC_WINDOW_MINIMIZED, SDL_WINDOW_MINIMIZED},
{cc::ISystemWindow::CC_WINDOW_MAXIMIZED, SDL_WINDOW_MAXIMIZED},
{cc::ISystemWindow::CC_WINDOW_INPUT_GRABBED, SDL_WINDOW_INPUT_GRABBED},
{cc::ISystemWindow::CC_WINDOW_INPUT_FOCUS, SDL_WINDOW_INPUT_FOCUS},
{cc::ISystemWindow::CC_WINDOW_MOUSE_FOCUS, SDL_WINDOW_MOUSE_FOCUS},
{cc::ISystemWindow::CC_WINDOW_FULLSCREEN_DESKTOP, SDL_WINDOW_FULLSCREEN_DESKTOP},
{cc::ISystemWindow::CC_WINDOW_FOREIGN, SDL_WINDOW_FOREIGN},
{cc::ISystemWindow::CC_WINDOW_ALLOW_HIGHDPI, SDL_WINDOW_ALLOW_HIGHDPI},
{cc::ISystemWindow::CC_WINDOW_MOUSE_CAPTURE, SDL_WINDOW_MOUSE_CAPTURE},
{cc::ISystemWindow::CC_WINDOW_ALWAYS_ON_TOP, SDL_WINDOW_ALWAYS_ON_TOP},
{cc::ISystemWindow::CC_WINDOW_SKIP_TASKBAR, SDL_WINDOW_SKIP_TASKBAR},
{cc::ISystemWindow::CC_WINDOW_UTILITY, SDL_WINDOW_UTILITY},
{cc::ISystemWindow::CC_WINDOW_TOOLTIP, SDL_WINDOW_TOOLTIP},
{cc::ISystemWindow::CC_WINDOW_POPUP_MENU, SDL_WINDOW_POPUP_MENU},
{cc::ISystemWindow::CC_WINDOW_VULKAN, SDL_WINDOW_VULKAN}};
int windowFlagsToSDLWindowFlag(int flags) {
int sdlFlags = 0;
for (auto it : gWindowFlagMap) {
if ((it.first & flags) == it.first) {
sdlFlags |= it.second;
}
}
return sdlFlags;
}
} // namespace
namespace cc {
QnxPlatform::QnxPlatform() = default;
QnxPlatform::QnxPlatform() {
// _sdl = std::make_unique<SDLHelper>(this);
}
QnxPlatform::~QnxPlatform() {
if (_screenWin) {
screen_destroy_window(_screenWin);
@@ -163,12 +50,7 @@ QnxPlatform::~QnxPlatform() {
int32_t QnxPlatform::init() {
UniversalPlatform::init();
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
// Display error message
CC_LOG_ERROR("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return false;
}
// return _sdl->init() ? 0 : -1;
return 0;
}
@@ -190,13 +72,12 @@ int32_t QnxPlatform::loop() {
while (!_quit) {
curTime = getCurrentMillSecond();
desiredInterval = static_cast<long>(1000.0 / getFps());
// pollEvent();
pollEvent();
actualInterval = curTime - lastTime;
if (actualInterval >= desiredInterval) {
lastTime = getCurrentMillSecond();
runTask();
//SDL_GL_SwapWindow(_handle);
//_sdl->swapWindow();
} else {
usleep((desiredInterval - curTime + lastTime) * 1000);
}
@@ -206,165 +87,9 @@ int32_t QnxPlatform::loop() {
return 0;
}
void QnxPlatform::handleWindowEvent(SDL_WindowEvent &wevent) {
WindowEvent ev;
switch (wevent.event) {
case SDL_WINDOWEVENT_SHOWN: {
ev.type = WindowEvent::Type::SHOW;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_RESTORED: {
ev.type = WindowEvent::Type::RESTORED;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_SIZE_CHANGED: {
ev.type = WindowEvent::Type::SIZE_CHANGED;
ev.width = wevent.data1;
ev.height = wevent.data2;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_RESIZED: {
ev.type = WindowEvent::Type::RESIZED;
ev.width = wevent.data1;
ev.height = wevent.data2;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_HIDDEN: {
ev.type = WindowEvent::Type::HIDDEN;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_MINIMIZED: {
ev.type = WindowEvent::Type::MINIMIZED;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_ENTER: {
SDL_CaptureMouse(SDL_TRUE);
break;
}
case SDL_WINDOWEVENT_CLOSE: {
ev.type = WindowEvent::Type::CLOSE;
dispatchEvent(ev);
break;
}
}
}
void QnxPlatform::pollEvent() {
SDL_Event sdlEvent;
int cnt = SDL_PollEvent(&sdlEvent);
if (cnt == 0) {
return;
}
cc::TouchEvent touch;
cc::MouseEvent mouse;
cc::KeyboardEvent keyboard;
switch (sdlEvent.type) {
case SDL_QUIT: {
_quit = true;
WindowEvent ev;
ev.type = WindowEvent::Type::QUIT;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT: {
handleWindowEvent(sdlEvent.window);
break;
}
case SDL_MOUSEBUTTONDOWN: {
SDL_MouseButtonEvent &event = sdlEvent.button;
mouse.type = MouseEvent::Type::DOWN;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
/*if (0 > mouse.x || mouse.x > this->_width || 0 > mouse.y || mouse.y > this->_height) {
break;
}*/
mouse.button = event.button - 1;
dispatchEvent(mouse);
break;
}
case SDL_MOUSEBUTTONUP: {
SDL_MouseButtonEvent &event = sdlEvent.button;
mouse.type = MouseEvent::Type::UP;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = event.button - 1;
dispatchEvent(mouse);
break;
}
case SDL_MOUSEMOTION: {
SDL_MouseMotionEvent &event = sdlEvent.motion;
mouse.type = MouseEvent::Type::MOVE;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = 0;
dispatchEvent(mouse);
break;
}
case SDL_MOUSEWHEEL: {
SDL_MouseWheelEvent &event = sdlEvent.wheel;
mouse.type = MouseEvent::Type::WHEEL;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = 0; //TODO: direction
dispatchEvent(mouse);
break;
}
case SDL_FINGERUP: {
SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::ENDED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
dispatchEvent(touch);
break;
}
case SDL_FINGERDOWN: {
SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::BEGAN;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
dispatchEvent(touch);
break;
}
case SDL_FINGERMOTION: {
SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::MOVED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
dispatchEvent(touch);
break;
}
case SDL_KEYDOWN: {
SDL_KeyboardEvent &event = sdlEvent.key;
auto mode = event.keysym.mod;
keyboard.action = KeyboardEvent::Action::PRESS;
keyboard.key = sdlKeycodeToCocosCode(event.keysym.sym, mode);
keyboard.altKeyActive = mode & KMOD_ALT;
keyboard.ctrlKeyActive = mode & KMOD_CTRL;
keyboard.shiftKeyActive = mode & KMOD_SHIFT;
//CC_LOG_DEBUG("==> key %d -> code %d", event.keysym.sym, keyboard.key);
//cc::EventDispatcher::dispatchKeyboardEvent(keyboard);
dispatchEvent(keyboard);
break;
}
case SDL_KEYUP: {
SDL_KeyboardEvent &event = sdlEvent.key;
auto mode = event.keysym.mod;
keyboard.action = KeyboardEvent::Action::RELEASE;
keyboard.key = sdlKeycodeToCocosCode(event.keysym.sym, mode);
keyboard.altKeyActive = mode & KMOD_ALT;
keyboard.ctrlKeyActive = mode & KMOD_CTRL;
keyboard.shiftKeyActive = mode & KMOD_SHIFT;
dispatchEvent(keyboard);
//cc::EventDispatcher::dispatchKeyboardEvent(keyboard);
break;
}
default:
break;
}
//_sdl->pollEvent(&_quit);
}
bool QnxPlatform::createWindow(const char *title,
@@ -404,21 +129,12 @@ bool QnxPlatform::createWindow(const char *title,
perror("screen_create_window_buffers");
return EXIT_FAILURE;
}
// // Create window
// int sdlFlags = windowFlagsToSDLWindowFlag(flags);
// _handle = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, sdlFlags);
// if (_handle == nullptr) {
// // Display error message
// CC_LOG_ERROR("Window could not be created! SDL_Error: %s\n", SDL_GetError());
// return false;
// }
// _inited = true;
// return _sdl->createWindow(title, x, y, w, h, flags);
return true;
}
uintptr_t QnxPlatform::getWindowHandler() const {
//int32_t *window = (int32_t *)(_handle->driverdata);
// return _sdl->getWindowHandler();
return reinterpret_cast<uintptr_t>(_screenWin);
}
+1 -5
View File
@@ -29,9 +29,6 @@
#include "platform/UniversalPlatform.h"
#include "platform/qnx/modules/SystemWindow.h"
struct SDL_WindowEvent;
struct SDL_Window;
namespace cc {
class QnxPlatform : public UniversalPlatform,
@@ -59,11 +56,10 @@ public:
}
private:
SDL_Window* _handle;
void pollEvent() override;
void handleWindowEvent(SDL_WindowEvent& wevent);
bool _inited{false};
bool _quit{false};
// std::unique_ptr<SDLHelper> _sdl;
screen_context_t _screenCtx;
screen_window_t _screenWin;
};
+13 -306
View File
@@ -24,22 +24,19 @@
****************************************************************************/
#include "platform/win32/WindowsPlatform.h"
#include "sdl2/SDL.h"
#include "sdl2/SDL_main.h"
#include "sdl2/SDL_syswm.h"
#include <MMSystem.h>
#include <Windows.h>
#include <shellapi.h>
#include <sstream>
#include "platform/interfaces/OSInterface.h"
#include "platform/interfaces/modules/ISystemWindow.h"
#include "platform/SDLHelper.h"
namespace {
/**
@brief This function changes the PVRFrame show/hide setting in register.
@param bEnable If true show the PVRFrame window, otherwise hide.
*/
@brief This function changes the PVRFrame show/hide setting in register.
@param bEnable If true show the PVRFrame window, otherwise hide.
*/
void PVRFrameEnableControlWindow(bool bEnable) {
HKEY hKey = 0;
@@ -72,128 +69,14 @@ void PVRFrameEnableControlWindow(bool bEnable) {
RegCloseKey(hKey);
}
std::unordered_map<int, cc::KeyCode> gKeyMap = {
{SDLK_APPLICATION, cc::KeyCode::CONTEXT_MENU},
{SDLK_SCROLLLOCK, cc::KeyCode::SCROLLLOCK},
{SDLK_PAUSE, cc::KeyCode::PAUSE},
{SDLK_PRINTSCREEN, cc::KeyCode::PRINT_SCREEN},
{SDLK_INSERT, cc::KeyCode::INSERT},
{SDLK_ESCAPE, cc::KeyCode::ESCAPE},
{SDLK_MINUS, cc::KeyCode::MINUS},
{SDLK_LSHIFT, cc::KeyCode::SHIFT_LEFT},
{SDLK_RSHIFT, cc::KeyCode::SHIFT_RIGHT},
{SDLK_EQUALS, cc::KeyCode::EQUAL},
{SDLK_BACKSLASH, cc::KeyCode::BACKSLASH},
{SDLK_BACKQUOTE, cc::KeyCode::BACKQUOTE},
{SDLK_BACKSPACE, cc::KeyCode::BACKSPACE},
{SDLK_RETURN, cc::KeyCode::ENTER},
{SDLK_RETURN2, cc::KeyCode::ENTER},
{SDLK_LEFTBRACKET, cc::KeyCode::BRACKET_LEFT},
{SDLK_RIGHTBRACKET, cc::KeyCode::BRACKET_RIGHT},
{SDLK_SEMICOLON, cc::KeyCode::SEMICOLON},
{SDLK_QUOTE, cc::KeyCode::QUOTE},
{SDLK_TAB, cc::KeyCode::TAB},
{SDLK_LCTRL, cc::KeyCode::CONTROL_LEFT},
{SDLK_RCTRL, cc::KeyCode::CONTROL_RIGHT},
{SDLK_LALT, cc::KeyCode::ALT_LEFT},
{SDLK_RALT, cc::KeyCode::ALT_RIGHT},
{SDLK_LEFT, cc::KeyCode::ARROW_LEFT},
{SDLK_RIGHT, cc::KeyCode::ARROW_RIGHT},
{SDLK_UP, cc::KeyCode::ARROW_UP},
{SDLK_DOWN, cc::KeyCode::ARROW_DOWN},
{SDLK_KP_ENTER, cc::KeyCode::NUMPAD_ENTER},
{SDLK_KP_PLUS, cc::KeyCode::NUMPAD_PLUS},
{SDLK_KP_MULTIPLY, cc::KeyCode::NUMPAD_MULTIPLY},
{SDLK_KP_DIVIDE, cc::KeyCode::NUMPAD_DIVIDE},
{SDLK_KP_MINUS, cc::KeyCode::NUMPAD_MINUS},
{SDLK_KP_PERIOD, cc::KeyCode::NUMPAD_DECIMAL},
{SDLK_KP_BACKSPACE, cc::KeyCode::BACKSPACE},
{SDLK_NUMLOCKCLEAR, cc::KeyCode::NUM_LOCK},
{SDLK_HOME, cc::KeyCode::HOME},
{SDLK_PAGEUP, cc::KeyCode::PAGE_UP},
{SDLK_PAGEDOWN, cc::KeyCode::PAGE_DOWN},
{SDLK_END, cc::KeyCode::END},
{SDLK_COMMA, cc::KeyCode::COMMA},
{SDLK_PERIOD, cc::KeyCode::PERIOD},
{SDLK_SLASH, cc::KeyCode::SLASH},
{SDLK_SPACE, cc::KeyCode::SPACE},
{SDLK_DELETE, cc::KeyCode::DELETE_KEY},
{SDLK_CAPSLOCK, cc::KeyCode::CAPS_LOCK},
{SDLK_KP_0, cc::KeyCode::NUMPAD_0},
{SDLK_KP_1, cc::KeyCode::NUMPAD_1},
{SDLK_KP_2, cc::KeyCode::NUMPAD_2},
{SDLK_KP_3, cc::KeyCode::NUMPAD_3},
{SDLK_KP_4, cc::KeyCode::NUMPAD_4},
{SDLK_KP_5, cc::KeyCode::NUMPAD_5},
{SDLK_KP_6, cc::KeyCode::NUMPAD_6},
{SDLK_KP_7, cc::KeyCode::NUMPAD_7},
{SDLK_KP_8, cc::KeyCode::NUMPAD_8},
{SDLK_KP_9, cc::KeyCode::NUMPAD_9}};
int sdlKeycodeToCocosCode(int sdlCode, int mode) {
auto it = gKeyMap.find(sdlCode);
if (it != gKeyMap.end()) {
return static_cast<int>(it->second);
}
if (sdlCode >= SDLK_F1 && sdlCode <= SDLK_F12) {
// F1 ~ F12
return 112 + (sdlCode - SDLK_F1);
} else {
int code = sdlCode & (~(1 << 30));
if (code >= SDLK_a && code <= SDLK_z) {
return 'A' + (code - SDLK_a);
} else {
return code;
}
}
}
std::unordered_map<cc::ISystemWindow::WindowFlags, SDL_WindowFlags> gWindowFlagMap = {
{cc::ISystemWindow::CC_WINDOW_FULLSCREEN, SDL_WINDOW_FULLSCREEN},
{cc::ISystemWindow::CC_WINDOW_OPENGL, SDL_WINDOW_OPENGL},
{cc::ISystemWindow::CC_WINDOW_SHOWN, SDL_WINDOW_SHOWN},
{cc::ISystemWindow::CC_WINDOW_HIDDEN, SDL_WINDOW_HIDDEN},
{cc::ISystemWindow::CC_WINDOW_BORDERLESS, SDL_WINDOW_BORDERLESS},
{cc::ISystemWindow::CC_WINDOW_RESIZABLE, SDL_WINDOW_RESIZABLE},
{cc::ISystemWindow::CC_WINDOW_MINIMIZED, SDL_WINDOW_MINIMIZED},
{cc::ISystemWindow::CC_WINDOW_MAXIMIZED, SDL_WINDOW_MAXIMIZED},
{cc::ISystemWindow::CC_WINDOW_INPUT_GRABBED, SDL_WINDOW_INPUT_GRABBED},
{cc::ISystemWindow::CC_WINDOW_INPUT_FOCUS, SDL_WINDOW_INPUT_FOCUS},
{cc::ISystemWindow::CC_WINDOW_MOUSE_FOCUS, SDL_WINDOW_MOUSE_FOCUS},
{cc::ISystemWindow::CC_WINDOW_FULLSCREEN_DESKTOP, SDL_WINDOW_FULLSCREEN_DESKTOP},
{cc::ISystemWindow::CC_WINDOW_FOREIGN, SDL_WINDOW_FOREIGN},
{cc::ISystemWindow::CC_WINDOW_ALLOW_HIGHDPI, SDL_WINDOW_ALLOW_HIGHDPI},
{cc::ISystemWindow::CC_WINDOW_MOUSE_CAPTURE, SDL_WINDOW_MOUSE_CAPTURE},
{cc::ISystemWindow::CC_WINDOW_ALWAYS_ON_TOP, SDL_WINDOW_ALWAYS_ON_TOP},
{cc::ISystemWindow::CC_WINDOW_SKIP_TASKBAR, SDL_WINDOW_SKIP_TASKBAR},
{cc::ISystemWindow::CC_WINDOW_UTILITY, SDL_WINDOW_UTILITY},
{cc::ISystemWindow::CC_WINDOW_TOOLTIP, SDL_WINDOW_TOOLTIP},
{cc::ISystemWindow::CC_WINDOW_POPUP_MENU, SDL_WINDOW_POPUP_MENU},
{cc::ISystemWindow::CC_WINDOW_VULKAN, SDL_WINDOW_VULKAN}};
int windowFlagsToSDLWindowFlag(int flags) {
int sdlFlags = 0;
for (auto it : gWindowFlagMap) {
if ((it.first & flags) == it.first) {
sdlFlags |= it.second;
}
}
return sdlFlags;
}
} // namespace
namespace cc {
WindowsPlatform::WindowsPlatform() = default;
WindowsPlatform::WindowsPlatform() {
_sdl = std::make_unique<SDLHelper>(this);
}
WindowsPlatform::~WindowsPlatform() {
if (_handle) {
SDL_DestroyWindow(_handle);
_handle = nullptr;
}
if (_inited) {
SDL_Quit();
}
#ifdef USE_WIN32_CONSOLE
FreeConsole();
#endif
@@ -210,12 +93,7 @@ int32_t WindowsPlatform::init() {
PVRFrameEnableControlWindow(false);
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
// Display error message
CC_LOG_ERROR("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return false;
}
return 0;
return _sdl->init() ? 0 : -1;
}
int32_t WindowsPlatform::loop() {
@@ -254,7 +132,7 @@ int32_t WindowsPlatform::loop() {
if (actualInterval >= desiredInterval) {
nLast.QuadPart = nNow.QuadPart;
runTask();
SDL_GL_SwapWindow(_handle);
_sdl->swapWindow();
} else {
// The precision of timer on Windows is set to highest (1ms) by 'timeBeginPeriod' from above code,
// but it's still not precise enough. For example, if the precision of timer is 1ms,
@@ -274,190 +152,19 @@ int32_t WindowsPlatform::loop() {
return 0;
}
void WindowsPlatform::dispatchWindowEvent(const SDL_WindowEvent &wevent) {
WindowEvent ev;
switch (wevent.event) {
case SDL_WINDOWEVENT_SHOWN: {
ev.type = WindowEvent::Type::SHOW;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_RESTORED: {
ev.type = WindowEvent::Type::RESTORED;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_SIZE_CHANGED: {
ev.type = WindowEvent::Type::SIZE_CHANGED;
ev.width = wevent.data1;
ev.height = wevent.data2;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_RESIZED: {
ev.type = WindowEvent::Type::RESIZED;
ev.width = wevent.data1;
ev.height = wevent.data2;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_HIDDEN: {
ev.type = WindowEvent::Type::HIDDEN;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_MINIMIZED: {
ev.type = WindowEvent::Type::MINIMIZED;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT_ENTER: {
SDL_CaptureMouse(SDL_TRUE);
break;
}
case SDL_WINDOWEVENT_CLOSE: {
ev.type = WindowEvent::Type::CLOSE;
dispatchEvent(ev);
break;
}
}
}
void WindowsPlatform::dispatchSDLEvent(const SDL_Event &sdlEvent) {
cc::TouchEvent touch;
cc::MouseEvent mouse;
cc::KeyboardEvent keyboard;
switch (sdlEvent.type) {
case SDL_QUIT: {
_quit = true;
WindowEvent ev;
ev.type = WindowEvent::Type::QUIT;
dispatchEvent(ev);
break;
}
case SDL_WINDOWEVENT: {
dispatchWindowEvent(sdlEvent.window);
break;
}
case SDL_MOUSEBUTTONDOWN: {
const SDL_MouseButtonEvent &event = sdlEvent.button;
mouse.type = MouseEvent::Type::DOWN;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
/*if (0 > mouse.x || mouse.x > this->_width || 0 > mouse.y || mouse.y > this->_height) {
break;
}*/
mouse.button = event.button - 1;
dispatchEvent(mouse);
break;
}
case SDL_MOUSEBUTTONUP: {
const SDL_MouseButtonEvent &event = sdlEvent.button;
mouse.type = MouseEvent::Type::UP;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = event.button - 1;
dispatchEvent(mouse);
break;
}
case SDL_MOUSEMOTION: {
const SDL_MouseMotionEvent &event = sdlEvent.motion;
mouse.type = MouseEvent::Type::MOVE;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = 0;
dispatchEvent(mouse);
break;
}
case SDL_MOUSEWHEEL: {
const SDL_MouseWheelEvent &event = sdlEvent.wheel;
mouse.type = MouseEvent::Type::WHEEL;
mouse.x = static_cast<float>(event.x);
mouse.y = static_cast<float>(event.y);
mouse.button = 0; //TODO: direction
dispatchEvent(mouse);
break;
}
case SDL_FINGERUP: {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::ENDED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
dispatchEvent(touch);
break;
}
case SDL_FINGERDOWN: {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::BEGAN;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
dispatchEvent(touch);
break;
}
case SDL_FINGERMOTION: {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::MOVED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
dispatchEvent(touch);
break;
}
case SDL_KEYDOWN: {
const SDL_KeyboardEvent &event = sdlEvent.key;
auto mode = event.keysym.mod;
keyboard.action = KeyboardEvent::Action::PRESS;
keyboard.key = sdlKeycodeToCocosCode(event.keysym.sym, mode);
keyboard.altKeyActive = mode & KMOD_ALT;
keyboard.ctrlKeyActive = mode & KMOD_CTRL;
keyboard.shiftKeyActive = mode & KMOD_SHIFT;
//CC_LOG_DEBUG("==> key %d -> code %d", event.keysym.sym, keyboard.key);
dispatchEvent(keyboard);
break;
}
case SDL_KEYUP: {
const SDL_KeyboardEvent &event = sdlEvent.key;
auto mode = event.keysym.mod;
keyboard.action = KeyboardEvent::Action::RELEASE;
keyboard.key = sdlKeycodeToCocosCode(event.keysym.sym, mode);
keyboard.altKeyActive = mode & KMOD_ALT;
keyboard.ctrlKeyActive = mode & KMOD_CTRL;
keyboard.shiftKeyActive = mode & KMOD_SHIFT;
dispatchEvent(keyboard);
break;
}
default:
break;
}
}
void WindowsPlatform::pollEvent() {
SDL_Event sdlEvent;
int cnt = 0;
while ((cnt = SDL_PollEvent(&sdlEvent)) != 0) {
dispatchSDLEvent(sdlEvent);
}
_sdl->pollEvent(&_quit);
}
bool WindowsPlatform::createWindow(const char *title,
int x, int y, int w,
int h, int flags) {
if (_inited) {
return true;
}
// Create window
int sdlFlags = windowFlagsToSDLWindowFlag(flags);
_handle = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, sdlFlags);
if (_handle == nullptr) {
// Display error message
CC_LOG_ERROR("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return false;
}
_inited = true;
return true;
return _sdl->createWindow(title, x, y, w, h, flags);
}
uintptr_t WindowsPlatform::getWindowHandler() const {
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(_handle, &wmInfo);
return reinterpret_cast<uintptr_t>(wmInfo.info.win.window);
return _sdl->getWindowHandler();
}
} // namespace cc
@@ -28,12 +28,8 @@
#include "platform/UniversalPlatform.h"
#include "platform/win32/modules/SystemWindow.h"
struct SDL_WindowEvent;
struct SDL_Window;
union SDL_Event;
namespace cc {
class SDLHelper;
class WindowsPlatform : public UniversalPlatform,
public SystemWindow::Delegate {
public:
@@ -57,11 +53,8 @@ public:
private:
void pollEvent() override;
void dispatchSDLEvent(const SDL_Event& sdlEvent);
void dispatchWindowEvent(const SDL_WindowEvent& wevent);
bool _inited{false};
bool _quit{false};
struct SDL_Window* _handle{nullptr};
std::unique_ptr<SDLHelper> _sdl;
};
} // namespace cc