diff options
Diffstat (limited to '3rdparty/bgfx/examples')
14 files changed, 249 insertions, 39 deletions
diff --git a/3rdparty/bgfx/examples/common/entry/entry.cpp b/3rdparty/bgfx/examples/common/entry/entry.cpp index 947d4204666..1007787a0bd 100644 --- a/3rdparty/bgfx/examples/common/entry/entry.cpp +++ b/3rdparty/bgfx/examples/common/entry/entry.cpp @@ -34,6 +34,43 @@ namespace entry } #endif // ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR + char keyToAscii(Key::Enum _key, uint8_t _modifiers) + { + const bool isAscii = (Key::Key0 <= _key && _key <= Key::KeyZ) + || (Key::Esc <= _key && _key <= Key::Minus); + if (!isAscii) + { + return '\0'; + } + + const bool isNumber = (Key::Key0 <= _key && _key <= Key::Key9); + if (isNumber) + { + return '0' + (_key - Key::Key0); + } + + const bool isChar = (Key::KeyA <= _key && _key <= Key::KeyZ); + if (isChar) + { + enum { ShiftMask = Modifier::LeftShift|Modifier::RightShift }; + + const bool shift = !!(_modifiers&ShiftMask); + return (shift ? 'A' : 'a') + (_key - Key::KeyA); + } + + switch (_key) + { + case Key::Esc: { return 0x1b; } break; + case Key::Return: { return 0x0d; } break; + case Key::Tab: { return 0x09; } break; + case Key::Space: { return 0xa0; } break; + case Key::Backspace: { return 0x08; } break; + case Key::Plus: { return 0x2b; } break; + case Key::Minus: { return 0x2d; } break; + default: { return '\0'; } break; + } + } + bool setOrToggle(uint32_t& _flags, const char* _name, uint32_t _bit, int _first, int _argc, char const* const* _argv) { if (0 == strcmp(_argv[_first], _name) ) diff --git a/3rdparty/bgfx/examples/common/entry/entry.h b/3rdparty/bgfx/examples/common/entry/entry.h index 7f6524b8a28..be1d35d75a2 100644 --- a/3rdparty/bgfx/examples/common/entry/entry.h +++ b/3rdparty/bgfx/examples/common/entry/entry.h @@ -211,6 +211,7 @@ namespace entry void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height); void setWindowTitle(WindowHandle _handle, const char* _title); void toggleWindowFrame(WindowHandle _handle); + void toggleFullscreen(WindowHandle _handle); void setMouseLock(WindowHandle _handle, bool _lock); struct WindowState diff --git a/3rdparty/bgfx/examples/common/entry/entry_android.cpp b/3rdparty/bgfx/examples/common/entry/entry_android.cpp index 5ad35d1c13c..2d7c852c86c 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_android.cpp +++ b/3rdparty/bgfx/examples/common/entry/entry_android.cpp @@ -440,6 +440,11 @@ namespace entry BX_UNUSED(_handle); } + void toggleFullscreen(WindowHandle _handle) + { + BX_UNUSED(_handle); + } + void setMouseLock(WindowHandle _handle, bool _lock) { BX_UNUSED(_handle, _lock); diff --git a/3rdparty/bgfx/examples/common/entry/entry_asmjs.cpp b/3rdparty/bgfx/examples/common/entry/entry_asmjs.cpp index 6c221246e30..3ac5497b1d0 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_asmjs.cpp +++ b/3rdparty/bgfx/examples/common/entry/entry_asmjs.cpp @@ -59,6 +59,11 @@ namespace entry BX_UNUSED(_handle); } + void toggleFullscreen(WindowHandle _handle) + { + BX_UNUSED(_handle); + } + void setMouseLock(WindowHandle _handle, bool _lock) { BX_UNUSED(_handle, _lock); diff --git a/3rdparty/bgfx/examples/common/entry/entry_ios.mm b/3rdparty/bgfx/examples/common/entry/entry_ios.mm index 9a9a2d6aa4b..8fc67ffe8c7 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_ios.mm +++ b/3rdparty/bgfx/examples/common/entry/entry_ios.mm @@ -126,6 +126,11 @@ namespace entry BX_UNUSED(_handle); } + void toggleFullscreen(WindowHandle _handle) + { + BX_UNUSED(_handle); + } + void setMouseLock(WindowHandle _handle, bool _lock) { BX_UNUSED(_handle, _lock); diff --git a/3rdparty/bgfx/examples/common/entry/entry_nacl.cpp b/3rdparty/bgfx/examples/common/entry/entry_nacl.cpp index fca37180cc9..8d6fac7baff 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_nacl.cpp +++ b/3rdparty/bgfx/examples/common/entry/entry_nacl.cpp @@ -94,6 +94,11 @@ namespace entry BX_UNUSED(_handle); } + void toggleFullscreen(WindowHandle _handle) + { + BX_UNUSED(_handle); + } + void setMouseLock(WindowHandle _handle, bool _lock) { BX_UNUSED(_handle, _lock); diff --git a/3rdparty/bgfx/examples/common/entry/entry_osx.mm b/3rdparty/bgfx/examples/common/entry/entry_osx.mm index 51e9d86be00..b46fbdb4b9f 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_osx.mm +++ b/3rdparty/bgfx/examples/common/entry/entry_osx.mm @@ -81,7 +81,9 @@ namespace entry , m_mx(0) , m_my(0) , m_scroll(0) + , m_style(0) , m_exit(false) + , m_fullscreen(false) { s_translateKey[27] = Key::Esc; s_translateKey[13] = Key::Return; @@ -138,7 +140,7 @@ namespace entry NSWindow* window = m_window[handle.idx]; NSRect originalFrame = [window frame]; NSPoint location = [window mouseLocationOutsideOfEventStream]; - NSRect adjustFrame = [NSWindow contentRectForFrameRect: originalFrame styleMask: NSTitledWindowMask]; + NSRect adjustFrame = [window contentRectForFrameRect: originalFrame]; int x = location.x; int y = (int)adjustFrame.size.height - (int)location.y; @@ -246,9 +248,9 @@ namespace entry case NSLeftMouseDown: { // TODO: remove! - // Shift + Left Mouse Button acts as middle! This just a temporary solution! + // Command + Left Mouse Button acts as middle! This just a temporary solution! // This is becase the average OSX user doesn't have middle mouse click. - MouseButton::Enum mb = ([event modifierFlags] & NSShiftKeyMask) ? MouseButton::Middle : MouseButton::Left; + MouseButton::Enum mb = ([event modifierFlags] & NSCommandKeyMask) ? MouseButton::Middle : MouseButton::Left; m_eventQueue.postMouseEvent(s_defaultWindow, m_mx, m_my, m_scroll, mb, true); break; } @@ -306,16 +308,22 @@ namespace entry { m_eventQueue.postExitEvent(); } - else if ( (Key::Key0 <= key && key <= Key::KeyZ) - || (Key::Esc <= key && key <= Key::Minus) ) - { - m_eventQueue.postCharEvent(s_defaultWindow, 1, pressedChar); - return false; - } else { - m_eventQueue.postKeyEvent(s_defaultWindow, key, modifiers, true); - return false; + enum { ShiftMask = Modifier::LeftShift|Modifier::RightShift }; + const bool nonShiftModifiers = (0 != (modifiers&(~ShiftMask) ) ); + const bool isCharPressed = (Key::Key0 <= key && key <= Key::KeyZ) || (Key::Esc <= key && key <= Key::Minus); + const bool isText = isCharPressed && !nonShiftModifiers; + if (isText) + { + m_eventQueue.postCharEvent(s_defaultWindow, 1, pressedChar); + return false; + } + else + { + m_eventQueue.postKeyEvent(s_defaultWindow, key, modifiers, true); + return false; + } } } @@ -354,7 +362,7 @@ namespace entry WindowHandle handle = { 0 }; NSWindow* window = m_window[handle.idx]; NSRect originalFrame = [window frame]; - NSRect rect = [NSWindow contentRectForFrameRect: originalFrame styleMask: NSTitledWindowMask]; + NSRect rect = [window contentRectForFrameRect: originalFrame]; uint32_t width = uint32_t(rect.size.width); uint32_t height = uint32_t(rect.size.height); m_eventQueue.postSizeEvent(handle, width, height); @@ -398,26 +406,33 @@ namespace entry [menubar addItem:appMenuItem]; [NSApp setMainMenu:menubar]; + m_style = 0 + | NSTitledWindowMask + | NSClosableWindowMask + | NSMiniaturizableWindowMask + | NSResizableWindowMask + ; + + NSRect screenRect = [[NSScreen mainScreen] frame]; + const float centerX = (screenRect.size.width - (float)ENTRY_DEFAULT_WIDTH )*0.5f; + const float centerY = (screenRect.size.height - (float)ENTRY_DEFAULT_HEIGHT)*0.5f; + m_windowAlloc.alloc(); - NSRect rect = NSMakeRect(0, 0, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT); + NSRect rect = NSMakeRect(centerX, centerY, (float)ENTRY_DEFAULT_WIDTH, (float)ENTRY_DEFAULT_HEIGHT); NSWindow* window = [[NSWindow alloc] initWithContentRect:rect - styleMask:0 - |NSTitledWindowMask - |NSClosableWindowMask - |NSMiniaturizableWindowMask - |NSResizableWindowMask + styleMask:m_style backing:NSBackingStoreBuffered defer:NO ]; NSString* appName = [[NSProcessInfo processInfo] processName]; [window setTitle:appName]; - [window cascadeTopLeftFromPoint:NSMakePoint(20,20)]; [window makeKeyAndOrderFront:window]; [window setAcceptsMouseMovedEvents:YES]; [window setBackgroundColor:[NSColor blackColor]]; [[Window sharedDelegate] windowCreated:window]; m_window[0] = window; + m_windowFrame = [window frame]; bgfx::osxSetNSWindow(window); @@ -448,16 +463,24 @@ namespace entry return 0; } + bool isValid(WindowHandle _handle) + { + return m_windowAlloc.isValid(_handle.idx); + } + EventQueue m_eventQueue; bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc; NSWindow* m_window[ENTRY_CONFIG_MAX_WINDOWS]; + NSRect m_windowFrame; float m_scrollf; int32_t m_mx; int32_t m_my; int32_t m_scroll; + int32_t m_style; bool m_exit; + bool m_fullscreen; }; static Context s_ctx; @@ -486,27 +509,104 @@ namespace entry void destroyWindow(WindowHandle _handle) { - BX_UNUSED(_handle); + if (s_ctx.isValid(_handle) ) + { + dispatch_async(dispatch_get_main_queue() + , ^{ + [s_ctx.m_window[_handle.idx] performClose: nil]; + }); + } } void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y) { - BX_UNUSED(_handle, _x, _y); + if (s_ctx.isValid(_handle) ) + { + NSWindow* window = s_ctx.m_window[_handle.idx]; + NSScreen* screen = [window screen]; + + NSRect screenRect = [screen frame]; + CGFloat menuBarHeight = [[[NSApplication sharedApplication] mainMenu] menuBarHeight]; + + NSPoint position = { float(_x), screenRect.size.height - menuBarHeight - float(_y) }; + + dispatch_async(dispatch_get_main_queue() + , ^{ + [window setFrameTopLeftPoint: position]; + }); + } } void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height) { - BX_UNUSED(_handle, _width, _height); + if (s_ctx.isValid(_handle) ) + { + NSSize size = { float(_width), float(_height) }; + dispatch_async(dispatch_get_main_queue() + , ^{ + [s_ctx.m_window[_handle.idx] setContentSize: size]; + }); + } } void setWindowTitle(WindowHandle _handle, const char* _title) { - BX_UNUSED(_handle, _title); + if (s_ctx.isValid(_handle) ) + { + NSString* title = [[NSString alloc] initWithCString:_title encoding:1]; + dispatch_async(dispatch_get_main_queue() + , ^{ + [s_ctx.m_window[_handle.idx] setTitle: title]; + }); + [title release]; + } } void toggleWindowFrame(WindowHandle _handle) { - BX_UNUSED(_handle); + if (s_ctx.isValid(_handle) ) + { + s_ctx.m_style ^= NSTitledWindowMask; + dispatch_async(dispatch_get_main_queue() + , ^{ + [s_ctx.m_window[_handle.idx] setStyleMask: s_ctx.m_style]; + }); + } + } + + void toggleFullscreen(WindowHandle _handle) + { + if (s_ctx.isValid(_handle) ) + { + NSWindow* window = s_ctx.m_window[_handle.idx]; + NSScreen* screen = [window screen]; + NSRect screenRect = [screen frame]; + + if (!s_ctx.m_fullscreen) + { + [NSMenu setMenuBarVisible: false]; + s_ctx.m_style &= ~NSTitledWindowMask; + dispatch_async(dispatch_get_main_queue() + , ^{ + [window setStyleMask: s_ctx.m_style]; + [window setFrame:screenRect display:YES]; + }); + + s_ctx.m_fullscreen = true; + } + else + { + [NSMenu setMenuBarVisible: true]; + s_ctx.m_style |= NSTitledWindowMask; + dispatch_async(dispatch_get_main_queue() + , ^{ + [window setStyleMask: s_ctx.m_style]; + [window setFrame:s_ctx.m_windowFrame display:YES]; + }); + + s_ctx.m_fullscreen = false; + } + } } void setMouseLock(WindowHandle _handle, bool _lock) @@ -610,6 +710,7 @@ namespace entry using namespace entry; s_ctx.windowDidResize(); } + @end int main(int _argc, char** _argv) diff --git a/3rdparty/bgfx/examples/common/entry/entry_p.h b/3rdparty/bgfx/examples/common/entry/entry_p.h index 39a289ed737..74d4756b5cb 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_p.h +++ b/3rdparty/bgfx/examples/common/entry/entry_p.h @@ -56,6 +56,8 @@ namespace entry int main(int _argc, char** _argv); + char keyToAscii(Key::Enum _key, uint8_t _modifiers); + struct Event { enum Enum diff --git a/3rdparty/bgfx/examples/common/entry/entry_qnx.cpp b/3rdparty/bgfx/examples/common/entry/entry_qnx.cpp index 0ec4da6e1c8..c99393bb3ca 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_qnx.cpp +++ b/3rdparty/bgfx/examples/common/entry/entry_qnx.cpp @@ -59,6 +59,11 @@ namespace entry BX_UNUSED(_handle); } + void toggleFullscreen(WindowHandle _handle) + { + BX_UNUSED(_handle); + } + void setMouseLock(WindowHandle _handle, bool _lock) { BX_UNUSED(_handle, _lock); diff --git a/3rdparty/bgfx/examples/common/entry/entry_sdl.cpp b/3rdparty/bgfx/examples/common/entry/entry_sdl.cpp index e44ebcefddb..c279f0fd2a6 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_sdl.cpp +++ b/3rdparty/bgfx/examples/common/entry/entry_sdl.cpp @@ -179,6 +179,7 @@ namespace entry SDL_USER_WINDOW_SET_POS, SDL_USER_WINDOW_SET_SIZE, SDL_USER_WINDOW_TOGGLE_FRAME, + SDL_USER_WINDOW_TOGGLE_FULL_SCREEN, SDL_USER_WINDOW_MOUSE_LOCK, }; @@ -191,7 +192,7 @@ namespace entry union { void* p; WindowHandle h; } cast; cast.h = _handle; uev.data1 = cast.p; - + uev.data2 = _msg; uev.code = _code; SDL_PushEvent(&event); @@ -211,6 +212,7 @@ namespace entry , m_height(ENTRY_DEFAULT_HEIGHT) , m_aspectRatio(16.0f/9.0f) , m_mouseLock(false) + , m_fullscreen(false) { memset(s_translateKey, 0, sizeof(s_translateKey) ); initTranslateKey(SDL_SCANCODE_ESCAPE, Key::Esc); @@ -410,6 +412,32 @@ namespace entry break; case SDL_KEYDOWN: + { + const SDL_KeyboardEvent& kev = event.key; + WindowHandle handle = findHandle(kev.windowID); + if (isValid(handle) ) + { + uint8_t modifiers = translateKeyModifiers(kev.keysym.mod); + Key::Enum key = translateKey(kev.keysym.scancode); + + const uint8_t shiftMask = Modifier::LeftShift|Modifier::RightShift; + const bool nonShiftModifiers = (0 != (modifiers&(~shiftMask) ) ); + const bool isCharPressed = (Key::Key0 <= key && key <= Key::KeyZ) || (Key::Esc <= key && key <= Key::Minus); + const bool isText = isCharPressed && !nonShiftModifiers; + + if (isText) + { + uint8_t pressedChar[4]; + pressedChar[0] = keyToAscii(key, modifiers); + m_eventQueue.postCharEvent(handle, 1, pressedChar); + } + else + { + m_eventQueue.postKeyEvent(handle, key, modifiers, kev.state == SDL_PRESSED); + } + } + } + break; case SDL_KEYUP: { const SDL_KeyboardEvent& kev = event.key; @@ -615,6 +643,14 @@ namespace entry } break; + case SDL_USER_WINDOW_TOGGLE_FULL_SCREEN: + { + WindowHandle handle = getWindowHandle(uev); + m_fullscreen = !m_fullscreen; + SDL_SetWindowFullscreen(m_window[handle.idx], m_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); + } + break; + case SDL_USER_WINDOW_MOUSE_LOCK: { SDL_SetRelativeMouseMode(!!uev.code ? SDL_TRUE : SDL_FALSE); @@ -669,19 +705,6 @@ namespace entry 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); - } - SDL_SetWindowSize(m_window[_handle.idx], m_width, m_height); m_eventQueue.postSizeEvent(_handle, m_width, m_height); } @@ -723,6 +746,7 @@ namespace entry int32_t m_mx; int32_t m_my; bool m_mouseLock; + bool m_fullscreen; }; static Context s_ctx; @@ -805,6 +829,11 @@ namespace entry sdlPostEvent(SDL_USER_WINDOW_TOGGLE_FRAME, _handle); } + void toggleFullscreen(WindowHandle _handle) + { + sdlPostEvent(SDL_USER_WINDOW_TOGGLE_FULL_SCREEN, _handle); + } + void setMouseLock(WindowHandle _handle, bool _lock) { sdlPostEvent(SDL_USER_WINDOW_MOUSE_LOCK, _handle, NULL, _lock); diff --git a/3rdparty/bgfx/examples/common/entry/entry_windows.cpp b/3rdparty/bgfx/examples/common/entry/entry_windows.cpp index 335d5cb45b6..65fd6013c6b 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_windows.cpp +++ b/3rdparty/bgfx/examples/common/entry/entry_windows.cpp @@ -1067,6 +1067,11 @@ namespace entry PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0); } + void toggleFullscreen(WindowHandle _handle) + { + BX_UNUSED(_handle); + } + void setMouseLock(WindowHandle _handle, bool _lock) { PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_MOUSE_LOCK, _handle.idx, _lock); diff --git a/3rdparty/bgfx/examples/common/entry/entry_winrt.cpp b/3rdparty/bgfx/examples/common/entry/entry_winrt.cpp index f50b58a3960..e7cf8214de6 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_winrt.cpp +++ b/3rdparty/bgfx/examples/common/entry/entry_winrt.cpp @@ -175,6 +175,11 @@ namespace entry BX_UNUSED(_handle); } + void toggleFullscreen(WindowHandle _handle) + { + BX_UNUSED(_handle); + } + void setMouseLock(WindowHandle _handle, bool _lock) { BX_UNUSED(_handle, _lock); diff --git a/3rdparty/bgfx/examples/common/entry/entry_x11.cpp b/3rdparty/bgfx/examples/common/entry/entry_x11.cpp index 627263faa23..249b8a0483c 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_x11.cpp +++ b/3rdparty/bgfx/examples/common/entry/entry_x11.cpp @@ -664,6 +664,11 @@ namespace entry BX_UNUSED(_handle); } + void toggleFullscreen(WindowHandle _handle) + { + BX_UNUSED(_handle); + } + void setMouseLock(WindowHandle _handle, bool _lock) { BX_UNUSED(_handle, _lock); diff --git a/3rdparty/bgfx/examples/common/imgui/ocornut_imgui.cpp b/3rdparty/bgfx/examples/common/imgui/ocornut_imgui.cpp index e77b58a59e7..62cb1c87a79 100644 --- a/3rdparty/bgfx/examples/common/imgui/ocornut_imgui.cpp +++ b/3rdparty/bgfx/examples/common/imgui/ocornut_imgui.cpp @@ -165,7 +165,7 @@ struct OcornutImguiContext ImGui::NewFrame(); -// ImGui::ShowTestWindow(); + ImGui::ShowTestWindow(); } void endFrame() |