374 lines
13 KiB
C++
374 lines
13 KiB
C++
#include "pch.h"
|
|
#include "log.h"
|
|
#include "shader.h"
|
|
#include "shape.h"
|
|
#include "texture.h"
|
|
#include "image.h"
|
|
#include "app.h"
|
|
#include "keymap.h"
|
|
#include "main.h"
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
#include <Cocoa/Cocoa.h>
|
|
#include <CoreVideo/CoreVideo.h>
|
|
#include <OpenGL/OpenGL.h>
|
|
|
|
@implementation View
|
|
- (void)async_lock
|
|
{
|
|
CGLLockContext([glctx CGLContextObj]);
|
|
[glctx makeCurrentContext];
|
|
}
|
|
- (void)async_unlock
|
|
{
|
|
CGLUnlockContext([glctx CGLContextObj]);
|
|
}
|
|
- (void)async_swap
|
|
{
|
|
CGLFlushDrawable([glctx CGLContextObj]);
|
|
}
|
|
- (instancetype)initWithFrame:(NSRect)frameRect
|
|
{
|
|
gl_ready = false;
|
|
NSOpenGLPixelFormatAttribute attrs[] =
|
|
{
|
|
NSOpenGLPFADoubleBuffer,
|
|
NSOpenGLPFADepthSize, 24,
|
|
// Must specify the 3.2 Core Profile to use OpenGL 3.2
|
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
|
// Multisample
|
|
NSOpenGLPFAMultisample,
|
|
NSOpenGLPFASamples, 2,
|
|
NSOpenGLPFASampleBuffers, 1,
|
|
0
|
|
};
|
|
NSOpenGLPixelFormat *pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
|
|
NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
|
|
self = [super initWithFrame:frameRect pixelFormat:pf];
|
|
[self setPixelFormat:pf];
|
|
[self setOpenGLContext:context];
|
|
return self;
|
|
}
|
|
- (void)prepareOpenGL
|
|
{
|
|
NSLog(@"prepare");
|
|
|
|
// Synchronize buffer swaps with vertical refresh rate
|
|
GLint swapInt = 1;
|
|
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
|
|
|
|
// Create a display link capable of being used with all active displays
|
|
CVDisplayLinkCreateWithActiveCGDisplays(&dl);
|
|
|
|
// Set the renderer output callback function
|
|
CVDisplayLinkSetOutputCallback(dl, &MyDisplayLinkCallback, (__bridge void*)self);
|
|
|
|
// Set the display link for the current renderer
|
|
CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
|
|
CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
|
|
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(dl, cglContext, cglPixelFormat);
|
|
|
|
CGLEnable([self.openGLContext CGLContextObj], kCGLCECrashOnRemovedFunctions);
|
|
|
|
// Activate the display link
|
|
CVDisplayLinkStart(dl);
|
|
|
|
cgl = [[self openGLContext] CGLContextObj];
|
|
glctx = [self openGLContext];
|
|
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
App::I.init();
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
gl_ready = true;
|
|
}
|
|
|
|
- (void)terminateGL
|
|
{
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
CVDisplayLinkRelease(dl);
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
|
|
// This is the renderer output callback function
|
|
static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now,
|
|
const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)
|
|
{
|
|
CVReturn result = [(__bridge View*)displayLinkContext getFrameForTime:outputTime];
|
|
return result;
|
|
}
|
|
|
|
- (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime
|
|
{
|
|
static double _timeFreq = CVGetHostClockFrequency();
|
|
static double _prevTime = (double)outputTime->hostTime / _timeFreq;
|
|
double hostTime = (double)outputTime->hostTime;
|
|
double now = hostTime / _timeFreq;
|
|
|
|
// this will not update unless 1/30th of a second has passed since the last update
|
|
if (1 /*now < _prevTime + (1.0 / 30.0) &&*/ )
|
|
{
|
|
// We draw on a secondary thread through the display link
|
|
// When resizing the view, -reshape is called automatically on the main
|
|
// thread. Add a mutex around to avoid the threads accessing the context
|
|
// simultaneously when resizing
|
|
CGLLockContext([glctx CGLContextObj]);
|
|
[glctx makeCurrentContext];
|
|
|
|
if (App::I.redraw)
|
|
{
|
|
App::I.clear();
|
|
App::I.update(now - _prevTime);
|
|
CGLFlushDrawable([glctx CGLContextObj]);
|
|
}
|
|
|
|
//[[self openGLContext] flushBuffer];
|
|
// returning NO will cause the layer to NOT be redrawn
|
|
|
|
CGLUnlockContext([glctx CGLContextObj]);
|
|
return NO;
|
|
}
|
|
else
|
|
{
|
|
// change whatever you want to change here, as a function of time elapsed
|
|
_prevTime = now;
|
|
// return YES to have your layer redrawn
|
|
return YES;
|
|
}
|
|
|
|
return kCVReturnSuccess;
|
|
}
|
|
|
|
- (void)dealloc
|
|
{
|
|
// Release the display link
|
|
CVDisplayLinkRelease(dl);
|
|
}
|
|
|
|
- (void)drawRect:(NSRect)dirtyRect
|
|
{
|
|
NSLog(@"drawRect");
|
|
|
|
// We draw on a secondary thread through the display link
|
|
// When resizing the view, -reshape is called automatically on the main
|
|
// thread. Add a mutex around to avoid the threads accessing the context
|
|
// simultaneously when resizing
|
|
CGLLockContext(cgl);
|
|
[glctx makeCurrentContext];
|
|
|
|
App::I.redraw = true;
|
|
App::I.clear();
|
|
App::I.update(0);
|
|
|
|
//[[self openGLContext] flushBuffer];
|
|
// returning NO will cause the layer to NOT be redrawn
|
|
|
|
CGLFlushDrawable(cgl);
|
|
CGLUnlockContext(cgl);
|
|
}
|
|
|
|
- (void)reshape
|
|
{
|
|
[super reshape];
|
|
|
|
// We draw on a secondary thread through the display link. However, when
|
|
// resizing the view, -drawRect is called on the main thread.
|
|
// Add a mutex around to avoid the threads accessing the context
|
|
// simultaneously when resizing.
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
|
|
// Get the view size in Points
|
|
NSRect viewRectPoints = [self bounds];
|
|
|
|
#if SUPPORT_RETINA_RESOLUTION
|
|
|
|
// Rendering at retina resolutions will reduce aliasing, but at the potential
|
|
// cost of framerate and battery life due to the GPU needing to render more
|
|
// pixels.
|
|
|
|
// Any calculations the renderer does which use pixel dimentions, must be
|
|
// in "retina" space. [NSView convertRectToBacking] converts point sizes
|
|
// to pixel sizes. Thus the renderer gets the size in pixels, not points,
|
|
// so that it can set it's viewport and perform and other pixel based
|
|
// calculations appropriately.
|
|
// viewRectPixels will be larger than viewRectPoints for retina displays.
|
|
// viewRectPixels will be the same as viewRectPoints for non-retina displays
|
|
NSRect viewRectPixels = [self convertRectToBacking:viewRectPoints];
|
|
|
|
#else //if !SUPPORT_RETINA_RESOLUTION
|
|
|
|
// App will typically render faster and use less power rendering at
|
|
// non-retina resolutions since the GPU needs to render less pixels.
|
|
// There is the cost of more aliasing, but it will be no-worse than
|
|
// on a Mac without a retina display.
|
|
|
|
// Points:Pixels is always 1:1 when not supporting retina resolutions
|
|
NSRect viewRectPixels = viewRectPoints;
|
|
|
|
#endif // !SUPPORT_RETINA_RESOLUTION
|
|
App::I.resize(viewRectPixels.size.width, viewRectPixels.size.height);
|
|
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
- (void)renewGState
|
|
{
|
|
// Called whenever graphics state updated (such as window resize)
|
|
|
|
// OpenGL rendering is not synchronous with other rendering on the OSX.
|
|
// Therefore, call disableScreenUpdatesUntilFlush so the window server
|
|
// doesn't render non-OpenGL content in the window asynchronously from
|
|
// OpenGL content, which could cause flickering. (non-OpenGL content
|
|
// includes the title bar and drawing done by the app with other APIs)
|
|
[[self window] disableScreenUpdatesUntilFlush];
|
|
|
|
[super renewGState];
|
|
}
|
|
- (void)mouseDown:(NSEvent *)theEvent
|
|
{
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
|
App::I.mouse_down(0, mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure, kEventSource::Mouse);
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
- (void)rightMouseDown:(NSEvent *)theEvent
|
|
{
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
|
App::I.mouse_down(1, mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure, kEventSource::Mouse);
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
- (void)mouseUp:(NSEvent *)theEvent
|
|
{
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
|
App::I.mouse_up(0, mouseLoc.x, App::I.height - mouseLoc.y - 1, kEventSource::Mouse);
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
- (void)rightMouseUp:(NSEvent *)theEvent
|
|
{
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
|
App::I.mouse_up(1, mouseLoc.x, App::I.height - mouseLoc.y - 1, kEventSource::Mouse);
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
- (void)mouseMoved:(NSEvent *)theEvent
|
|
{
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
|
App::I.mouse_move(mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure, kEventSource::Mouse);
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
-(void)mouseDragged:(NSEvent *)theEvent
|
|
{
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
|
App::I.mouse_move(mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure, kEventSource::Mouse);
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
- (void)rightMouseDragged:(NSEvent *)theEvent
|
|
{
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
|
App::I.mouse_move(mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure, kEventSource::Mouse);
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
- (void)scrollWheel:(NSEvent *)theEvent
|
|
{
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
|
App::I.mouse_scroll(mouseLoc.x, App::I.height - mouseLoc.y - 1, [theEvent deltaY]);
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
- (void)keyDown:(NSEvent *)theEvent
|
|
{
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
auto keyCode = [theEvent keyCode];
|
|
auto chars = [theEvent characters];
|
|
App::I.key_down(convert_key(keyCode));
|
|
if (const char* cstr = [chars cStringUsingEncoding:NSASCIIStringEncoding])
|
|
App::I.key_char(cstr[0]);
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
- (void)keyUp:(NSEvent *)theEvent
|
|
{
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
auto keyCode = [theEvent keyCode];
|
|
auto chars = [theEvent characters];
|
|
App::I.key_up(convert_key(keyCode));
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
}
|
|
@end
|
|
|
|
@interface Window : NSWindow
|
|
@end @implementation Window
|
|
@end
|
|
|
|
@interface Controller : NSWindowController<NSWindowDelegate>
|
|
@end @implementation Controller
|
|
- (void)windowWillClose:(NSNotification *)notification
|
|
{
|
|
[[NSApplication sharedApplication] terminate:nil];
|
|
}
|
|
@end
|
|
|
|
@interface AppOSX : NSApplication<NSApplicationDelegate>
|
|
{
|
|
Window* window;
|
|
Controller* controller;
|
|
View* view;
|
|
}
|
|
@end @implementation AppOSX
|
|
- (instancetype)init
|
|
{
|
|
self = [super init];
|
|
[self setActivationPolicy:NSApplicationActivationPolicyRegular]; // make it to the front
|
|
[self setDelegate:self];
|
|
return self;
|
|
}
|
|
- (void)applicationWillTerminate:(NSNotification *)notification
|
|
{
|
|
[view terminateGL];
|
|
[window close];
|
|
}
|
|
- (void)applicationDidFinishLaunching:(NSNotification *)notification
|
|
{
|
|
App::I.initLog();
|
|
App::I.create();
|
|
NSRect r = NSMakeRect(0, 0, App::I.width, App::I.height);
|
|
|
|
view = [[View alloc] initWithFrame:r];
|
|
controller = [[Controller alloc] initWithWindow:window];
|
|
App::I.osx_view = view;
|
|
|
|
auto style = NSTitledWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask|NSClosableWindowMask;
|
|
window = [[Window alloc] initWithContentRect:r styleMask:style backing:NSBackingStoreBuffered defer:NO];
|
|
[window setDelegate:controller];
|
|
[window setTitle:@"PanoPainter 0.1.2 alpha"];
|
|
[window center];
|
|
[window makeKeyAndOrderFront:controller];
|
|
[window setContentView:view];
|
|
[window setAcceptsMouseMovedEvents:true];
|
|
[window makeFirstResponder:view];
|
|
|
|
auto menubar = [NSMenu new];
|
|
auto appMenuItem = [NSMenuItem new];
|
|
[menubar addItem:appMenuItem];
|
|
[self setMainMenu:menubar];
|
|
|
|
auto appMenu = [NSMenu new];
|
|
auto appName = [[NSProcessInfo processInfo] processName];
|
|
auto quitTitle = [@"Quit " stringByAppendingString:appName];
|
|
auto quitMenuItem = [[NSMenuItem alloc] initWithTitle:quitTitle
|
|
action:@selector(terminate:) keyEquivalent:@"q"];
|
|
[appMenu addItem:quitMenuItem];
|
|
[appMenuItem setSubmenu:appMenu];
|
|
|
|
NSLog(@"app launched");
|
|
}
|
|
@end
|
|
|
|
int main(int argc, const char * argv[])
|
|
{
|
|
AppOSX* app = [AppOSX sharedApplication];
|
|
[app run];
|
|
return 0;
|
|
}
|