Add WindowClosedEvent

This commit is contained in:
Hajime Hoshi 2013-12-11 00:49:30 +09:00
parent 2e17c366ca
commit 54557aadf2
6 changed files with 48 additions and 18 deletions

View File

@ -67,9 +67,13 @@ func main() {
} }
drawing := make(chan *graphics.LazyCanvas) drawing := make(chan *graphics.LazyCanvas)
quit := make(chan struct{})
go func() { go func() {
defer close(quit)
inputStateUpdated := window.InputStateUpdated() inputStateUpdated := window.InputStateUpdated()
screenSizeUpdated := window.ScreenSizeUpdated() screenSizeUpdated := window.ScreenSizeUpdated()
windowClosed := window.WindowClosed()
game := NewGame() game := NewGame()
frameTime := time.Duration(int64(time.Second) / int64(fps)) frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime) tick := time.Tick(frameTime)
@ -87,6 +91,8 @@ func main() {
} else { } else {
screenSizeUpdated = nil screenSizeUpdated = nil
} }
case <-windowClosed:
return
case <-tick: case <-tick:
game.Update() game.Update()
case canvas := <-drawing: case canvas := <-drawing:
@ -98,10 +104,15 @@ func main() {
for { for {
u.PollEvents() u.PollEvents()
select {
default:
window.Draw(func(actualCanvas graphics.Canvas) { window.Draw(func(actualCanvas graphics.Canvas) {
drawing <- graphics.NewLazyCanvas() drawing <- graphics.NewLazyCanvas()
canvas := <-drawing canvas := <-drawing
canvas.Flush(actualCanvas) canvas.Flush(actualCanvas)
}) })
case <-quit:
return
}
} }
} }

View File

@ -5,23 +5,10 @@
@implementation EbitenController { @implementation EbitenController {
} }
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
(void)aNotification;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowClosing:)
name:NSWindowWillCloseNotification
object:nil];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: - (BOOL)applicationShouldTerminateAfterLastWindowClosed:
(NSApplication*)theApplication { (NSApplication*)theApplication {
(void)theApplication; (void)theApplication;
return YES; return YES;
} }
- (void)windowClosing:(NSNotification*)aNotification {
(void)aNotification;
[NSApp terminate:nil];
}
@end @end

View File

@ -6,6 +6,8 @@
#import "ebiten_content_view.h" #import "ebiten_content_view.h"
void ebiten_WindowClosed(void* nativeWindow);
@implementation EbitenWindow { @implementation EbitenWindow {
@private @private
NSOpenGLContext* glContext_; NSOpenGLContext* glContext_;
@ -75,6 +77,7 @@
if (returnCode == NSAlertDefaultReturn) { if (returnCode == NSAlertDefaultReturn) {
[self->glContext_ release]; [self->glContext_ release];
[self close]; [self close];
ebiten_WindowClosed(self);
} }
} }

View File

@ -123,3 +123,9 @@ func ebiten_InputUpdated(nativeWindow unsafe.Pointer, inputType C.InputType, cx,
e := ui.InputStateUpdatedEvent{x, y} e := ui.InputStateUpdatedEvent{x, y}
w.notifyInputStateUpdated(e) w.notifyInputStateUpdated(e)
} }
//export ebiten_WindowClosed
func ebiten_WindowClosed(nativeWindow unsafe.Pointer) {
w := windows[nativeWindow]
w.notifyWindowClosed(ui.WindowClosedEvent{})
}

View File

@ -7,6 +7,7 @@ import (
type windowEvents struct { type windowEvents struct {
screenSizeUpdated chan ui.ScreenSizeUpdatedEvent // initialized lazily screenSizeUpdated chan ui.ScreenSizeUpdatedEvent // initialized lazily
inputStateUpdated chan ui.InputStateUpdatedEvent // initialized lazily inputStateUpdated chan ui.InputStateUpdatedEvent // initialized lazily
windowClosed chan ui.WindowClosedEvent // initialized lazily
} }
func (w *windowEvents) ScreenSizeUpdated() <-chan ui.ScreenSizeUpdatedEvent { func (w *windowEvents) ScreenSizeUpdated() <-chan ui.ScreenSizeUpdatedEvent {
@ -42,3 +43,21 @@ func (w *windowEvents) notifyInputStateUpdated(e ui.InputStateUpdatedEvent) {
w.inputStateUpdated <- e w.inputStateUpdated <- e
}() }()
} }
func (w *windowEvents) WindowClosed() <-chan ui.WindowClosedEvent {
if w.windowClosed != nil {
return w.windowClosed
}
w.windowClosed = make(chan ui.WindowClosedEvent)
return w.windowClosed
}
func (w *windowEvents) notifyWindowClosed(e ui.WindowClosedEvent) {
if w.windowClosed == nil {
return
}
go func() {
w.windowClosed <- e
}()
}

View File

@ -14,6 +14,9 @@ type InputStateUpdatedEvent struct {
Y int Y int
} }
type WindowClosedEvent struct {
}
type UI interface { type UI interface {
PollEvents() PollEvents()
CreateWindow(screenWidth, screenHeight, screenScale int, title string) Window CreateWindow(screenWidth, screenHeight, screenScale int, title string) Window
@ -22,6 +25,7 @@ type UI interface {
type WindowEvents interface { type WindowEvents interface {
ScreenSizeUpdated() <-chan ScreenSizeUpdatedEvent ScreenSizeUpdated() <-chan ScreenSizeUpdatedEvent
InputStateUpdated() <-chan InputStateUpdatedEvent InputStateUpdated() <-chan InputStateUpdatedEvent
WindowClosed() <-chan WindowClosedEvent
} }
type Window interface { type Window interface {