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

View File

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

View File

@ -6,6 +6,8 @@
#import "ebiten_content_view.h"
void ebiten_WindowClosed(void* nativeWindow);
@implementation EbitenWindow {
@private
NSOpenGLContext* glContext_;
@ -75,6 +77,7 @@
if (returnCode == NSAlertDefaultReturn) {
[self->glContext_ release];
[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}
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 {
screenSizeUpdated chan ui.ScreenSizeUpdatedEvent // initialized lazily
inputStateUpdated chan ui.InputStateUpdatedEvent // initialized lazily
windowClosed chan ui.WindowClosedEvent // initialized lazily
}
func (w *windowEvents) ScreenSizeUpdated() <-chan ui.ScreenSizeUpdatedEvent {
@ -42,3 +43,21 @@ func (w *windowEvents) notifyInputStateUpdated(e ui.InputStateUpdatedEvent) {
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
}
type WindowClosedEvent struct {
}
type UI interface {
PollEvents()
CreateWindow(screenWidth, screenHeight, screenScale int, title string) Window
@ -22,6 +25,7 @@ type UI interface {
type WindowEvents interface {
ScreenSizeUpdated() <-chan ScreenSizeUpdatedEvent
InputStateUpdated() <-chan InputStateUpdatedEvent
WindowClosed() <-chan WindowClosedEvent
}
type Window interface {