ebiten/ebiten.go

47 lines
883 B
Go
Raw Normal View History

2013-06-18 17:48:41 +02:00
package ebiten
import (
"github.com/hajimehoshi/go-ebiten/graphics"
2013-06-19 01:49:54 +02:00
"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-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)
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
}