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)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

View File

@ -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];
}
}

View File

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

View File

@ -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];
}