ebiten/ebiten.go

46 lines
876 B
Go
Raw Normal View History

2013-06-18 17:48:41 +02:00
package ebiten
import (
2013-06-20 16:29:51 +02:00
"github.com/hajimehoshi/go.ebiten/graphics"
"github.com/hajimehoshi/go.ebiten/graphics/opengl"
2013-06-19 16:51:41 +02:00
"time"
2013-06-18 17:48:41 +02:00
)
type Game interface {
2013-06-21 16:16:52 +02:00
ScreenWidth() int
ScreenHeight() int
2013-06-19 16:08:24 +02:00
Init(tf graphics.TextureFactory)
2013-06-18 17:48:41 +02:00
Update()
Draw(g graphics.GraphicsContext, offscreen graphics.Texture)
2013-06-18 17:48:41 +02:00
}
2013-06-19 02:36:57 +02:00
type UI interface {
Run(device graphics.Device)
}
2013-06-21 16:16:52 +02:00
func OpenGLRun(game Game, ui UI, screenScale int) {
2013-06-18 17:48:41 +02:00
ch := make(chan bool, 1)
2013-06-19 01:49:54 +02:00
device := opengl.NewDevice(
2013-06-21 16:16:52 +02:00
game.ScreenWidth(), game.ScreenHeight(), screenScale,
func(g graphics.GraphicsContext, offscreen graphics.Texture) {
2013-06-18 17:48:41 +02:00
ticket := <-ch
game.Draw(g, offscreen)
2013-06-19 16:51:41 +02:00
ch <- ticket
2013-06-18 17:48:41 +02:00
})
go func() {
const frameTime = time.Second / 60
tick := time.Tick(frameTime)
for {
<-tick
ticket := <-ch
game.Update()
2013-06-19 16:51:41 +02:00
ch <- ticket
2013-06-18 17:48:41 +02:00
}
}()
2013-06-19 16:08:24 +02:00
game.Init(device.TextureFactory())
2013-06-19 16:51:41 +02:00
ch <- true
2013-06-19 02:36:57 +02:00
ui.Run(device)
2013-06-18 17:48:41 +02:00
}