internal/ui: refactoring

This commit is contained in:
Hajime Hoshi 2023-10-15 16:38:05 +09:00
parent a89aaa0756
commit a16a03c9db
5 changed files with 23 additions and 23 deletions

View File

@ -28,7 +28,7 @@ type graphicsDriverCreator interface {
newMetal() (graphicsdriver.Graphics, error) newMetal() (graphicsdriver.Graphics, error)
} }
func newGraphicsDriver(creator graphicsDriverCreator, graphicsLibrary GraphicsLibrary) (graphicsdriver.Graphics, error) { func newGraphicsDriver(creator graphicsDriverCreator, graphicsLibrary GraphicsLibrary) (graphicsdriver.Graphics, GraphicsLibrary, error) {
if graphicsLibrary == GraphicsLibraryAuto { if graphicsLibrary == GraphicsLibraryAuto {
envName := "EBITENGINE_GRAPHICS_LIBRARY" envName := "EBITENGINE_GRAPHICS_LIBRARY"
env := os.Getenv(envName) env := os.Getenv(envName)
@ -48,7 +48,7 @@ func newGraphicsDriver(creator graphicsDriverCreator, graphicsLibrary GraphicsLi
case "metal": case "metal":
graphicsLibrary = GraphicsLibraryMetal graphicsLibrary = GraphicsLibraryMetal
default: default:
return nil, fmt.Errorf("ui: an unsupported graphics library is specified by the environment variable: %s", env) return nil, 0, fmt.Errorf("ui: an unsupported graphics library is specified by the environment variable: %s", env)
} }
} }
@ -56,45 +56,41 @@ func newGraphicsDriver(creator graphicsDriverCreator, graphicsLibrary GraphicsLi
case GraphicsLibraryAuto: case GraphicsLibraryAuto:
g, lib, err := creator.newAuto() g, lib, err := creator.newAuto()
if err != nil { if err != nil {
return nil, err return nil, 0, err
} }
if g == nil { if g == nil {
return nil, fmt.Errorf("ui: no graphics library is available") return nil, 0, fmt.Errorf("ui: no graphics library is available")
} }
theGlobalState.setGraphicsLibrary(lib) return g, lib, nil
return g, nil
case GraphicsLibraryOpenGL: case GraphicsLibraryOpenGL:
g, err := creator.newOpenGL() g, err := creator.newOpenGL()
if err != nil { if err != nil {
return nil, err return nil, 0, err
} }
if g == nil { if g == nil {
return nil, fmt.Errorf("ui: %s is specified but OpenGL is not available", graphicsLibrary) return nil, 0, fmt.Errorf("ui: %s is specified but OpenGL is not available", graphicsLibrary)
} }
theGlobalState.setGraphicsLibrary(GraphicsLibraryOpenGL) return g, GraphicsLibraryOpenGL, nil
return g, nil
case GraphicsLibraryDirectX: case GraphicsLibraryDirectX:
g, err := creator.newDirectX() g, err := creator.newDirectX()
if err != nil { if err != nil {
return nil, err return nil, 0, err
} }
if g == nil { if g == nil {
return nil, fmt.Errorf("ui: %s is specified but DirectX is not available.", graphicsLibrary) return nil, 0, fmt.Errorf("ui: %s is specified but DirectX is not available.", graphicsLibrary)
} }
theGlobalState.setGraphicsLibrary(GraphicsLibraryDirectX) return g, GraphicsLibraryDirectX, nil
return g, nil
case GraphicsLibraryMetal: case GraphicsLibraryMetal:
g, err := creator.newMetal() g, err := creator.newMetal()
if err != nil { if err != nil {
return nil, err return nil, 0, err
} }
if g == nil { if g == nil {
return nil, fmt.Errorf("ui: %s is specified but Metal is not available", graphicsLibrary) return nil, 0, fmt.Errorf("ui: %s is specified but Metal is not available", graphicsLibrary)
} }
theGlobalState.setGraphicsLibrary(GraphicsLibraryMetal) return g, GraphicsLibraryMetal, nil
return g, nil
default: default:
return nil, fmt.Errorf("ui: an unsupported graphics library is specified: %d", graphicsLibrary) return nil, 0, fmt.Errorf("ui: an unsupported graphics library is specified: %d", graphicsLibrary)
} }
} }

View File

@ -1127,13 +1127,14 @@ func (u *UserInterface) initOnMainThread(options *RunOptions) error {
return err return err
} }
g, err := newGraphicsDriver(&graphicsDriverCreatorImpl{ g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{
transparent: options.ScreenTransparent, transparent: options.ScreenTransparent,
}, options.GraphicsLibrary) }, options.GraphicsLibrary)
if err != nil { if err != nil {
return err return err
} }
u.graphicsDriver = g u.graphicsDriver = g
theGlobalState.setGraphicsLibrary(lib)
u.graphicsDriver.SetTransparent(options.ScreenTransparent) u.graphicsDriver.SetTransparent(options.ScreenTransparent)
if u.graphicsDriver.IsGL() { if u.graphicsDriver.IsGL() {

View File

@ -748,13 +748,14 @@ func (u *UserInterface) Run(game Game, options *RunOptions) error {
} }
} }
u.running = true u.running = true
g, err := newGraphicsDriver(&graphicsDriverCreatorImpl{ g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{
canvas: canvas, canvas: canvas,
}, options.GraphicsLibrary) }, options.GraphicsLibrary)
if err != nil { if err != nil {
return err return err
} }
u.graphicsDriver = g u.graphicsDriver = g
theGlobalState.setGraphicsLibrary(lib)
if bodyStyle := document.Get("body").Get("style"); options.ScreenTransparent { if bodyStyle := document.Get("body").Get("style"); options.ScreenTransparent {
bodyStyle.Set("backgroundColor", "transparent") bodyStyle.Set("backgroundColor", "transparent")

View File

@ -281,13 +281,14 @@ func (u *UserInterface) run(game Game, mainloop bool, options *RunOptions) (err
graphicscommand.SetRenderThread(u.renderThread) graphicscommand.SetRenderThread(u.renderThread)
} }
g, err := newGraphicsDriver(&graphicsDriverCreatorImpl{ g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{
gomobileContext: mgl, gomobileContext: mgl,
}, options.GraphicsLibrary) }, options.GraphicsLibrary)
if err != nil { if err != nil {
return err return err
} }
u.graphicsDriver = g u.graphicsDriver = g
theGlobalState.setGraphicsLibrary(lib)
close(u.graphicsDriverInitCh) close(u.graphicsDriverInitCh)
// If gomobile-build is used, wait for the outside size fixed. // If gomobile-build is used, wait for the outside size fixed.

View File

@ -80,11 +80,12 @@ func (u *UserInterface) init() error {
func (u *UserInterface) Run(game Game, options *RunOptions) error { func (u *UserInterface) Run(game Game, options *RunOptions) error {
u.context = newContext(game) u.context = newContext(game)
g, err := newGraphicsDriver(&graphicsDriverCreatorImpl{}, options.GraphicsLibrary) g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{}, options.GraphicsLibrary)
if err != nil { if err != nil {
return err return err
} }
u.graphicsDriver = g u.graphicsDriver = g
theGlobalState.setGraphicsLibrary(lib)
n := C.ebitengine_Initialize() n := C.ebitengine_Initialize()
if err := u.egl.init(n); err != nil { if err := u.egl.init(n); err != nil {