From e5b95691427be1e897939832a3fd84630793b9f5 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 29 Dec 2022 15:44:18 +0900 Subject: [PATCH] internal/ui: bug fix: the env variable should be used only when the library is not specified Updates #2378 --- doc.go | 2 +- internal/ui/graphics.go | 39 ++++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/doc.go b/doc.go index d33815118..d61e9418f 100644 --- a/doc.go +++ b/doc.go @@ -67,7 +67,7 @@ // // `EBITENGINE_GRAPHICS_LIBRARY` environment variable specifies the graphics library. // If the specified graphics library is not available, RunGame returns an error. -// This environment variable can also be set programmatically through os.Setenv before RunGame is called. +// This environment variable works when RunGame is called or RunGameWithOptions is called with GraphicsLibraryAuto. // This can take one of the following value: // // "auto": Ebitengine chooses the graphics library automatically. This is the default value. diff --git a/internal/ui/graphics.go b/internal/ui/graphics.go index a87c5a608..932e25a7b 100644 --- a/internal/ui/graphics.go +++ b/internal/ui/graphics.go @@ -29,26 +29,27 @@ type graphicsDriverCreator interface { } func newGraphicsDriver(creator graphicsDriverCreator, graphicsLibrary GraphicsLibrary) (graphicsdriver.Graphics, error) { - envName := "EBITENGINE_GRAPHICS_LIBRARY" - env := os.Getenv(envName) - if env == "" { - // For backward compatibility, read the EBITEN_ version. - envName = "EBITEN_GRAPHICS_LIBRARY" - env = os.Getenv(envName) - } + if graphicsLibrary == GraphicsLibraryAuto { + envName := "EBITENGINE_GRAPHICS_LIBRARY" + env := os.Getenv(envName) + if env == "" { + // For backward compatibility, read the EBITEN_ version. + envName = "EBITEN_GRAPHICS_LIBRARY" + env = os.Getenv(envName) + } - switch env { - case "", "auto": - // Use the specified graphics library. - // Otherwise, prefer the environment variable. - case "opengl": - graphicsLibrary = GraphicsLibraryOpenGL - case "directx": - graphicsLibrary = GraphicsLibraryDirectX - case "metal": - graphicsLibrary = GraphicsLibraryMetal - default: - return nil, fmt.Errorf("ui: an unsupported graphics library is specified by the environment variable: %s", env) + switch env { + case "", "auto": + // Keep the automatic choosing. + case "opengl": + graphicsLibrary = GraphicsLibraryOpenGL + case "directx": + graphicsLibrary = GraphicsLibraryDirectX + case "metal": + graphicsLibrary = GraphicsLibraryMetal + default: + return nil, fmt.Errorf("ui: an unsupported graphics library is specified by the environment variable: %s", env) + } } switch graphicsLibrary {