Created
January 28, 2024 06:49
-
-
Save colrdavidson/53b02691b69f19301c14a5d4a60e4835 to your computer and use it in GitHub Desktop.
Getting OpenGL on OSX with Odin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+build darwin | |
package main | |
import "core:fmt" | |
import "core:strings" | |
import "core:os" | |
import gl "vendor:OpenGL" | |
import NS "vendor:darwin/Foundation" | |
import CA "vendor:darwin/QuartzCore" | |
open_file_dialog :: proc() -> (string, bool) { | |
panel := NS.OpenPanel.openPanel() | |
panel->setCanChooseFiles(true) | |
panel->setResolvesAliases(true) | |
panel->setCanChooseDirectories(false) | |
panel->setAllowsMultipleSelection(false) | |
if panel->runModal() == .OK { | |
urls := panel->URLs() | |
ret_count := urls->count() | |
if ret_count != 1 { | |
return "", false | |
} | |
url := urls->objectAs(0, ^NS.URL) | |
return strings.clone_from_cstring(url->fileSystemRepresentation()), true | |
} | |
return "", false | |
} | |
GFX_Context :: struct { | |
app: ^NS.Application, | |
window: ^NS.Window, | |
ctx: ^NS.OpenGLContext, | |
} | |
create_context :: proc(width, height: int) -> GFX_Context { | |
gfx := GFX_Context{} | |
frame_rect := NS.Rect{{0, 0}, {NS.Float(width), NS.Float(height)}} | |
app: ^NS.Application | |
window: ^NS.Window | |
{ | |
app = NS.Application.sharedApplication() | |
app->setActivationPolicy(.Regular) | |
window = NS.Window_alloc() | |
window->initWithContentRect(frame_rect, {.Resizable, .Miniaturizable, .Closable, .Titled}, .Buffered, false) | |
window->setTitle(NS.AT("Title Test")) | |
{ | |
delegate := NS.window_delegate_register_and_alloc({ | |
windowShouldClose = proc(^NS.Window) -> NS.BOOL { | |
running = false | |
return true | |
}, | |
}, "window_delegate", context) | |
window->setDelegate(delegate) | |
} | |
window->makeKeyAndOrderFront(nil) | |
app->activateIgnoringOtherApps(true) | |
} | |
attrs := [?]NS.OpenGLPixelFormatAttribute{ | |
NS.OpenGLPFAAccelerated, NS.OpenGLPFADoubleBuffer, | |
NS.OpenGLPFAOpenGLProfile, NS.OpenGLProfileVersion3_2Core, | |
NS.OpenGLPFAColorSize, 24, | |
NS.OpenGLPFAAlphaSize, 8, | |
NS.OpenGLPFADepthSize, 24, | |
NS.OpenGLPFAStencilSize, 8, | |
NS.OpenGLPFASampleBuffers, 0, | |
0, | |
} | |
gl_pixelfmt := NS.OpenGLPixelFormat_alloc() | |
gl_pixelfmt->initWithAttributes(raw_data(attrs[:])) | |
gl_view := NS.OpenGLView_alloc() | |
gl_view->initWithFrame(frame_rect, gl_pixelfmt) | |
gl_view->updateTrackingAreas() | |
gl_view->setWantsBestResolutionOpenGLSurface(true) | |
gl_ctx := gl_view->getOpenGLContext() | |
gl_ctx->makeCurrentContext() | |
swap_int : i32 = 1 | |
gl_ctx->setValues(&swap_int, NS.OpenGLContextParameterSwapInterval) | |
window->setContentView(gl_view) | |
window->makeFirstResponder(gl_view) | |
major_version := 3 | |
minor_version := 3 | |
gl.load_up_to(major_version, minor_version, NS.gl_set_proc_address) | |
gfx.app = app | |
gfx.window = window | |
gfx.ctx = gl_ctx | |
return gfx | |
} | |
get_next_event :: proc(gfx: ^GFX_Context) -> Event { | |
if !running { | |
return Event{type = .Exit} | |
} | |
event := gfx.app->nextEventMatchingMask(NS.EventMaskAny, nil, NS.DefaultRunLoopMode, true) | |
if event == nil { | |
return Event{type = .None} | |
} | |
ev_type := event->type() | |
if ev_type == .LeftMouseDown { | |
pos := event->locationInWindow() | |
fmt.printf("clicked! (%v, %v)\n", pos.x, pos.y) | |
} | |
gfx.app->sendEvent(event) | |
return Event{type = .None} | |
} | |
swap_buffers :: proc(gfx: ^GFX_Context) { | |
gfx.ctx->flushBuffer() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment