mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
driver: Rename GraphicsContext -> UIContext
This commit is contained in:
parent
aecfd6b13d
commit
f6367308fc
@ -19,7 +19,7 @@ import (
|
||||
"image"
|
||||
)
|
||||
|
||||
type GraphicsContext interface {
|
||||
type UIContext interface {
|
||||
SetSize(width, height int, scale float64)
|
||||
Update(afterFrameUpdate func()) error
|
||||
SuspendAudio()
|
||||
@ -40,7 +40,7 @@ type UI interface {
|
||||
IsWindowDecorated() bool
|
||||
IsWindowResizable() bool
|
||||
Loop(ch <-chan error) error
|
||||
Run(width, height int, scale float64, title string, g GraphicsContext, mainloop bool, graphics Graphics) error
|
||||
Run(width, height int, scale float64, title string, context UIContext, mainloop bool, graphics Graphics) error
|
||||
ScreenPadding() (x0, y0, x1, y1 float64)
|
||||
ScreenScale() float64
|
||||
ScreenSizeInFullscreen() (int, int)
|
||||
|
@ -555,7 +555,7 @@ func (u *UserInterface) DeviceScaleFactor() float64 {
|
||||
return f
|
||||
}
|
||||
|
||||
func (u *UserInterface) Run(width, height int, scale float64, title string, g driver.GraphicsContext, mainloop bool, graphics driver.Graphics) error {
|
||||
func (u *UserInterface) Run(width, height int, scale float64, title string, context driver.UIContext, mainloop bool, graphics driver.Graphics) error {
|
||||
_ = mainthread.Run(func() error {
|
||||
u.graphics = graphics
|
||||
|
||||
@ -662,7 +662,7 @@ func (u *UserInterface) Run(width, height int, scale float64, title string, g dr
|
||||
return nil
|
||||
})
|
||||
graphics.SetWindow(w)
|
||||
return u.loop(g)
|
||||
return u.loop(context)
|
||||
}
|
||||
|
||||
// getSize must be called from the main thread.
|
||||
@ -699,7 +699,7 @@ func (u *UserInterface) actualScreenScale() float64 {
|
||||
return u.getScale() * devicescale.GetAt(u.currentMonitor().GetPos())
|
||||
}
|
||||
|
||||
func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) {
|
||||
func (u *UserInterface) updateGraphics(context driver.UIContext) {
|
||||
actualScale := 0.0
|
||||
sizeChanged := false
|
||||
// TODO: Is it possible to reduce 'runOnMainThread' calls?
|
||||
@ -719,11 +719,11 @@ func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) {
|
||||
return nil
|
||||
})
|
||||
if sizeChanged {
|
||||
g.SetSize(u.width, u.height, actualScale)
|
||||
context.SetSize(u.width, u.height, actualScale)
|
||||
}
|
||||
}
|
||||
|
||||
func (u *UserInterface) update(g driver.GraphicsContext) error {
|
||||
func (u *UserInterface) update(context driver.UIContext) error {
|
||||
shouldClose := false
|
||||
_ = mainthread.Run(func() error {
|
||||
shouldClose = u.window.ShouldClose()
|
||||
@ -742,17 +742,17 @@ func (u *UserInterface) update(g driver.GraphicsContext) error {
|
||||
})
|
||||
|
||||
// This call is needed for initialization.
|
||||
u.updateGraphicsContext(g)
|
||||
u.updateGraphics(context)
|
||||
|
||||
_ = mainthread.Run(func() error {
|
||||
glfw.PollEvents()
|
||||
|
||||
u.input.update(u.window, u.getScale()*u.glfwScale())
|
||||
|
||||
defer g.ResumeAudio()
|
||||
defer context.ResumeAudio()
|
||||
|
||||
for !u.isRunnableInBackground() && u.window.GetAttrib(glfw.Focused) == 0 {
|
||||
g.SuspendAudio()
|
||||
context.SuspendAudio()
|
||||
// Wait for an arbitrary period to avoid busy loop.
|
||||
time.Sleep(time.Second / 60)
|
||||
glfw.PollEvents()
|
||||
@ -762,9 +762,9 @@ func (u *UserInterface) update(g driver.GraphicsContext) error {
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err := g.Update(func() {
|
||||
if err := context.Update(func() {
|
||||
// The offscreens must be updated every frame (#490).
|
||||
u.updateGraphicsContext(g)
|
||||
u.updateGraphics(context)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -782,7 +782,7 @@ func (u *UserInterface) update(g driver.GraphicsContext) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *UserInterface) loop(g driver.GraphicsContext) error {
|
||||
func (u *UserInterface) loop(context driver.UIContext) error {
|
||||
defer func() {
|
||||
_ = mainthread.Run(func() error {
|
||||
glfw.Terminate()
|
||||
@ -790,7 +790,7 @@ func (u *UserInterface) loop(g driver.GraphicsContext) error {
|
||||
})
|
||||
}()
|
||||
for {
|
||||
if err := u.update(g); err != nil {
|
||||
if err := u.update(context); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ type UserInterface struct {
|
||||
|
||||
lastActualScale float64
|
||||
|
||||
context driver.GraphicsContext
|
||||
context driver.UIContext
|
||||
input Input
|
||||
}
|
||||
|
||||
@ -188,7 +188,7 @@ func (u *UserInterface) actualScreenScale() float64 {
|
||||
return u.getScale() * devicescale.GetAt(0, 0)
|
||||
}
|
||||
|
||||
func (u *UserInterface) updateGraphicsContext() {
|
||||
func (u *UserInterface) updateGraphics() {
|
||||
a := u.actualScreenScale()
|
||||
if u.lastActualScale != a {
|
||||
u.updateScreenSize()
|
||||
@ -213,17 +213,17 @@ func (u *UserInterface) update() error {
|
||||
u.context.ResumeAudio()
|
||||
|
||||
u.input.UpdateGamepads()
|
||||
u.updateGraphicsContext()
|
||||
u.updateGraphics()
|
||||
if err := u.context.Update(func() {
|
||||
u.updateGraphicsContext()
|
||||
u.updateGraphics()
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *UserInterface) loop(g driver.GraphicsContext) <-chan error {
|
||||
u.context = g
|
||||
func (u *UserInterface) loop(context driver.UIContext) <-chan error {
|
||||
u.context = context
|
||||
|
||||
ch := make(chan error)
|
||||
var cf js.Callback
|
||||
@ -388,11 +388,11 @@ func (u *UserInterface) Loop(ch <-chan error) error {
|
||||
return <-ch
|
||||
}
|
||||
|
||||
func (u *UserInterface) Run(width, height int, scale float64, title string, g driver.GraphicsContext, mainloop bool, graphics driver.Graphics) error {
|
||||
func (u *UserInterface) Run(width, height int, scale float64, title string, context driver.UIContext, mainloop bool, graphics driver.Graphics) error {
|
||||
document.Set("title", title)
|
||||
u.setScreenSize(width, height, scale, u.fullscreen)
|
||||
canvas.Call("focus")
|
||||
ch := u.loop(g)
|
||||
ch := u.loop(context)
|
||||
if runtime.GOARCH == "wasm" {
|
||||
return <-ch
|
||||
}
|
||||
|
@ -153,7 +153,7 @@ func (u *UserInterface) appMain(a app.App) {
|
||||
}
|
||||
}
|
||||
|
||||
func (u *UserInterface) Run(width, height int, scale float64, title string, g driver.GraphicsContext, mainloop bool, graphics driver.Graphics) error {
|
||||
func (u *UserInterface) Run(width, height int, scale float64, title string, context driver.UIContext, mainloop bool, graphics driver.Graphics) error {
|
||||
if graphics != opengl.Get() {
|
||||
panic("ui: graphics driver must be OpenGL")
|
||||
}
|
||||
@ -174,9 +174,9 @@ func (u *UserInterface) Run(width, height int, scale float64, title string, g dr
|
||||
}
|
||||
|
||||
// Force to set the screen size
|
||||
u.updateGraphicsContext(g)
|
||||
u.updateGraphics(context)
|
||||
for {
|
||||
if err := u.update(g); err != nil {
|
||||
if err := u.update(context); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -193,7 +193,7 @@ func (u *UserInterface) Loop(ch <-chan error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) {
|
||||
func (u *UserInterface) updateGraphics(context driver.UIContext) {
|
||||
width, height := 0, 0
|
||||
actualScale := 0.0
|
||||
|
||||
@ -209,7 +209,7 @@ func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) {
|
||||
|
||||
if sizeChanged {
|
||||
// Sizing also calls GL functions
|
||||
g.SetSize(width, height, actualScale)
|
||||
context.SetSize(width, height, actualScale)
|
||||
}
|
||||
}
|
||||
|
||||
@ -228,25 +228,25 @@ func (u *UserInterface) scaleImpl() float64 {
|
||||
return scale
|
||||
}
|
||||
|
||||
func (u *UserInterface) update(g driver.GraphicsContext) error {
|
||||
func (u *UserInterface) update(context driver.UIContext) error {
|
||||
render:
|
||||
for {
|
||||
select {
|
||||
case <-renderCh:
|
||||
break render
|
||||
case <-time.After(500 * time.Millisecond):
|
||||
g.SuspendAudio()
|
||||
context.SuspendAudio()
|
||||
continue
|
||||
}
|
||||
}
|
||||
g.ResumeAudio()
|
||||
context.ResumeAudio()
|
||||
|
||||
defer func() {
|
||||
renderChEnd <- struct{}{}
|
||||
}()
|
||||
|
||||
if err := g.Update(func() {
|
||||
u.updateGraphicsContext(g)
|
||||
if err := context.Update(func() {
|
||||
u.updateGraphics(context)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
14
run.go
14
run.go
@ -88,15 +88,15 @@ func IsRunningSlowly() bool {
|
||||
return IsDrawingSkipped()
|
||||
}
|
||||
|
||||
var theGraphicsContext atomic.Value
|
||||
var theUIContext atomic.Value
|
||||
|
||||
func run(width, height int, scale float64, title string, g *graphicsContext, mainloop bool) error {
|
||||
func run(width, height int, scale float64, title string, context *uiContext, mainloop bool) error {
|
||||
atomic.StoreInt32(&isRunning, 1)
|
||||
// On GopherJS, run returns immediately.
|
||||
if !web.IsGopherJS() {
|
||||
defer atomic.StoreInt32(&isRunning, 0)
|
||||
}
|
||||
if err := uiDriver().Run(width, height, scale, title, g, mainloop, graphicsDriver()); err != nil {
|
||||
if err := uiDriver().Run(width, height, scale, title, context, mainloop, graphicsDriver()); err != nil {
|
||||
if err == driver.RegularTermination {
|
||||
return nil
|
||||
}
|
||||
@ -147,8 +147,8 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
|
||||
go func() {
|
||||
defer close(ch)
|
||||
|
||||
g := newGraphicsContext(f)
|
||||
theGraphicsContext.Store(g)
|
||||
g := newUIContext(f)
|
||||
theUIContext.Store(g)
|
||||
if err := run(width, height, scale, title, g, true); err != nil {
|
||||
ch <- err
|
||||
return
|
||||
@ -173,8 +173,8 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64,
|
||||
go func() {
|
||||
defer close(ch)
|
||||
|
||||
g := newGraphicsContext(f)
|
||||
theGraphicsContext.Store(g)
|
||||
g := newUIContext(f)
|
||||
theUIContext.Store(g)
|
||||
if err := run(width, height, scale, title, g, false); err != nil {
|
||||
ch <- err
|
||||
return
|
||||
|
@ -29,13 +29,13 @@ func init() {
|
||||
graphicscommand.SetGraphicsDriver(graphicsDriver())
|
||||
}
|
||||
|
||||
func newGraphicsContext(f func(*Image) error) *graphicsContext {
|
||||
return &graphicsContext{
|
||||
func newUIContext(f func(*Image) error) *uiContext {
|
||||
return &uiContext{
|
||||
f: f,
|
||||
}
|
||||
}
|
||||
|
||||
type graphicsContext struct {
|
||||
type uiContext struct {
|
||||
f func(*Image) error
|
||||
offscreen *Image
|
||||
screen *Image
|
||||
@ -47,7 +47,7 @@ type graphicsContext struct {
|
||||
offsetY float64
|
||||
}
|
||||
|
||||
func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale float64) {
|
||||
func (c *uiContext) SetSize(screenWidth, screenHeight int, screenScale float64) {
|
||||
c.screenScale = screenScale
|
||||
|
||||
if c.screen != nil {
|
||||
@ -71,7 +71,7 @@ func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale flo
|
||||
c.offsetY = py0
|
||||
}
|
||||
|
||||
func (c *graphicsContext) initializeIfNeeded() error {
|
||||
func (c *uiContext) initializeIfNeeded() error {
|
||||
if !c.initialized {
|
||||
if err := shareable.InitializeGraphicsDriverState(); err != nil {
|
||||
return err
|
||||
@ -84,7 +84,7 @@ func (c *graphicsContext) initializeIfNeeded() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *graphicsContext) Update(afterFrameUpdate func()) error {
|
||||
func (c *uiContext) Update(afterFrameUpdate func()) error {
|
||||
tps := int(MaxTPS())
|
||||
updateCount := clock.Update(tps)
|
||||
|
||||
@ -151,11 +151,11 @@ func (c *graphicsContext) Update(afterFrameUpdate func()) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *graphicsContext) needsRestoring() (bool, error) {
|
||||
func (c *uiContext) needsRestoring() (bool, error) {
|
||||
return c.offscreen.mipmap.original().IsInvalidated()
|
||||
}
|
||||
|
||||
func (c *graphicsContext) restoreIfNeeded() error {
|
||||
func (c *uiContext) restoreIfNeeded() error {
|
||||
if !shareable.IsRestoringEnabled() {
|
||||
return nil
|
||||
}
|
||||
@ -172,10 +172,10 @@ func (c *graphicsContext) restoreIfNeeded() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *graphicsContext) SuspendAudio() {
|
||||
func (c *uiContext) SuspendAudio() {
|
||||
hooks.SuspendAudio()
|
||||
}
|
||||
|
||||
func (c *graphicsContext) ResumeAudio() {
|
||||
func (c *uiContext) ResumeAudio() {
|
||||
hooks.ResumeAudio()
|
||||
}
|
Loading…
Reference in New Issue
Block a user