2013-07-05 15:25:45 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-10-14 04:34:58 +02:00
|
|
|
"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-12-02 16:15:01 +01:00
|
|
|
"github.com/hajimehoshi/go-ebiten/ui"
|
2013-10-14 04:34:58 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten/ui/cocoa"
|
2013-12-07 17:35:24 +01:00
|
|
|
"image"
|
2013-07-05 15:25:45 +02:00
|
|
|
"os"
|
|
|
|
"runtime"
|
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-12-07 17:35:24 +01:00
|
|
|
func loadImage(path string) (image.Image, error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
img, _, err := image.Decode(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return img, nil
|
|
|
|
}
|
|
|
|
|
2013-07-05 15:25:45 +02:00
|
|
|
func main() {
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2013-12-04 13:51:39 +01:00
|
|
|
runtime.LockOSThread()
|
2013-07-05 15:25:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-12-06 18:20:48 +01:00
|
|
|
type UI interface {
|
|
|
|
ui.UI
|
2013-12-07 17:35:24 +01:00
|
|
|
graphics.TextureFactory2
|
2013-12-06 18:20:48 +01:00
|
|
|
}
|
|
|
|
var u UI = cocoa.New(screenWidth, screenHeight, screenScale, title)
|
2013-12-07 17:35:24 +01:00
|
|
|
|
|
|
|
// TODO: Remove this
|
2013-12-02 16:15:01 +01:00
|
|
|
u.LoadResources(game.InitTextures)
|
2013-12-07 17:35:24 +01:00
|
|
|
|
|
|
|
textureCreated := u.TextureCreated()
|
2013-12-03 15:24:34 +01:00
|
|
|
inputStateUpdated := u.InputStateUpdated()
|
|
|
|
screenSizeUpdated := u.ScreenSizeUpdated()
|
2013-11-23 10:10:15 +01:00
|
|
|
|
2013-12-07 17:35:24 +01:00
|
|
|
img, err := loadImage("images/ebiten.png")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
u.CreateTexture("ebiten", img)
|
|
|
|
|
2013-12-02 13:45:10 +01:00
|
|
|
drawing := make(chan *graphics.LazyCanvas)
|
2013-11-23 11:31:10 +01:00
|
|
|
go func() {
|
2013-11-23 11:51:24 +01:00
|
|
|
frameTime := time.Duration(int64(time.Second) / int64(fps))
|
|
|
|
tick := time.Tick(frameTime)
|
2013-11-29 19:21:10 +01:00
|
|
|
for {
|
|
|
|
select {
|
2013-12-07 17:35:24 +01:00
|
|
|
case e, ok := <-textureCreated:
|
|
|
|
if ok {
|
|
|
|
print(e.Error)
|
|
|
|
} else {
|
|
|
|
textureCreated = nil
|
|
|
|
}
|
2013-12-01 13:42:41 +01:00
|
|
|
case e, ok := <-inputStateUpdated:
|
2013-12-02 16:15:01 +01:00
|
|
|
// TODO: Use Adaptor?
|
2013-12-01 13:42:41 +01:00
|
|
|
if ok {
|
2013-12-06 18:20:48 +01:00
|
|
|
if game2, ok := game.(interface {
|
2013-12-02 16:15:01 +01:00
|
|
|
OnInputStateUpdated(ui.InputStateUpdatedEvent)
|
2013-12-06 18:20:48 +01:00
|
|
|
}); ok {
|
2013-12-01 13:42:41 +01:00
|
|
|
game2.OnInputStateUpdated(e)
|
|
|
|
}
|
2013-12-03 14:14:51 +01:00
|
|
|
} else {
|
|
|
|
inputStateUpdated = nil
|
2013-11-29 20:24:52 +01:00
|
|
|
}
|
2013-12-01 13:42:41 +01:00
|
|
|
case e, ok := <-screenSizeUpdated:
|
|
|
|
if ok {
|
2013-12-06 18:20:48 +01:00
|
|
|
if game2, ok := game.(interface {
|
2013-12-02 16:15:01 +01:00
|
|
|
OnScreenSizeUpdated(ui.ScreenSizeUpdatedEvent)
|
2013-12-06 18:20:48 +01:00
|
|
|
}); ok {
|
2013-12-01 13:42:41 +01:00
|
|
|
game2.OnScreenSizeUpdated(e)
|
|
|
|
}
|
2013-12-03 14:14:51 +01:00
|
|
|
} else {
|
|
|
|
screenSizeUpdated = nil
|
2013-11-29 19:21:10 +01:00
|
|
|
}
|
2013-12-02 13:45:10 +01:00
|
|
|
case <-tick:
|
|
|
|
game.Update()
|
|
|
|
case canvas := <-drawing:
|
|
|
|
game.Draw(canvas)
|
|
|
|
drawing <- canvas
|
2013-11-29 19:21:10 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-02 13:45:10 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
2013-12-02 16:15:01 +01:00
|
|
|
u.PollEvents()
|
|
|
|
u.Draw(func(actualCanvas graphics.Canvas) {
|
2013-12-02 13:45:10 +01:00
|
|
|
drawing <- graphics.NewLazyCanvas()
|
|
|
|
canvas := <-drawing
|
2013-12-02 16:15:01 +01:00
|
|
|
canvas.Flush(actualCanvas)
|
2013-11-23 11:31:10 +01:00
|
|
|
})
|
2013-10-15 02:12:43 +02:00
|
|
|
}
|
2013-07-05 15:25:45 +02:00
|
|
|
}
|