2013-07-05 15:25:45 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-10-14 04:34:58 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/example/game/blank"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/example/game/input"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/example/game/monochrome"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/example/game/rects"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/example/game/rotating"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/example/game/sprites"
|
2013-10-18 19:58:00 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten/example/game/testpattern"
|
2013-11-23 11:31:10 +01:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
2013-10-14 04:34:58 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten/ui/cocoa"
|
2013-07-05 15:25:45 +02:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2013-11-23 11:31:10 +01:00
|
|
|
"sync"
|
2013-11-23 10:10:15 +01:00
|
|
|
"time"
|
2013-07-05 15:25:45 +02:00
|
|
|
)
|
|
|
|
|
2013-11-29 19:21:10 +01:00
|
|
|
type Game interface {
|
|
|
|
InitTextures(tf graphics.TextureFactory)
|
2013-11-29 20:24:52 +01:00
|
|
|
Update()
|
2013-11-29 19:21:10 +01:00
|
|
|
Draw(canvas graphics.Canvas)
|
|
|
|
}
|
|
|
|
|
2013-07-05 15:25:45 +02:00
|
|
|
func main() {
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
|
|
|
gameName := ""
|
|
|
|
if 2 <= len(os.Args) {
|
|
|
|
gameName = os.Args[1]
|
|
|
|
}
|
|
|
|
|
2013-11-29 19:21:10 +01:00
|
|
|
var game Game
|
2013-07-05 15:25:45 +02:00
|
|
|
switch gameName {
|
|
|
|
case "blank":
|
|
|
|
game = blank.New()
|
|
|
|
case "input":
|
|
|
|
game = input.New()
|
|
|
|
case "monochrome":
|
|
|
|
game = monochrome.New()
|
|
|
|
case "rects":
|
|
|
|
game = rects.New()
|
2013-10-15 02:12:43 +02:00
|
|
|
default:
|
|
|
|
fallthrough
|
2013-07-05 15:25:45 +02:00
|
|
|
case "rotating":
|
|
|
|
game = rotating.New()
|
|
|
|
case "sprites":
|
|
|
|
game = sprites.New()
|
2013-10-18 19:58:00 +02:00
|
|
|
case "testpattern":
|
|
|
|
game = testpattern.New()
|
2013-07-05 15:25:45 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 02:12:43 +02:00
|
|
|
const screenWidth = 256
|
|
|
|
const screenHeight = 240
|
2013-07-05 15:25:45 +02:00
|
|
|
const screenScale = 2
|
2013-11-23 11:51:24 +01:00
|
|
|
const fps = 60
|
2013-10-15 02:12:43 +02:00
|
|
|
const title = "Ebiten Demo"
|
2013-11-29 19:21:10 +01:00
|
|
|
|
|
|
|
var ui ebiten.UI = cocoa.New(screenWidth, screenHeight, screenScale, title)
|
2013-11-23 10:10:15 +01:00
|
|
|
ui.InitTextures(game.InitTextures)
|
|
|
|
|
2013-11-23 11:31:10 +01:00
|
|
|
lock := sync.Mutex{}
|
|
|
|
go func() {
|
2013-11-23 11:51:24 +01:00
|
|
|
frameTime := time.Duration(int64(time.Second) / int64(fps))
|
|
|
|
tick := time.Tick(frameTime)
|
2013-11-23 11:31:10 +01:00
|
|
|
for {
|
|
|
|
<-tick
|
2013-11-29 20:24:52 +01:00
|
|
|
func() {
|
2013-11-23 11:31:10 +01:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
2013-11-29 20:24:52 +01:00
|
|
|
game.Update()
|
|
|
|
}()
|
2013-11-23 11:31:10 +01:00
|
|
|
}
|
|
|
|
}()
|
2013-11-29 19:21:10 +01:00
|
|
|
|
2013-12-01 13:23:03 +01:00
|
|
|
inputStateUpdated := ui.ObserveInputStateUpdated()
|
|
|
|
screenSizeUpdated := ui.ObserveScreenSizeUpdated()
|
2013-11-22 18:56:18 +01:00
|
|
|
for {
|
2013-11-23 10:10:15 +01:00
|
|
|
ui.PollEvents()
|
2013-11-29 19:21:10 +01:00
|
|
|
events:
|
|
|
|
for {
|
|
|
|
select {
|
2013-12-01 13:42:41 +01:00
|
|
|
case e, ok := <-inputStateUpdated:
|
|
|
|
if ok {
|
|
|
|
type Handler interface {
|
|
|
|
OnInputStateUpdated(ebiten.InputStateUpdatedEvent)
|
|
|
|
}
|
|
|
|
if game2, ok := game.(Handler); ok {
|
|
|
|
game2.OnInputStateUpdated(e)
|
|
|
|
}
|
2013-11-29 20:24:52 +01:00
|
|
|
}
|
2013-12-01 13:42:41 +01:00
|
|
|
inputStateUpdated = ui.ObserveInputStateUpdated()
|
|
|
|
case e, ok := <-screenSizeUpdated:
|
|
|
|
if ok {
|
|
|
|
type Handler interface {
|
|
|
|
OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent)
|
|
|
|
}
|
|
|
|
if game2, ok := game.(Handler); ok {
|
|
|
|
game2.OnScreenSizeUpdated(e)
|
|
|
|
}
|
2013-11-29 19:21:10 +01:00
|
|
|
}
|
2013-12-01 13:42:41 +01:00
|
|
|
screenSizeUpdated = ui.ObserveScreenSizeUpdated()
|
2013-11-29 19:21:10 +01:00
|
|
|
default:
|
|
|
|
break events
|
|
|
|
}
|
|
|
|
}
|
2013-11-29 14:06:22 +01:00
|
|
|
ui.Draw(func(c graphics.Canvas) {
|
2013-11-23 11:31:10 +01:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
game.Draw(c)
|
|
|
|
})
|
2013-10-15 02:12:43 +02:00
|
|
|
}
|
2013-07-05 15:25:45 +02:00
|
|
|
}
|