1 |
jakw |
1 |
//
|
|
|
2 |
// OpenGLContext.Mac.mm
|
|
|
3 |
//
|
|
|
4 |
// Created by Jakob Wilm on 18/03/13.
|
|
|
5 |
// Copyright (c) 2013 Jakob Wilm. All rights reserved.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
#include "OpenGLContext.h"
|
|
|
9 |
|
|
|
10 |
#include <Cocoa/Cocoa.h>
|
|
|
11 |
#include <OpenGL/OpenGL.h>
|
|
|
12 |
|
|
|
13 |
struct OpenGLContext::OpenGLContextInfo{
|
|
|
14 |
NSOpenGLContext *context;
|
|
|
15 |
OpenGLContextInfo() : context(NULL){}
|
|
|
16 |
};
|
|
|
17 |
|
|
|
18 |
std::vector<ScreenInfo> OpenGLContext::GetScreenInfo(){
|
|
|
19 |
std::vector<ScreenInfo> ret;
|
|
|
20 |
|
|
|
21 |
NSArray *screens = [NSScreen screens];
|
|
|
22 |
for (unsigned int i=0; i<[screens count]; i++) {
|
|
|
23 |
NSRect frame = [[screens objectAtIndex:i] frame];
|
|
|
24 |
ScreenInfo screenInfo;
|
|
|
25 |
screenInfo.resX = frame.size.width;
|
|
|
26 |
screenInfo.resY = frame.size.height;
|
|
|
27 |
ret.push_back(screenInfo);
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
return ret;
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
OpenGLContext::OpenGLContext(unsigned int _screenNum){
|
|
|
34 |
// Set instance var
|
|
|
35 |
screenNum = _screenNum;
|
|
|
36 |
std::vector<ScreenInfo> screens = GetScreenInfo();
|
|
|
37 |
|
|
|
38 |
if(screenNum > screens.size())
|
|
|
39 |
throw "Could not create OpenGLContext. Screen not available!";
|
|
|
40 |
|
|
|
41 |
contextInfo = new OpenGLContextInfo();
|
|
|
42 |
|
|
|
43 |
// Create a fullscreen OpenGL OpenGLContext on the specified screen
|
|
|
44 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
45 |
|
|
|
46 |
// Instatiate NSApplication and connect to window server
|
|
|
47 |
[NSApplication sharedApplication];
|
|
|
48 |
|
|
|
49 |
// Chose application with dock icon (OS X Mavericks)
|
|
|
50 |
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
|
51 |
|
|
|
52 |
NSRect displayRect = [[[NSScreen screens] objectAtIndex:screenNum] frame];
|
|
|
53 |
screenResX = displayRect.size.width;
|
|
|
54 |
screenResY = displayRect.size.height;
|
|
|
55 |
|
|
|
56 |
NSRect viewRect = NSMakeRect(0.0, 0.0, displayRect.size.width, displayRect.size.height);
|
|
|
57 |
|
|
|
58 |
NSOpenGLPixelFormatAttribute attrs[] = {NSOpenGLPFADoubleBuffer,0};
|
|
|
59 |
NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
|
|
|
60 |
|
|
|
61 |
NSOpenGLView *openGLView = [[NSOpenGLView alloc] initWithFrame:viewRect pixelFormat:pixelFormat];
|
|
|
62 |
|
|
|
63 |
NSDictionary *fullScreenOptions = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:FALSE], @"NSFullScreenModeAllScreens", nil];
|
|
|
64 |
[openGLView enterFullScreenMode:[[NSScreen screens] objectAtIndex:screenNum] withOptions:fullScreenOptions];
|
|
|
65 |
|
|
|
66 |
contextInfo->context = [openGLView openGLContext];
|
|
|
67 |
[contextInfo->context makeCurrentContext];
|
|
|
68 |
|
|
|
69 |
// Set the swap interval to match vsync
|
|
|
70 |
const GLint swapInterval = 1;
|
|
|
71 |
[contextInfo->context setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
|
|
|
72 |
|
|
|
73 |
// Adjust gamma to one
|
|
|
74 |
NSNumber *screenNumber = [[[[NSScreen screens] objectAtIndex:screenNum] deviceDescription] objectForKey:@"NSScreenNumber"];
|
|
|
75 |
CGSetDisplayTransferByFormula([screenNumber intValue], 0, 1, 1, 0, 1, 1, 0, 1, 1);
|
|
|
76 |
|
|
|
77 |
// // Disable cursor
|
|
|
78 |
// [openGLView addCursorRect:viewRect cursor:[NSCursor crosshairCursor]];
|
|
|
79 |
[pool drain];
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
void OpenGLContext::setGamma(float gamma){
|
|
|
83 |
// Adjust gamma
|
|
|
84 |
NSNumber *screenNumber = [[[[NSScreen screens] objectAtIndex:screenNum] deviceDescription] objectForKey:@"NSScreenNumber"];
|
|
|
85 |
CGSetDisplayTransferByFormula([screenNumber intValue], 0, 1, gamma, 0, 1, gamma, 0, 1, gamma);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
OpenGLContext::~OpenGLContext(){
|
|
|
89 |
[[contextInfo->context view] exitFullScreenModeWithOptions:Nil];
|
|
|
90 |
//[[_openGLContext view] dealloc];
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
void OpenGLContext::flush(){
|
|
|
94 |
// Flush OpenGL commands
|
|
|
95 |
[contextInfo->context flushBuffer];
|
|
|
96 |
//glSwapAPPLE();
|
|
|
97 |
//glFinishRenderAPPLE();
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
void OpenGLContext::makeContextCurrent(){
|
|
|
101 |
[contextInfo->context makeCurrentContext];
|
|
|
102 |
}
|