ebiten/ui/cocoa/mainloop.c

115 lines
3.2 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
#import "ebiten_controller.h"
#import "ebiten_window.h"
2013-12-08 12:45:14 +01:00
void initMenu(void) {
NSString* processName = [[NSProcessInfo processInfo] processName];
NSMenu* menuBar = [NSMenu new];
[NSApp setMainMenu: menuBar];
NSMenuItem* rootMenuItem = [NSMenuItem new];
[menuBar addItem:rootMenuItem];
NSMenu* appMenu = [NSMenu new];
[rootMenuItem setSubmenu:appMenu];
[appMenu addItemWithTitle:[@"Quit " stringByAppendingString:processName]
action:@selector(performClose:)
keyEquivalent:@"q"];
}
void StartApplication(void) {
2013-11-23 17:17:22 +01:00
EbitenController* controller = [[EbitenController alloc] init];
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 setDelegate:controller];
[app finishLaunching];
}
2013-11-24 18:12:07 +01:00
void* CreateGLContext(void* 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
shareContext:(NSOpenGLContext*)sharedGLContext];
2013-11-23 18:00:09 +01:00
[format release];
return glContext;
}
2013-12-08 14:13:48 +01:00
void* CreateWindow(size_t width, size_t height, const char* title, void* glContext_) {
NSOpenGLContext* glContext = (NSOpenGLContext*)glContext_;
2013-11-25 14:27:59 +01:00
2013-11-23 17:17:22 +01:00
NSSize size = NSMakeSize(width, height);
EbitenWindow* window = [[EbitenWindow alloc]
initWithSize:size
2013-11-25 14:27:59 +01:00
glContext:glContext];
[window setTitle: [[NSString alloc]
initWithUTF8String:title]];
2013-11-23 17:17:22 +01:00
[window makeKeyAndOrderFront:nil];
2013-11-24 18:12:07 +01:00
[(NSOpenGLContext*)glContext setView:[window contentView]];
2013-11-23 17:17:22 +01:00
return window;
}
2013-11-22 18:56:18 +01:00
2013-11-23 11:31:10 +01:00
void PollEvents(void) {
for (;;) {
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event == nil) {
break;
}
[NSApp sendEvent:event];
}
2013-12-08 15:03:00 +01:00
static BOOL initialBoot = YES;
2013-12-08 14:22:54 +01:00
if (initialBoot) {
[NSApp activateIgnoringOtherApps:YES];
initialBoot = NO;
}
2013-11-23 11:31:10 +01:00
}
2013-12-07 17:35:24 +01:00
void UseGLContext(void* glContextPtr) {
NSOpenGLContext* glContext = (NSOpenGLContext*)glContextPtr;
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
}
2013-12-08 14:13:48 +01:00
void* GetGLContext(void* window) {
return [(EbitenWindow*)window glContext];
}
2013-11-23 17:17:22 +01:00
void BeginDrawing(void* window) {
2013-12-06 18:20:48 +01:00
// TODO: CGLLock
2013-11-24 18:12:07 +01:00
[[(EbitenWindow*)window glContext] makeCurrentContext];
2013-11-23 18:00:09 +01:00
glClear(GL_COLOR_BUFFER_BIT);
2013-11-22 18:56:18 +01:00
}
2013-11-23 17:17:22 +01:00
void EndDrawing(void* window) {
[[(EbitenWindow*)window glContext] flushBuffer];
2013-11-22 18:56:18 +01:00
}