diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index 607ad7ed5..b03477636 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -25,12 +25,15 @@ import ( // 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) { +// +// 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(); err != nil { + if err := g.init(context); err != nil { return nil, err } return g, nil diff --git a/internal/graphicsdriver/opengl/graphics_gl.go b/internal/graphicsdriver/opengl/graphics_gl.go index f4a792da5..f519982bc 100644 --- a/internal/graphicsdriver/opengl/graphics_gl.go +++ b/internal/graphicsdriver/opengl/graphics_gl.go @@ -20,6 +20,6 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl" ) -func (g *Graphics) init() error { +func (g *Graphics) init(context any) error { return gl.Init() } diff --git a/internal/graphicsdriver/opengl/graphics_gles.go b/internal/graphicsdriver/opengl/graphics_gles.go index bcb881f02..2a6a8f6da 100644 --- a/internal/graphicsdriver/opengl/graphics_gles.go +++ b/internal/graphicsdriver/opengl/graphics_gles.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build (android || ios || opengles) && !js +//go:build !android && !ios && !js && opengles package opengl @@ -20,7 +20,7 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl" ) -func (g *Graphics) init() error { +func (g *Graphics) init(context any) error { g.context.ctx = gl.DefaultContext{} return nil } diff --git a/internal/graphicsdriver/opengl/graphics_js.go b/internal/graphicsdriver/opengl/graphics_js.go index 46b7f964d..1f748449a 100644 --- a/internal/graphicsdriver/opengl/graphics_js.go +++ b/internal/graphicsdriver/opengl/graphics_js.go @@ -18,7 +18,7 @@ import ( "syscall/js" ) -func (g *Graphics) init() error { +func (g *Graphics) init(_ any) error { // Do nothing. return nil } diff --git a/internal/graphicsdriver/opengl/graphics_mobile.go b/internal/graphicsdriver/opengl/graphics_mobile.go index 58ba63170..02610f189 100644 --- a/internal/graphicsdriver/opengl/graphics_mobile.go +++ b/internal/graphicsdriver/opengl/graphics_mobile.go @@ -22,6 +22,11 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl" ) -func (g *Graphics) SetGomobileGLContext(context mgl.Context) { - g.context.ctx = gl.NewGomobileContext(context) +func (g *Graphics) init(context any) error { + if context != nil { + g.context.ctx = gl.NewGomobileContext(context.(mgl.Context)) + } else { + g.context.ctx = gl.DefaultContext{} + } + return nil } diff --git a/internal/ui/ui_android.go b/internal/ui/ui_android.go index 228ed04ee..2c7a027ce 100644 --- a/internal/ui/ui_android.go +++ b/internal/ui/ui_android.go @@ -15,12 +15,14 @@ package ui import ( + "golang.org/x/mobile/gl" + "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl" ) type graphicsDriverCreatorImpl struct { - gomobileBuild bool + gomobileContext gl.Context } func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) { @@ -28,8 +30,8 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics return graphics, GraphicsLibraryOpenGL, err } -func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { - return opengl.NewGraphics() +func (g *graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { + return opengl.NewGraphics(g.gomobileContext) } func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) { diff --git a/internal/ui/ui_glfw_darwin.go b/internal/ui/ui_glfw_darwin.go index b124646a9..aa18230bc 100644 --- a/internal/ui/ui_glfw_darwin.go +++ b/internal/ui/ui_glfw_darwin.go @@ -172,7 +172,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { - return opengl.NewGraphics() + return opengl.NewGraphics(nil) } func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) { diff --git a/internal/ui/ui_glfw_unix.go b/internal/ui/ui_glfw_unix.go index 2bc00984b..0394f16fb 100644 --- a/internal/ui/ui_glfw_unix.go +++ b/internal/ui/ui_glfw_unix.go @@ -40,7 +40,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { - return opengl.NewGraphics() + return opengl.NewGraphics(nil) } func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) { diff --git a/internal/ui/ui_glfw_windows.go b/internal/ui/ui_glfw_windows.go index 79ae17ab5..f49878bf1 100644 --- a/internal/ui/ui_glfw_windows.go +++ b/internal/ui/ui_glfw_windows.go @@ -47,7 +47,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { - return opengl.NewGraphics() + return opengl.NewGraphics(nil) } func (g *graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) { diff --git a/internal/ui/ui_ios.go b/internal/ui/ui_ios.go index cdf2d0acf..32ec17b5e 100644 --- a/internal/ui/ui_ios.go +++ b/internal/ui/ui_ios.go @@ -17,13 +17,15 @@ package ui import ( "fmt" + "golang.org/x/mobile/gl" + "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl" ) type graphicsDriverCreatorImpl struct { - gomobileBuild bool + gomobileContext gl.Context } func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) { @@ -38,8 +40,8 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics return nil, GraphicsLibraryUnknown, fmt.Errorf("ui: failed to choose graphics drivers: Metal: %v, OpenGL: %v", err1, err2) } -func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { - return opengl.NewGraphics() +func (g *graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { + return opengl.NewGraphics(g.gomobileContext) } func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) { @@ -47,7 +49,7 @@ func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) } func (g *graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) { - if g.gomobileBuild { + if g.gomobileContext != nil { return nil, fmt.Errorf("ui: Metal is not available with gomobile-build") } return metal.NewGraphics() diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index c6b47a586..76afc5bc8 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -34,7 +34,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { - return opengl.NewGraphics() + return opengl.NewGraphics(nil) } func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) { diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index 91a47f1d4..71aab8043 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -35,7 +35,6 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/gamepad" "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" - "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/restorable" "github.com/hajimehoshi/ebiten/v2/internal/thread" @@ -270,24 +269,25 @@ func (u *userInterfaceImpl) run(game Game, mainloop bool) (err error) { }() u.context = newContext(game) + + var mgl gl.Context + if mainloop { + // When gomobile-build is used, GL functions must be called via + // gl.Context so that they are called on the appropriate thread. + mgl = <-glContextCh + } else { + u.t = thread.NewOSThread() + graphicscommand.SetRenderingThread(u.t) + } + g, err := newGraphicsDriver(&graphicsDriverCreatorImpl{ - gomobileBuild: mainloop, + gomobileContext: mgl, }) if err != nil { return err } u.graphicsDriver = g - if mainloop { - // When gomobile-build is used, GL functions must be called via - // gl.Context so that they are called on the appropriate thread. - ctx := <-glContextCh - g.(*opengl.Graphics).SetGomobileGLContext(ctx) - } else { - u.t = thread.NewOSThread() - graphicscommand.SetRenderingThread(u.t) - } - // If gomobile-build is used, wait for the outside size fixed. if u.setGBuildSizeCh != nil { <-u.setGBuildSizeCh diff --git a/internal/ui/ui_nintendosdk.go b/internal/ui/ui_nintendosdk.go index 08250b317..4f661372d 100644 --- a/internal/ui/ui_nintendosdk.go +++ b/internal/ui/ui_nintendosdk.go @@ -32,7 +32,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { - return opengl.NewGraphics() + return opengl.NewGraphics(nil) } func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {