internal/ui: refactoring: make Graphics a global function

This commit is contained in:
Hajime Hoshi 2022-02-06 20:29:06 +09:00
parent 0f09ddfbd2
commit 52f4c409ac
8 changed files with 22 additions and 22 deletions

View File

@ -20,5 +20,5 @@ import (
) )
func init() { func init() {
graphicscommand.SetGraphicsDriver(ui.Get().Graphics()) graphicscommand.SetGraphicsDriver(ui.Graphics())
} }

View File

@ -64,6 +64,6 @@ func init() {
graphics = opengl.Get() graphics = opengl.Get()
} }
func (*UserInterface) Graphics() driver.Graphics { func Graphics() driver.Graphics {
return graphics return graphics
} }

View File

@ -27,7 +27,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
) )
func (*UserInterface) Graphics() driver.Graphics { func Graphics() driver.Graphics {
if _, err := mtl.CreateSystemDefaultDevice(); err != nil { if _, err := mtl.CreateSystemDefaultDevice(); err != nil {
panic(fmt.Sprintf("mobile: mtl.CreateSystemDefaultDevice failed on iOS: %v", err)) panic(fmt.Sprintf("mobile: mtl.CreateSystemDefaultDevice failed on iOS: %v", err))
} }

View File

@ -22,6 +22,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
) )
func (*UserInterface) Graphics() driver.Graphics { func Graphics() driver.Graphics {
return opengl.Get() return opengl.Get()
} }

View File

@ -195,7 +195,7 @@ func (u *UserInterface) setNativeFullscreen(fullscreen bool) {
} }
func (u *UserInterface) adjustViewSize() { func (u *UserInterface) adjustViewSize() {
if u.Graphics().IsGL() { if Graphics().IsGL() {
return return
} }
C.adjustViewSize(C.uintptr_t(u.window.GetCocoaWindow())) C.adjustViewSize(C.uintptr_t(u.window.GetCocoaWindow()))

View File

@ -700,7 +700,7 @@ func (u *UserInterface) createWindow() error {
// Ensure to consume this callback. // Ensure to consume this callback.
u.waitForFramebufferSizeCallback(u.window, nil) u.waitForFramebufferSizeCallback(u.window, nil)
if u.Graphics().IsGL() { if Graphics().IsGL() {
u.window.MakeContextCurrent() u.window.MakeContextCurrent()
} }
@ -735,7 +735,7 @@ func (u *UserInterface) registerWindowSetSizeCallback() {
if err := u.runOnAnotherThreadFromMainThread(func() error { if err := u.runOnAnotherThreadFromMainThread(func() error {
// Disable Vsync temporarily. On macOS, getting a next frame can get stuck (#1740). // Disable Vsync temporarily. On macOS, getting a next frame can get stuck (#1740).
u.Graphics().SetVsyncEnabled(false) Graphics().SetVsyncEnabled(false)
var outsideWidth, outsideHeight float64 var outsideWidth, outsideHeight float64
@ -752,7 +752,7 @@ func (u *UserInterface) registerWindowSetSizeCallback() {
if err := u.context.ForceUpdateFrame(); err != nil { if err := u.context.ForceUpdateFrame(); err != nil {
return err return err
} }
if u.Graphics().IsGL() { if Graphics().IsGL() {
u.t.Call(func() { u.t.Call(func() {
u.swapBuffers() u.swapBuffers()
}) })
@ -852,7 +852,7 @@ event:
} }
func (u *UserInterface) init() error { func (u *UserInterface) init() error {
if u.Graphics().IsGL() { if Graphics().IsGL() {
glfw.WindowHint(glfw.ClientAPI, glfw.OpenGLAPI) glfw.WindowHint(glfw.ClientAPI, glfw.OpenGLAPI)
glfw.WindowHint(glfw.ContextVersionMajor, 2) glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1) glfw.WindowHint(glfw.ContextVersionMinor, 1)
@ -873,7 +873,7 @@ func (u *UserInterface) init() error {
transparent = glfw.True transparent = glfw.True
} }
glfw.WindowHint(glfw.TransparentFramebuffer, transparent) glfw.WindowHint(glfw.TransparentFramebuffer, transparent)
u.Graphics().SetTransparent(u.isInitScreenTransparent()) Graphics().SetTransparent(u.isInitScreenTransparent())
resizable := glfw.False resizable := glfw.False
if u.isInitWindowResizable() { if u.isInitWindowResizable() {
@ -931,7 +931,7 @@ func (u *UserInterface) init() error {
u.window.SetTitle(u.title) u.window.SetTitle(u.title)
u.window.Show() u.window.Show()
if g, ok := u.Graphics().(interface{ SetWindow(uintptr) }); ok { if g, ok := Graphics().(interface{ SetWindow(uintptr) }); ok {
g.SetWindow(u.nativeWindow()) g.SetWindow(u.nativeWindow())
} }
@ -1109,7 +1109,7 @@ func (u *UserInterface) loop() error {
// swapBuffers also checks IsGL, so this condition is redundant. // swapBuffers also checks IsGL, so this condition is redundant.
// However, (*thread).Call is not good for performance due to channels. // However, (*thread).Call is not good for performance due to channels.
// Let's avoid this whenever possible (#1367). // Let's avoid this whenever possible (#1367).
if u.Graphics().IsGL() { if Graphics().IsGL() {
u.t.Call(u.swapBuffers) u.t.Call(u.swapBuffers)
} }
@ -1131,7 +1131,7 @@ func (u *UserInterface) loop() error {
// swapBuffers must be called from the main thread. // swapBuffers must be called from the main thread.
func (u *UserInterface) swapBuffers() { func (u *UserInterface) swapBuffers() {
if u.Graphics().IsGL() { if Graphics().IsGL() {
u.window.SwapBuffers() u.window.SwapBuffers()
} }
} }
@ -1186,7 +1186,7 @@ func (u *UserInterface) adjustWindowSizeBasedOnSizeLimitsInDIP(width, height int
func (u *UserInterface) setWindowSizeInDIP(width, height int, fullscreen bool) { func (u *UserInterface) setWindowSizeInDIP(width, height int, fullscreen bool) {
width, height = u.adjustWindowSizeBasedOnSizeLimitsInDIP(width, height) width, height = u.adjustWindowSizeBasedOnSizeLimitsInDIP(width, height)
u.Graphics().SetFullscreen(fullscreen) Graphics().SetFullscreen(fullscreen)
scale := u.deviceScaleFactor(u.currentMonitor()) scale := u.deviceScaleFactor(u.currentMonitor())
if u.windowWidthInDIP == width && u.windowHeightInDIP == height && u.isFullscreen() == fullscreen && u.lastDeviceScaleFactor == scale { if u.windowWidthInDIP == width && u.windowHeightInDIP == height && u.isFullscreen() == fullscreen && u.lastDeviceScaleFactor == scale {
@ -1226,7 +1226,7 @@ func (u *UserInterface) setWindowSizeInDIP(width, height int, fullscreen bool) {
u.windowHeightInDIP = height u.windowHeightInDIP = height
if windowRecreated { if windowRecreated {
if g, ok := u.Graphics().(interface{ SetWindow(uintptr) }); ok { if g, ok := Graphics().(interface{ SetWindow(uintptr) }); ok {
g.SetWindow(u.nativeWindow()) g.SetWindow(u.nativeWindow())
} }
} }
@ -1249,7 +1249,7 @@ func (u *UserInterface) setWindowSizeInDIPImpl(width, height int, fullscreen boo
// Swapping buffer is necesary to prevent the image lag (#1004). // Swapping buffer is necesary to prevent the image lag (#1004).
// TODO: This might not work when vsync is disabled. // TODO: This might not work when vsync is disabled.
if u.Graphics().IsGL() { if Graphics().IsGL() {
glfw.PollEvents() glfw.PollEvents()
u.swapBuffers() u.swapBuffers()
} }
@ -1269,7 +1269,7 @@ func (u *UserInterface) setWindowSizeInDIPImpl(width, height int, fullscreen boo
if u.isNativeFullscreenAvailable() && u.isNativeFullscreen() { if u.isNativeFullscreenAvailable() && u.isNativeFullscreen() {
u.setNativeFullscreen(false) u.setNativeFullscreen(false)
} else if !u.isNativeFullscreenAvailable() && u.window.GetMonitor() != nil { } else if !u.isNativeFullscreenAvailable() && u.window.GetMonitor() != nil {
if u.Graphics().IsGL() { if Graphics().IsGL() {
// When OpenGL is used, swapping buffer is enough to solve the image-lag // When OpenGL is used, swapping buffer is enough to solve the image-lag
// issue (#1004). Rather, recreating window destroys GPU resources. // issue (#1004). Rather, recreating window destroys GPU resources.
// TODO: This might not work when vsync is disabled. // TODO: This might not work when vsync is disabled.
@ -1326,7 +1326,7 @@ func (u *UserInterface) setWindowSizeInDIPImpl(width, height int, fullscreen boo
// updateVsync must be called on the main thread. // updateVsync must be called on the main thread.
func (u *UserInterface) updateVsync() { func (u *UserInterface) updateVsync() {
if u.Graphics().IsGL() { if Graphics().IsGL() {
// SwapInterval is affected by the current monitor of the window. // SwapInterval is affected by the current monitor of the window.
// This needs to be called at least after SetMonitor. // This needs to be called at least after SetMonitor.
// Without SwapInterval after SetMonitor, vsynch doesn't work (#375). // Without SwapInterval after SetMonitor, vsynch doesn't work (#375).
@ -1340,7 +1340,7 @@ func (u *UserInterface) updateVsync() {
glfw.SwapInterval(0) glfw.SwapInterval(0)
} }
} }
u.Graphics().SetVsyncEnabled(u.fpsMode == FPSModeVsyncOn) Graphics().SetVsyncEnabled(u.fpsMode == FPSModeVsyncOn)
} }
// initialMonitor returns the initial monitor to show the window. // initialMonitor returns the initial monitor to show the window.

View File

@ -279,7 +279,7 @@ func (u *UserInterface) run(context Context, mainloop bool) (err error) {
// When mainloop is true, gomobile-build is used. In this case, GL functions must be called via // When mainloop is true, gomobile-build is used. In this case, GL functions must be called via
// gl.Context so that they are called on the appropriate thread. // gl.Context so that they are called on the appropriate thread.
ctx := <-glContextCh ctx := <-glContextCh
u.Graphics().(*opengl.Graphics).SetGomobileGLContext(ctx) Graphics().(*opengl.Graphics).SetGomobileGLContext(ctx)
} else { } else {
u.t = thread.NewOSThread() u.t = thread.NewOSThread()
graphicscommand.SetMainThread(u.t) graphicscommand.SetMainThread(u.t)

View File

@ -200,7 +200,7 @@ func (c *uiContext) updateFrameImpl(updateCount int) error {
} }
c.game.Draw(c.offscreen) c.game.Draw(c.offscreen)
if ui.Get().Graphics().NeedsClearingScreen() { if ui.Graphics().NeedsClearingScreen() {
// This clear is needed for fullscreen mode or some mobile platforms (#622). // This clear is needed for fullscreen mode or some mobile platforms (#622).
c.screen.Clear() c.screen.Clear()
} }
@ -208,7 +208,7 @@ func (c *uiContext) updateFrameImpl(updateCount int) error {
op := &DrawImageOptions{} op := &DrawImageOptions{}
s := c.screenScale(ui.Get().DeviceScaleFactor()) s := c.screenScale(ui.Get().DeviceScaleFactor())
switch vd := ui.Get().Graphics().FramebufferYDirection(); vd { switch vd := ui.Graphics().FramebufferYDirection(); vd {
case driver.Upward: case driver.Upward:
op.GeoM.Scale(s, -s) op.GeoM.Scale(s, -s)
_, h := c.offscreen.Size() _, h := c.offscreen.Size()