bgfx/examples/common/entry/entry_osx.mm

602 lines
14 KiB
Text
Raw Normal View History

/*
2014-02-11 01:18:39 -05:00
* Copyright 2011-2014 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
2013-08-15 00:08:46 -04:00
#include "entry_p.h"
2013-08-15 00:08:46 -04:00
#if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_OSX
2013-08-08 01:50:01 -04:00
#import <Cocoa/Cocoa.h>
2013-08-08 02:11:20 -04:00
#include <bgfxplatform.h>
2013-08-08 01:50:01 -04:00
#include <bx/uint32_t.h>
#include <bx/thread.h>
#include <bx/os.h>
#define DEFAULT_WIDTH 1280
#define DEFAULT_HEIGHT 720
2013-07-22 00:53:20 -04:00
@interface AppDelegate : NSObject<NSApplicationDelegate>
{
bool terminated;
}
2013-07-22 00:53:20 -04:00
+ (AppDelegate *)sharedDelegate;
- (id)init;
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
- (bool)applicationHasTerminated;
2013-07-22 00:53:20 -04:00
@end
2013-07-22 00:53:20 -04:00
@implementation AppDelegate
+ (AppDelegate *)sharedDelegate
{
static id delegate = [AppDelegate new];
return delegate;
}
2013-07-22 00:53:20 -04:00
- (id)init
{
self = [super init];
2013-07-22 00:53:20 -04:00
if (nil == self)
{
return nil;
}
2013-07-22 00:53:20 -04:00
self->terminated = false;
return self;
}
2013-07-22 00:53:20 -04:00
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
2013-12-07 13:19:54 -05:00
BX_UNUSED(sender);
self->terminated = true;
return NSTerminateCancel;
}
2013-07-22 00:53:20 -04:00
- (bool)applicationHasTerminated
{
return self->terminated;
}
2013-07-22 00:53:20 -04:00
@end
2013-07-22 00:53:20 -04:00
@interface Window : NSObject<NSWindowDelegate>
{
unsigned int windowCount;
}
2013-07-22 00:53:20 -04:00
+ (Window *)sharedDelegate;
- (id)init;
- (void)windowCreated:(NSWindow *)window;
- (BOOL)windowShouldClose:(NSWindow *)window;
2013-07-22 00:53:20 -04:00
@end
2013-07-22 00:53:20 -04:00
@implementation Window
+ (Window *)sharedDelegate
{
static id windowDelegate = [Window new];
return windowDelegate;
}
2013-07-22 00:53:20 -04:00
- (id)init
{
self = [super init];
2013-07-22 00:53:20 -04:00
if (nil == self)
{
return nil;
}
2013-07-22 00:53:20 -04:00
self->windowCount = 0;
return self;
}
2013-07-22 00:53:20 -04:00
- (void)windowCreated:(NSWindow *)window
{
assert(window);
2013-07-21 17:44:53 -04:00
[window setDelegate:self];
assert(self->windowCount < ~0u);
self->windowCount += 1;
}
2013-07-22 00:53:20 -04:00
- (BOOL)windowShouldClose:(NSWindow *)window
{
assert(window);
2013-07-21 17:44:53 -04:00
[window setDelegate:nil];
assert(self->windowCount);
self->windowCount -= 1;
2013-07-21 17:44:53 -04:00
2013-07-22 00:53:20 -04:00
if (self->windowCount == 0)
{
[NSApp terminate:self];
return false;
}
2013-07-22 00:53:20 -04:00
return true;
}
@end
// Scan codes on Mac taken from http://boredzo.org/blog/archives/2007-05-22/virtual-key-codes
#define KEY_RETURN 36
#define KEY_TAB 48
#define KEY_DELETE 51
#define KEY_ESCAPE 53
namespace entry
{
static WindowHandle s_defaultWindow = { 0 }; // TODO: Add support for more windows
static uint8_t s_translateKey[256];
struct MainThreadEntry
{
int m_argc;
char** m_argv;
2013-07-21 17:44:53 -04:00
static int32_t threadFunc(void* _userData)
{
MainThreadEntry* self = (MainThreadEntry*)_userData;
2013-08-08 01:50:01 -04:00
return main(self->m_argc, self->m_argv);
}
};
2013-07-21 17:44:53 -04:00
struct Context
{
Context()
: m_exit(false)
{
s_translateKey[KEY_ESCAPE] = Key::Esc;
s_translateKey[KEY_RETURN] = Key::Return;
s_translateKey[KEY_TAB] = Key::Tab;
s_translateKey[KEY_DELETE] = Key::Backspace;
//s_translateKey[VK_SPACE] = Key::Space;
//s_translateKey[VK_UP] = Key::Up;
//s_translateKey[VK_DOWN] = Key::Down;
//s_translateKey[VK_LEFT] = Key::Left;
//s_translateKey[VK_RIGHT] = Key::Right;
//s_translateKey[VK_PRIOR] = Key::PageUp;
//s_translateKey[VK_NEXT] = Key::PageUp;
//s_translateKey[VK_HOME] = Key::Home;
//s_translateKey[VK_END] = Key::End;
//s_translateKey[VK_SNAPSHOT] = Key::Print;
//s_translateKey[VK_OEM_PLUS] = Key::Plus;
//s_translateKey[VK_OEM_MINUS] = Key::Minus;
//s_translateKey[VK_F1] = Key::F1;
//s_translateKey[VK_F2] = Key::F2;
//s_translateKey[VK_F3] = Key::F3;
//s_translateKey[VK_F4] = Key::F4;
//s_translateKey[VK_F5] = Key::F5;
//s_translateKey[VK_F6] = Key::F6;
//s_translateKey[VK_F7] = Key::F7;
//s_translateKey[VK_F8] = Key::F8;
//s_translateKey[VK_F9] = Key::F9;
//s_translateKey[VK_F10] = Key::F10;
//s_translateKey[VK_F11] = Key::F11;
//s_translateKey[VK_F12] = Key::F12;
//s_translateKey[VK_NUMPAD0] = Key::NumPad0;
//s_translateKey[VK_NUMPAD1] = Key::NumPad1;
//s_translateKey[VK_NUMPAD2] = Key::NumPad2;
//s_translateKey[VK_NUMPAD3] = Key::NumPad3;
//s_translateKey[VK_NUMPAD4] = Key::NumPad4;
//s_translateKey[VK_NUMPAD5] = Key::NumPad5;
//s_translateKey[VK_NUMPAD6] = Key::NumPad6;
//s_translateKey[VK_NUMPAD7] = Key::NumPad7;
//s_translateKey[VK_NUMPAD8] = Key::NumPad8;
//s_translateKey[VK_NUMPAD9] = Key::NumPad9;
s_translateKey[uint8_t('0')] = Key::Key0;
s_translateKey[uint8_t('1')] = Key::Key1;
s_translateKey[uint8_t('2')] = Key::Key2;
s_translateKey[uint8_t('3')] = Key::Key3;
s_translateKey[uint8_t('4')] = Key::Key4;
s_translateKey[uint8_t('5')] = Key::Key5;
s_translateKey[uint8_t('6')] = Key::Key6;
s_translateKey[uint8_t('7')] = Key::Key7;
s_translateKey[uint8_t('8')] = Key::Key8;
s_translateKey[uint8_t('9')] = Key::Key9;
s_translateKey[uint8_t('A')] = Key::KeyA;
s_translateKey[uint8_t('B')] = Key::KeyB;
s_translateKey[uint8_t('C')] = Key::KeyC;
s_translateKey[uint8_t('D')] = Key::KeyD;
s_translateKey[uint8_t('E')] = Key::KeyE;
s_translateKey[uint8_t('F')] = Key::KeyF;
s_translateKey[uint8_t('G')] = Key::KeyG;
s_translateKey[uint8_t('H')] = Key::KeyH;
s_translateKey[uint8_t('I')] = Key::KeyI;
s_translateKey[uint8_t('J')] = Key::KeyJ;
s_translateKey[uint8_t('K')] = Key::KeyK;
s_translateKey[uint8_t('L')] = Key::KeyL;
s_translateKey[uint8_t('M')] = Key::KeyM;
s_translateKey[uint8_t('N')] = Key::KeyN;
s_translateKey[uint8_t('O')] = Key::KeyO;
s_translateKey[uint8_t('P')] = Key::KeyP;
s_translateKey[uint8_t('Q')] = Key::KeyQ;
s_translateKey[uint8_t('R')] = Key::KeyR;
s_translateKey[uint8_t('S')] = Key::KeyS;
s_translateKey[uint8_t('T')] = Key::KeyT;
s_translateKey[uint8_t('U')] = Key::KeyU;
s_translateKey[uint8_t('V')] = Key::KeyV;
s_translateKey[uint8_t('W')] = Key::KeyW;
s_translateKey[uint8_t('X')] = Key::KeyX;
s_translateKey[uint8_t('Y')] = Key::KeyY;
s_translateKey[uint8_t('Z')] = Key::KeyZ;
s_translateKey[uint8_t('a')] = Key::KeyA;
s_translateKey[uint8_t('b')] = Key::KeyB;
s_translateKey[uint8_t('c')] = Key::KeyC;
s_translateKey[uint8_t('d')] = Key::KeyD;
s_translateKey[uint8_t('e')] = Key::KeyE;
s_translateKey[uint8_t('f')] = Key::KeyF;
s_translateKey[uint8_t('g')] = Key::KeyG;
s_translateKey[uint8_t('h')] = Key::KeyH;
s_translateKey[uint8_t('i')] = Key::KeyI;
s_translateKey[uint8_t('j')] = Key::KeyJ;
s_translateKey[uint8_t('k')] = Key::KeyK;
s_translateKey[uint8_t('l')] = Key::KeyL;
s_translateKey[uint8_t('m')] = Key::KeyM;
s_translateKey[uint8_t('n')] = Key::KeyN;
s_translateKey[uint8_t('o')] = Key::KeyO;
s_translateKey[uint8_t('p')] = Key::KeyP;
s_translateKey[uint8_t('q')] = Key::KeyQ;
s_translateKey[uint8_t('r')] = Key::KeyR;
s_translateKey[uint8_t('s')] = Key::KeyS;
s_translateKey[uint8_t('t')] = Key::KeyT;
s_translateKey[uint8_t('u')] = Key::KeyU;
s_translateKey[uint8_t('v')] = Key::KeyV;
s_translateKey[uint8_t('w')] = Key::KeyW;
s_translateKey[uint8_t('x')] = Key::KeyX;
s_translateKey[uint8_t('y')] = Key::KeyY;
s_translateKey[uint8_t('z')] = Key::KeyZ;
}
2013-07-21 17:44:53 -04:00
NSEvent* waitEvent()
2013-07-22 00:53:20 -04:00
{
return [NSApp
2013-07-21 17:44:53 -04:00
nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture] // wait for event
inMode:NSDefaultRunLoopMode
2013-07-22 00:53:20 -04:00
dequeue:YES
];
}
2013-07-21 17:44:53 -04:00
NSEvent* peekEvent()
2013-07-22 00:53:20 -04:00
{
return [NSApp
2013-07-21 17:44:53 -04:00
nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast] // do not wait for event
inMode:NSDefaultRunLoopMode
2013-07-22 00:53:20 -04:00
dequeue:YES
];
}
2013-07-21 17:44:53 -04:00
void getMousePos(int* outX, int* outY)
{
NSRect originalFrame = [m_window frame];
NSPoint location = [m_window mouseLocationOutsideOfEventStream];
NSRect adjustFrame = [NSWindow contentRectForFrameRect: originalFrame styleMask: NSTitledWindowMask];
int x = location.x;
int y = (int)adjustFrame.size.height - (int)location.y;
// clamp within the range of the window
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x > (int)adjustFrame.size.width) x = (int)adjustFrame.size.width;
if (y > (int)adjustFrame.size.height) y = (int)adjustFrame.size.height;
*outX = x;
*outY = y;
}
uint8_t translateModifiers(int flags)
{
uint8_t mask = 0;
if (flags & NSShiftKeyMask)
mask |= Modifier::LeftShift | Modifier::RightShift;
if (flags & NSAlternateKeyMask)
mask |= Modifier::LeftAlt | Modifier::RightAlt;
if (flags & NSControlKeyMask)
mask |= Modifier::LeftCtrl | Modifier::RightCtrl;
if (flags & NSCommandKeyMask)
mask |= Modifier::LeftMeta | Modifier::RightMeta;
return mask;
}
Key::Enum handleKeyEvent(NSEvent* event, uint8_t* specialKeys)
{
NSString* key = [event charactersIgnoringModifiers];
unichar keyChar = 0;
if ([key length] == 0)
return Key::None;
keyChar = [key characterAtIndex:0];
int keyCode = keyChar;
*specialKeys = translateModifiers([event modifierFlags]);
// if this is a unhandled key just return None
if (keyCode >= 256)
return Key::None;
return (Key::Enum)s_translateKey[keyCode];
}
bool dispatchEvent(NSEvent* event)
2013-07-22 00:53:20 -04:00
{
if (event)
{
NSEventType eventType = [event type];
switch (eventType)
{
case NSMouseMoved :
{
int x, y;
getMousePos(&x, &y);
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, 0);
break;
}
case NSLeftMouseDown:
{
int x, y;
getMousePos(&x, &y);
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, 0, MouseButton::Left, true);
break;
}
case NSLeftMouseUp:
{
int x, y;
getMousePos(&x, &y);
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, 0, MouseButton::Left, false);
break;
}
case NSRightMouseDown:
{
int x, y;
getMousePos(&x, &y);
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, 0, MouseButton::Right, true);
break;
}
case NSRightMouseUp:
{
int x, y;
getMousePos(&x, &y);
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, 0, MouseButton::Right, false);
break;
}
case NSKeyDown:
{
uint8_t modifiers = 0;
Key::Enum key = handleKeyEvent(event, &modifiers);
// If KeyCode is none we don't don't handle the key and special case for cmd+q (quit)
// Note that return false here means that we take care of the key (instead of the default behavior)
if (key != Key::None)
{
if (key != Key::KeyQ && !((modifiers & Modifier::RightMeta)))
{
m_eventQueue.postKeyEvent(s_defaultWindow, key, modifiers, true);
return false;
}
}
break;
}
case NSKeyUp:
{
uint8_t modifiers = 0;
Key::Enum key = handleKeyEvent(event, &modifiers);
if (key != Key::None)
{
m_eventQueue.postKeyEvent(s_defaultWindow, key, modifiers, false);
return false;
}
break;
}
}
[NSApp sendEvent:event];
[NSApp updateWindows];
return true;
}
2013-07-22 00:53:20 -04:00
return false;
}
2013-07-21 17:44:53 -04:00
2013-07-22 00:53:20 -04:00
int32_t run(int _argc, char** _argv)
{
2013-07-21 17:44:53 -04:00
[NSApplication sharedApplication];
2013-07-22 00:53:20 -04:00
id dg = [AppDelegate sharedDelegate];
[NSApp setDelegate:dg];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES];
[NSApp finishLaunching];
2013-07-22 00:53:20 -04:00
[[NSNotificationCenter defaultCenter]
2013-07-21 17:44:53 -04:00
postNotificationName:NSApplicationWillFinishLaunchingNotification
object:NSApp];
2013-07-22 00:53:20 -04:00
[[NSNotificationCenter defaultCenter]
2013-07-21 17:44:53 -04:00
postNotificationName:NSApplicationDidFinishLaunchingNotification
object:NSApp];
id quitMenuItem = [NSMenuItem new];
[quitMenuItem
initWithTitle:@"Quit"
action:@selector(terminate:)
keyEquivalent:@"q"];
2013-07-22 00:53:20 -04:00
id appMenu = [NSMenu new];
[appMenu addItem:quitMenuItem];
2013-07-22 00:53:20 -04:00
id appMenuItem = [NSMenuItem new];
[appMenuItem setSubmenu:appMenu];
2013-07-22 00:53:20 -04:00
id menubar = [[NSMenu new] autorelease];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
2013-07-21 17:44:53 -04:00
NSRect rect = NSMakeRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
NSWindow* window = [NSWindow alloc];
[window
2013-07-21 17:44:53 -04:00
initWithContentRect:rect
styleMask:0
|NSTitledWindowMask
|NSClosableWindowMask
|NSMiniaturizableWindowMask
|NSResizableWindowMask
backing:NSBackingStoreBuffered defer:NO
];
NSString* appName = [[NSProcessInfo processInfo] processName];
[window setTitle:appName];
[window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
[window makeKeyAndOrderFront:window];
// [window setContentView:nil];
[window setAcceptsMouseMovedEvents:YES];
[window setBackgroundColor:[NSColor blackColor]];
2013-07-22 00:53:20 -04:00
[[Window sharedDelegate] windowCreated:window];
2013-07-21 17:44:53 -04:00
m_window = window;
bgfx::osxSetNSWindow(window);
2013-07-21 17:44:53 -04:00
MainThreadEntry mte;
mte.m_argc = _argc;
mte.m_argv = _argv;
2013-07-21 17:44:53 -04:00
bx::Thread thread;
thread.init(mte.threadFunc, &mte);
2013-07-22 00:53:20 -04:00
while (!(m_exit = [dg applicationHasTerminated]) )
{
if (bgfx::RenderFrame::Exiting == bgfx::renderFrame() )
{
break;
}
while (dispatchEvent(peekEvent() ) )
{
}
}
m_eventQueue.postExitEvent();
2013-07-21 17:44:53 -04:00
2013-12-07 13:19:54 -05:00
while (bgfx::RenderFrame::NoContext != bgfx::renderFrame() ) {};
thread.shutdown();
2013-07-21 17:44:53 -04:00
return 0;
}
2013-07-21 17:44:53 -04:00
EventQueue m_eventQueue;
2013-07-21 17:44:53 -04:00
bool m_exit;
NSWindow* m_window;
};
2013-07-21 17:44:53 -04:00
static Context s_ctx;
2013-07-21 17:44:53 -04:00
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);
}
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)
{
BX_UNUSED(_x, _y, _width, _height, _flags, _title);
WindowHandle handle = { UINT16_MAX };
return handle;
}
void destroyWindow(WindowHandle _handle)
{
BX_UNUSED(_handle);
}
void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
{
BX_UNUSED(_handle, _x, _y);
}
2014-09-19 13:33:13 -04:00
void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
{
2014-09-19 13:33:13 -04:00
BX_UNUSED(_handle, _width, _height);
}
2014-09-19 13:33:13 -04:00
void setWindowTitle(WindowHandle _handle, const char* _title)
2014-08-05 15:57:52 -04:00
{
2014-09-19 13:33:13 -04:00
BX_UNUSED(_handle, _title);
2014-08-05 15:57:52 -04:00
}
2014-09-19 13:33:13 -04:00
void toggleWindowFrame(WindowHandle _handle)
{
2014-09-19 13:33:13 -04:00
BX_UNUSED(_handle);
}
2014-09-19 13:33:13 -04:00
void setMouseLock(WindowHandle _handle, bool _lock)
2013-01-19 03:22:25 -05:00
{
2014-09-19 13:33:13 -04:00
BX_UNUSED(_handle, _lock);
2013-01-19 03:22:25 -05:00
}
} // namespace entry
int main(int _argc, char** _argv)
{
using namespace entry;
2013-07-22 00:53:20 -04:00
return s_ctx.run(_argc, _argv);
}
#endif // BX_PLATFORM_OSX