mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 19:22:49 +01:00
47 lines
829 B
Go
47 lines
829 B
Go
package ui
|
|
|
|
import (
|
|
"github.com/hajimehoshi/ebiten/graphics"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
type Game interface {
|
|
Update() error
|
|
Draw(context graphics.Context) error
|
|
}
|
|
|
|
func Run(u UI, game Game, width, height, scale int, title string, fps int) error {
|
|
canvas, err := u.Start(width, height, scale, title)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
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:
|
|
if err := canvas.Draw(game); err != nil {
|
|
return err
|
|
}
|
|
case <-tick:
|
|
if err := game.Update(); err != nil {
|
|
return err
|
|
}
|
|
if canvas.IsClosed() {
|
|
return nil
|
|
}
|
|
case <-sigterm:
|
|
return nil
|
|
}
|
|
}
|
|
}
|