This commit is contained in:
bkaradzic 2013-07-21 21:53:20 -07:00
parent a17be960c6
commit d927f59bd8
2 changed files with 80 additions and 41 deletions

View file

@ -19,63 +19,86 @@
extern int _main_(int _argc, char** _argv); extern int _main_(int _argc, char** _argv);
@interface bgfxApplicationDelegate : NSObject <NSApplicationDelegate> { @interface AppDelegate : NSObject<NSApplicationDelegate>
{
bool terminated; bool terminated;
} }
+ (bgfxApplicationDelegate *)sharedDelegate;
+ (AppDelegate *)sharedDelegate;
- (id)init; - (id)init;
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
- (bool)applicationHasTerminated; - (bool)applicationHasTerminated;
@end @end
@implementation bgfxApplicationDelegate @implementation AppDelegate
+ (bgfxApplicationDelegate *)sharedDelegate {
static id delegate = [bgfxApplicationDelegate new]; + (AppDelegate *)sharedDelegate
{
static id delegate = [AppDelegate new];
return delegate; return delegate;
} }
- (id)init { - (id)init
{
self = [super init]; self = [super init];
if (self) {
self->terminated = false; if (nil == self)
{
return nil;
} }
self->terminated = false;
return self; return self;
} }
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
self->terminated = true; self->terminated = true;
return NSTerminateCancel; return NSTerminateCancel;
} }
- (bool)applicationHasTerminated { - (bool)applicationHasTerminated
{
return self->terminated; return self->terminated;
} }
@end @end
@interface bgfxWindowDelegate : NSObject <NSWindowDelegate> { @interface Window : NSObject<NSWindowDelegate>
{
unsigned int windowCount; unsigned int windowCount;
} }
+ (bgfxWindowDelegate *)sharedDelegate;
+ (Window *)sharedDelegate;
- (id)init; - (id)init;
- (void)windowCreated:(NSWindow *)window; - (void)windowCreated:(NSWindow *)window;
- (BOOL)windowShouldClose:(NSWindow *)window; - (BOOL)windowShouldClose:(NSWindow *)window;
@end @end
@implementation bgfxWindowDelegate @implementation Window
+ (bgfxWindowDelegate *)sharedDelegate {
static id windowDelegate = [bgfxWindowDelegate new]; + (Window *)sharedDelegate
{
static id windowDelegate = [Window new];
return windowDelegate; return windowDelegate;
} }
- (id)init { - (id)init
{
self = [super init]; self = [super init];
if (self) { if (nil == self)
self->windowCount = 0; {
return nil;
} }
self->windowCount = 0;
return self; return self;
} }
- (void)windowCreated:(NSWindow *)window { - (void)windowCreated:(NSWindow *)window
{
assert(window); assert(window);
[window setDelegate:self]; [window setDelegate:self];
@ -84,7 +107,8 @@ extern int _main_(int _argc, char** _argv);
self->windowCount += 1; self->windowCount += 1;
} }
- (BOOL)windowShouldClose:(NSWindow *)window { - (BOOL)windowShouldClose:(NSWindow *)window
{
assert(window); assert(window);
[window setDelegate:nil]; [window setDelegate:nil];
@ -92,10 +116,12 @@ extern int _main_(int _argc, char** _argv);
assert(self->windowCount); assert(self->windowCount);
self->windowCount -= 1; self->windowCount -= 1;
if (self->windowCount == 0) { if (self->windowCount == 0)
{
[NSApp terminate:self]; [NSApp terminate:self];
return false; return false;
} }
return true; return true;
} }
@end @end
@ -121,44 +147,52 @@ namespace entry
{ {
} }
NSEvent* WaitEvent () { NSEvent* WaitEvent()
return {
[NSApp return [NSApp
nextEventMatchingMask:NSAnyEventMask nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture] // wait for event untilDate:[NSDate distantFuture] // wait for event
inMode:NSDefaultRunLoopMode inMode:NSDefaultRunLoopMode
dequeue:YES]; dequeue:YES
];
} }
NSEvent* PeekEvent () { NSEvent* PeekEvent()
return {
[NSApp return [NSApp
nextEventMatchingMask:NSAnyEventMask nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast] // do not wait for event untilDate:[NSDate distantPast] // do not wait for event
inMode:NSDefaultRunLoopMode inMode:NSDefaultRunLoopMode
dequeue:YES]; dequeue:YES
];
} }
bool DispatchEvent (NSEvent* event) { bool DispatchEvent(NSEvent* event)
if (event) { {
if (event)
{
[NSApp sendEvent:event]; [NSApp sendEvent:event];
[NSApp updateWindows]; [NSApp updateWindows];
return true; return true;
} }
return false; return false;
} }
int32_t main(int _argc, char** _argv) int32_t run(int _argc, char** _argv)
{ {
[NSApplication sharedApplication]; [NSApplication sharedApplication];
id dg = [bgfxApplicationDelegate sharedDelegate];
id dg = [AppDelegate sharedDelegate];
[NSApp setDelegate:dg]; [NSApp setDelegate:dg];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES]; [NSApp activateIgnoringOtherApps:YES];
[NSApp finishLaunching]; [NSApp finishLaunching];
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
postNotificationName:NSApplicationWillFinishLaunchingNotification postNotificationName:NSApplicationWillFinishLaunchingNotification
object:NSApp]; object:NSApp];
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
postNotificationName:NSApplicationDidFinishLaunchingNotification postNotificationName:NSApplicationDidFinishLaunchingNotification
object:NSApp]; object:NSApp];
@ -168,10 +202,13 @@ namespace entry
initWithTitle:@"Quit" initWithTitle:@"Quit"
action:@selector(terminate:) action:@selector(terminate:)
keyEquivalent:@"q"]; keyEquivalent:@"q"];
id appMenu = [NSMenu new]; id appMenu = [NSMenu new];
[appMenu addItem:quitMenuItem]; [appMenu addItem:quitMenuItem];
id appMenuItem = [NSMenuItem new]; id appMenuItem = [NSMenuItem new];
[appMenuItem setSubmenu:appMenu]; [appMenuItem setSubmenu:appMenu];
id menubar = [[NSMenu new] autorelease]; id menubar = [[NSMenu new] autorelease];
[menubar addItem:appMenuItem]; [menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar]; [NSApp setMainMenu:menubar];
@ -191,7 +228,7 @@ namespace entry
[window setTitle:appName]; [window setTitle:appName];
[window cascadeTopLeftFromPoint:NSMakePoint(20,20)]; [window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
[window makeKeyAndOrderFront:nil]; [window makeKeyAndOrderFront:nil];
[[bgfxWindowDelegate sharedDelegate] windowCreated:window]; [[Window sharedDelegate] windowCreated:window];
bgfx::osxSetNSWindow(window); bgfx::osxSetNSWindow(window);
@ -202,10 +239,10 @@ namespace entry
bx::Thread thread; bx::Thread thread;
thread.init(mte.threadFunc, &mte); thread.init(mte.threadFunc, &mte);
while (!(m_exit = [dg applicationHasTerminated])) while (!(m_exit = [dg applicationHasTerminated]) )
{ {
DispatchEvent(WaitEvent()); DispatchEvent(WaitEvent() );
while (DispatchEvent(PeekEvent())); while (DispatchEvent(PeekEvent() ) );
} }
m_eventQueue.postExitEvent(); m_eventQueue.postExitEvent();
@ -248,7 +285,7 @@ namespace entry
int main(int _argc, char** _argv) int main(int _argc, char** _argv)
{ {
using namespace entry; using namespace entry;
return s_ctx.main(_argc, _argv); return s_ctx.run(_argc, _argv);
} }
#endif // BX_PLATFORM_OSX #endif // BX_PLATFORM_OSX

View file

@ -75,6 +75,9 @@ namespace bgfx
GL_CHECK(glDeleteRenderbuffers(1, &m_depthRbo) ); GL_CHECK(glDeleteRenderbuffers(1, &m_depthRbo) );
m_depthRbo = 0; m_depthRbo = 0;
} }
EAGLContext* context = (EAGLContext*)m_context;
[context release];
} }
void GlContext::resize(uint32_t _width, uint32_t _height, bool _vsync) void GlContext::resize(uint32_t _width, uint32_t _height, bool _vsync)
@ -84,9 +87,8 @@ namespace bgfx
void GlContext::swap() void GlContext::swap()
{ {
EAGLContext* context = (EAGLContext*)m_context;
[EAGLContext setCurrentContext:context];
GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_colorRbo) ); GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_colorRbo) );
EAGLContext* context = (EAGLContext*)m_context;
[context presentRenderbuffer:GL_RENDERBUFFER]; [context presentRenderbuffer:GL_RENDERBUFFER];
} }