ebiten/example/main.go

65 lines
1.3 KiB
Go
Raw Normal View History

2013-07-05 15:25:45 +02:00
package main
import (
2013-12-18 10:05:28 +01:00
"github.com/hajimehoshi/go-ebiten/example/blocks"
2013-11-23 11:31:10 +01:00
"github.com/hajimehoshi/go-ebiten/graphics"
2013-12-16 01:39:49 +01:00
"github.com/hajimehoshi/go-ebiten/ui"
2013-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten/ui/cocoa"
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-11 14:24:37 +02:00
Update(state ui.CanvasState)
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
2013-12-16 01:39:49 +01:00
u := cocoa.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-05-11 12:54:19 +02:00
textureFactory := cocoa.TextureFactory()
var game Game = blocks.NewGame(NewTextures(textureFactory))
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-11 14:24:37 +02:00
state := canvas.State()
game.Update(state)
if state.IsClosed {
return
}
/*case e := <-windowEvents:
2014-05-11 12:54:19 +02:00
game.HandleEvent(e)
if _, ok := e.(ui.WindowClosedEvent); ok {
return
2014-05-11 14:24:37 +02:00
}*/
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
}