diff --git a/example/main.go b/example/main.go index 6e2544228..214618a11 100644 --- a/example/main.go +++ b/example/main.go @@ -33,7 +33,7 @@ func main() { textureFactory := cocoa.TextureFactory() window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title) - drawing := make(chan *graphics.LazyContext) + drawing := make(chan struct{}) quit := make(chan struct{}) go func() { defer close(quit) @@ -54,14 +54,17 @@ func main() { } case <-tick: game.Update() - case context := <-drawing: - game.Draw(context) - drawing <- context + case <-drawing: + window.Draw(func(context graphics.Context) { + game.Draw(context) + }) + drawing <- struct{}{} } } }() - u.RunMainLoop() + u.Start() + defer u.Terminate() s := make(chan os.Signal, 1) signal.Notify(s, os.Interrupt, syscall.SIGTERM) @@ -69,15 +72,8 @@ func main() { u.DoEvents() select { default: - drawing <- graphics.NewLazyContext() - context := <-drawing - - window.Draw(func(actualContext graphics.Context) { - context.Flush(actualContext) - }) - after := time.After(time.Duration(int64(time.Second) / 120)) - u.DoEvents() - <-after + drawing <- struct{}{} + <-drawing case <-s: return case <-quit: diff --git a/ui/cocoa/ebiten_game_window.c b/ui/cocoa/ebiten_game_window.c index 24bf027b2..868f7742e 100644 --- a/ui/cocoa/ebiten_game_window.c +++ b/ui/cocoa/ebiten_game_window.c @@ -57,11 +57,11 @@ void ebiten_WindowClosed(void* nativeWindow); - (BOOL)windowShouldClose:(id)sender { if ([sender isDocumentEdited]) { // TODO: add the application's name - NSAlert* alert = [NSAlert alertWithMessageText:@"Quit the game?" - defaultButton:@"Quit" - alternateButton:nil - otherButton:@"Cancel" - informativeTextWithFormat:@""]; + NSAlert* alert = [NSAlert new]; + [alert setMessageText:@"Quit the game?"]; + [alert addButtonWithTitle:@"Quit"]; + [alert addButtonWithTitle:@"Cancel"]; + [alert setAlertStyle:NSWarningAlertStyle]; SEL selector = @selector(alertDidEnd:returnCode:contextInfo:); [alert beginSheetModalForWindow:sender modalDelegate:self @@ -75,9 +75,7 @@ void ebiten_WindowClosed(void* nativeWindow); - (void)alertDidEnd:(NSAlert*)alert returnCode:(NSInteger)returnCode contextInfo:(void*)contextInfo { - (void)alert; - (void)contextInfo; - if (returnCode == NSAlertDefaultReturn) { + if (returnCode == NSAlertFirstButtonReturn) { [self close]; ebiten_WindowClosed(self); } diff --git a/ui/cocoa/game_window.go b/ui/cocoa/game_window.go index b385598c6..342c5d65d 100644 --- a/ui/cocoa/game_window.go +++ b/ui/cocoa/game_window.go @@ -98,14 +98,17 @@ func (w *GameWindow) loop(context *opengl.Context, glContext *C.NSOpenGLContext) } func (w *GameWindow) Draw(f func(graphics.Context)) { + //after := time.After(time.Duration(int64(time.Second) / 120)) + //defer <-after + select { case <-w.closed: return default: - w.useGLContext(func(context *opengl.Context) { - context.Update(f) - }) } + w.useGLContext(func(context *opengl.Context) { + context.Update(f) + }) } func (w *GameWindow) useGLContext(f func(*opengl.Context)) { diff --git a/ui/cocoa/mainloop.c b/ui/cocoa/mainloop.c index c0c685508..fb304fd0d 100644 --- a/ui/cocoa/mainloop.c +++ b/ui/cocoa/mainloop.c @@ -5,6 +5,8 @@ #import "ebiten_game_window.h" +static NSAutoreleasePool* pool = NULL; + void initMenu(void) { NSString* processName = [[NSProcessInfo processInfo] processName]; @@ -25,16 +27,9 @@ void initMenu(void) { keyEquivalent:@"q"]; } -void Run(void) { - NSAutoreleasePool * pool = [NSAutoreleasePool new]; - NSApplication* app = [NSApplication sharedApplication]; - [app setActivationPolicy:NSApplicationActivationPolicyRegular]; - //initMenu(); - [app run]; - [pool drain]; -} - void StartApplication(void) { + pool = [NSAutoreleasePool new]; + NSApplication* app = [NSApplication sharedApplication]; [app setActivationPolicy:NSApplicationActivationPolicyRegular]; @@ -43,6 +38,32 @@ void StartApplication(void) { [app finishLaunching]; } +void DoEvents(void) { + for (;;) { + NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask + untilDate:[NSDate distantPast] + inMode:NSDefaultRunLoopMode + dequeue:YES]; + if (event == nil) { + break; + } + [NSApp sendEvent:event]; + } + + static BOOL initialBoot = YES; + if (initialBoot) { + [NSApp activateIgnoringOtherApps:YES]; + initialBoot = NO; + } + + [pool drain]; + pool = [NSAutoreleasePool new]; +} + +void TerminateApplication(void) { + [pool drain]; +} + NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext) { NSOpenGLPixelFormatAttribute attributes[] = { NSOpenGLPFAWindow, @@ -77,24 +98,6 @@ EbitenGameWindow* CreateGameWindow(size_t width, size_t height, const char* titl return window; } -void DoEvents(void) { - for (;;) { - NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask - untilDate:[NSDate distantPast] - inMode:NSDefaultRunLoopMode - dequeue:YES]; - if (event == nil) { - break; - } - [NSApp sendEvent:event]; - } - static BOOL initialBoot = YES; - if (initialBoot) { - [NSApp activateIgnoringOtherApps:YES]; - initialBoot = NO; - } -} - void UseGLContext(NSOpenGLContext* glContext) { CGLContextObj cglContext = [glContext CGLContextObj]; CGLLockContext(cglContext); diff --git a/ui/cocoa/ui.go b/ui/cocoa/ui.go index 37a9fe4de..2196d73d7 100644 --- a/ui/cocoa/ui.go +++ b/ui/cocoa/ui.go @@ -3,9 +3,9 @@ package cocoa // #cgo CFLAGS: -x objective-c // #cgo LDFLAGS: -framework Cocoa -framework OpenGL // -// void Run(void); // void StartApplication(void); // void DoEvents(void); +// void TerminateApplication(void); // import "C" import ( @@ -46,18 +46,12 @@ func (u *cocoaUI) DoEvents() { C.DoEvents() } -func (u *cocoaUI) RunMainLoop() { +func (u *cocoaUI) Start() { C.StartApplication() currentUI.sharedContext.run() - - // TODO: Enable the loop - //C.Run() } -/*func (u *cocoaUI) CreateTexture(tag interface{}, img image.Image, filter graphics.Filter) { - t.sharedContext.CreateTexture(tag, img, filter) +func (u *cocoaUI) Terminate() { + // TODO: Close existing windows + C.TerminateApplication() } - -func (u *cocoaUI) CreateRenderTarget(tag interface{}, width, height int) { - t.sharedContext.CreateRenderTarget(tag, width, height) -}*/ diff --git a/ui/ui.go b/ui/ui.go index eba3dac42..5ecc2d274 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -33,9 +33,10 @@ type WindowClosedEvent struct { } type UI interface { - DoEvents() CreateGameWindow(screenWidth, screenHeight, screenScale int, title string) GameWindow - RunMainLoop() + Start() + DoEvents() + Terminate() } type Window interface {