mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
internal/graphicsdriver/direct: replace Get -> NewGraphics
Updates #2142
This commit is contained in:
parent
138463e219
commit
9962fc5ee5
@ -20,7 +20,6 @@ import (
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
@ -34,28 +33,22 @@ import (
|
||||
|
||||
const frameCount = 2
|
||||
|
||||
var (
|
||||
// isDirectXAvailable indicates whether DirectX is available or not.
|
||||
isDirectXAvailable bool
|
||||
isDirectXAvailableOnce sync.Once
|
||||
)
|
||||
func NewGraphics() (graphicsdriver.Graphics, error) {
|
||||
const is64bit = uint64(^uintptr(0)) == ^uint64(0)
|
||||
|
||||
var theGraphics Graphics
|
||||
|
||||
func Get() *Graphics {
|
||||
isDirectXAvailableOnce.Do(func() {
|
||||
const is64bit = uint64(^uintptr(0)) == ^uint64(0)
|
||||
|
||||
// In 32bit machines, DirectX is not used because
|
||||
// 1) The functions syscall.Syscall cannot accept 64bit values as one argument
|
||||
// 2) The struct layouts can be different
|
||||
// TODO: Support DirectX for 32bit machines (#2088).
|
||||
isDirectXAvailable = is64bit && theGraphics.initializeDevice() == nil
|
||||
})
|
||||
if !isDirectXAvailable {
|
||||
return nil
|
||||
// In 32bit machines, DirectX is not used because
|
||||
// 1) The functions syscall.Syscall cannot accept 64bit values as one argument
|
||||
// 2) The struct layouts can be different
|
||||
// TODO: Support DirectX for 32bit machines (#2088).
|
||||
if !is64bit {
|
||||
return nil, fmt.Errorf("directx: DirectX is not available on a 32bit machine")
|
||||
}
|
||||
return &theGraphics
|
||||
|
||||
g := &Graphics{}
|
||||
if err := g.initializeDevice(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
var inputElementDescs []_D3D12_INPUT_ELEMENT_DESC
|
||||
|
@ -24,7 +24,7 @@ import (
|
||||
type graphicsDriverCreator interface {
|
||||
newAuto() (graphicsdriver.Graphics, error)
|
||||
newOpenGL() (graphicsdriver.Graphics, error)
|
||||
getDirectX() graphicsdriver.Graphics
|
||||
newDirectX() (graphicsdriver.Graphics, error)
|
||||
newMetal() (graphicsdriver.Graphics, error)
|
||||
}
|
||||
|
||||
@ -51,10 +51,14 @@ func newGraphicsDriver(creator graphicsDriverCreator) (graphicsdriver.Graphics,
|
||||
}
|
||||
return g, nil
|
||||
case "directx":
|
||||
if g := creator.getDirectX(); g != nil {
|
||||
return g, nil
|
||||
g, err := creator.newDirectX()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, fmt.Errorf("ui: %s=%s is specified but DirectX is not available.", envName, env)
|
||||
if g == nil {
|
||||
return nil, fmt.Errorf("ui: %s=%s is specified but DirectX is not available.", envName, env)
|
||||
}
|
||||
return g, nil
|
||||
case "metal":
|
||||
g, err := creator.newMetal()
|
||||
if err != nil {
|
||||
|
@ -31,8 +31,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||
return opengl.NewGraphics()
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
|
||||
return nil
|
||||
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
|
||||
|
@ -35,8 +35,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||
return opengl.NewGraphics()
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
|
||||
return nil
|
||||
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
|
||||
|
@ -276,8 +276,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||
return opengl.NewGraphics()
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
|
||||
return nil
|
||||
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
|
||||
|
@ -43,8 +43,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||
return opengl.NewGraphics()
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
|
||||
return nil
|
||||
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
|
||||
|
@ -36,24 +36,26 @@ type graphicsDriverCreatorImpl struct {
|
||||
}
|
||||
|
||||
func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) {
|
||||
if d := g.getDirectX(); d != nil {
|
||||
d, err1 := g.newDirectX()
|
||||
if err1 == nil {
|
||||
return d, nil
|
||||
}
|
||||
return g.newOpenGL()
|
||||
o, err2 := g.newOpenGL()
|
||||
if err2 == nil {
|
||||
return o, nil
|
||||
}
|
||||
return nil, fmt.Errorf("ui: failed to choose graphics drivers: DirectX: %v, OpenGL: %v", err1, err2)
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||
return opengl.NewGraphics()
|
||||
}
|
||||
|
||||
func (g *graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
|
||||
func (g *graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
|
||||
if g.transparent {
|
||||
return nil
|
||||
return nil, fmt.Errorf("ui: DirectX is not available with a transparent window")
|
||||
}
|
||||
if d := directx.Get(); d != nil {
|
||||
return d
|
||||
}
|
||||
return nil
|
||||
return directx.NewGraphics()
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
|
||||
|
@ -45,8 +45,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||
return opengl.NewGraphics()
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
|
||||
return nil
|
||||
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (g *graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
|
||||
|
@ -35,8 +35,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
||||
return opengl.NewGraphics()
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
|
||||
return nil
|
||||
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user