2014-12-05 18:26:02 +01:00
|
|
|
package glfw
|
|
|
|
|
|
|
|
import (
|
|
|
|
glfw "github.com/go-gl/glfw3"
|
|
|
|
"github.com/hajimehoshi/ebiten/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/graphics/opengl"
|
|
|
|
"github.com/hajimehoshi/ebiten/ui"
|
|
|
|
"image"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2014-12-07 15:18:56 +01:00
|
|
|
type canvas struct {
|
2014-12-07 16:07:36 +01:00
|
|
|
window *glfw.Window
|
|
|
|
contextUpdater *opengl.ContextUpdater
|
2014-12-07 20:22:50 +01:00
|
|
|
keyboard keyboard
|
2014-12-08 14:51:40 +01:00
|
|
|
mouse mouse
|
2014-12-07 16:07:36 +01:00
|
|
|
funcs chan func()
|
|
|
|
funcsDone chan struct{}
|
2014-12-05 18:26:02 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:18:56 +01:00
|
|
|
func (c *canvas) Draw(d ui.Drawer) (err error) {
|
2014-12-05 18:26:02 +01:00
|
|
|
c.use(func() {
|
2014-12-07 16:07:36 +01:00
|
|
|
err = c.contextUpdater.Update(d)
|
2014-12-05 18:26:02 +01:00
|
|
|
c.window.SwapBuffers()
|
|
|
|
})
|
2014-12-06 20:14:35 +01:00
|
|
|
return
|
2014-12-05 18:26:02 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:18:56 +01:00
|
|
|
func (c *canvas) IsClosed() bool {
|
2014-12-05 18:26:02 +01:00
|
|
|
return c.window.ShouldClose()
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:18:56 +01:00
|
|
|
func (c *canvas) NewTextureID(img image.Image, filter graphics.Filter) (graphics.TextureID, error) {
|
2014-12-06 21:21:20 +01:00
|
|
|
var id graphics.TextureID
|
2014-12-05 18:26:02 +01:00
|
|
|
var err error
|
|
|
|
c.use(func() {
|
2014-12-07 11:25:49 +01:00
|
|
|
id, err = opengl.NewTextureID(img, filter)
|
2014-12-05 18:26:02 +01:00
|
|
|
})
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:18:56 +01:00
|
|
|
func (c *canvas) NewRenderTargetID(width, height int, filter graphics.Filter) (graphics.RenderTargetID, error) {
|
2014-12-06 21:21:20 +01:00
|
|
|
var id graphics.RenderTargetID
|
2014-12-05 18:26:02 +01:00
|
|
|
var err error
|
|
|
|
c.use(func() {
|
2014-12-07 11:25:49 +01:00
|
|
|
id, err = opengl.NewRenderTargetID(width, height, filter)
|
2014-12-05 18:26:02 +01:00
|
|
|
})
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
2014-12-07 20:22:50 +01:00
|
|
|
func (c *canvas) run(width, height, scale int) {
|
2014-12-05 18:26:02 +01:00
|
|
|
go func() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
c.window.MakeContextCurrent()
|
|
|
|
glfw.SwapInterval(1)
|
|
|
|
for {
|
|
|
|
f := <-c.funcs
|
|
|
|
f()
|
|
|
|
c.funcsDone <- struct{}{}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:18:56 +01:00
|
|
|
func (c *canvas) use(f func()) {
|
2014-12-05 18:26:02 +01:00
|
|
|
c.funcs <- f
|
|
|
|
<-c.funcsDone
|
|
|
|
}
|
2014-12-05 19:10:17 +01:00
|
|
|
|
2014-12-07 15:18:56 +01:00
|
|
|
func (c *canvas) update() {
|
2014-12-06 19:27:55 +01:00
|
|
|
c.keyboard.update(c.window)
|
2014-12-08 14:51:40 +01:00
|
|
|
c.mouse.update(c.window)
|
2014-12-05 19:10:17 +01:00
|
|
|
}
|