2013-10-12 20:17:51 +02:00
|
|
|
// -*- objc -*-
|
|
|
|
|
|
|
|
#include "ebiten_opengl_view.h"
|
2013-10-13 16:33:22 +02:00
|
|
|
#include "input.h"
|
2013-10-12 20:17:51 +02:00
|
|
|
|
2013-10-13 16:06:05 +02:00
|
|
|
void ebiten_EbitenOpenGLView_Initialized(void);
|
|
|
|
void ebiten_EbitenOpenGLView_Updating(void);
|
2013-10-13 16:39:10 +02:00
|
|
|
void ebiten_EbitenOpenGLView_InputUpdated(InputType inputType, int x, int y);
|
2013-10-13 16:06:05 +02:00
|
|
|
|
2013-10-12 20:17:51 +02:00
|
|
|
// Reference:
|
|
|
|
// http://developer.apple.com/library/mac/#qa/qa1385/_index.html
|
|
|
|
// http://www.alecjacobson.com/weblog/?p=2185
|
|
|
|
|
|
|
|
// TODO: Use NSViewController?
|
|
|
|
|
|
|
|
static CVReturn
|
|
|
|
EbitenDisplayLinkCallback(CVDisplayLinkRef displayLink,
|
|
|
|
CVTimeStamp const* now,
|
|
|
|
CVTimeStamp const* outputTime,
|
|
|
|
CVOptionFlags flagsIn,
|
|
|
|
CVOptionFlags* flagsOut,
|
|
|
|
void* displayLinkContext) {
|
|
|
|
(void)displayLink;
|
|
|
|
(void)now;
|
|
|
|
(void)flagsIn;
|
|
|
|
(void)flagsOut;
|
|
|
|
@autoreleasepool {
|
|
|
|
EbitenOpenGLView* view = (__bridge EbitenOpenGLView*)displayLinkContext;
|
|
|
|
return [view getFrameForTime:outputTime];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation EbitenOpenGLView {
|
|
|
|
@private
|
|
|
|
CVDisplayLinkRef displayLink_;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
CVDisplayLinkRelease(self->displayLink_);
|
|
|
|
// Do not call [super dealloc] because of ARC.
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)prepareOpenGL {
|
|
|
|
[super prepareOpenGL];
|
|
|
|
NSOpenGLContext* openGLContext = [self openGLContext];
|
|
|
|
assert(openGLContext != nil);
|
2013-10-13 16:06:05 +02:00
|
|
|
GLint swapInterval = 1;
|
2013-10-12 20:17:51 +02:00
|
|
|
[openGLContext setValues:&swapInterval
|
|
|
|
forParameter:NSOpenGLCPSwapInterval];
|
|
|
|
CVDisplayLinkCreateWithActiveCGDisplays(&self->displayLink_);
|
|
|
|
CVDisplayLinkSetOutputCallback(self->displayLink_,
|
|
|
|
&EbitenDisplayLinkCallback,
|
|
|
|
(__bridge void*)self);
|
|
|
|
CGLContextObj cglContext = (CGLContextObj)[openGLContext CGLContextObj];
|
|
|
|
CGLPixelFormatObj cglPixelFormat =
|
|
|
|
(CGLPixelFormatObj)[[self pixelFormat] CGLPixelFormatObj];
|
|
|
|
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(self->displayLink_,
|
|
|
|
cglContext,
|
|
|
|
cglPixelFormat);
|
|
|
|
CVDisplayLinkStart(self->displayLink_);
|
2013-10-13 16:06:05 +02:00
|
|
|
|
|
|
|
ebiten_EbitenOpenGLView_Initialized();
|
2013-10-12 20:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (CVReturn)getFrameForTime:(CVTimeStamp const*)outputTime {
|
|
|
|
(void)outputTime;
|
|
|
|
NSOpenGLContext* context = [self openGLContext];
|
|
|
|
assert(context != nil);
|
|
|
|
[context makeCurrentContext];
|
|
|
|
{
|
|
|
|
CGLLockContext((CGLContextObj)[context CGLContextObj]);
|
2013-10-13 16:06:05 +02:00
|
|
|
ebiten_EbitenOpenGLView_Updating();
|
2013-10-12 20:17:51 +02:00
|
|
|
[context flushBuffer];
|
|
|
|
CGLUnlockContext((CGLContextObj)[context CGLContextObj]);
|
|
|
|
}
|
|
|
|
return kCVReturnSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isFlipped {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDown:(NSEvent*)theEvent {
|
|
|
|
NSPoint location = [self convertPoint:[theEvent locationInWindow]
|
|
|
|
fromView:nil];
|
2013-10-13 16:33:22 +02:00
|
|
|
int x = location.x;
|
|
|
|
int y = location.y;
|
|
|
|
ebiten_EbitenOpenGLView_InputUpdated(InputTypeMouseDown, x, y);
|
2013-10-12 20:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseUp:(NSEvent*)theEvent {
|
|
|
|
(void)theEvent;
|
2013-10-13 16:33:22 +02:00
|
|
|
NSPoint location = [self convertPoint:[theEvent locationInWindow]
|
|
|
|
fromView:nil];
|
|
|
|
int x = location.x;
|
|
|
|
int y = location.y;
|
|
|
|
ebiten_EbitenOpenGLView_InputUpdated(InputTypeMouseUp, x, y);
|
2013-10-12 20:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDragged:(NSEvent*)theEvent {
|
|
|
|
NSPoint location = [self convertPoint:[theEvent locationInWindow]
|
|
|
|
fromView:nil];
|
2013-10-13 16:33:22 +02:00
|
|
|
int x = location.x;
|
|
|
|
int y = location.y;
|
|
|
|
ebiten_EbitenOpenGLView_InputUpdated(InputTypeMouseDragged, x, y);
|
2013-10-12 20:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|