mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-09-21 13:52:22 +02:00
80 lines
1.5 KiB
Go
80 lines
1.5 KiB
Go
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"
|
|
)
|
|
|
|
type canvas struct {
|
|
window *glfw.Window
|
|
context *opengl.Context
|
|
keyboard keyboard
|
|
mouse mouse
|
|
funcs chan func()
|
|
funcsDone chan struct{}
|
|
}
|
|
|
|
func (c *canvas) Draw(d ui.Drawer) (err error) {
|
|
c.use(func() {
|
|
c.context.PreUpdate()
|
|
})
|
|
if err = d.Draw(&context{c}); err != nil {
|
|
return
|
|
}
|
|
c.use(func() {
|
|
c.context.PostUpdate()
|
|
c.window.SwapBuffers()
|
|
})
|
|
return
|
|
}
|
|
|
|
func (c *canvas) IsClosed() bool {
|
|
return c.window.ShouldClose()
|
|
}
|
|
|
|
func (c *canvas) NewTextureID(img image.Image, filter graphics.Filter) (graphics.TextureID, error) {
|
|
var id graphics.TextureID
|
|
var err error
|
|
c.use(func() {
|
|
id, err = opengl.NewTextureID(img, filter)
|
|
})
|
|
return id, err
|
|
}
|
|
|
|
func (c *canvas) NewRenderTargetID(width, height int, filter graphics.Filter) (graphics.RenderTargetID, error) {
|
|
var id graphics.RenderTargetID
|
|
var err error
|
|
c.use(func() {
|
|
id, err = opengl.NewRenderTargetID(width, height, filter)
|
|
})
|
|
return id, err
|
|
}
|
|
|
|
func (c *canvas) run(width, height, scale int) {
|
|
go func() {
|
|
runtime.LockOSThread()
|
|
c.window.MakeContextCurrent()
|
|
glfw.SwapInterval(1)
|
|
for {
|
|
|
|
f := <-c.funcs
|
|
f()
|
|
c.funcsDone <- struct{}{}
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (c *canvas) use(f func()) {
|
|
c.funcs <- f
|
|
<-c.funcsDone
|
|
}
|
|
|
|
func (c *canvas) update() {
|
|
c.keyboard.update(c.window)
|
|
c.mouse.update(c.window)
|
|
}
|