graphics: Add worker consumer

This commit is contained in:
Hajime Hoshi 2016-02-19 11:43:16 +09:00
parent 93450b1664
commit 269d94b145
3 changed files with 21 additions and 7 deletions

View File

@ -67,14 +67,24 @@ func NewContext() *Context {
}
var (
gl mgl.Context
worker mgl.Worker
gl mgl.Context
worker mgl.Worker
workerCreated = make(chan struct{})
)
// TODO: Implement updating Worker
func Loop() {
<-workerCreated
for {
select {
case <-worker.WorkAvailable():
worker.DoWork()
}
}
}
func (c *Context) init() {
gl, worker = mgl.NewContext()
close(workerCreated)
// Textures' pixel formats are alpha premultiplied.
gl.Enable(mgl.BLEND)
gl.BlendFunc(mgl.ONE, mgl.ONE_MINUS_SRC_ALPHA)

View File

@ -22,6 +22,7 @@ import (
"time"
glfw "github.com/go-gl/glfw/v3.1/glfw"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
)
func Now() int64 {
@ -51,18 +52,22 @@ func Init() {
funcs: make(chan func()),
}
go func() {
runtime.LockOSThread()
u.window.MakeContextCurrent()
glfw.SwapInterval(1)
for f := range u.funcs {
f()
}
}()
go func() {
runtime.LockOSThread()
u.window.MakeContextCurrent()
glfw.SwapInterval(1)
opengl.Loop()
}()
currentUI = u
}
func ExecOnUIThread(f func()) {
// TODO: Rename this function: f is actually NOT executed on UI threads
ch := make(chan struct{})
currentUI.funcs <- func() {
defer close(ch)

1
run.go
View File

@ -57,7 +57,6 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
ui.ExecOnUIThread(func() {
glContext.Check()
})
graphicsContext, err := newGraphicsContext(width, height, actualScale)
if err != nil {
return err