ebiten/ui/cocoa/mainloop.c

89 lines
2.6 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-11-23 17:17:22 +01:00
void StartApplication() {
EbitenController* controller = [[EbitenController alloc] init];
NSApplication* app = [NSApplication sharedApplication];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
[app setDelegate:controller];
[app finishLaunching];
[app activateIgnoringOtherApps:YES];
}
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-11-25 14:27:59 +01:00
void* CreateWindow(size_t width, size_t height, const char* title, void* sharedGLContext) {
NSOpenGLContext* glContext = CreateGLContext(sharedGLContext);
[glContext makeCurrentContext];
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-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-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
}