ebiten/ui/cocoa/mainloop.c

107 lines
2.9 KiB
C
Raw Normal View History

2013-11-22 18:56:18 +01:00
// -*- objc -*-
#include <stdlib.h>
2013-11-23 18:00:09 +01:00
#include <OpenGL/gl.h>
2013-11-22 18:56:18 +01:00
2013-12-31 10:17:35 +01:00
#import "ebiten_game_window.h"
2013-11-22 18:56:18 +01:00
2014-01-13 07:26:20 +01:00
static NSAutoreleasePool* pool = NULL;
2013-12-08 12:45:14 +01:00
void initMenu(void) {
NSString* processName = [[NSProcessInfo processInfo] processName];
NSMenu* menuBar = [NSMenu new];
[NSApp setMainMenu: menuBar];
2013-12-29 17:55:53 +01:00
[menuBar release];
2013-12-08 12:45:14 +01:00
NSMenuItem* rootMenuItem = [NSMenuItem new];
[menuBar addItem:rootMenuItem];
2013-12-29 17:55:53 +01:00
[rootMenuItem release];
2013-12-08 12:45:14 +01:00
NSMenu* appMenu = [NSMenu new];
[rootMenuItem setSubmenu:appMenu];
2013-12-29 17:55:53 +01:00
[appMenu release];
2013-12-08 12:45:14 +01:00
[appMenu addItemWithTitle:[@"Quit " stringByAppendingString:processName]
action:@selector(performClose:)
keyEquivalent:@"q"];
}
void StartApplication(void) {
2014-01-13 07:26:20 +01:00
pool = [NSAutoreleasePool new];
2013-11-23 17:17:22 +01:00
NSApplication* app = [NSApplication sharedApplication];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
2013-12-08 12:45:14 +01:00
initMenu();
2013-11-23 17:17:22 +01:00
[app finishLaunching];
2014-01-13 07:30:27 +01:00
[NSApp activateIgnoringOtherApps:YES];
2013-11-23 17:17:22 +01:00
}
2014-01-13 07:26:20 +01:00
void DoEvents(void) {
for (;;) {
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event == nil) {
break;
}
[NSApp sendEvent:event];
}
[pool drain];
pool = [NSAutoreleasePool new];
}
void TerminateApplication(void) {
[pool drain];
}
2013-12-30 19:17:39 +01:00
NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext) {
2013-11-23 18:00:09 +01:00
NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFADepthSize, 32,
0,
};
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc]
initWithAttributes:attributes];
2013-11-24 18:12:07 +01:00
NSOpenGLContext* glContext =
[[NSOpenGLContext alloc] initWithFormat:format
2013-12-30 19:17:39 +01:00
shareContext:sharedGLContext];
2013-11-23 18:00:09 +01:00
[format release];
return glContext;
}
2014-01-03 17:47:05 +01:00
EbitenGameWindow* CreateGameWindow(size_t width, size_t height, const char* title, NSOpenGLContext* glContext) {
2013-11-23 17:17:22 +01:00
NSSize size = NSMakeSize(width, height);
2013-12-31 10:17:35 +01:00
EbitenGameWindow* window = [[EbitenGameWindow alloc]
initWithSize:size
glContext:glContext];
2013-12-29 17:55:53 +01:00
[glContext release];
2013-12-29 17:36:11 +01:00
NSString* nsTitle = [[NSString alloc]
initWithUTF8String:title];
[window setTitle: nsTitle];
[nsTitle release];
2013-11-23 17:17:22 +01:00
[window makeKeyAndOrderFront:nil];
2013-12-29 17:36:11 +01:00
[glContext setView:[window contentView]];
2013-11-23 17:17:22 +01:00
return window;
}
2013-11-22 18:56:18 +01:00
2013-12-30 19:17:39 +01:00
void UseGLContext(NSOpenGLContext* glContext) {
2013-12-07 17:35:24 +01:00
CGLContextObj cglContext = [glContext CGLContextObj];
CGLLockContext(cglContext);
[glContext makeCurrentContext];
}
void UnuseGLContext(void) {
NSOpenGLContext* glContext = [NSOpenGLContext currentContext];
[glContext flushBuffer];
[NSOpenGLContext clearCurrentContext];
CGLContextObj cglContext = [glContext CGLContextObj];
CGLUnlockContext(cglContext);
2013-12-06 18:20:48 +01:00
}