From 638da2ed2c38973851346158e98438ce97f5db00 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 24 Nov 2013 03:30:12 +0900 Subject: [PATCH] Have EbitenWindow have a NSOpenGLContext --- ui/cocoa/ebiten_controller.c | 10 ++++++++++ ui/cocoa/ebiten_window.c | 17 ++++++++++++++--- ui/cocoa/ebiten_window.h | 4 +++- ui/cocoa/mainloop.c | 16 +++++++++------- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/ui/cocoa/ebiten_controller.c b/ui/cocoa/ebiten_controller.c index 68b74c387..b852bd20b 100644 --- a/ui/cocoa/ebiten_controller.c +++ b/ui/cocoa/ebiten_controller.c @@ -24,6 +24,11 @@ - (void)applicationDidFinishLaunching:(NSNotification*)aNotification { (void)aNotification; [self initMenu]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowClosing:) + name:NSWindowWillCloseNotification + object:nil]; } - (BOOL)applicationShouldTerminateAfterLastWindowClosed: @@ -32,4 +37,9 @@ return YES; } +- (void)windowClosing:(NSNotification*)aNotification { + (void)aNotification; + [NSApp terminate:nil]; +} + @end diff --git a/ui/cocoa/ebiten_window.c b/ui/cocoa/ebiten_window.c index ceb3fb95f..e982c703a 100644 --- a/ui/cocoa/ebiten_window.c +++ b/ui/cocoa/ebiten_window.c @@ -6,9 +6,15 @@ #import "ebiten_content_view.h" -@implementation EbitenWindow +@implementation EbitenWindow { +@private + NSOpenGLContext* glContext_; +} + +- (id)initWithSize:(NSSize)size + glContext:(NSOpenGLContext*)glContext { + self->glContext_ = glContext; -- (id)initWithSize:(NSSize)size { NSUInteger style = (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask); NSRect windowRect = @@ -39,6 +45,10 @@ return self; } +- (NSOpenGLContext*)glContext { + return self->glContext_; +} + - (BOOL)windowShouldClose:(id)sender { if ([sender isDocumentEdited]) { // TODO: add the application's name @@ -63,7 +73,8 @@ (void)alert; (void)contextInfo; if (returnCode == NSAlertDefaultReturn) { - [NSApp terminate:nil]; + [self->glContext_ release]; + [self close]; } } diff --git a/ui/cocoa/ebiten_window.h b/ui/cocoa/ebiten_window.h index 9e736e883..c4e6baf8f 100644 --- a/ui/cocoa/ebiten_window.h +++ b/ui/cocoa/ebiten_window.h @@ -7,7 +7,9 @@ @interface EbitenWindow : NSWindow -- (id)initWithSize:(NSSize)size; +- (id)initWithSize:(NSSize)size + glContext:(NSOpenGLContext*)glContext; +- (NSOpenGLContext*)glContext; @end diff --git a/ui/cocoa/mainloop.c b/ui/cocoa/mainloop.c index 6cd7dfba6..120c4ed68 100644 --- a/ui/cocoa/mainloop.c +++ b/ui/cocoa/mainloop.c @@ -6,8 +6,6 @@ #import "ebiten_controller.h" #import "ebiten_window.h" -static NSOpenGLContext* glContext_; - void StartApplication() { EbitenController* controller = [[EbitenController alloc] init]; NSApplication* app = [NSApplication sharedApplication]; @@ -34,13 +32,18 @@ void* CreateGLContext() { } void* CreateWindow(size_t width, size_t height, const char* title) { + NSOpenGLContext* glContext = CreateGLContext(); + [glContext makeCurrentContext]; + NSSize size = NSMakeSize(width, height); EbitenWindow* window = [[EbitenWindow alloc] - initWithSize:size]; + initWithSize:size + glContext:glContext]; [window setTitle: [[NSString alloc] initWithUTF8String:title]]; [window makeKeyAndOrderFront:nil]; - glContext_ = CreateGLContext(); - [glContext_ makeCurrentContext]; + + [glContext setView:[window contentView]]; + return window; } @@ -58,10 +61,9 @@ void PollEvents(void) { } void BeginDrawing(void* window) { - [glContext_ setView:[(EbitenWindow*)window contentView]]; glClear(GL_COLOR_BUFFER_BIT); } void EndDrawing(void* window) { - [glContext_ flushBuffer]; + [[(EbitenWindow*)window glContext] flushBuffer]; }