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)
}
func newGraphicsDriver(creator graphicsDriverCreator, graphicsLibrary GraphicsLibrary) (graphicsdriver.Graphics, error) {
func newGraphicsDriver(creator graphicsDriverCreator, graphicsLibrary GraphicsLibrary) (graphicsdriver.Graphics, GraphicsLibrary, error) {
if graphicsLibrary == GraphicsLibraryAuto {
envName := "EBITENGINE_GRAPHICS_LIBRARY"
env := os.Getenv(envName)
@ -48,7 +48,7 @@ func newGraphicsDriver(creator graphicsDriverCreator, graphicsLibrary GraphicsLi
case "metal":
graphicsLibrary = GraphicsLibraryMetal
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:
g, lib, err := creator.newAuto()
if err != nil {
return nil, err
return nil, 0, err
}
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, nil
return g, lib, nil
case GraphicsLibraryOpenGL:
g, err := creator.newOpenGL()
if err != nil {
return nil, err
return nil, 0, err
}
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, nil
return g, GraphicsLibraryOpenGL, nil
case GraphicsLibraryDirectX:
g, err := creator.newDirectX()
if err != nil {
return nil, err
return nil, 0, err
}
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, nil
return g, GraphicsLibraryDirectX, nil
case GraphicsLibraryMetal:
g, err := creator.newMetal()
if err != nil {
return nil, err
return nil, 0, err
}
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, nil
return g, GraphicsLibraryMetal, nil
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
}
g, err := newGraphicsDriver(&graphicsDriverCreatorImpl{
g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{
transparent: options.ScreenTransparent,
}, options.GraphicsLibrary)
if err != nil {
return err
}
u.graphicsDriver = g
theGlobalState.setGraphicsLibrary(lib)
u.graphicsDriver.SetTransparent(options.ScreenTransparent)
if u.graphicsDriver.IsGL() {

View File

@ -748,13 +748,14 @@ func (u *UserInterface) Run(game Game, options *RunOptions) error {
}
}
u.running = true
g, err := newGraphicsDriver(&graphicsDriverCreatorImpl{
g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{
canvas: canvas,
}, options.GraphicsLibrary)
if err != nil {
return err
}
u.graphicsDriver = g
theGlobalState.setGraphicsLibrary(lib)
if bodyStyle := document.Get("body").Get("style"); options.ScreenTransparent {
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)
}
g, err := newGraphicsDriver(&graphicsDriverCreatorImpl{
g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{
gomobileContext: mgl,
}, options.GraphicsLibrary)
if err != nil {
return err
}
u.graphicsDriver = g
theGlobalState.setGraphicsLibrary(lib)
close(u.graphicsDriverInitCh)
// 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 {
u.context = newContext(game)
g, err := newGraphicsDriver(&graphicsDriverCreatorImpl{}, options.GraphicsLibrary)
g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{}, options.GraphicsLibrary)
if err != nil {
return err
}
u.graphicsDriver = g
theGlobalState.setGraphicsLibrary(lib)
n := C.ebitengine_Initialize()
if err := u.egl.init(n); err != nil {