ebiten/ui/run.go

40 lines
682 B
Go
Raw Normal View History

2014-12-06 17:09:59 +01:00
package ui
import (
"github.com/hajimehoshi/ebiten/graphics"
"os"
"os/signal"
"syscall"
"time"
)
type Game interface {
Draw(context graphics.Context)
2014-12-06 19:27:55 +01:00
Update()
2014-12-06 17:09:59 +01:00
}
func Run(u UI, game Game, width, height, scale int, title string, fps int) {
2014-12-06 18:27:08 +01:00
canvas := u.Start(width, height, scale, title)
2014-12-06 17:09:59 +01:00
frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime)
sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, os.Interrupt, syscall.SIGTERM)
defer u.Terminate()
for {
u.DoEvents()
select {
default:
canvas.Draw(game.Draw)
case <-tick:
2014-12-06 19:27:55 +01:00
game.Update()
2014-12-06 17:09:59 +01:00
if canvas.IsClosed() {
return
}
case <-sigterm:
return
}
}
}