-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathc4g_context_macos.mm
79 lines (58 loc) · 1.52 KB
/
c4g_context_macos.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
** C4GPU.
**
** For the latest info, see https://github.com/c4gpu/c4gpu_runtime/
**
** Copyright (C) 2017 Wang Renxin. All rights reserved.
*/
#include "c4g_context_macos.h"
#include <GLKit/GLKit.h>
#ifdef C4G_RUNTIME_OS_MAC
namespace c4g {
namespace gl {
struct Context {
CGLContextObj _context = nullptr;
CGLContextObj _oldContext = nullptr;
};
Context* createContext(void) {
Context* result = new Context();
CGLPixelFormatAttribute pixelFormatAttributes[] = {
kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core,
kCGLPFAColorSize, (CGLPixelFormatAttribute)24,
kCGLPFAAlphaSize, (CGLPixelFormatAttribute)8,
kCGLPFAAccelerated,
(CGLPixelFormatAttribute)0
};
CGLPixelFormatObj pixelFormat;
GLint numberOfPixels;
CGLChoosePixelFormat(pixelFormatAttributes, &pixelFormat, &numberOfPixels);
CGLCreateContext(pixelFormat, 0, &result->_context);
CGLDestroyPixelFormat(pixelFormat);
pushContext(result);
return result;
}
void finishCreatingContext(Context* ctx) {
popContext(ctx);
}
void destroyContext(Context* ctx) {
if (!ctx) return;
CGLDestroyContext(ctx->_context);
delete ctx;
}
void pushContext(Context* ctx) {
if (!ctx) return;
ctx->_oldContext = CGLGetCurrentContext();
CGLSetCurrentContext(ctx->_context);
}
void popContext(Context* ctx) {
if (!ctx) return;
CGLSetCurrentContext(ctx->_oldContext);
ctx->_oldContext = nullptr;
}
bool isContext(Context* ctx) {
if (!ctx) return false;
return ctx->_context == CGLGetCurrentContext();
}
}
}
#endif /* C4G_RUNTIME_OS_MAC */