2013-07-05 15:25:45 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
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-12-07 17:35:24 +01:00
|
|
|
"image"
|
2013-12-08 08:19:24 +01:00
|
|
|
_ "image/png"
|
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-12-09 01:45:40 +01:00
|
|
|
func init() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
}
|
|
|
|
|
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() {
|
2013-10-15 02:12:43 +02:00
|
|
|
const screenWidth = 256
|
|
|
|
const screenHeight = 240
|
2013-12-10 16:54:00 +01:00
|
|
|
const screenScale = 3
|
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-11 14:31:13 +01:00
|
|
|
ui := cocoa.UI()
|
|
|
|
textureFactory := cocoa.TextureFactory()
|
|
|
|
window := ui.CreateWindow(screenWidth, screenHeight, screenScale, title)
|
2013-12-07 17:35:24 +01:00
|
|
|
|
2013-12-11 14:31:13 +01:00
|
|
|
textureCreated := textureFactory.TextureCreated()
|
|
|
|
renderTargetCreated := textureFactory.RenderTargetCreated()
|
2013-12-12 16:31:00 +01:00
|
|
|
|
2013-12-08 08:19:24 +01:00
|
|
|
for tag, path := range TexturePaths {
|
2013-12-08 08:28:18 +01:00
|
|
|
tag := tag
|
|
|
|
path := path
|
|
|
|
go func() {
|
2013-12-08 08:19:24 +01:00
|
|
|
img, err := loadImage(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-12-11 14:31:13 +01:00
|
|
|
textureFactory.CreateTexture(tag, img)
|
2013-12-08 08:28:18 +01:00
|
|
|
}()
|
2013-12-07 17:35:24 +01:00
|
|
|
}
|
|
|
|
|
2013-12-08 15:03:00 +01:00
|
|
|
for tag, size := range RenderTargetSizes {
|
|
|
|
tag := tag
|
|
|
|
size := size
|
|
|
|
go func() {
|
2013-12-11 14:31:13 +01:00
|
|
|
textureFactory.CreateRenderTarget(tag, size.Width, size.Height)
|
2013-12-08 15:03:00 +01:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2013-12-02 13:45:10 +01:00
|
|
|
drawing := make(chan *graphics.LazyCanvas)
|
2013-12-10 16:49:30 +01:00
|
|
|
quit := make(chan struct{})
|
2013-11-23 11:31:10 +01:00
|
|
|
go func() {
|
2013-12-10 16:49:30 +01:00
|
|
|
defer close(quit)
|
|
|
|
|
2013-12-10 16:18:08 +01:00
|
|
|
inputStateUpdated := window.InputStateUpdated()
|
|
|
|
screenSizeUpdated := window.ScreenSizeUpdated()
|
2013-12-10 16:49:30 +01:00
|
|
|
windowClosed := window.WindowClosed()
|
2013-12-08 08:19:24 +01:00
|
|
|
game := NewGame()
|
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-08 15:03:00 +01:00
|
|
|
case e := <-textureCreated:
|
|
|
|
game.OnTextureCreated(e)
|
|
|
|
case e := <-renderTargetCreated:
|
|
|
|
game.OnRenderTargetCreated(e)
|
|
|
|
case e := <-inputStateUpdated:
|
|
|
|
game.OnInputStateUpdated(e)
|
2013-12-08 08:19:24 +01:00
|
|
|
case _, ok := <-screenSizeUpdated:
|
2013-12-12 17:27:02 +01:00
|
|
|
if !ok {
|
2013-12-03 14:14:51 +01:00
|
|
|
screenSizeUpdated = nil
|
2013-11-29 19:21:10 +01:00
|
|
|
}
|
2013-12-10 16:49:30 +01:00
|
|
|
case <-windowClosed:
|
|
|
|
return
|
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
|
|
|
}()
|
|
|
|
|
2013-12-12 17:27:02 +01:00
|
|
|
frameTime := time.Duration(int64(time.Second) / 120)
|
|
|
|
tick := time.Tick(frameTime)
|
2013-12-02 13:45:10 +01:00
|
|
|
for {
|
2013-12-11 14:31:13 +01:00
|
|
|
ui.PollEvents()
|
2013-12-10 16:49:30 +01:00
|
|
|
select {
|
|
|
|
default:
|
2013-12-12 17:27:02 +01:00
|
|
|
drawing <- graphics.NewLazyCanvas()
|
|
|
|
canvas := <-drawing
|
2013-12-10 16:49:30 +01:00
|
|
|
window.Draw(func(actualCanvas graphics.Canvas) {
|
|
|
|
canvas.Flush(actualCanvas)
|
|
|
|
})
|
2013-12-12 17:27:02 +01:00
|
|
|
// To avoid a busy loop, take a rest after drawing.
|
|
|
|
<-tick
|
2013-12-10 16:49:30 +01:00
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
}
|
2013-10-15 02:12:43 +02:00
|
|
|
}
|
2013-07-05 15:25:45 +02:00
|
|
|
}
|