mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
ui: Unify the context thread and the UI thread
This commit is contained in:
parent
8371426888
commit
3553fc55c3
@ -67,31 +67,18 @@ func init() {
|
||||
}
|
||||
|
||||
type context struct {
|
||||
funcs chan func()
|
||||
init bool
|
||||
runOnMainThread func(func() error) error
|
||||
}
|
||||
|
||||
func NewContext() (*Context, error) {
|
||||
func NewContext(runOnMainThread func(func() error) error) (*Context, error) {
|
||||
c := &Context{}
|
||||
c.funcs = make(chan func())
|
||||
c.runOnMainThread = runOnMainThread
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *Context) Loop() {
|
||||
for f := range c.funcs {
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Context) RunOnContextThread(f func() error) error {
|
||||
ch := make(chan struct{})
|
||||
var err error
|
||||
c.funcs <- func() {
|
||||
err = f()
|
||||
close(ch)
|
||||
}
|
||||
<-ch
|
||||
return err
|
||||
return c.runOnMainThread(f)
|
||||
}
|
||||
|
||||
func (c *Context) Reset() error {
|
||||
|
@ -70,29 +70,18 @@ func initialize() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
u := &userInterface{
|
||||
window: window,
|
||||
funcs: make(chan func()),
|
||||
sizeChanged: true,
|
||||
}
|
||||
ch := make(chan error)
|
||||
go func() {
|
||||
runtime.LockOSThread()
|
||||
u.window.MakeContextCurrent()
|
||||
glfw.SwapInterval(1)
|
||||
var err error
|
||||
u.context, err = opengl.NewContext()
|
||||
u.context, err = opengl.NewContext(u.runOnMainThread)
|
||||
if err != nil {
|
||||
ch <- err
|
||||
}
|
||||
close(ch)
|
||||
u.context.Loop()
|
||||
}()
|
||||
currentUI = u
|
||||
if err := <-ch; err != nil {
|
||||
return err
|
||||
}
|
||||
currentUI = u
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -258,11 +247,8 @@ func (u *userInterface) swapBuffers() error {
|
||||
if err := u.context.BindScreenFramebuffer(); err != nil {
|
||||
return err
|
||||
}
|
||||
u.context.RunOnContextThread(func() error {
|
||||
u.window.SwapBuffers()
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *userInterface) FinishRendering() error {
|
||||
|
Loading…
Reference in New Issue
Block a user