ebiten/ui/cocoa/ebiten_game_window.c

91 lines
2.5 KiB
C
Raw Normal View History

2013-10-12 20:17:51 +02:00
// -*- objc -*-
2013-12-31 10:17:35 +01:00
#import "ebiten_game_window.h"
2013-10-12 20:17:51 +02:00
2013-12-31 10:17:35 +01:00
#import "ebiten_game_content_view.h"
2013-10-13 19:04:26 +02:00
2013-12-29 17:36:11 +01:00
@class NSOpenGLContext;
2013-12-10 16:49:30 +01:00
void ebiten_WindowClosed(void* nativeWindow);
2013-12-31 10:17:35 +01:00
@implementation EbitenGameWindow {
@private
NSOpenGLContext* glContext_;
}
- (id)initWithSize:(NSSize)size
glContext:(NSOpenGLContext*)glContext {
self->glContext_ = glContext;
2013-12-29 17:36:11 +01:00
[self->glContext_ retain];
2013-10-12 20:17:51 +02:00
NSUInteger style = (NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask);
NSRect windowRect =
[NSWindow frameRectForContentRect:NSMakeRect(0, 0, size.width, size.height)
styleMask:style];
NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
NSSize screenSize = [screen visibleFrame].size;
2013-12-29 17:55:53 +01:00
NSRect contentRect = NSMakeRect(0, 0, size.width, size.height);
2013-10-12 20:17:51 +02:00
self = [super initWithContentRect:contentRect
styleMask:style
backing:NSBackingStoreBuffered
defer:YES];
2013-12-29 17:36:11 +01:00
if (self != nil) {
2013-12-29 17:55:53 +01:00
[self center];
2013-12-29 17:36:11 +01:00
[self setReleasedWhenClosed:YES];
[self setDelegate:self];
[self setDocumentEdited:YES];
NSRect rect = NSMakeRect(0, 0, size.width, size.height);
2013-12-31 10:17:35 +01:00
NSView* contentView = [[EbitenGameContentView alloc] initWithFrame:rect];
2013-12-29 17:36:11 +01:00
[self setContentView:contentView];
[contentView release];
}
2013-11-22 18:56:18 +01:00
return self;
}
2013-12-30 19:17:39 +01:00
- (void)dealloc {
[self->glContext_ release];
[super dealloc];
}
- (NSOpenGLContext*)glContext {
return self->glContext_;
}
2013-10-12 20:17:51 +02:00
- (BOOL)windowShouldClose:(id)sender {
if ([sender isDocumentEdited]) {
// TODO: add the application's name
NSAlert* alert = [NSAlert alertWithMessageText:@"Quit the game?"
defaultButton:@"Quit"
alternateButton:nil
otherButton:@"Cancel"
informativeTextWithFormat:@""];
2013-10-14 17:49:30 +02:00
SEL selector = @selector(alertDidEnd:returnCode:contextInfo:);
2013-10-12 20:17:51 +02:00
[alert beginSheetModalForWindow:sender
modalDelegate:self
2013-10-14 17:49:30 +02:00
didEndSelector:selector
2013-10-12 20:17:51 +02:00
contextInfo:nil];
2013-11-22 18:56:18 +01:00
[alert release];
2013-10-12 20:17:51 +02:00
}
return NO;
}
- (void)alertDidEnd:(NSAlert*)alert
returnCode:(NSInteger)returnCode
contextInfo:(void*)contextInfo {
(void)alert;
(void)contextInfo;
if (returnCode == NSAlertDefaultReturn) {
[self close];
2013-12-10 16:49:30 +01:00
ebiten_WindowClosed(self);
2013-10-12 20:17:51 +02:00
}
}
2013-11-23 17:17:22 +01:00
- (BOOL)canBecomeMainWindow {
return YES;
}
2013-11-22 18:56:18 +01:00
@end