From a6d415ebf27e8dbb53717538d9466d8ee5ee7f8a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 17 Jun 2022 02:02:29 +0900 Subject: [PATCH] internal/graphicsdriver/opengl: replace Get with NewGraphics (#2146) This is a prepartion to return an error when a graphics driver, especially DirectX, fails to initialize. Updates #2142 --- internal/graphicsdriver/opengl/graphics.go | 12 ++++++---- .../graphicsdriver/opengl/graphics_mobile.go | 4 ++-- .../opengl/graphics_notmobile.go | 22 +++++++++++++++++ internal/ui/graphics.go | 24 ++++++++++++------- internal/ui/ui_android.go | 11 ++++----- internal/ui/ui_cbackend.go | 11 ++++----- internal/ui/ui_glfw_darwin.go | 13 ++++------ internal/ui/ui_glfw_unix.go | 11 ++++----- internal/ui/ui_glfw_windows.go | 13 ++++------ internal/ui/ui_ios.go | 13 ++++------ internal/ui/ui_js.go | 11 ++++----- 11 files changed, 78 insertions(+), 67 deletions(-) create mode 100644 internal/graphicsdriver/opengl/graphics_notmobile.go diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index 2c86ff7fe..7109f2475 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -23,13 +23,15 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/shaderir" ) -var theGraphics Graphics - -func Get() *Graphics { +// NewGraphics creates an implementation of graphicsdriver.Graphcis for OpenGL. +// The returned graphics value is nil iff the error is not nil. +func NewGraphics() (graphicsdriver.Graphics, error) { if microsoftgdk.IsXbox() { - return nil + return nil, fmt.Errorf("opengl: OpenGL is not supported on Xbox") } - return &theGraphics + g := &Graphics{} + g.init() + return g, nil } type activatedTexture struct { diff --git a/internal/graphicsdriver/opengl/graphics_mobile.go b/internal/graphicsdriver/opengl/graphics_mobile.go index c0e753910..849636f21 100644 --- a/internal/graphicsdriver/opengl/graphics_mobile.go +++ b/internal/graphicsdriver/opengl/graphics_mobile.go @@ -23,8 +23,8 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gles" ) -func init() { - theGraphics.context.ctx = gles.DefaultContext{} +func (g *Graphics) init() { + g.context.ctx = gles.DefaultContext{} } func (g *Graphics) SetGomobileGLContext(context gl.Context) { diff --git a/internal/graphicsdriver/opengl/graphics_notmobile.go b/internal/graphicsdriver/opengl/graphics_notmobile.go new file mode 100644 index 000000000..b943ffda1 --- /dev/null +++ b/internal/graphicsdriver/opengl/graphics_notmobile.go @@ -0,0 +1,22 @@ +// Copyright 2022 The Ebitengine Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !android && !ios +// +build !android,!ios + +package opengl + +func (g *Graphics) init() { + // Do nothing. +} diff --git a/internal/ui/graphics.go b/internal/ui/graphics.go index 839f52b6a..1ce0b8edf 100644 --- a/internal/ui/graphics.go +++ b/internal/ui/graphics.go @@ -22,8 +22,8 @@ import ( ) type graphicsDriverGetter interface { - getAuto() graphicsdriver.Graphics - getOpenGL() graphicsdriver.Graphics + newAuto() (graphicsdriver.Graphics, error) + newOpenGL() (graphicsdriver.Graphics, error) getDirectX() graphicsdriver.Graphics getMetal() graphicsdriver.Graphics } @@ -33,15 +33,23 @@ func chooseGraphicsDriver(getter graphicsDriverGetter) (graphicsdriver.Graphics, switch env := os.Getenv(envName); env { case "", "auto": - if g := getter.getAuto(); g != nil { - return g, nil + g, err := getter.newAuto() + if err != nil { + return nil, err } - return nil, fmt.Errorf("ui: no graphics library is available") + if g == nil { + return nil, fmt.Errorf("ui: no graphics library is available") + } + return g, nil case "opengl": - if g := getter.getOpenGL(); g != nil { - return g, nil + g, err := getter.newOpenGL() + if err != nil { + return nil, err } - return nil, fmt.Errorf("ui: %s=%s is specified but OpenGL is not available", envName, env) + if g == nil { + return nil, fmt.Errorf("ui: %s=%s is specified but OpenGL is not available", envName, env) + } + return g, nil case "directx": if g := getter.getDirectX(); g != nil { return g, nil diff --git a/internal/ui/ui_android.go b/internal/ui/ui_android.go index f855474c4..74c2fbcf8 100644 --- a/internal/ui/ui_android.go +++ b/internal/ui/ui_android.go @@ -23,15 +23,12 @@ type graphicsDriverGetterImpl struct { gomobileBuild bool } -func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics { - return g.getOpenGL() +func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) { + return g.newOpenGL() } -func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics { - if g := opengl.Get(); g != nil { - return g - } - return nil +func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) { + return opengl.NewGraphics() } func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics { diff --git a/internal/ui/ui_cbackend.go b/internal/ui/ui_cbackend.go index 3f7ec859e..5e8561884 100644 --- a/internal/ui/ui_cbackend.go +++ b/internal/ui/ui_cbackend.go @@ -27,15 +27,12 @@ import ( type graphicsDriverGetterImpl struct{} -func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics { - return g.getOpenGL() +func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) { + return g.newOpenGL() } -func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics { - if g := opengl.Get(); g != nil { - return g - } - return nil +func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) { + return opengl.NewGraphics() } func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics { diff --git a/internal/ui/ui_glfw_darwin.go b/internal/ui/ui_glfw_darwin.go index 0bea680da..56310d19d 100644 --- a/internal/ui/ui_glfw_darwin.go +++ b/internal/ui/ui_glfw_darwin.go @@ -258,18 +258,15 @@ type graphicsDriverGetterImpl struct { transparent bool } -func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics { +func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) { if m := g.getMetal(); m != nil { - return m + return m, nil } - return g.getOpenGL() + return g.newOpenGL() } -func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics { - if g := opengl.Get(); g != nil { - return g - } - return nil +func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) { + return opengl.NewGraphics() } func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics { diff --git a/internal/ui/ui_glfw_unix.go b/internal/ui/ui_glfw_unix.go index 5b83c173a..7ac42f4f1 100644 --- a/internal/ui/ui_glfw_unix.go +++ b/internal/ui/ui_glfw_unix.go @@ -35,15 +35,12 @@ type graphicsDriverGetterImpl struct { transparent bool } -func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics { - return g.getOpenGL() +func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) { + return g.newOpenGL() } -func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics { - if g := opengl.Get(); g != nil { - return g - } - return nil +func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) { + return opengl.NewGraphics() } func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics { diff --git a/internal/ui/ui_glfw_windows.go b/internal/ui/ui_glfw_windows.go index 738cec130..83f8d149f 100644 --- a/internal/ui/ui_glfw_windows.go +++ b/internal/ui/ui_glfw_windows.go @@ -35,18 +35,15 @@ type graphicsDriverGetterImpl struct { transparent bool } -func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics { +func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) { if d := g.getDirectX(); d != nil { - return d + return d, nil } - return g.getOpenGL() + return g.newOpenGL() } -func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics { - if g := opengl.Get(); g != nil { - return g - } - return nil +func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) { + return opengl.NewGraphics() } func (g *graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics { diff --git a/internal/ui/ui_ios.go b/internal/ui/ui_ios.go index adf1d560e..d09fa950d 100644 --- a/internal/ui/ui_ios.go +++ b/internal/ui/ui_ios.go @@ -27,18 +27,15 @@ type graphicsDriverGetterImpl struct { gomobileBuild bool } -func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics { +func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) { if m := g.getMetal(); m != nil { - return m + return m, nil } - return g.getOpenGL() + return g.newOpenGL() } -func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics { - if g := opengl.Get(); g != nil { - return g - } - return nil +func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) { + return opengl.NewGraphics() } func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics { diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index a73dd24c4..c4002e8f3 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -27,15 +27,12 @@ import ( type graphicsDriverGetterImpl struct{} -func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics { - return g.getOpenGL() +func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) { + return g.newOpenGL() } -func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics { - if g := opengl.Get(); g != nil { - return g - } - return nil +func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) { + return opengl.NewGraphics() } func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {