diff --git a/ui/cocoa/cocoa.go b/ui/cocoa/cocoa.go index 0cc6b1e83..93f104fe1 100644 --- a/ui/cocoa/cocoa.go +++ b/ui/cocoa/cocoa.go @@ -7,7 +7,9 @@ package cocoa // #include "input.h" // // void StartApplication(void); -// void* CreateWindow(size_t width, size_t height, const char* title); +// void* CreateGLContext(void* sharedGLContext); +// void SetCurrentGLContext(void* glContext); +// void* CreateWindow(size_t width, size_t height, const char* title, void* glContext); // void PollEvents(void); // void BeginDrawing(void* window); // void EndDrawing(void* window); @@ -77,14 +79,17 @@ func New(screenWidth, screenHeight, screenScale int, title string) *UI { C.StartApplication() - ui.window = C.CreateWindow(C.size_t(ui.screenWidth * ui.screenScale), - C.size_t(ui.screenHeight * ui.screenScale), - cTitle) + context := C.CreateGLContext(unsafe.Pointer(nil)) + C.SetCurrentGLContext(context); ui.graphicsDevice = opengl.NewDevice( ui.screenWidth, ui.screenHeight, ui.screenScale) + ui.window = C.CreateWindow(C.size_t(ui.screenWidth * ui.screenScale), + C.size_t(ui.screenHeight * ui.screenScale), + cTitle, + context) currentUI = ui return ui } diff --git a/ui/cocoa/mainloop.c b/ui/cocoa/mainloop.c index 120c4ed68..b85854e15 100644 --- a/ui/cocoa/mainloop.c +++ b/ui/cocoa/mainloop.c @@ -15,7 +15,7 @@ void StartApplication() { [app activateIgnoringOtherApps:YES]; } -void* CreateGLContext() { +void* CreateGLContext(void* sharedGLContext) { NSOpenGLPixelFormatAttribute attributes[] = { NSOpenGLPFAWindow, NSOpenGLPFADoubleBuffer, @@ -25,24 +25,27 @@ void* CreateGLContext() { }; NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]; - NSOpenGLContext* glContext = [[NSOpenGLContext alloc] initWithFormat:format - shareContext:nil]; + NSOpenGLContext* glContext = + [[NSOpenGLContext alloc] initWithFormat:format + shareContext:(NSOpenGLContext*)sharedGLContext]; [format release]; return glContext; } -void* CreateWindow(size_t width, size_t height, const char* title) { - NSOpenGLContext* glContext = CreateGLContext(); - [glContext makeCurrentContext]; +void SetCurrentGLContext(void* glContext) { + [(NSOpenGLContext*)glContext makeCurrentContext]; +} +// This takes the ownership of glContext. +void* CreateWindow(size_t width, size_t height, const char* title, void* glContext) { NSSize size = NSMakeSize(width, height); EbitenWindow* window = [[EbitenWindow alloc] initWithSize:size - glContext:glContext]; + glContext:(NSOpenGLContext*)glContext]; [window setTitle: [[NSString alloc] initWithUTF8String:title]]; [window makeKeyAndOrderFront:nil]; - [glContext setView:[window contentView]]; + [(NSOpenGLContext*)glContext setView:[window contentView]]; return window; } @@ -61,6 +64,7 @@ void PollEvents(void) { } void BeginDrawing(void* window) { + [[(EbitenWindow*)window glContext] makeCurrentContext]; glClear(GL_COLOR_BUFFER_BIT); }