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"
|
2013-07-05 15:25:45 +02:00
|
|
|
"runtime"
|
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
|
|
|
|
2013-12-16 01:39:49 +01:00
|
|
|
textureFactoryEvents := textureFactory.Events()
|
2013-12-12 16:31:00 +01:00
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
drawing := make(chan *graphics.LazyContext)
|
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)
|
|
|
|
|
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()
|
2013-12-13 22:10:24 +01:00
|
|
|
case context := <-drawing:
|
|
|
|
game.Draw(context)
|
|
|
|
drawing <- context
|
2013-11-29 19:21:10 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-02 13:45:10 +01:00
|
|
|
}()
|
|
|
|
|
2014-01-06 18:14:53 +01:00
|
|
|
u.RunMainLoop()
|
2014-01-06 16:10:46 +01:00
|
|
|
|
2013-12-02 13:45:10 +01:00
|
|
|
for {
|
2013-12-16 01:39:49 +01:00
|
|
|
u.PollEvents()
|
2013-12-10 16:49:30 +01:00
|
|
|
select {
|
|
|
|
default:
|
2013-12-13 22:10:24 +01:00
|
|
|
drawing <- graphics.NewLazyContext()
|
|
|
|
context := <-drawing
|
2013-12-13 21:34:27 +01:00
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
window.Draw(func(actualContext graphics.Context) {
|
|
|
|
context.Flush(actualContext)
|
2013-12-10 16:49:30 +01:00
|
|
|
})
|
2013-12-13 21:34:27 +01:00
|
|
|
after := time.After(time.Duration(int64(time.Second) / 120))
|
2013-12-16 01:39:49 +01:00
|
|
|
u.PollEvents()
|
2013-12-13 21:34:27 +01:00
|
|
|
<-after
|
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
|
|
|
}
|