aboutsummaryrefslogtreecommitdiffstats
path: root/code/platform_mac/mac_4ed_opengl.mm
blob: f700ea0ee62131d0c1b8921109f2b6f17f2b1277 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* Mac OpenGL layer for 4coder */

#include "mac_4ed_renderer.h"

////////////////////////////////
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>

#include "opengl/4ed_opengl_defines.h"

#define GL_FUNC(N,R,P) typedef R (CALL_CONVENTION N##_Function)P; N##_Function *N = 0;
#include "mac_4ed_opengl_funcs.h"

////////////////////////////////

#include "opengl/4ed_opengl_render.cpp"

////////////////////////////////

@interface OpenGL_View : NSOpenGLView
- (void)init_gl;
- (void)render:(Render_Target*)target;
@end

////////////////////////////////

struct Mac_OpenGL{
    Mac_Renderer base;
    
    OpenGL_View *view;
};

////////////////////////////////

@implementation OpenGL_View{
    b32 gl_is_initialized;
}

- (id)init{
    self = [super init];
    if (self == nil){
        return nil;
    }
    
    gl_is_initialized = false;
    [self init_gl];
    
    return self;
}

- (void)dealloc{
    [super dealloc];
}

- (void)prepareOpenGL{
    [super prepareOpenGL];
    
    [[self openGLContext] makeCurrentContext];
    
    // NOTE(yuval): Setup vsync
    GLint swap_int = 1;
    [[self openGLContext] setValues:&swap_int forParameter:NSOpenGLCPSwapInterval];
}

- (void)awakeFromNib{
    [self init_gl];
}

- (void)reshape{
    [super reshape];
    
	[[self openGLContext] makeCurrentContext];
	[[self openGLContext] update];
}

- (void)init_gl{
    if (gl_is_initialized){
        return;
    }
    
    // NOTE(yuval): Setup OpenGL
    NSOpenGLPixelFormatAttribute opengl_attrs[] = {
        NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
        NSOpenGLPFAAccelerated,
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFAColorSize, 32,
        NSOpenGLPFAAlphaSize, 8,
        NSOpenGLPFADepthSize, 24,
        0
    };
    
    NSOpenGLPixelFormat *pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:opengl_attrs];
    if (pixel_format == nil){
        fprintf(stderr, "Error creating OpenGLPixelFormat\n");
        exit(1);
    }
    
    NSOpenGLContext *context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:nil];
    
    [self setPixelFormat:pixel_format];
    [self setOpenGLContext:context];
    
    [context makeCurrentContext];
    
    [pixel_format release];
    
    gl_is_initialized = true;
}

- (void)render:(Render_Target*)target{
    Assert(gl_is_initialized);
    
    CGLLockContext([[self openGLContext] CGLContextObj]);
    [[self openGLContext] makeCurrentContext];
    
    gl_render(target);
    
    [[self openGLContext] flushBuffer];
    CGLUnlockContext([[self openGLContext] CGLContextObj]);
}
@end

////////////////////////////////

function
mac_render_sig(mac_gl__render){
#if defined(FRED_INTERNAL)
    printf("Redering using OpenGL!\n");
#endif
    
    Mac_OpenGL *gl = (Mac_OpenGL*)renderer;
    [gl->view render:target];
}

function
mac_get_texture_sig(mac_gl__get_texture){
    u32 result = gl__get_texture(dim, texture_kind);
    return(result);
}

function
mac_fill_texture_sig(mac_gl__fill_texture){
    b32 result = gl__fill_texture(texture_kind, texture, p, dim, data);
    return(result);
}

function
mac_free_texture_sig(mac_gl__free_texture){
    gl__free_texture(texture);
}

function Mac_OpenGL*
mac_gl__init(NSWindow *window, Render_Target *target){
    // NOTE(yuval): Create the Mac OpenGL Renderer
    Mac_OpenGL *gl = (Mac_OpenGL*)system_memory_allocate(sizeof(Mac_OpenGL),
                                                         file_name_line_number_lit_u8);
    gl->base.render = mac_gl__render;
    gl->base.get_texture = mac_gl__get_texture;
    gl->base.fill_texture = mac_gl__fill_texture;
    gl->base.free_texture = mac_gl__free_texture;
    
    // NOTE(yuval): Create the OpenGL view
    NSView *content_view = [window contentView];
    
    gl->view = [[OpenGL_View alloc] init];
    [gl->view setFrame:[content_view bounds]];
    [gl->view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
    [gl->view setWantsBestResolutionOpenGLSurface:YES];
    
    // NOTE(yuval): Add the OpenGL view as a subview of the window
    [content_view addSubview:gl->view];
    
    // NOTE(yuval): Load gl functions
    void *gl_image = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
    
#define GL_FUNC(f,R,P) ((f) = (f##_Function*)dlsym(gl_image, #f));
#include "mac_4ed_opengl_funcs.h"
    
    return(gl);
}

////////////////////////////////

// TODO(yuval): This function should be exported to a DLL
function
mac_load_renderer_sig(mac_load_opengl_renderer){
    Mac_Renderer *renderer = (Mac_Renderer*)mac_gl__init(window, target);
    return(renderer);
}