ebiten/ui/cocoa/ebiten_window.c

109 lines
3.2 KiB
C
Raw Normal View History

2013-10-12 20:17:51 +02:00
// -*- objc -*-
#import "ebiten_window.h"
2013-11-22 18:56:18 +01:00
#include <OpenGL/gl.h>
2013-11-23 10:10:15 +01:00
#import "ebiten_content_view.h"
2013-10-13 19:04:26 +02:00
2013-11-22 18:56:18 +01:00
void ebiten_EbitenOpenGLView_Initialized(void);
2013-10-12 20:17:51 +02:00
@implementation EbitenWindow
2013-11-22 18:56:18 +01:00
{
NSOpenGLContext* glContext_;
}
2013-10-12 20:17:51 +02:00
- (id)initWithSize:(NSSize)size {
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-10-14 17:49:30 +02:00
// Reference: Mac OS X Human Interface Guidelines: UI Element Guidelines:
// Windows
2013-10-12 20:17:51 +02:00
// http://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AppleHIGuidelines/Windows/Windows.html
2013-10-14 17:49:30 +02:00
NSRect contentRect =
NSMakeRect((screenSize.width - windowRect.size.width) / 2,
(screenSize.height - windowRect.size.height) * 2 / 3,
size.width, size.height);
2013-10-12 20:17:51 +02:00
self = [super initWithContentRect:contentRect
styleMask:style
backing:NSBackingStoreBuffered
defer:YES];
assert(self != nil);
[self setReleasedWhenClosed:YES];
[self setDelegate:self];
[self setDocumentEdited:YES];
2013-10-13 19:04:26 +02:00
NSRect rect = NSMakeRect(0, 0, size.width, size.height);
2013-11-23 10:10:15 +01:00
NSView* contentView = [[EbitenContentView alloc] initWithFrame:rect];
2013-11-22 18:56:18 +01:00
[self setContentView:contentView];
return self;
}
- (NSOpenGLContext*)glContext {
if (self->glContext_ != nil)
return self->glContext_;
2013-10-13 19:04:26 +02:00
NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFADepthSize, 32,
0,
};
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc]
initWithAttributes:attributes];
2013-11-22 18:56:18 +01:00
self->glContext_ = [[NSOpenGLContext alloc] initWithFormat:format
shareContext:nil];
[self->glContext_ setView:[self contentView]];
[self->glContext_ makeCurrentContext];
ebiten_EbitenOpenGLView_Initialized();
[format release];
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) {
2013-10-13 10:36:18 +02:00
[NSApp terminate:nil];
2013-10-12 20:17:51 +02:00
}
}
2013-11-22 18:56:18 +01:00
- (void)beginDrawing {
[[self glContext] makeCurrentContext];
glClear(GL_COLOR_BUFFER_BIT);
}
- (void)endDrawing {
[[self glContext] flushBuffer];
}
2013-10-12 20:17:51 +02:00
2013-11-22 18:56:18 +01:00
@end