ebiten/example/main.go

59 lines
1.1 KiB
Go
Raw Normal View History

2013-07-05 15:25:45 +02:00
package main
import (
2014-12-05 14:16:58 +01:00
"github.com/hajimehoshi/ebiten/example/blocks"
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/ui"
2014-12-05 18:26:02 +01:00
"github.com/hajimehoshi/ebiten/ui/glfw"
2014-01-11 07:14:47 +01:00
"os"
"os/signal"
2013-07-05 15:25:45 +02:00
"runtime"
2014-01-11 07:14:47 +01:00
"syscall"
2013-11-23 10:10:15 +01:00
"time"
2013-07-05 15:25:45 +02:00
)
2013-12-18 10:05:28 +01:00
type Game interface {
2014-05-12 03:04:28 +02:00
Update(state ui.InputState)
2013-12-18 10:05:28 +01:00
Draw(c graphics.Context)
2013-12-09 01:45:40 +01:00
}
2013-12-18 10:05:28 +01:00
func init() {
runtime.LockOSThread()
2013-12-07 17:35:24 +01:00
}
2013-07-05 15:25:45 +02:00
func main() {
2013-12-18 10:05:28 +01:00
const screenWidth = blocks.ScreenWidth
const screenHeight = blocks.ScreenHeight
const screenScale = 2
2013-11-23 11:51:24 +01:00
const fps = 60
2014-05-11 12:54:19 +02:00
const frameTime = time.Duration(int64(time.Second) / int64(fps))
2013-10-15 02:12:43 +02:00
const title = "Ebiten Demo"
2013-11-29 19:21:10 +01:00
2014-12-05 18:26:02 +01:00
u := new(glfw.UI)
2014-05-11 14:24:37 +02:00
canvas := u.CreateCanvas(screenWidth, screenHeight, screenScale, title)
2013-12-07 17:35:24 +01:00
2014-12-05 18:26:02 +01:00
textureFactory := u.TextureFactory()
game := blocks.NewGame(NewTextures(textureFactory))
2014-05-11 12:54:19 +02:00
tick := time.Tick(frameTime)
2013-12-10 16:49:30 +01:00
2014-05-11 12:54:19 +02:00
sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, os.Interrupt, syscall.SIGTERM)
2013-12-02 13:45:10 +01:00
2014-01-13 07:26:20 +01:00
u.Start()
defer u.Terminate()
2013-12-02 13:45:10 +01:00
for {
2014-01-11 10:11:13 +01:00
u.DoEvents()
2013-12-10 16:49:30 +01:00
select {
default:
2014-05-11 14:24:37 +02:00
canvas.Draw(game.Draw)
2014-05-11 12:54:19 +02:00
case <-tick:
2014-05-12 03:04:28 +02:00
game.Update(canvas.InputState())
if canvas.IsClosed() {
2014-05-11 14:24:37 +02:00
return
}
2014-05-11 12:54:19 +02:00
case <-sigterm:
2013-12-10 16:49:30 +01:00
return
}
2013-10-15 02:12:43 +02:00
}
2013-07-05 15:25:45 +02:00
}