Skip to content

Commit 70a6576

Browse files
committed
Added the base for an opengl backend example
1 parent dce82cc commit 70a6576

File tree

7 files changed

+218
-1
lines changed

7 files changed

+218
-1
lines changed

examples/hello_macgl/AppDelegate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import <Cocoa/Cocoa.h>
2+
3+
@interface AppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate>
4+
5+
@end

examples/hello_macgl/AppDelegate.mm

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#import "./AppDelegate.h"
2+
#import "./GLView.h"
3+
4+
@implementation AppDelegate {
5+
NSString * _appName;
6+
NSWindow * _window;
7+
}
8+
9+
- (nonnull instancetype) init {
10+
if ((self = [super init])) {
11+
_appName = [[NSProcessInfo processInfo] processName];
12+
13+
NSApplication * application = [NSApplication sharedApplication];
14+
[application setMainMenu:[self createMenu]];
15+
_window = [self createWindow];
16+
}
17+
18+
return self;
19+
}
20+
21+
- (void) applicationWillFinishLaunching:(NSNotification *)notification {
22+
[_window setTitle:_appName];
23+
[_window cascadeTopLeftFromPoint:NSMakePoint(20, 20)];
24+
[_window makeKeyAndOrderFront:self];
25+
}
26+
27+
- (BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
28+
return TRUE;
29+
}
30+
31+
- (NSMenu *) createMenu {
32+
NSMenuItem * quit = [[NSMenuItem alloc] initWithTitle:[@"Quit " stringByAppendingString:_appName] action:@selector(terminate:) keyEquivalent:@"q"];
33+
NSMenuItem * item = [[NSMenuItem alloc] init];
34+
item.submenu = [[NSMenu alloc] init];
35+
[item.submenu addItem:quit];
36+
37+
NSMenu * mainMenu = [[NSMenu alloc] init];
38+
[mainMenu addItem:item];
39+
40+
return mainMenu;
41+
}
42+
43+
- (NSWindow *) createWindow {
44+
NSWindow * window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 640, 480)
45+
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable
46+
backing:NSBackingStoreBuffered
47+
defer:NO];
48+
49+
GLView * glView = [[GLView alloc] initWithFrame:window.contentView.frame];
50+
[window.contentView addSubview:glView];
51+
52+
return window;
53+
}
54+
55+
@end

examples/hello_macgl/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
if(NOT APPLE)
4+
message(
5+
WARNING
6+
"Cannot create `hello_macgl` example"
7+
)
8+
else()
9+
project(hello_macgl)
10+
set(CMAKE_CXX_STANDARD 17)
11+
12+
add_executable(hello_macgl)
13+
14+
target_sources(hello_macgl
15+
PRIVATE
16+
main.mm
17+
AppDelegate.mm
18+
GLView.mm
19+
)
20+
21+
target_link_libraries(hello_macgl
22+
PRIVATE
23+
graphics
24+
"-framework Cocoa"
25+
"-framework CoreVideo"
26+
"-framework OpenGL"
27+
)
28+
endif()

examples/hello_macgl/GLView.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#define GL_SILENCE_DEPRECATION 1
2+
#import <Cocoa/Cocoa.h>
3+
4+
@interface GLView : NSOpenGLView
5+
6+
- (void) drawFrame;
7+
8+
@end

examples/hello_macgl/GLView.mm

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#import "./GLView.h"
2+
3+
#import <CoreVideo/CoreVideo.h> // CVDisplayLinkRef
4+
#include <graphics/graphics.hpp>
5+
6+
#define SIZEOF_ARRAY(array) (sizeof(array) / sizeof(*array))
7+
8+
// Declare the graphics data used for initialization
9+
// #include "../shared/decl.inc"
10+
11+
namespace {
12+
CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp * now, const CVTimeStamp * outputTime, CVOptionFlags flagsIn, CVOptionFlags * flagsOut, void * displayLinkContext) {
13+
@autoreleasepool {
14+
// go back to Obj-C for easy access to instance variables
15+
[((__bridge GLView *) displayLinkContext) drawFrame];
16+
return kCVReturnSuccess;
17+
}
18+
}
19+
}
20+
21+
@implementation GLView {
22+
CVDisplayLinkRef _displayLink;
23+
graphics::Context * _context;
24+
}
25+
26+
- (nonnull instancetype) initWithFrame:(NSRect)frameRect {
27+
NSOpenGLPixelFormatAttribute pixelFormatAttribs[] = {
28+
NSOpenGLPFAAccelerated,
29+
NSOpenGLPFADoubleBuffer,
30+
NSOpenGLPFAColorSize, 32,
31+
NSOpenGLPFADepthSize, 32,
32+
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
33+
0,
34+
};
35+
36+
NSOpenGLPixelFormat * pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttribs];
37+
if (pixelFormat == nil) {
38+
fprintf(stderr, "Unable to create windowed pixel format.\n");
39+
abort();
40+
}
41+
42+
if ((self = [super initWithFrame:frameRect pixelFormat:pixelFormat])) {
43+
NSOpenGLContext * currentContext = self.openGLContext;
44+
[currentContext makeCurrentContext];
45+
46+
// Remember to lock the context before we touch it since display link is threaded
47+
CGLContextObj cglContext = currentContext.CGLContextObj;
48+
CGLLockContext(cglContext);
49+
50+
// Enable vsync to eliminate tearing
51+
GLint vsync = 1;
52+
[[self openGLContext] setValues:&vsync forParameter:NSOpenGLContextParameterSwapInterval];
53+
54+
// Create the graphics context
55+
// _context = new graphics::Context();
56+
57+
// Initialize the graphics objects
58+
// #include "../shared/init.inc"
59+
60+
CGLUnlockContext(cglContext);
61+
62+
// Set up the display link
63+
CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
64+
CVDisplayLinkSetOutputCallback(_displayLink, displayLinkCallback, (__bridge void *) self);
65+
CGLPixelFormatObj cglPixelFormat = (CGLPixelFormatObj) [[self pixelFormat] CGLPixelFormatObj];
66+
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(_displayLink, cglContext, cglPixelFormat);
67+
68+
CVDisplayLinkStart(_displayLink);
69+
}
70+
71+
return self;
72+
}
73+
74+
- (void) dealloc {
75+
CVDisplayLinkStop(_displayLink);
76+
77+
[super dealloc];
78+
}
79+
80+
- (void) drawFrame {
81+
NSOpenGLContext * currentContext = self.openGLContext;
82+
[currentContext makeCurrentContext];
83+
84+
// Must lock GL context because display link is threaded
85+
CGLLockContext(currentContext.CGLContextObj);
86+
87+
// Render the frame
88+
// #include "../shared/update.inc"
89+
90+
// Render the frame
91+
// #include "../shared/render.inc"
92+
93+
glClearColor(0.0, 0.5, 0.75, 1.0);
94+
glClear(GL_COLOR_BUFFER_BIT);
95+
96+
[currentContext flushBuffer];
97+
CGLUnlockContext(currentContext.CGLContextObj);
98+
}
99+
100+
- (void) drawRect:(NSRect)rect {
101+
[self drawFrame];
102+
}
103+
104+
@end

examples/hello_macgl/main.mm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#import <Cocoa/Cocoa.h>
2+
3+
#import "./AppDelegate.h"
4+
5+
int main() {
6+
@autoreleasepool {
7+
NSApplication * application = [NSApplication sharedApplication];
8+
[application setActivationPolicy:NSApplicationActivationPolicyRegular];
9+
10+
AppDelegate * appDelegate = [[AppDelegate alloc] init];
11+
[application setDelegate:appDelegate];
12+
[application activateIgnoringOtherApps:YES];
13+
[application run];
14+
}
15+
16+
return EXIT_SUCCESS;
17+
}

include/graphics/graphics.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#endif
2121

2222
#if GRAPHICS_USE_OPENGL
23-
#include "graphics/opengl/graphics.hpp"
23+
#include "graphics/opengl/context.hpp"
2424
namespace graphics {
2525
using namespace opengl;
2626
}

0 commit comments

Comments
 (0)