Remove ui.GameWindow; Add ui.Canvas

This commit is contained in:
Hajime Hoshi 2014-05-11 21:24:37 +09:00
parent 214f099ef7
commit fb4751e8cd
6 changed files with 102 additions and 121 deletions

View File

@ -52,14 +52,6 @@ func NewGame(textures Textures) *Game {
return game return game
} }
func (game *Game) HandleEvent(e interface{}) {
switch e := e.(type) {
case ui.KeyStateUpdatedEvent:
game.input.UpdateKeys(e.Keys)
case ui.MouseStateUpdatedEvent:
}
}
func (game *Game) isInitialized() bool { func (game *Game) isInitialized() bool {
for name, _ := range texturePaths { for name, _ := range texturePaths {
if !game.textures.Has(name) { if !game.textures.Has(name) {
@ -74,11 +66,11 @@ func (game *Game) isInitialized() bool {
return true return true
} }
func (game *Game) Update() { func (game *Game) Update(state ui.CanvasState) {
if !game.isInitialized() { if !game.isInitialized() {
return return
} }
game.input.Update() game.input.Update(state.Keys)
game.sceneManager.Update(&GameState{ game.sceneManager.Update(&GameState{
SceneManager: game.sceneManager, SceneManager: game.sceneManager,
Input: game.input, Input: game.input,

View File

@ -5,8 +5,7 @@ import (
) )
type Input struct { type Input struct {
states map[ui.Key]int states map[ui.Key]int
lastPressedKeys map[ui.Key]struct{}
} }
func NewInput() *Input { func NewInput() *Input {
@ -23,19 +22,17 @@ func (i *Input) StateForKey(key ui.Key) int {
return i.states[key] return i.states[key]
} }
func (i *Input) Update() { func (i *Input) Update(keys []ui.Key) {
pressedKeys := map[ui.Key]struct{}{}
for _, key := range keys {
pressedKeys[key] = struct{}{}
}
for key, _ := range i.states { for key, _ := range i.states {
if _, ok := i.lastPressedKeys[key]; !ok { if _, ok := pressedKeys[key]; !ok {
i.states[key] = 0 i.states[key] = 0
continue continue
} }
i.states[key] += 1 i.states[key] += 1
} }
} }
func (i *Input) UpdateKeys(keys []ui.Key) {
i.lastPressedKeys = map[ui.Key]struct{}{}
for _, key := range keys {
i.lastPressedKeys[key] = struct{}{}
}
}

View File

@ -13,8 +13,7 @@ import (
) )
type Game interface { type Game interface {
HandleEvent(e interface{}) Update(state ui.CanvasState)
Update()
Draw(c graphics.Context) Draw(c graphics.Context)
} }
@ -31,9 +30,8 @@ func main() {
const title = "Ebiten Demo" const title = "Ebiten Demo"
u := cocoa.UI() u := cocoa.UI()
window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title) canvas := u.CreateCanvas(screenWidth, screenHeight, screenScale, title)
windowEvents := window.Events()
textureFactory := cocoa.TextureFactory() textureFactory := cocoa.TextureFactory()
var game Game = blocks.NewGame(NewTextures(textureFactory)) var game Game = blocks.NewGame(NewTextures(textureFactory))
tick := time.Tick(frameTime) tick := time.Tick(frameTime)
@ -47,16 +45,18 @@ func main() {
u.DoEvents() u.DoEvents()
select { select {
default: default:
window.Draw(func(context graphics.Context) { canvas.Draw(game.Draw)
game.Draw(context)
})
case <-tick: case <-tick:
game.Update() state := canvas.State()
case e := <-windowEvents: game.Update(state)
if state.IsClosed {
return
}
/*case e := <-windowEvents:
game.HandleEvent(e) game.HandleEvent(e)
if _, ok := e.(ui.WindowClosedEvent); ok { if _, ok := e.(ui.WindowClosedEvent); ok {
return return
} }*/
case <-sigterm: case <-sigterm:
return return
} }

View File

@ -21,35 +21,41 @@ import (
"github.com/hajimehoshi/go-ebiten/graphics/opengl" "github.com/hajimehoshi/go-ebiten/graphics/opengl"
"github.com/hajimehoshi/go-ebiten/ui" "github.com/hajimehoshi/go-ebiten/ui"
"runtime" "runtime"
"sync"
"time" "time"
"unsafe" "unsafe"
) )
type GameWindow struct { type GameWindow struct {
screenWidth int state ui.CanvasState
screenHeight int title string
screenScale int native *C.EbitenGameWindow
title string pressedKeys map[ui.Key]struct{}
native *C.EbitenGameWindow funcs chan func(*opengl.Context)
pressedKeys map[ui.Key]struct{} funcsDone chan struct{}
funcs chan func(*opengl.Context) closed chan struct{}
funcsDone chan struct{} sync.RWMutex
closed chan struct{}
events chan interface{}
} }
var windows = map[*C.EbitenGameWindow]*GameWindow{} var windows = map[*C.EbitenGameWindow]*GameWindow{}
func newGameWindow(width, height, scale int, title string) *GameWindow { func newGameWindow(width, height, scale int, title string) *GameWindow {
state := ui.CanvasState{
Width: width,
Height: height,
Scale: scale,
Keys: []ui.Key{},
MouseX: -1,
MouseY: -1,
IsClosed: false,
}
return &GameWindow{ return &GameWindow{
screenWidth: width, state: state,
screenHeight: height, title: title,
screenScale: scale, pressedKeys: map[ui.Key]struct{}{},
title: title, funcs: make(chan func(*opengl.Context)),
pressedKeys: map[ui.Key]struct{}{}, funcsDone: make(chan struct{}),
funcs: make(chan func(*opengl.Context)), closed: make(chan struct{}),
funcsDone: make(chan struct{}),
closed: make(chan struct{}),
} }
} }
@ -61,8 +67,9 @@ func (w *GameWindow) run(sharedGLContext *C.NSOpenGLContext) {
go func() { go func() {
runtime.LockOSThread() runtime.LockOSThread()
glContext := C.CreateGLContext(sharedGLContext) glContext := C.CreateGLContext(sharedGLContext)
w.native = C.CreateGameWindow(C.size_t(w.screenWidth*w.screenScale), w.native = C.CreateGameWindow(
C.size_t(w.screenHeight*w.screenScale), C.size_t(w.state.Width*w.state.Scale),
C.size_t(w.state.Height*w.state.Scale),
cTitle, cTitle,
glContext) glContext)
windows[w.native] = w windows[w.native] = w
@ -70,7 +77,7 @@ func (w *GameWindow) run(sharedGLContext *C.NSOpenGLContext) {
C.UseGLContext(glContext) C.UseGLContext(glContext)
context := opengl.NewContext( context := opengl.NewContext(
w.screenWidth, w.screenHeight, w.screenScale) w.state.Width, w.state.Height, w.state.Scale)
C.UnuseGLContext() C.UnuseGLContext()
defer func() { defer func() {
@ -117,39 +124,10 @@ func (w *GameWindow) useGLContext(f func(*opengl.Context)) {
<-w.funcsDone <-w.funcsDone
} }
func (w *GameWindow) Events() <-chan interface{} { func (w *GameWindow) State() ui.CanvasState {
if w.events != nil { w.RLock()
return w.events defer w.RUnlock()
} return w.state
w.events = make(chan interface{})
return w.events
}
func (w *GameWindow) notify(e interface{}) {
if w.events == nil {
return
}
go func() {
w.events <- e
}()
}
// Now this function is not used anywhere.
//export ebiten_WindowSizeUpdated
func ebiten_WindowSizeUpdated(nativeWindow C.EbitenGameWindowPtr, width, height int) {
w := windows[nativeWindow]
e := ui.WindowSizeUpdatedEvent{width, height}
w.notify(e)
}
func (w *GameWindow) keyStateUpdatedEvent() ui.KeyStateUpdatedEvent {
keys := []ui.Key{}
for key, _ := range w.pressedKeys {
keys = append(keys, key)
}
return ui.KeyStateUpdatedEvent{
Keys: keys,
}
} }
var cocoaKeyCodeToKey = map[int]ui.Key{ var cocoaKeyCodeToKey = map[int]ui.Key{
@ -168,7 +146,15 @@ func ebiten_KeyDown(nativeWindow C.EbitenGameWindowPtr, keyCode int) {
} }
w := windows[nativeWindow] w := windows[nativeWindow]
w.pressedKeys[key] = struct{}{} w.pressedKeys[key] = struct{}{}
w.notify(w.keyStateUpdatedEvent())
keys := []ui.Key{}
for key, _ := range w.pressedKeys {
keys = append(keys, key)
}
w.Lock()
defer w.Unlock()
w.state.Keys = keys
} }
//export ebiten_KeyUp //export ebiten_KeyUp
@ -179,7 +165,15 @@ func ebiten_KeyUp(nativeWindow C.EbitenGameWindowPtr, keyCode int) {
} }
w := windows[nativeWindow] w := windows[nativeWindow]
delete(w.pressedKeys, key) delete(w.pressedKeys, key)
w.notify(w.keyStateUpdatedEvent())
keys := []ui.Key{}
for key, _ := range w.pressedKeys {
keys = append(keys, key)
}
w.Lock()
defer w.Unlock()
w.state.Keys = keys
} }
//export ebiten_MouseStateUpdated //export ebiten_MouseStateUpdated
@ -187,32 +181,41 @@ func ebiten_MouseStateUpdated(nativeWindow C.EbitenGameWindowPtr, inputType C.In
w := windows[nativeWindow] w := windows[nativeWindow]
if inputType == C.InputTypeMouseUp { if inputType == C.InputTypeMouseUp {
e := ui.MouseStateUpdatedEvent{-1, -1} w.Lock()
w.notify(e) defer w.Unlock()
w.state.MouseX = -1
w.state.MouseY = -1
return return
} }
x, y := int(cx), int(cy) x, y := int(cx), int(cy)
x /= w.screenScale x /= w.state.Scale
y /= w.screenScale y /= w.state.Scale
if x < 0 { if x < 0 {
x = 0 x = 0
} else if w.screenWidth <= x { } else if w.state.Width <= x {
x = w.screenWidth - 1 x = w.state.Width - 1
} }
if y < 0 { if y < 0 {
y = 0 y = 0
} else if w.screenHeight <= y { } else if w.state.Height <= y {
y = w.screenHeight - 1 y = w.state.Height - 1
} }
e := ui.MouseStateUpdatedEvent{x, y}
w.notify(e) w.Lock()
defer w.Unlock()
w.state.MouseX = x
w.state.MouseY = y
} }
//export ebiten_WindowClosed //export ebiten_WindowClosed
func ebiten_WindowClosed(nativeWindow C.EbitenGameWindowPtr) { func ebiten_WindowClosed(nativeWindow C.EbitenGameWindowPtr) {
w := windows[nativeWindow] w := windows[nativeWindow]
close(w.closed) close(w.closed)
w.notify(ui.WindowClosedEvent{})
w.Lock()
defer w.Unlock()
w.state.IsClosed = true
delete(windows, nativeWindow) delete(windows, nativeWindow)
} }

View File

@ -38,7 +38,7 @@ func TextureFactory() graphics.TextureFactory {
return getCurrentUI().sharedContext return getCurrentUI().sharedContext
} }
func (u *cocoaUI) CreateGameWindow(width, height, scale int, title string) ui.GameWindow { func (u *cocoaUI) CreateCanvas(width, height, scale int, title string) ui.Canvas {
return u.sharedContext.createGameWindow(width, height, scale, title) return u.sharedContext.createGameWindow(width, height, scale, title)
} }

View File

@ -15,35 +15,24 @@ const (
KeyMax KeyMax
) )
type WindowSizeUpdatedEvent struct {
Width int
Height int
}
type KeyStateUpdatedEvent struct {
Keys []Key
}
type MouseStateUpdatedEvent struct {
X int
Y int
}
type WindowClosedEvent struct {
}
type UI interface { type UI interface {
CreateGameWindow(screenWidth, screenHeight, screenScale int, title string) GameWindow CreateCanvas(widht, height, scale int, title string) Canvas
Start() Start()
DoEvents() DoEvents()
Terminate() Terminate()
} }
type Window interface { type CanvasState struct {
Events() <-chan interface{} Width int
Height int
Scale int
Keys []Key
MouseX int
MouseY int
IsClosed bool
} }
type GameWindow interface { type Canvas interface {
Draw(func(graphics.Context)) Draw(func(graphics.Context))
Window State() CanvasState
} }