mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 13:07:26 +01:00
81 lines
1.5 KiB
Go
81 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/hajimehoshi/go-ebiten/example/blocks"
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
|
"github.com/hajimehoshi/go-ebiten/ui"
|
|
"github.com/hajimehoshi/go-ebiten/ui/cocoa"
|
|
"os"
|
|
"os/signal"
|
|
"runtime"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
type Game interface {
|
|
HandleEvent(e interface{})
|
|
Update()
|
|
Draw(c graphics.Context)
|
|
}
|
|
|
|
func init() {
|
|
runtime.LockOSThread()
|
|
}
|
|
|
|
func main() {
|
|
const screenWidth = blocks.ScreenWidth
|
|
const screenHeight = blocks.ScreenHeight
|
|
const screenScale = 2
|
|
const fps = 60
|
|
const title = "Ebiten Demo"
|
|
|
|
u := cocoa.UI()
|
|
textureFactory := cocoa.TextureFactory()
|
|
window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title)
|
|
|
|
drawing := make(chan struct{})
|
|
quit := make(chan struct{})
|
|
go func() {
|
|
defer close(quit)
|
|
|
|
windowEvents := window.Events()
|
|
var game Game = blocks.NewGame(textureFactory)
|
|
frameTime := time.Duration(int64(time.Second) / int64(fps))
|
|
tick := time.Tick(frameTime)
|
|
for {
|
|
select {
|
|
case e := <-windowEvents:
|
|
game.HandleEvent(e)
|
|
if _, ok := e.(ui.WindowClosedEvent); ok {
|
|
return
|
|
}
|
|
case <-tick:
|
|
game.Update()
|
|
case <-drawing:
|
|
window.Draw(func(context graphics.Context) {
|
|
game.Draw(context)
|
|
})
|
|
drawing <- struct{}{}
|
|
}
|
|
}
|
|
}()
|
|
|
|
u.Start()
|
|
defer u.Terminate()
|
|
|
|
s := make(chan os.Signal, 1)
|
|
signal.Notify(s, os.Interrupt, syscall.SIGTERM)
|
|
for {
|
|
u.DoEvents()
|
|
select {
|
|
default:
|
|
drawing <- struct{}{}
|
|
<-drawing
|
|
case <-s:
|
|
return
|
|
case <-quit:
|
|
return
|
|
}
|
|
}
|
|
}
|