]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/cgl/cocoaglcontext.m
Add OpenGL context support on OS X
[libs/gui.git] / source / graphics / cgl / cocoaglcontext.m
1 #import <AppKit/NSOpenGL.h>
2 #import <AppKit/NSWindow.h>
3 #include <stdlib.h>
4 #include "cocoaglcontext.h"
5
6 NSOpenGLPixelFormat *get_native_pixel_format(CocoaPixelFormat *);
7 NSWindow *get_native_window(CocoaWindow *);
8
9 struct _CocoaGLContext
10 {
11         NSOpenGLContext *context;
12 };
13
14 CocoaGLContext *create_gl_context(CocoaPixelFormat *format)
15 {
16         NSOpenGLContext *ctx = [NSOpenGLContext alloc];
17         ctx = [ctx initWithFormat:get_native_pixel_format(format) shareContext:nil];
18         if(!ctx)
19                 return NULL;
20
21         CocoaGLContext *wrapper = (CocoaGLContext *)malloc(sizeof(CocoaGLContext));
22         wrapper->context = ctx;
23         return wrapper;
24 }
25
26 void destroy_gl_context(CocoaGLContext *context)
27 {
28         [context->context release];
29         free(context);
30 }
31
32 void attach_gl_context_to_window(CocoaGLContext *context, CocoaWindow *window)
33 {
34         NSWindow *nswindow = get_native_window(window);
35         [context->context setView:[nswindow contentView]];
36 }
37
38 void make_gl_context_current(CocoaGLContext *context)
39 {
40         [context->context makeCurrentContext];
41 }
42
43 void flush_gl_buffer(CocoaGLContext *context)
44 {
45         [context->context flushBuffer];
46 }