diff --git a/internal/ui/graphics_darwin.go b/internal/ui/graphics_darwin.go index 123a7f86e..19cc4b81a 100644 --- a/internal/ui/graphics_darwin.go +++ b/internal/ui/graphics_darwin.go @@ -34,6 +34,8 @@ package ui import "C" import ( + "sync" + "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl" @@ -56,14 +58,15 @@ func supportsMetal() bool { return true } -func init() { - if supportsMetal() { - graphics = metal.Get() - return - } - graphics = opengl.Get() -} +var graphicsOnce sync.Once func Graphics() graphicsdriver.Graphics { + graphicsOnce.Do(func() { + if supportsMetal() { + graphics = metal.Get() + return + } + graphics = opengl.Get() + }) return graphics } diff --git a/init.go b/internal/ui/init.go similarity index 85% rename from init.go rename to internal/ui/init.go index b783bcc48..eb9a367e7 100644 --- a/init.go +++ b/internal/ui/init.go @@ -12,13 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -package ebiten +package ui import ( "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" - "github.com/hajimehoshi/ebiten/v2/internal/ui" ) func init() { - graphicscommand.SetGraphicsDriver(ui.Graphics()) + graphicscommand.SetGraphicsDriver(Graphics()) }