mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 19:28:57 +01:00
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
This commit is contained in:
parent
195b060911
commit
a6d415ebf2
@ -23,13 +23,15 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
||||||
)
|
)
|
||||||
|
|
||||||
var theGraphics Graphics
|
// NewGraphics creates an implementation of graphicsdriver.Graphcis for OpenGL.
|
||||||
|
// The returned graphics value is nil iff the error is not nil.
|
||||||
func Get() *Graphics {
|
func NewGraphics() (graphicsdriver.Graphics, error) {
|
||||||
if microsoftgdk.IsXbox() {
|
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 {
|
type activatedTexture struct {
|
||||||
|
@ -23,8 +23,8 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gles"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gles"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func (g *Graphics) init() {
|
||||||
theGraphics.context.ctx = gles.DefaultContext{}
|
g.context.ctx = gles.DefaultContext{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Graphics) SetGomobileGLContext(context gl.Context) {
|
func (g *Graphics) SetGomobileGLContext(context gl.Context) {
|
||||||
|
22
internal/graphicsdriver/opengl/graphics_notmobile.go
Normal file
22
internal/graphicsdriver/opengl/graphics_notmobile.go
Normal file
@ -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.
|
||||||
|
}
|
@ -22,8 +22,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type graphicsDriverGetter interface {
|
type graphicsDriverGetter interface {
|
||||||
getAuto() graphicsdriver.Graphics
|
newAuto() (graphicsdriver.Graphics, error)
|
||||||
getOpenGL() graphicsdriver.Graphics
|
newOpenGL() (graphicsdriver.Graphics, error)
|
||||||
getDirectX() graphicsdriver.Graphics
|
getDirectX() graphicsdriver.Graphics
|
||||||
getMetal() graphicsdriver.Graphics
|
getMetal() graphicsdriver.Graphics
|
||||||
}
|
}
|
||||||
@ -33,15 +33,23 @@ func chooseGraphicsDriver(getter graphicsDriverGetter) (graphicsdriver.Graphics,
|
|||||||
|
|
||||||
switch env := os.Getenv(envName); env {
|
switch env := os.Getenv(envName); env {
|
||||||
case "", "auto":
|
case "", "auto":
|
||||||
if g := getter.getAuto(); g != nil {
|
g, err := getter.newAuto()
|
||||||
return g, nil
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if g == nil {
|
||||||
return nil, fmt.Errorf("ui: no graphics library is available")
|
return nil, fmt.Errorf("ui: no graphics library is available")
|
||||||
case "opengl":
|
|
||||||
if g := getter.getOpenGL(); g != nil {
|
|
||||||
return g, nil
|
|
||||||
}
|
}
|
||||||
|
return g, nil
|
||||||
|
case "opengl":
|
||||||
|
g, err := getter.newOpenGL()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if g == nil {
|
||||||
return nil, fmt.Errorf("ui: %s=%s is specified but OpenGL is not available", envName, env)
|
return nil, fmt.Errorf("ui: %s=%s is specified but OpenGL is not available", envName, env)
|
||||||
|
}
|
||||||
|
return g, nil
|
||||||
case "directx":
|
case "directx":
|
||||||
if g := getter.getDirectX(); g != nil {
|
if g := getter.getDirectX(); g != nil {
|
||||||
return g, nil
|
return g, nil
|
||||||
|
@ -23,15 +23,12 @@ type graphicsDriverGetterImpl struct {
|
|||||||
gomobileBuild bool
|
gomobileBuild bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
|
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
|
||||||
return g.getOpenGL()
|
return g.newOpenGL()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||||
if g := opengl.Get(); g != nil {
|
return opengl.NewGraphics()
|
||||||
return g
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
||||||
|
@ -27,15 +27,12 @@ import (
|
|||||||
|
|
||||||
type graphicsDriverGetterImpl struct{}
|
type graphicsDriverGetterImpl struct{}
|
||||||
|
|
||||||
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
|
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
|
||||||
return g.getOpenGL()
|
return g.newOpenGL()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||||
if g := opengl.Get(); g != nil {
|
return opengl.NewGraphics()
|
||||||
return g
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
||||||
|
@ -258,18 +258,15 @@ type graphicsDriverGetterImpl struct {
|
|||||||
transparent bool
|
transparent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
|
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
|
||||||
if m := g.getMetal(); m != nil {
|
if m := g.getMetal(); m != nil {
|
||||||
return m
|
return m, nil
|
||||||
}
|
}
|
||||||
return g.getOpenGL()
|
return g.newOpenGL()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||||
if g := opengl.Get(); g != nil {
|
return opengl.NewGraphics()
|
||||||
return g
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
||||||
|
@ -35,15 +35,12 @@ type graphicsDriverGetterImpl struct {
|
|||||||
transparent bool
|
transparent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
|
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
|
||||||
return g.getOpenGL()
|
return g.newOpenGL()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||||
if g := opengl.Get(); g != nil {
|
return opengl.NewGraphics()
|
||||||
return g
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
||||||
|
@ -35,18 +35,15 @@ type graphicsDriverGetterImpl struct {
|
|||||||
transparent bool
|
transparent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
|
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
|
||||||
if d := g.getDirectX(); d != nil {
|
if d := g.getDirectX(); d != nil {
|
||||||
return d
|
return d, nil
|
||||||
}
|
}
|
||||||
return g.getOpenGL()
|
return g.newOpenGL()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||||
if g := opengl.Get(); g != nil {
|
return opengl.NewGraphics()
|
||||||
return g
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
func (g *graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
||||||
|
@ -27,18 +27,15 @@ type graphicsDriverGetterImpl struct {
|
|||||||
gomobileBuild bool
|
gomobileBuild bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
|
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
|
||||||
if m := g.getMetal(); m != nil {
|
if m := g.getMetal(); m != nil {
|
||||||
return m
|
return m, nil
|
||||||
}
|
}
|
||||||
return g.getOpenGL()
|
return g.newOpenGL()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||||
if g := opengl.Get(); g != nil {
|
return opengl.NewGraphics()
|
||||||
return g
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
||||||
|
@ -27,15 +27,12 @@ import (
|
|||||||
|
|
||||||
type graphicsDriverGetterImpl struct{}
|
type graphicsDriverGetterImpl struct{}
|
||||||
|
|
||||||
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
|
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
|
||||||
return g.getOpenGL()
|
return g.newOpenGL()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||||
if g := opengl.Get(); g != nil {
|
return opengl.NewGraphics()
|
||||||
return g
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {
|
||||||
|
Loading…
Reference in New Issue
Block a user