mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
internal/ui: refactoring
This commit is contained in:
parent
a89aaa0756
commit
a16a03c9db
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -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")
|
||||
|
@ -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.
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user