ebiten/ui/cocoa/mainloop.c

70 lines
2.0 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-23 18:00:09 +01:00
void* CreateGLContext() {
NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFADepthSize, 32,
0,
};
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc]
initWithAttributes:attributes];
NSOpenGLContext* glContext = [[NSOpenGLContext alloc] initWithFormat:format
shareContext:nil];
[format release];
return glContext;
}
2013-11-23 17:17:22 +01:00
void* CreateWindow(size_t width, size_t height, const char* title) {
NSOpenGLContext* glContext = CreateGLContext();
[glContext makeCurrentContext];
2013-11-23 17:17:22 +01:00
NSSize size = NSMakeSize(width, height);
EbitenWindow* window = [[EbitenWindow alloc]
initWithSize:size
glContext:glContext];
2013-11-23 17:17:22 +01:00
[window setTitle: [[NSString alloc] initWithUTF8String:title]];
[window makeKeyAndOrderFront:nil];
[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-11-23 17:17:22 +01:00
void BeginDrawing(void* window) {
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
}