internal/graphicsdriver/opengl: refactoring

This commit is contained in:
Hajime Hoshi 2022-11-13 15:25:22 +09:00
parent 42566c6c9a
commit 8c49c88b08
9 changed files with 35 additions and 30 deletions

View File

@ -19,26 +19,9 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir" "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
) )
// NewGraphics creates an implementation of graphicsdriver.Graphics for OpenGL.
// The returned graphics value is nil iff the error is not nil.
//
// context is an additional information to initialize the underlying context.
// context type depends on environments.
func NewGraphics(context any) (graphicsdriver.Graphics, error) {
if microsoftgdk.IsXbox() {
return nil, fmt.Errorf("opengl: OpenGL is not supported on Xbox")
}
g := &Graphics{}
if err := g.init(context); err != nil {
return nil, err
}
return g, nil
}
type activatedTexture struct { type activatedTexture struct {
textureNative textureNative textureNative textureNative
index int index int

View File

@ -17,9 +17,22 @@
package opengl package opengl
import ( import (
"fmt"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
) )
func (g *Graphics) init(context any) error { // NewGraphics creates an implementation of graphicsdriver.Graphics for OpenGL.
return gl.Init() // The returned graphics value is nil iff the error is not nil.
func NewGraphics() (graphicsdriver.Graphics, error) {
if microsoftgdk.IsXbox() {
return nil, fmt.Errorf("opengl: OpenGL is not supported on Xbox")
}
g := &Graphics{}
if err := gl.Init(); err != nil {
return nil, err
}
return g, nil
} }

View File

@ -20,7 +20,10 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
) )
func (g *Graphics) init(context any) error { // NewGraphics creates an implementation of graphicsdriver.Graphics for OpenGL.
// The returned graphics value is nil iff the error is not nil.
func NewGraphics() (graphicsdriver.Graphics, error) {
g := &Graphics{}
g.context.ctx = gl.DefaultContext{} g.context.ctx = gl.DefaultContext{}
return nil return g, nil
} }

View File

@ -18,7 +18,10 @@ import (
"syscall/js" "syscall/js"
) )
func (g *Graphics) init(canvas any) error { // NewGraphics creates an implementation of graphicsdriver.Graphics for OpenGL.
g.context.canvas = canvas.(js.Value) // The returned graphics value is nil iff the error is not nil.
return nil func NewGraphics(canvas js.Value) (graphicsdriver.Graphics, error) {
g := &Graphics{}
g.context.canvas = canvas
return g, nil
} }

View File

@ -22,11 +22,14 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
) )
func (g *Graphics) init(context any) error { // NewGraphics creates an implementation of graphicsdriver.Graphics for OpenGL.
// The returned graphics value is nil iff the error is not nil.
func NewGraphics(context mgl.Context) (graphicsdriver.Graphics, error) {
g := &Graphics{}
if context != nil { if context != nil {
g.context.ctx = gl.NewGomobileContext(context.(mgl.Context)) g.context.ctx = gl.NewGomobileContext(context.(mgl.Context))
} else { } else {
g.context.ctx = gl.DefaultContext{} g.context.ctx = gl.DefaultContext{}
} }
return nil return g, nil
} }

View File

@ -172,7 +172,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics
} }
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics(nil) return opengl.NewGraphics()
} }
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) { func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {

View File

@ -40,7 +40,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics
} }
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics(nil) return opengl.NewGraphics()
} }
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) { func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {

View File

@ -47,7 +47,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics
} }
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics(nil) return opengl.NewGraphics()
} }
func (g *graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) { func (g *graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {

View File

@ -32,7 +32,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics
} }
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics(nil) return opengl.NewGraphics()
} }
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) { func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {