diff --git a/example/main.go b/example/main.go index 0ea6bae42..1ff15c229 100644 --- a/example/main.go +++ b/example/main.go @@ -28,7 +28,7 @@ func main() { u := cocoa.UI() textureFactory := cocoa.TextureFactory() - window := u.CreateWindow(screenWidth, screenHeight, screenScale, title) + window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title) textureFactoryEvents := textureFactory.Events() diff --git a/ui/cocoa/ebiten_window.c b/ui/cocoa/ebiten_window.c index 01bbf2803..350c0a02a 100644 --- a/ui/cocoa/ebiten_window.c +++ b/ui/cocoa/ebiten_window.c @@ -45,6 +45,11 @@ void ebiten_WindowClosed(void* nativeWindow); return self; } +- (void)dealloc { + [self->glContext_ release]; + [super dealloc]; +} + - (NSOpenGLContext*)glContext { return self->glContext_; } @@ -73,7 +78,6 @@ void ebiten_WindowClosed(void* nativeWindow); (void)alert; (void)contextInfo; if (returnCode == NSAlertDefaultReturn) { - [self->glContext_ release]; [self close]; ebiten_WindowClosed(self); } diff --git a/ui/cocoa/mainloop.c b/ui/cocoa/mainloop.c index 6be352a72..761747667 100644 --- a/ui/cocoa/mainloop.c +++ b/ui/cocoa/mainloop.c @@ -34,7 +34,7 @@ void StartApplication(void) { [app finishLaunching]; } -void* CreateGLContext(void* sharedGLContext) { +NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext) { NSOpenGLPixelFormatAttribute attributes[] = { NSOpenGLPFAWindow, NSOpenGLPFADoubleBuffer, @@ -46,13 +46,12 @@ void* CreateGLContext(void* sharedGLContext) { initWithAttributes:attributes]; NSOpenGLContext* glContext = [[NSOpenGLContext alloc] initWithFormat:format - shareContext:(NSOpenGLContext*)sharedGLContext]; + shareContext:sharedGLContext]; [format release]; return glContext; } -void* CreateWindow(size_t width, size_t height, const char* title, void* glContext_) { - NSOpenGLContext* glContext = (NSOpenGLContext*)glContext_; +NSWindow* CreateWindow(size_t width, size_t height, const char* title, NSOpenGLContext* glContext) { NSSize size = NSMakeSize(width, height); EbitenWindow* window = [[EbitenWindow alloc] initWithSize:size @@ -87,8 +86,7 @@ void PollEvents(void) { } } -void UseGLContext(void* glContextPtr) { - NSOpenGLContext* glContext = (NSOpenGLContext*)glContextPtr; +void UseGLContext(NSOpenGLContext* glContext) { CGLContextObj cglContext = [glContext CGLContextObj]; CGLLockContext(cglContext); [glContext makeCurrentContext]; @@ -102,16 +100,6 @@ void UnuseGLContext(void) { CGLUnlockContext(cglContext); } -void* GetGLContext(void* window) { +NSOpenGLContext* GetGLContext(NSWindow* window) { return [(EbitenWindow*)window glContext]; } - -void BeginDrawing(void* window) { - // TODO: CGLLock - [[(EbitenWindow*)window glContext] makeCurrentContext]; - glClear(GL_COLOR_BUFFER_BIT); -} - -void EndDrawing(void* window) { - [[(EbitenWindow*)window glContext] flushBuffer]; -} diff --git a/ui/cocoa/texture_factory.go b/ui/cocoa/texture_factory.go index e39f61374..9007f21e1 100644 --- a/ui/cocoa/texture_factory.go +++ b/ui/cocoa/texture_factory.go @@ -1,17 +1,18 @@ package cocoa -// void* CreateGLContext(void* sharedGLContext); -// void UseGLContext(void* glContext); +// @class NSOpenGLContext; +// +// NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext); +// void UseGLContext(NSOpenGLContext* glContext); // void UnuseGLContext(void); // import "C" import ( "runtime" - "unsafe" ) type textureFactory struct { - sharedContext unsafe.Pointer + sharedContext *C.NSOpenGLContext funcs chan func() funcsDone chan struct{} } @@ -24,7 +25,7 @@ func runTextureFactory() *textureFactory { ch := make(chan struct{}) go func() { runtime.LockOSThread() - t.sharedContext = C.CreateGLContext(unsafe.Pointer(nil)) + t.sharedContext = C.CreateGLContext(nil) close(ch) t.loop() }() diff --git a/ui/cocoa/ui.go b/ui/cocoa/ui.go index 26e969862..785c39b2e 100644 --- a/ui/cocoa/ui.go +++ b/ui/cocoa/ui.go @@ -47,7 +47,7 @@ func TextureFactory() graphics.TextureFactory { return getCurrentUI() } -func (u *cocoaUI) CreateWindow(width, height, scale int, title string) ui.Window { +func (u *cocoaUI) CreateGameWindow(width, height, scale int, title string) ui.GameWindow { return u.textureFactory.createWindow(u, width, height, scale, title) } diff --git a/ui/cocoa/window.go b/ui/cocoa/window.go index 45f7bd98d..852273f83 100644 --- a/ui/cocoa/window.go +++ b/ui/cocoa/window.go @@ -4,11 +4,16 @@ package cocoa // // #include "input.h" // -// void* CreateWindow(size_t width, size_t height, const char* title, void* glContext); -// void* CreateGLContext(void* sharedGLContext); +// @class NSWindow; +// @class NSOpenGLContext; // -// void* GetGLContext(void* window); -// void UseGLContext(void* glContext); +// typedef NSWindow* NSWindowPtr; +// +// NSWindow* CreateWindow(size_t width, size_t height, const char* title, NSOpenGLContext* glContext); +// NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext); +// +// NSOpenGLContext* GetGLContext(NSWindow* window); +// void UseGLContext(NSOpenGLContext* glContext); // void UnuseGLContext(void); // import "C" @@ -26,7 +31,7 @@ type Window struct { screenHeight int screenScale int closed bool - native unsafe.Pointer + native *C.NSWindow pressedKeys map[ui.Key]struct{} context *opengl.Context funcs chan func() @@ -34,9 +39,9 @@ type Window struct { events chan interface{} } -var windows = map[unsafe.Pointer]*Window{} +var windows = map[*C.NSWindow]*Window{} -func runWindow(cocoaUI *cocoaUI, width, height, scale int, title string, sharedContext unsafe.Pointer) *Window { +func runWindow(cocoaUI *cocoaUI, width, height, scale int, title string, sharedContext *C.NSOpenGLContext) *Window { w := &Window{ ui: cocoaUI, screenWidth: width, @@ -116,7 +121,7 @@ func (w *Window) notify(e interface{}) { // Now this function is not used anywhere. //export ebiten_WindowSizeUpdated -func ebiten_WindowSizeUpdated(nativeWindow unsafe.Pointer, width, height int) { +func ebiten_WindowSizeUpdated(nativeWindow C.NSWindowPtr, width, height int) { w := windows[nativeWindow] e := ui.WindowSizeUpdatedEvent{width, height} w.notify(e) @@ -141,7 +146,7 @@ var cocoaKeyCodeToKey = map[int]ui.Key{ } //export ebiten_KeyDown -func ebiten_KeyDown(nativeWindow unsafe.Pointer, keyCode int) { +func ebiten_KeyDown(nativeWindow C.NSWindowPtr, keyCode int) { key, ok := cocoaKeyCodeToKey[keyCode] if !ok { return @@ -152,7 +157,7 @@ func ebiten_KeyDown(nativeWindow unsafe.Pointer, keyCode int) { } //export ebiten_KeyUp -func ebiten_KeyUp(nativeWindow unsafe.Pointer, keyCode int) { +func ebiten_KeyUp(nativeWindow C.NSWindowPtr, keyCode int) { key, ok := cocoaKeyCodeToKey[keyCode] if !ok { return @@ -163,7 +168,7 @@ func ebiten_KeyUp(nativeWindow unsafe.Pointer, keyCode int) { } //export ebiten_MouseStateUpdated -func ebiten_MouseStateUpdated(nativeWindow unsafe.Pointer, inputType C.InputType, cx, cy C.int) { +func ebiten_MouseStateUpdated(nativeWindow C.NSWindowPtr, inputType C.InputType, cx, cy C.int) { w := windows[nativeWindow] if inputType == C.InputTypeMouseUp { @@ -190,7 +195,7 @@ func ebiten_MouseStateUpdated(nativeWindow unsafe.Pointer, inputType C.InputType } //export ebiten_WindowClosed -func ebiten_WindowClosed(nativeWindow unsafe.Pointer) { +func ebiten_WindowClosed(nativeWindow C.NSWindowPtr) { w := windows[nativeWindow] w.closed = true w.notify(ui.WindowClosedEvent{}) diff --git a/ui/ui.go b/ui/ui.go index 40e9ae320..e64f50300 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -34,10 +34,14 @@ type WindowClosedEvent struct { type UI interface { PollEvents() - CreateWindow(screenWidth, screenHeight, screenScale int, title string) Window + CreateGameWindow(screenWidth, screenHeight, screenScale int, title string) GameWindow } type Window interface { + Events() <-chan interface{} +} + +type GameWindow interface { Draw(func(graphics.Context)) Events() <-chan interface{} }