mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
ui/mobile: Move 'DoWork' logic to ui/mobile package
This simplifies driver.Graphics interface, and will make it easy to use another graphics driver than OpenGL.
This commit is contained in:
parent
e499535728
commit
480c5527a3
@ -17,7 +17,6 @@
|
|||||||
package opengl
|
package opengl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
stdcontext "context"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
@ -70,28 +69,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type contextImpl struct {
|
type contextImpl struct {
|
||||||
gl mgl.Context
|
gl mgl.Context
|
||||||
worker mgl.Worker
|
|
||||||
}
|
|
||||||
|
|
||||||
// doWork consumes the queued GL tasks.
|
|
||||||
//
|
|
||||||
// doWork is called only on gomobile-bind.
|
|
||||||
func (c *context) doWork(context stdcontext.Context) {
|
|
||||||
if c.worker == nil {
|
|
||||||
panic("opengl: worker must be initialized but not")
|
|
||||||
}
|
|
||||||
// TODO: Check this is called on the rendering thread
|
|
||||||
workAvailable := c.worker.WorkAvailable()
|
|
||||||
loop:
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-workAvailable:
|
|
||||||
c.worker.DoWork()
|
|
||||||
case <-context.Done():
|
|
||||||
break loop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) reset() error {
|
func (c *context) reset() error {
|
||||||
|
@ -17,19 +17,9 @@
|
|||||||
package opengl
|
package opengl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
stdcontext "context"
|
|
||||||
|
|
||||||
"golang.org/x/mobile/gl"
|
"golang.org/x/mobile/gl"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *Driver) DoWork(context stdcontext.Context) {
|
func (d *Driver) SetMobileGLContext(context gl.Context) {
|
||||||
d.context.doWork(context)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Driver) Init() {
|
|
||||||
d.context.gl, d.context.worker = gl.NewContext()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Driver) InitWithContext(context gl.Context) {
|
|
||||||
d.context.gl = context
|
d.context.gl = context
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,25 @@ func (u *UserInterface) Render() {
|
|||||||
<-renderEndCh
|
<-renderEndCh
|
||||||
cancel()
|
cancel()
|
||||||
}()
|
}()
|
||||||
opengl.Get().DoWork(ctx)
|
|
||||||
|
if u.graphics.IsGL() {
|
||||||
|
if u.glWorker == nil {
|
||||||
|
panic("mobile: glWorker must be initialized but not")
|
||||||
|
}
|
||||||
|
workAvailable := u.glWorker.WorkAvailable()
|
||||||
|
loop:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-workAvailable:
|
||||||
|
u.glWorker.DoWork()
|
||||||
|
case <-ctx.Done():
|
||||||
|
break loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Create and run the thread loop like the GLFW driver does.
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserInterface struct {
|
type UserInterface struct {
|
||||||
@ -75,8 +93,12 @@ type UserInterface struct {
|
|||||||
fullscreenWidthPx int
|
fullscreenWidthPx int
|
||||||
fullscreenHeightPx int
|
fullscreenHeightPx int
|
||||||
|
|
||||||
|
graphics driver.Graphics
|
||||||
|
|
||||||
input Input
|
input Input
|
||||||
|
|
||||||
|
glWorker gl.Worker
|
||||||
|
|
||||||
m sync.RWMutex
|
m sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,23 +193,23 @@ func (u *UserInterface) RunWithoutMainLoop(width, height int, scale float64, tit
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) run(width, height int, scale float64, title string, context driver.UIContext, graphics driver.Graphics, mainloop bool) error {
|
func (u *UserInterface) run(width, height int, scale float64, title string, context driver.UIContext, graphics driver.Graphics, mainloop bool) error {
|
||||||
if graphics != opengl.Get() {
|
|
||||||
panic("ui: graphics driver must be OpenGL")
|
|
||||||
}
|
|
||||||
|
|
||||||
u.m.Lock()
|
u.m.Lock()
|
||||||
u.width = width
|
u.width = width
|
||||||
u.height = height
|
u.height = height
|
||||||
u.scale = scale
|
u.scale = scale
|
||||||
u.sizeChanged = true
|
u.sizeChanged = true
|
||||||
|
u.graphics = graphics
|
||||||
u.m.Unlock()
|
u.m.Unlock()
|
||||||
// title is ignored?
|
// title is ignored?
|
||||||
|
|
||||||
if mainloop {
|
if graphics.IsGL() {
|
||||||
ctx := <-glContextCh
|
var ctx gl.Context
|
||||||
opengl.Get().InitWithContext(ctx)
|
if mainloop {
|
||||||
} else {
|
ctx = <-glContextCh
|
||||||
opengl.Get().Init()
|
} else {
|
||||||
|
ctx, u.glWorker = gl.NewContext()
|
||||||
|
}
|
||||||
|
graphics.(*opengl.Driver).SetMobileGLContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Force to set the screen size
|
// Force to set the screen size
|
||||||
|
Loading…
Reference in New Issue
Block a user