2020-01-01 23:13:32 +00:00
|
|
|
/* Mac OpenGL layer for 4coder */
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2020-01-02 22:09:22 +00:00
|
|
|
@interface OpenGLView : NSOpenGLView
|
|
|
|
- (void)initGL;
|
|
|
|
- (void)render:(Render_Target*)target;
|
|
|
|
@end
|
|
|
|
|
|
|
|
global OpenGLView *opengl_view;
|
|
|
|
|
|
|
|
@implementation OpenGLView{
|
|
|
|
b32 glIsInitialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init{
|
|
|
|
self = [super init];
|
|
|
|
if (self == nil){
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
glIsInitialized = false;
|
|
|
|
[self initGL];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc{
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)prepareOpenGL{
|
|
|
|
[super prepareOpenGL];
|
|
|
|
|
|
|
|
[[self openGLContext] makeCurrentContext];
|
2020-01-01 23:13:32 +00:00
|
|
|
|
2020-01-02 22:09:22 +00:00
|
|
|
// NOTE(yuval): Setup vsync
|
|
|
|
GLint swapInt = 1;
|
|
|
|
printf("Using vsync: %d\n", swapInt);
|
|
|
|
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)awakeFromNib{
|
|
|
|
[self initGL];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reshape{
|
|
|
|
[super reshape];
|
|
|
|
|
|
|
|
[[self openGLContext] makeCurrentContext];
|
|
|
|
[[self openGLContext] update];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)initGL{
|
|
|
|
if (glIsInitialized){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(yuval): Setup OpenGL
|
|
|
|
NSOpenGLPixelFormatAttribute opengl_attrs[] = {
|
|
|
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
|
|
|
NSOpenGLPFAAccelerated,
|
|
|
|
NSOpenGLPFADoubleBuffer,
|
|
|
|
NSOpenGLPFAColorSize, 32,
|
|
|
|
NSOpenGLPFAAlphaSize, 8,
|
|
|
|
NSOpenGLPFADepthSize, 24,
|
|
|
|
NSOpenGLPFASampleBuffers, 1,
|
|
|
|
NSOpenGLPFASamples, 16,
|
|
|
|
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];
|
|
|
|
|
|
|
|
glIsInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)render:(Render_Target*)target{
|
|
|
|
Assert(glIsInitialized);
|
|
|
|
|
|
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
|
|
[[self openGLContext] makeCurrentContext];
|
|
|
|
gl_render(target);
|
|
|
|
[[self openGLContext] flushBuffer];
|
|
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
function void
|
|
|
|
mac_gl_init(NSWindow *window){
|
|
|
|
// NOTE(yuval): Create OpenGLView
|
|
|
|
NSView *content_view = [window contentView];
|
|
|
|
|
|
|
|
opengl_view = [[OpenGLView alloc] init];
|
|
|
|
[opengl_view setFrame:[content_view bounds]];
|
|
|
|
[opengl_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
|
|
|
|
[opengl_view setWantsBestResolutionOpenGLSurface:YES];
|
|
|
|
|
|
|
|
// NOTE(yuval): Add the OpenGL view as a subview of the window
|
|
|
|
[content_view addSubview:opengl_view];
|
2020-01-01 23:13:32 +00:00
|
|
|
|
|
|
|
// NOTE(yuval): Load gl functions
|
2020-01-02 22:09:22 +00:00
|
|
|
void *gl_image = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
|
2020-01-01 23:13:32 +00:00
|
|
|
|
2020-01-02 22:09:22 +00:00
|
|
|
#define GL_FUNC(f,R,P) ((f) = (f##_Function*)dlsym(gl_image, #f));
|
|
|
|
#include "mac_4ed_opengl_funcs.h"
|
2020-01-01 23:13:32 +00:00
|
|
|
}
|
2020-01-02 22:09:22 +00:00
|
|
|
|
|
|
|
function void
|
|
|
|
mac_gl_render(Render_Target* target){
|
|
|
|
f64 begin_time = system_now_time() / 1000000.0;
|
|
|
|
[opengl_view render:target];
|
|
|
|
f64 end_time = system_now_time() / 1000000.0;
|
|
|
|
printf("Render Time: %fs\n", (end_time - begin_time));
|
|
|
|
}
|