Skip to content

Instantly share code, notes, and snippets.

@slmjkdbtl
Last active January 27, 2021 21:04
Show Gist options
  • Save slmjkdbtl/15b031d3fcb9b28a5a342d19b2ac4354 to your computer and use it in GitHub Desktop.
Save slmjkdbtl/15b031d3fcb9b28a5a342d19b2ac4354 to your computer and use it in GitHub Desktop.
basic gfx context
// macOS
// CFLAGS += -ObjC
// LDFLAGS += -framework Cocoa -framework OpenGL
#define GL_SILENCE_DEPRECATION 1
#import <Cocoa/Cocoa.h>
#include <OpenGL/gl.h>
int main() {
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES];
NSWindow *window = [[NSWindow alloc]
initWithContentRect:NSMakeRect(0, 0, 400, 400)
styleMask:
0
| NSWindowStyleMaskTitled
| NSWindowStyleMaskClosable
| NSWindowStyleMaskResizable
| NSWindowStyleMaskMiniaturizable
backing:NSBackingStoreBuffered
defer:NO
];
NSOpenGLView *view = [[NSOpenGLView alloc] init];
[window setContentView:view];
[window makeFirstResponder:view];
[window setTitle:@"hi"];
[window center];
[window makeKeyAndOrderFront:nil];
NSOpenGLContext *ctx = [view openGLContext];
[ctx makeCurrentContext];
while (1) {
while (1) {
NSEvent *event = [NSApp
nextEventMatchingMask:NSEventMaskAny
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES
];
if (event == nil) {
break;
}
switch (event.type) {
case NSEventTypeKeyDown:
break;
default:
break;
}
[NSApp sendEvent:event];
}
NSPoint mpos = [window mouseLocationOutsideOfEventStream];
glClearColor(0, mpos.x / 400, mpos.y / 400, 1);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment