ebiten/example/main.go

84 lines
1.6 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 {
HandleEvent(e interface{})
Update()
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
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()
2013-12-11 14:31:13 +01:00
textureFactory := cocoa.TextureFactory()
2013-12-30 19:17:39 +01:00
window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title)
2013-12-07 17:35:24 +01:00
2014-01-13 07:26:20 +01:00
drawing := make(chan struct{})
2013-12-10 16:49:30 +01:00
quit := make(chan struct{})
2013-11-23 11:31:10 +01:00
go func() {
2013-12-10 16:49:30 +01:00
defer close(quit)
2014-01-11 07:18:04 +01:00
textureFactoryEvents := textureFactory.Events()
2013-12-16 01:39:49 +01:00
windowEvents := window.Events()
2013-12-18 10:05:28 +01:00
var game Game = blocks.NewGame(textureFactory)
2013-11-23 11:51:24 +01:00
frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime)
2013-11-29 19:21:10 +01:00
for {
select {
2013-12-16 01:39:49 +01:00
case e := <-textureFactoryEvents:
game.HandleEvent(e)
case e := <-windowEvents:
2013-12-18 10:05:28 +01:00
game.HandleEvent(e)
2013-12-16 01:39:49 +01:00
if _, ok := e.(ui.WindowClosedEvent); ok {
return
2013-11-29 19:21:10 +01:00
}
2013-12-02 13:45:10 +01:00
case <-tick:
game.Update()
2014-01-13 07:26:20 +01:00
case <-drawing:
window.Draw(func(context graphics.Context) {
game.Draw(context)
})
drawing <- struct{}{}
2013-11-29 19:21:10 +01:00
}
}
2013-12-02 13:45:10 +01:00
}()
2014-01-13 07:26:20 +01:00
u.Start()
defer u.Terminate()
2014-01-11 07:14:47 +01:00
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt, syscall.SIGTERM)
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-01-13 07:26:20 +01:00
drawing <- struct{}{}
<-drawing
2014-01-11 07:14:47 +01:00
case <-s:
return
2013-12-10 16:49:30 +01:00
case <-quit:
return
}
2013-10-15 02:12:43 +02:00
}
2013-07-05 15:25:45 +02:00
}