2013-08-12 23:47:41 -04:00
|
|
|
/*
|
2014-02-11 01:07:04 -05:00
|
|
|
* Copyright 2011-2014 Branimir Karadzic. All rights reserved.
|
2013-08-12 23:47:41 -04:00
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2013-08-15 00:08:46 -04:00
|
|
|
#include "entry_p.h"
|
|
|
|
|
|
|
|
#if ENTRY_CONFIG_USE_SDL
|
2013-08-12 23:47:41 -04:00
|
|
|
|
2013-08-15 00:08:46 -04:00
|
|
|
#if BX_PLATFORM_WINDOWS
|
|
|
|
# define SDL_MAIN_HANDLED
|
|
|
|
#endif // BX_PLATFORM_WINDOWS
|
2013-08-12 23:47:41 -04:00
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <bgfxplatform.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <bx/thread.h>
|
2014-10-11 23:42:42 -04:00
|
|
|
#include <bx/handlealloc.h>
|
|
|
|
#include <tinystl/allocator.h>
|
|
|
|
#include <tinystl/string.h>
|
2013-08-12 23:47:41 -04:00
|
|
|
|
|
|
|
namespace entry
|
|
|
|
{
|
2013-08-15 00:08:46 -04:00
|
|
|
static uint8_t translateKeyModifiers(uint16_t _sdl)
|
|
|
|
{
|
|
|
|
uint8_t modifiers = 0;
|
|
|
|
modifiers |= _sdl & KMOD_LALT ? Modifier::LeftAlt : 0;
|
|
|
|
modifiers |= _sdl & KMOD_RALT ? Modifier::RightAlt : 0;
|
|
|
|
modifiers |= _sdl & KMOD_LCTRL ? Modifier::LeftCtrl : 0;
|
|
|
|
modifiers |= _sdl & KMOD_RCTRL ? Modifier::RightCtrl : 0;
|
|
|
|
modifiers |= _sdl & KMOD_LSHIFT ? Modifier::LeftShift : 0;
|
|
|
|
modifiers |= _sdl & KMOD_RSHIFT ? Modifier::RightShift : 0;
|
|
|
|
modifiers |= _sdl & KMOD_LGUI ? Modifier::LeftMeta : 0;
|
|
|
|
modifiers |= _sdl & KMOD_RGUI ? Modifier::RightMeta : 0;
|
|
|
|
return modifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t s_translateKey[256];
|
|
|
|
|
|
|
|
static void initTranslateKey(uint16_t _sdl, Key::Enum _key)
|
|
|
|
{
|
|
|
|
BX_CHECK(_sdl < BX_COUNTOF(s_translateKey), "Out of bounds %d.", _sdl);
|
|
|
|
s_translateKey[_sdl&0xff] = (uint8_t)_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Key::Enum translateKey(SDL_Scancode _sdl)
|
|
|
|
{
|
|
|
|
return (Key::Enum)s_translateKey[_sdl&0xff];
|
|
|
|
}
|
|
|
|
|
2013-08-12 23:47:41 -04:00
|
|
|
struct MainThreadEntry
|
|
|
|
{
|
|
|
|
int m_argc;
|
|
|
|
char** m_argv;
|
|
|
|
|
|
|
|
static int32_t threadFunc(void* _userData);
|
|
|
|
};
|
|
|
|
|
2014-10-12 12:58:06 -04:00
|
|
|
///
|
2014-10-12 14:41:04 -04:00
|
|
|
static void* sdlNativeWindowHandle(SDL_Window* _window)
|
2014-10-12 12:58:06 -04:00
|
|
|
{
|
|
|
|
SDL_SysWMinfo wmi;
|
|
|
|
SDL_VERSION(&wmi.version);
|
|
|
|
if (!SDL_GetWindowWMInfo(_window, &wmi) )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
# if BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD
|
2014-10-12 14:41:04 -04:00
|
|
|
return (void*)wmi.info.x11.window;
|
2014-10-12 12:58:06 -04:00
|
|
|
# elif BX_PLATFORM_OSX
|
|
|
|
return wmi.info.cocoa.window;
|
|
|
|
# elif BX_PLATFORM_WINDOWS
|
|
|
|
return wmi.info.win.window;
|
|
|
|
# endif // BX_PLATFORM_
|
|
|
|
}
|
2013-08-15 00:08:46 -04:00
|
|
|
|
2014-10-11 23:42:42 -04:00
|
|
|
struct Msg
|
|
|
|
{
|
|
|
|
Msg()
|
|
|
|
: m_x(0)
|
|
|
|
, m_y(0)
|
|
|
|
, m_width(0)
|
|
|
|
, m_height(0)
|
|
|
|
, m_flags(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t m_x;
|
|
|
|
int32_t m_y;
|
|
|
|
uint32_t m_width;
|
|
|
|
uint32_t m_height;
|
|
|
|
uint32_t m_flags;
|
|
|
|
tinystl::string m_title;
|
|
|
|
};
|
2013-08-15 00:08:46 -04:00
|
|
|
|
2014-10-12 14:41:04 -04:00
|
|
|
static uint32_t s_userEventStart;
|
|
|
|
|
|
|
|
enum SDL_USER_WINDOW
|
|
|
|
{
|
|
|
|
SDL_USER_WINDOW_CREATE,
|
|
|
|
SDL_USER_WINDOW_DESTROY,
|
|
|
|
SDL_USER_WINDOW_SET_TITLE,
|
|
|
|
SDL_USER_WINDOW_SET_POS,
|
|
|
|
SDL_USER_WINDOW_SET_SIZE,
|
|
|
|
SDL_USER_WINDOW_TOGGLE_FRAME,
|
|
|
|
SDL_USER_WINDOW_MOUSE_LOCK,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void sdlPostEvent(SDL_USER_WINDOW _type, WindowHandle _handle, Msg* _msg = NULL, uint32_t _code = 0)
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
SDL_UserEvent& uev = event.user;
|
|
|
|
uev.type = s_userEventStart + _type;
|
|
|
|
|
|
|
|
union { void* p; WindowHandle h; } cast;
|
|
|
|
cast.h = _handle;
|
|
|
|
uev.data1 = cast.p;
|
|
|
|
|
|
|
|
uev.data2 = _msg;
|
|
|
|
uev.code = _code;
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static WindowHandle getWindowHandle(const SDL_UserEvent& _uev)
|
|
|
|
{
|
|
|
|
union { void* p; WindowHandle h; } cast;
|
|
|
|
cast.p = _uev.data1;
|
|
|
|
return cast.h;
|
|
|
|
}
|
|
|
|
|
2013-08-12 23:47:41 -04:00
|
|
|
struct Context
|
|
|
|
{
|
|
|
|
Context()
|
2014-10-11 23:42:42 -04:00
|
|
|
: m_width(ENTRY_DEFAULT_WIDTH)
|
2013-08-15 00:08:46 -04:00
|
|
|
, m_height(ENTRY_DEFAULT_HEIGHT)
|
|
|
|
, m_aspectRatio(16.0f/9.0f)
|
|
|
|
, m_mouseLock(false)
|
2013-08-12 23:47:41 -04:00
|
|
|
{
|
2013-08-15 00:08:46 -04:00
|
|
|
memset(s_translateKey, 0, sizeof(s_translateKey) );
|
|
|
|
initTranslateKey(SDL_SCANCODE_ESCAPE, Key::Esc);
|
|
|
|
initTranslateKey(SDL_SCANCODE_RETURN, Key::Return);
|
|
|
|
initTranslateKey(SDL_SCANCODE_TAB, Key::Tab);
|
|
|
|
initTranslateKey(SDL_SCANCODE_BACKSPACE, Key::Backspace);
|
|
|
|
initTranslateKey(SDL_SCANCODE_SPACE, Key::Space);
|
|
|
|
initTranslateKey(SDL_SCANCODE_UP, Key::Up);
|
|
|
|
initTranslateKey(SDL_SCANCODE_DOWN, Key::Down);
|
|
|
|
initTranslateKey(SDL_SCANCODE_LEFT, Key::Left);
|
|
|
|
initTranslateKey(SDL_SCANCODE_RIGHT, Key::Right);
|
|
|
|
initTranslateKey(SDL_SCANCODE_PAGEUP, Key::PageUp);
|
|
|
|
initTranslateKey(SDL_SCANCODE_PAGEDOWN, Key::PageDown);
|
|
|
|
initTranslateKey(SDL_SCANCODE_HOME, Key::Home);
|
|
|
|
initTranslateKey(SDL_SCANCODE_END, Key::End);
|
|
|
|
initTranslateKey(SDL_SCANCODE_PRINTSCREEN, Key::Print);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_PLUS, Key::Plus);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_MINUS, Key::Minus);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F1, Key::F1);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F2, Key::F2);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F3, Key::F3);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F4, Key::F4);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F5, Key::F5);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F6, Key::F6);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F7, Key::F7);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F8, Key::F8);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F9, Key::F9);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F10, Key::F10);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F11, Key::F11);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F12, Key::F12);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_0, Key::NumPad0);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_1, Key::NumPad1);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_2, Key::NumPad2);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_3, Key::NumPad3);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_4, Key::NumPad4);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_5, Key::NumPad5);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_6, Key::NumPad6);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_7, Key::NumPad7);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_8, Key::NumPad8);
|
|
|
|
initTranslateKey(SDL_SCANCODE_KP_9, Key::NumPad9);
|
|
|
|
initTranslateKey(SDL_SCANCODE_0, Key::Key0);
|
|
|
|
initTranslateKey(SDL_SCANCODE_1, Key::Key1);
|
|
|
|
initTranslateKey(SDL_SCANCODE_2, Key::Key2);
|
|
|
|
initTranslateKey(SDL_SCANCODE_3, Key::Key3);
|
|
|
|
initTranslateKey(SDL_SCANCODE_4, Key::Key4);
|
|
|
|
initTranslateKey(SDL_SCANCODE_5, Key::Key5);
|
|
|
|
initTranslateKey(SDL_SCANCODE_6, Key::Key6);
|
|
|
|
initTranslateKey(SDL_SCANCODE_7, Key::Key7);
|
|
|
|
initTranslateKey(SDL_SCANCODE_8, Key::Key8);
|
|
|
|
initTranslateKey(SDL_SCANCODE_9, Key::Key9);
|
|
|
|
initTranslateKey(SDL_SCANCODE_A, Key::KeyA);
|
|
|
|
initTranslateKey(SDL_SCANCODE_B, Key::KeyB);
|
|
|
|
initTranslateKey(SDL_SCANCODE_C, Key::KeyC);
|
|
|
|
initTranslateKey(SDL_SCANCODE_D, Key::KeyD);
|
|
|
|
initTranslateKey(SDL_SCANCODE_E, Key::KeyE);
|
|
|
|
initTranslateKey(SDL_SCANCODE_F, Key::KeyF);
|
|
|
|
initTranslateKey(SDL_SCANCODE_G, Key::KeyG);
|
|
|
|
initTranslateKey(SDL_SCANCODE_H, Key::KeyH);
|
|
|
|
initTranslateKey(SDL_SCANCODE_I, Key::KeyI);
|
|
|
|
initTranslateKey(SDL_SCANCODE_J, Key::KeyJ);
|
|
|
|
initTranslateKey(SDL_SCANCODE_K, Key::KeyK);
|
|
|
|
initTranslateKey(SDL_SCANCODE_L, Key::KeyL);
|
|
|
|
initTranslateKey(SDL_SCANCODE_M, Key::KeyM);
|
|
|
|
initTranslateKey(SDL_SCANCODE_N, Key::KeyN);
|
|
|
|
initTranslateKey(SDL_SCANCODE_O, Key::KeyO);
|
|
|
|
initTranslateKey(SDL_SCANCODE_P, Key::KeyP);
|
|
|
|
initTranslateKey(SDL_SCANCODE_Q, Key::KeyQ);
|
|
|
|
initTranslateKey(SDL_SCANCODE_R, Key::KeyR);
|
|
|
|
initTranslateKey(SDL_SCANCODE_S, Key::KeyS);
|
|
|
|
initTranslateKey(SDL_SCANCODE_T, Key::KeyT);
|
|
|
|
initTranslateKey(SDL_SCANCODE_U, Key::KeyU);
|
|
|
|
initTranslateKey(SDL_SCANCODE_V, Key::KeyV);
|
|
|
|
initTranslateKey(SDL_SCANCODE_W, Key::KeyW);
|
|
|
|
initTranslateKey(SDL_SCANCODE_X, Key::KeyX);
|
|
|
|
initTranslateKey(SDL_SCANCODE_Y, Key::KeyY);
|
|
|
|
initTranslateKey(SDL_SCANCODE_Z, Key::KeyZ);
|
2013-08-12 23:47:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
const char* argv[1] = { "sdl.so" };
|
|
|
|
m_mte.m_argc = 1;
|
|
|
|
m_mte.m_argv = const_cast<char**>(argv);
|
|
|
|
|
|
|
|
SDL_Init(SDL_INIT_VIDEO);
|
|
|
|
|
2014-10-11 23:42:42 -04:00
|
|
|
m_windowAlloc.alloc();
|
|
|
|
m_window[0] = SDL_CreateWindow("bgfx"
|
2013-08-12 23:47:41 -04:00
|
|
|
, SDL_WINDOWPOS_UNDEFINED
|
|
|
|
, SDL_WINDOWPOS_UNDEFINED
|
2013-08-15 00:08:46 -04:00
|
|
|
, m_width
|
|
|
|
, m_height
|
|
|
|
, SDL_WINDOW_SHOWN
|
|
|
|
| SDL_WINDOW_RESIZABLE
|
2013-08-12 23:47:41 -04:00
|
|
|
);
|
|
|
|
|
2014-10-11 23:42:42 -04:00
|
|
|
m_flags[0] = 0
|
|
|
|
| ENTRY_WINDOW_FLAG_ASPECT_RATIO
|
|
|
|
| ENTRY_WINDOW_FLAG_FRAME
|
|
|
|
;
|
|
|
|
|
|
|
|
s_userEventStart = SDL_RegisterEvents(7);
|
2013-08-15 00:08:46 -04:00
|
|
|
|
2014-10-11 23:42:42 -04:00
|
|
|
bgfx::sdlSetWindow(m_window[0]);
|
2014-10-12 00:55:24 -04:00
|
|
|
bgfx::renderFrame();
|
|
|
|
|
2013-08-12 23:47:41 -04:00
|
|
|
m_thread.init(MainThreadEntry::threadFunc, &m_mte);
|
|
|
|
|
2013-08-15 00:08:46 -04:00
|
|
|
// Force window resolution...
|
2014-09-21 12:52:55 -04:00
|
|
|
WindowHandle defaultWindow = { 0 };
|
|
|
|
setWindowSize(defaultWindow, m_width, m_height, true);
|
2013-08-15 00:08:46 -04:00
|
|
|
|
2013-08-12 23:47:41 -04:00
|
|
|
bool exit = false;
|
|
|
|
SDL_Event event;
|
2014-10-12 00:55:24 -04:00
|
|
|
while (!exit)
|
2013-08-12 23:47:41 -04:00
|
|
|
{
|
2014-10-12 00:55:24 -04:00
|
|
|
bgfx::renderFrame();
|
2013-08-15 00:08:46 -04:00
|
|
|
|
2014-10-12 00:55:24 -04:00
|
|
|
while (SDL_PollEvent(&event) )
|
|
|
|
{
|
|
|
|
switch (event.type)
|
2013-08-15 00:08:46 -04:00
|
|
|
{
|
2014-10-12 00:55:24 -04:00
|
|
|
case SDL_QUIT:
|
|
|
|
m_eventQueue.postExitEvent();
|
|
|
|
exit = true;
|
|
|
|
break;
|
2013-08-15 00:08:46 -04:00
|
|
|
|
2014-10-12 00:55:24 -04:00
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
{
|
|
|
|
const SDL_MouseMotionEvent& mev = event.motion;
|
2014-10-12 14:41:04 -04:00
|
|
|
WindowHandle handle = findHandle(mev.windowID);
|
|
|
|
m_eventQueue.postMouseEvent(handle, mev.x, mev.y, 0);
|
2014-10-12 00:55:24 -04:00
|
|
|
}
|
|
|
|
break;
|
2013-08-15 00:08:46 -04:00
|
|
|
|
2014-10-12 00:55:24 -04:00
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
|
|
|
{
|
|
|
|
const SDL_MouseButtonEvent& mev = event.button;
|
2014-10-12 14:41:04 -04:00
|
|
|
WindowHandle handle = findHandle(mev.windowID);
|
|
|
|
m_eventQueue.postMouseEvent(handle, mev.x, mev.y, 0, MouseButton::Left, mev.type == SDL_MOUSEBUTTONDOWN);
|
2014-10-12 00:55:24 -04:00
|
|
|
}
|
|
|
|
break;
|
2013-08-15 00:08:46 -04:00
|
|
|
|
2014-10-12 00:55:24 -04:00
|
|
|
case SDL_KEYDOWN:
|
|
|
|
case SDL_KEYUP:
|
2013-08-15 00:08:46 -04:00
|
|
|
{
|
2014-10-12 00:55:24 -04:00
|
|
|
const SDL_KeyboardEvent& kev = event.key;
|
2014-10-12 14:41:04 -04:00
|
|
|
WindowHandle handle = findHandle(kev.windowID);
|
2014-10-12 00:55:24 -04:00
|
|
|
uint8_t modifiers = translateKeyModifiers(kev.keysym.mod);
|
|
|
|
Key::Enum key = translateKey(kev.keysym.scancode);
|
2014-10-12 14:41:04 -04:00
|
|
|
m_eventQueue.postKeyEvent(handle, key, modifiers, kev.state == SDL_PRESSED);
|
2013-08-15 00:08:46 -04:00
|
|
|
}
|
2014-10-12 00:55:24 -04:00
|
|
|
break;
|
2013-08-15 00:08:46 -04:00
|
|
|
|
2014-10-12 00:55:24 -04:00
|
|
|
case SDL_WINDOWEVENT:
|
2014-10-11 23:42:42 -04:00
|
|
|
{
|
2014-10-12 00:55:24 -04:00
|
|
|
const SDL_WindowEvent& wev = event.window;
|
|
|
|
switch (wev.event)
|
2014-10-11 23:42:42 -04:00
|
|
|
{
|
2014-10-12 00:55:24 -04:00
|
|
|
case SDL_WINDOWEVENT_RESIZED:
|
|
|
|
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
2014-10-12 14:41:04 -04:00
|
|
|
{
|
|
|
|
WindowHandle handle = findHandle(wev.windowID);
|
|
|
|
setWindowSize(handle, wev.data1, wev.data2);
|
|
|
|
}
|
2014-10-12 00:55:24 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_SHOWN:
|
|
|
|
case SDL_WINDOWEVENT_HIDDEN:
|
|
|
|
case SDL_WINDOWEVENT_EXPOSED:
|
|
|
|
case SDL_WINDOWEVENT_MOVED:
|
|
|
|
case SDL_WINDOWEVENT_MINIMIZED:
|
|
|
|
case SDL_WINDOWEVENT_MAXIMIZED:
|
|
|
|
case SDL_WINDOWEVENT_RESTORED:
|
|
|
|
case SDL_WINDOWEVENT_ENTER:
|
|
|
|
case SDL_WINDOWEVENT_LEAVE:
|
|
|
|
case SDL_WINDOWEVENT_FOCUS_GAINED:
|
|
|
|
case SDL_WINDOWEVENT_FOCUS_LOST:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_CLOSE:
|
2014-10-12 14:41:04 -04:00
|
|
|
{
|
|
|
|
WindowHandle handle = findHandle(wev.windowID);
|
|
|
|
if (0 == handle.idx)
|
|
|
|
{
|
|
|
|
m_eventQueue.postExitEvent();
|
|
|
|
exit = true;
|
|
|
|
}
|
|
|
|
}
|
2014-10-12 00:55:24 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2014-10-12 12:58:06 -04:00
|
|
|
const SDL_UserEvent& uev = event.user;
|
|
|
|
switch (uev.type - s_userEventStart)
|
2014-10-12 00:55:24 -04:00
|
|
|
{
|
2014-10-12 12:58:06 -04:00
|
|
|
case SDL_USER_WINDOW_CREATE:
|
2014-10-12 00:55:24 -04:00
|
|
|
{
|
2014-10-12 14:41:04 -04:00
|
|
|
WindowHandle handle = getWindowHandle(uev);
|
2014-10-12 12:58:06 -04:00
|
|
|
Msg* msg = (Msg*)uev.data2;
|
|
|
|
|
|
|
|
m_window[handle.idx] = SDL_CreateWindow(msg->m_title.c_str()
|
|
|
|
, msg->m_x
|
|
|
|
, msg->m_y
|
|
|
|
, msg->m_width
|
|
|
|
, msg->m_height
|
|
|
|
, SDL_WINDOW_SHOWN
|
|
|
|
| SDL_WINDOW_RESIZABLE
|
|
|
|
);
|
|
|
|
|
|
|
|
m_flags[handle.idx] = msg->m_flags;
|
|
|
|
|
|
|
|
void* nwh = sdlNativeWindowHandle(m_window[handle.idx]);
|
|
|
|
if (NULL != nwh)
|
|
|
|
{
|
|
|
|
m_eventQueue.postWindowEvent(handle, nwh);
|
|
|
|
m_eventQueue.postSizeEvent(handle, msg->m_width, msg->m_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete msg;
|
2014-10-12 00:55:24 -04:00
|
|
|
}
|
2014-10-12 12:58:06 -04:00
|
|
|
break;
|
2014-10-11 23:42:42 -04:00
|
|
|
|
2014-10-12 12:58:06 -04:00
|
|
|
case SDL_USER_WINDOW_DESTROY:
|
|
|
|
{
|
2014-10-12 14:41:04 -04:00
|
|
|
WindowHandle handle = getWindowHandle(uev);
|
2014-10-12 12:58:06 -04:00
|
|
|
m_eventQueue.postWindowEvent(handle);
|
|
|
|
SDL_DestroyWindow(m_window[handle.idx]);
|
|
|
|
m_window[handle.idx] = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_USER_WINDOW_SET_TITLE:
|
|
|
|
{
|
2014-10-12 14:41:04 -04:00
|
|
|
WindowHandle handle = getWindowHandle(uev);
|
2014-10-12 12:58:06 -04:00
|
|
|
Msg* msg = (Msg*)uev.data2;
|
|
|
|
SDL_SetWindowTitle(m_window[handle.idx], msg->m_title.c_str());
|
|
|
|
delete msg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_USER_WINDOW_SET_POS:
|
|
|
|
{
|
2014-10-12 14:41:04 -04:00
|
|
|
WindowHandle handle = getWindowHandle(uev);
|
2014-10-12 12:58:06 -04:00
|
|
|
Msg* msg = (Msg*)uev.data2;
|
|
|
|
SDL_SetWindowPosition(m_window[handle.idx], msg->m_x, msg->m_y);
|
|
|
|
delete msg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_USER_WINDOW_SET_SIZE:
|
|
|
|
{
|
2014-10-12 14:41:04 -04:00
|
|
|
WindowHandle handle = getWindowHandle(uev);
|
|
|
|
Msg* msg = (Msg*)uev.data2;
|
|
|
|
setWindowSize(handle, msg->m_width, msg->m_height);
|
2014-10-12 12:58:06 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_USER_WINDOW_TOGGLE_FRAME:
|
|
|
|
{
|
2014-10-12 14:41:04 -04:00
|
|
|
WindowHandle handle = getWindowHandle(uev);
|
2014-10-12 12:58:06 -04:00
|
|
|
m_flags[handle.idx] ^= ENTRY_WINDOW_FLAG_FRAME;
|
|
|
|
SDL_SetWindowBordered(m_window[handle.idx], (SDL_bool)!!(m_flags[handle.idx] & ENTRY_WINDOW_FLAG_FRAME) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_USER_WINDOW_MOUSE_LOCK:
|
|
|
|
{
|
|
|
|
SDL_SetRelativeMouseMode(!!uev.code ? SDL_TRUE : SDL_FALSE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2014-10-12 00:55:24 -04:00
|
|
|
}
|
2013-08-15 00:08:46 -04:00
|
|
|
}
|
2014-10-12 00:55:24 -04:00
|
|
|
break;
|
2013-08-15 00:08:46 -04:00
|
|
|
}
|
2013-08-12 23:47:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 00:55:24 -04:00
|
|
|
while (bgfx::RenderFrame::NoContext != bgfx::renderFrame() ) {};
|
2013-08-12 23:47:41 -04:00
|
|
|
m_thread.shutdown();
|
|
|
|
|
2014-10-11 23:42:42 -04:00
|
|
|
SDL_DestroyWindow(m_window[0]);
|
2013-08-12 23:47:41 -04:00
|
|
|
SDL_Quit();
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:42:42 -04:00
|
|
|
WindowHandle findHandle(uint32_t _windowId)
|
2013-08-15 00:08:46 -04:00
|
|
|
{
|
2014-10-11 23:42:42 -04:00
|
|
|
SDL_Window* window = SDL_GetWindowFromID(_windowId);
|
|
|
|
return findHandle(window);
|
2013-08-15 00:08:46 -04:00
|
|
|
}
|
|
|
|
|
2014-10-11 23:42:42 -04:00
|
|
|
WindowHandle findHandle(SDL_Window* _window)
|
2013-08-15 00:08:46 -04:00
|
|
|
{
|
2014-10-11 23:42:42 -04:00
|
|
|
bx::LwMutexScope scope(m_lock);
|
|
|
|
for (uint32_t ii = 0, num = m_windowAlloc.getNumHandles(); ii < num; ++ii)
|
|
|
|
{
|
|
|
|
uint16_t idx = m_windowAlloc.getHandleAt(ii);
|
|
|
|
if (_window == m_window[idx])
|
|
|
|
{
|
|
|
|
WindowHandle handle = { idx };
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowHandle invalid = { UINT16_MAX };
|
|
|
|
return invalid;
|
2013-08-15 00:08:46 -04:00
|
|
|
}
|
|
|
|
|
2014-09-21 12:52:55 -04:00
|
|
|
void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height, bool _force = false)
|
2013-08-15 00:08:46 -04:00
|
|
|
{
|
|
|
|
if (_width != m_width
|
|
|
|
|| _height != m_height
|
|
|
|
|| _force)
|
|
|
|
{
|
|
|
|
m_width = _width;
|
|
|
|
m_height = _height;
|
|
|
|
|
|
|
|
if (m_width < m_height)
|
|
|
|
{
|
|
|
|
float aspectRatio = 1.0f/m_aspectRatio;
|
|
|
|
m_width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, m_width);
|
|
|
|
m_height = uint32_t(float(m_width)*aspectRatio);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float aspectRatio = m_aspectRatio;
|
|
|
|
m_height = bx::uint32_max(ENTRY_DEFAULT_HEIGHT/4, m_height);
|
|
|
|
m_width = uint32_t(float(m_height)*aspectRatio);
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:42:42 -04:00
|
|
|
SDL_SetWindowSize(m_window[_handle.idx], m_width, m_height);
|
2014-09-21 12:52:55 -04:00
|
|
|
m_eventQueue.postSizeEvent(_handle, m_width, m_height);
|
2013-08-15 00:08:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 23:47:41 -04:00
|
|
|
MainThreadEntry m_mte;
|
|
|
|
bx::Thread m_thread;
|
|
|
|
|
|
|
|
EventQueue m_eventQueue;
|
2014-10-11 23:42:42 -04:00
|
|
|
bx::LwMutex m_lock;
|
2013-08-12 23:47:41 -04:00
|
|
|
|
2014-10-11 23:42:42 -04:00
|
|
|
bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
|
|
|
|
SDL_Window* m_window[ENTRY_CONFIG_MAX_WINDOWS];
|
|
|
|
uint32_t m_flags[ENTRY_CONFIG_MAX_WINDOWS];
|
2013-08-15 00:08:46 -04:00
|
|
|
|
|
|
|
uint32_t m_width;
|
|
|
|
uint32_t m_height;
|
|
|
|
float m_aspectRatio;
|
|
|
|
|
|
|
|
int32_t m_mx;
|
|
|
|
int32_t m_my;
|
|
|
|
bool m_mouseLock;
|
2013-08-12 23:47:41 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static Context s_ctx;
|
|
|
|
|
|
|
|
const Event* poll()
|
|
|
|
{
|
|
|
|
return s_ctx.m_eventQueue.poll();
|
|
|
|
}
|
|
|
|
|
2014-09-22 22:34:10 -04:00
|
|
|
const Event* poll(WindowHandle _handle)
|
|
|
|
{
|
|
|
|
return s_ctx.m_eventQueue.poll(_handle);
|
|
|
|
}
|
|
|
|
|
2013-08-12 23:47:41 -04:00
|
|
|
void release(const Event* _event)
|
|
|
|
{
|
|
|
|
s_ctx.m_eventQueue.release(_event);
|
|
|
|
}
|
|
|
|
|
2014-09-22 22:34:10 -04:00
|
|
|
WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
|
|
|
|
{
|
2014-10-11 23:42:42 -04:00
|
|
|
bx::LwMutexScope scope(s_ctx.m_lock);
|
|
|
|
WindowHandle handle = { s_ctx.m_windowAlloc.alloc() };
|
|
|
|
|
|
|
|
if (UINT16_MAX != handle.idx)
|
|
|
|
{
|
|
|
|
Msg* msg = new Msg;
|
|
|
|
msg->m_x = _x;
|
|
|
|
msg->m_y = _y;
|
|
|
|
msg->m_width = _width;
|
|
|
|
msg->m_height = _height;
|
|
|
|
msg->m_title = _title;
|
|
|
|
msg->m_flags = _flags;
|
|
|
|
|
2014-10-12 14:41:04 -04:00
|
|
|
sdlPostEvent(SDL_USER_WINDOW_CREATE, handle, msg);
|
2014-10-11 23:42:42 -04:00
|
|
|
}
|
|
|
|
|
2014-09-22 22:34:10 -04:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroyWindow(WindowHandle _handle)
|
|
|
|
{
|
2014-10-11 23:42:42 -04:00
|
|
|
if (UINT16_MAX != _handle.idx)
|
|
|
|
{
|
2014-10-12 14:41:04 -04:00
|
|
|
sdlPostEvent(SDL_USER_WINDOW_DESTROY, _handle);
|
2014-10-11 23:42:42 -04:00
|
|
|
|
|
|
|
bx::LwMutexScope scope(s_ctx.m_lock);
|
|
|
|
s_ctx.m_windowAlloc.free(_handle.idx);
|
|
|
|
}
|
2014-09-22 22:34:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
|
|
|
|
{
|
2014-10-11 23:42:42 -04:00
|
|
|
Msg* msg = new Msg;
|
|
|
|
msg->m_x = _x;
|
|
|
|
msg->m_y = _y;
|
|
|
|
|
2014-10-12 14:41:04 -04:00
|
|
|
sdlPostEvent(SDL_USER_WINDOW_SET_POS, _handle, msg);
|
2014-09-22 22:34:10 -04:00
|
|
|
}
|
|
|
|
|
2014-09-21 12:52:55 -04:00
|
|
|
void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
|
2013-08-12 23:47:41 -04:00
|
|
|
{
|
2014-10-12 14:41:04 -04:00
|
|
|
Msg* msg = new Msg;
|
|
|
|
msg->m_width = _width;
|
|
|
|
msg->m_height = _height;
|
2014-10-11 23:42:42 -04:00
|
|
|
|
2014-10-12 14:41:04 -04:00
|
|
|
sdlPostEvent(SDL_USER_WINDOW_SET_SIZE, _handle, msg);
|
2013-08-12 23:47:41 -04:00
|
|
|
}
|
|
|
|
|
2014-09-21 12:52:55 -04:00
|
|
|
void setWindowTitle(WindowHandle _handle, const char* _title)
|
2014-08-05 15:57:52 -04:00
|
|
|
{
|
2014-10-11 23:42:42 -04:00
|
|
|
Msg* msg = new Msg;
|
|
|
|
msg->m_title = _title;
|
|
|
|
|
2014-10-12 14:41:04 -04:00
|
|
|
sdlPostEvent(SDL_USER_WINDOW_SET_TITLE, _handle, msg);
|
2014-08-05 15:57:52 -04:00
|
|
|
}
|
|
|
|
|
2014-09-21 12:52:55 -04:00
|
|
|
void toggleWindowFrame(WindowHandle _handle)
|
2013-08-12 23:47:41 -04:00
|
|
|
{
|
2014-10-12 14:41:04 -04:00
|
|
|
sdlPostEvent(SDL_USER_WINDOW_TOGGLE_FRAME, _handle);
|
2013-08-12 23:47:41 -04:00
|
|
|
}
|
|
|
|
|
2014-09-21 12:52:55 -04:00
|
|
|
void setMouseLock(WindowHandle _handle, bool _lock)
|
2013-08-12 23:47:41 -04:00
|
|
|
{
|
2014-10-12 14:41:04 -04:00
|
|
|
sdlPostEvent(SDL_USER_WINDOW_MOUSE_LOCK, _handle, NULL, _lock);
|
2013-08-12 23:47:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t MainThreadEntry::threadFunc(void* _userData)
|
|
|
|
{
|
|
|
|
MainThreadEntry* self = (MainThreadEntry*)_userData;
|
|
|
|
int32_t result = main(self->m_argc, self->m_argv);
|
|
|
|
|
|
|
|
SDL_Event event;
|
2013-08-15 00:08:46 -04:00
|
|
|
SDL_QuitEvent& qev = event.quit;
|
|
|
|
qev.type = SDL_QUIT;
|
2013-08-12 23:47:41 -04:00
|
|
|
SDL_PushEvent(&event);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace entry
|
|
|
|
|
|
|
|
int main(int _argc, const char* _argv[])
|
|
|
|
{
|
2014-01-25 00:49:15 -05:00
|
|
|
BX_UNUSED(_argc, _argv);
|
2013-08-12 23:47:41 -04:00
|
|
|
using namespace entry;
|
|
|
|
s_ctx.run();
|
2013-08-15 00:08:46 -04:00
|
|
|
return 0;
|
2013-08-12 23:47:41 -04:00
|
|
|
}
|
|
|
|
|
2013-08-15 00:08:46 -04:00
|
|
|
#endif // ENTRY_CONFIG_USE_SDL
|