Have EbitenWindow have a NSOpenGLContext

This commit is contained in:
Hajime Hoshi 2013-11-24 03:30:12 +09:00
parent 3c979825db
commit 638da2ed2c
4 changed files with 36 additions and 11 deletions

View File

@ -24,6 +24,11 @@
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification { - (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
(void)aNotification; (void)aNotification;
[self initMenu]; [self initMenu];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowClosing:)
name:NSWindowWillCloseNotification
object:nil];
} }
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: - (BOOL)applicationShouldTerminateAfterLastWindowClosed:
@ -32,4 +37,9 @@
return YES; return YES;
} }
- (void)windowClosing:(NSNotification*)aNotification {
(void)aNotification;
[NSApp terminate:nil];
}
@end @end

View File

@ -6,9 +6,15 @@
#import "ebiten_content_view.h" #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 | NSUInteger style = (NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask); NSMiniaturizableWindowMask);
NSRect windowRect = NSRect windowRect =
@ -39,6 +45,10 @@
return self; return self;
} }
- (NSOpenGLContext*)glContext {
return self->glContext_;
}
- (BOOL)windowShouldClose:(id)sender { - (BOOL)windowShouldClose:(id)sender {
if ([sender isDocumentEdited]) { if ([sender isDocumentEdited]) {
// TODO: add the application's name // TODO: add the application's name
@ -63,7 +73,8 @@
(void)alert; (void)alert;
(void)contextInfo; (void)contextInfo;
if (returnCode == NSAlertDefaultReturn) { if (returnCode == NSAlertDefaultReturn) {
[NSApp terminate:nil]; [self->glContext_ release];
[self close];
} }
} }

View File

@ -7,7 +7,9 @@
@interface EbitenWindow : NSWindow<NSWindowDelegate> @interface EbitenWindow : NSWindow<NSWindowDelegate>
- (id)initWithSize:(NSSize)size; - (id)initWithSize:(NSSize)size
glContext:(NSOpenGLContext*)glContext;
- (NSOpenGLContext*)glContext;
@end @end

View File

@ -6,8 +6,6 @@
#import "ebiten_controller.h" #import "ebiten_controller.h"
#import "ebiten_window.h" #import "ebiten_window.h"
static NSOpenGLContext* glContext_;
void StartApplication() { void StartApplication() {
EbitenController* controller = [[EbitenController alloc] init]; EbitenController* controller = [[EbitenController alloc] init];
NSApplication* app = [NSApplication sharedApplication]; NSApplication* app = [NSApplication sharedApplication];
@ -34,13 +32,18 @@ void* CreateGLContext() {
} }
void* CreateWindow(size_t width, size_t height, const char* title) { void* CreateWindow(size_t width, size_t height, const char* title) {
NSOpenGLContext* glContext = CreateGLContext();
[glContext makeCurrentContext];
NSSize size = NSMakeSize(width, height); NSSize size = NSMakeSize(width, height);
EbitenWindow* window = [[EbitenWindow alloc] EbitenWindow* window = [[EbitenWindow alloc]
initWithSize:size]; initWithSize:size
glContext:glContext];
[window setTitle: [[NSString alloc] initWithUTF8String:title]]; [window setTitle: [[NSString alloc] initWithUTF8String:title]];
[window makeKeyAndOrderFront:nil]; [window makeKeyAndOrderFront:nil];
glContext_ = CreateGLContext();
[glContext_ makeCurrentContext]; [glContext setView:[window contentView]];
return window; return window;
} }
@ -58,10 +61,9 @@ void PollEvents(void) {
} }
void BeginDrawing(void* window) { void BeginDrawing(void* window) {
[glContext_ setView:[(EbitenWindow*)window contentView]];
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
} }
void EndDrawing(void* window) { void EndDrawing(void* window) {
[glContext_ flushBuffer]; [[(EbitenWindow*)window glContext] flushBuffer];
} }