ebiten/ebiten.go

47 lines
880 B
Go
Raw Normal View History

2013-06-18 17:48:41 +02:00
package ebiten
import (
"time"
"github.com/hajimehoshi/go-ebiten/graphics"
2013-06-19 01:49:54 +02:00
"github.com/hajimehoshi/go-ebiten/graphics/opengl"
2013-06-18 17:48:41 +02:00
)
type Game interface {
2013-06-19 16:08:24 +02:00
Init(tf graphics.TextureFactory)
2013-06-18 17:48:41 +02:00
Update()
2013-06-19 16:08:24 +02:00
Draw(g graphics.GraphicsContext, offscreen graphics.TextureID)
2013-06-18 17:48:41 +02:00
}
2013-06-19 02:36:57 +02:00
type UI interface {
ScreenWidth() int
ScreenHeight() int
ScreenScale() int
Run(device graphics.Device)
}
func OpenGLRun(game Game, ui UI) {
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-19 02:36:57 +02:00
ui.ScreenWidth(), ui.ScreenHeight(), ui.ScreenScale(),
2013-06-19 16:08:24 +02:00
func(g graphics.GraphicsContext, offscreen graphics.TextureID) {
2013-06-18 17:48:41 +02:00
ticket := <-ch
game.Draw(g, offscreen)
ch<- ticket
})
go func() {
const frameTime = time.Second / 60
tick := time.Tick(frameTime)
for {
<-tick
ticket := <-ch
game.Update()
ch<- ticket
}
}()
2013-06-19 16:08:24 +02:00
game.Init(device.TextureFactory())
ch<- true
2013-06-19 02:36:57 +02:00
ui.Run(device)
2013-06-18 17:48:41 +02:00
}