diff --git a/examples/glut/main.go b/examples/glut/main.go index d82296a26..e1a8c359c 100644 --- a/examples/glut/main.go +++ b/examples/glut/main.go @@ -20,6 +20,7 @@ import "C" import ( "image/color" "os" + "time" "unsafe" "github.com/hajimehoshi/go-ebiten/graphics" ) @@ -33,7 +34,7 @@ func (game *DemoGame) Update() { } func (game *DemoGame) Draw(g *graphics.GraphicsContext, offscreen *graphics.Texture) { - g.Fill(&color.RGBA{R: 0, G: 0, B: 255, A: 255}) + g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255}) } //export display @@ -75,11 +76,26 @@ func main() { C.setDisplayFunc() C.setIdleFunc() + ch := make(chan bool, 1) game := &DemoGame{} device = graphics.NewDevice(screenWidth, screenHeight, screenScale, func(g *graphics.GraphicsContext, offscreen *graphics.Texture) { + ch<- true game.Draw(g, offscreen) + <-ch }) + go func() { + const frameTime = time.Second / 60 + lastTime := time.Now() + for { + ch<- true + game.Update() + <-ch + time.Sleep(frameTime - time.Since(lastTime)) + lastTime = time.Now() + } + }() + C.glutMainLoop() } diff --git a/graphics/device.go b/graphics/device.go index caaa78a26..47f9e38ab 100644 --- a/graphics/device.go +++ b/graphics/device.go @@ -35,7 +35,6 @@ func (device *Device) Update() { C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_NEAREST) g.SetOffscreen(device.offscreenTexture) g.Clear() - // TODO: lock this! device.drawFunc(g, device.offscreenTexture) g.flush()