diff --git a/internal/ui/egl_nintendosdk.go b/internal/graphicsdriver/opengl/egl_nintendosdk.go similarity index 90% rename from internal/ui/egl_nintendosdk.go rename to internal/graphicsdriver/opengl/egl_nintendosdk.go index d7de4e386..937545d11 100644 --- a/internal/ui/egl_nintendosdk.go +++ b/internal/graphicsdriver/opengl/egl_nintendosdk.go @@ -14,8 +14,11 @@ //go:build nintendosdk -package ui +package opengl +// #cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all +// #cgo darwin LDFLAGS: -Wl,-undefined,dynamic_lookup +// // #include // #include import "C" @@ -30,7 +33,7 @@ type egl struct { context C.EGLContext } -func (e *egl) init(nativeWindowHandle C.NativeWindowType) error { +func (e *egl) init(nativeWindowHandle uintptr) error { // Initialize EGL e.display = C.eglGetDisplay(C.NativeDisplayType(C.EGL_DEFAULT_DISPLAY)) if e.display == 0 { @@ -58,7 +61,7 @@ func (e *egl) init(nativeWindowHandle C.NativeWindowType) error { return fmt.Errorf("ui: eglChooseConfig failed: numConfigs must be 1 but %d", numConfigs) } - e.surface = C.eglCreateWindowSurface(e.display, config, nativeWindowHandle, nil) + e.surface = C.eglCreateWindowSurface(e.display, config, C.NativeWindowType(nativeWindowHandle), nil) if e.surface == C.EGLSurface(C.EGL_NO_SURFACE) { return fmt.Errorf("ui: eglCreateWindowSurface failed") } diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index d18b9e9ba..bf177e7d5 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -76,7 +76,9 @@ func (g *Graphics) End(present bool) error { // The last uniforms must be reset before swapping the buffer (#2517). if present { g.state.resetLastUniforms() + g.swapBuffers() } + return nil } @@ -157,7 +159,11 @@ func (g *Graphics) removeImage(img *Image) { } func (g *Graphics) Initialize() error { - return g.state.reset(&g.context) + g.makeContextCurrent() + if err := g.state.reset(&g.context); err != nil { + return err + } + return nil } // Reset resets or initializes the current OpenGL state. diff --git a/internal/graphicsdriver/opengl/graphics_default.go b/internal/graphicsdriver/opengl/graphics_default.go index 3810f0647..fdc694cd0 100644 --- a/internal/graphicsdriver/opengl/graphics_default.go +++ b/internal/graphicsdriver/opengl/graphics_default.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build !android && !ios && !js +//go:build !android && !ios && !js && !nintendosdk package opengl @@ -38,3 +38,11 @@ func NewGraphics() (graphicsdriver.Graphics, error) { return newGraphics(ctx), nil } + +func (g *Graphics) makeContextCurrent() { + // TODO: Implement this (#2714). +} + +func (g *Graphics) swapBuffers() { + // TODO: Implement this (#2714). +} diff --git a/internal/graphicsdriver/opengl/graphics_js.go b/internal/graphicsdriver/opengl/graphics_js.go index 5187a9b04..320d3603a 100644 --- a/internal/graphicsdriver/opengl/graphics_js.go +++ b/internal/graphicsdriver/opengl/graphics_js.go @@ -45,3 +45,9 @@ func NewGraphics(canvas js.Value) (graphicsdriver.Graphics, error) { return newGraphics(ctx), nil } + +func (g *Graphics) makeContextCurrent() { +} + +func (g *Graphics) swapBuffers() { +} diff --git a/internal/graphicsdriver/opengl/graphics_mobile.go b/internal/graphicsdriver/opengl/graphics_mobile.go index d8be6a5f2..0b212eae1 100644 --- a/internal/graphicsdriver/opengl/graphics_mobile.go +++ b/internal/graphicsdriver/opengl/graphics_mobile.go @@ -39,3 +39,11 @@ func NewGraphics(context mgl.Context) (graphicsdriver.Graphics, error) { return newGraphics(ctx), nil } + +func (g *Graphics) makeContextCurrent() { + // TODO: Implement this (#2714). +} + +func (g *Graphics) swapBuffers() { + // TODO: Implement this (#2714). +} diff --git a/internal/graphicsdriver/opengl/graphics_nintendosdk.go b/internal/graphicsdriver/opengl/graphics_nintendosdk.go new file mode 100644 index 000000000..b4b7dc1ef --- /dev/null +++ b/internal/graphicsdriver/opengl/graphics_nintendosdk.go @@ -0,0 +1,44 @@ +// Copyright 2023 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 nintendosdk + +package opengl + +import ( + "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" + "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl" +) + +var ( + theEGL egl +) + +func NewGraphics(nativeWindowType uintptr) (graphicsdriver.Graphics, error) { + theEGL.init(nativeWindowType) + + ctx, err := gl.NewDefaultContext() + if err != nil { + return nil, err + } + return newGraphics(ctx), nil +} + +func (g *Graphics) makeContextCurrent() { + theEGL.makeContextCurrent() +} + +func (g *Graphics) swapBuffers() { + theEGL.swapBuffers() +} diff --git a/internal/ui/ui_nintendosdk.go b/internal/ui/ui_nintendosdk.go index 29d871cda..a6f922bce 100644 --- a/internal/ui/ui_nintendosdk.go +++ b/internal/ui/ui_nintendosdk.go @@ -30,15 +30,17 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl" ) -type graphicsDriverCreatorImpl struct{} +type graphicsDriverCreatorImpl struct { + nativeWindow C.NativeWindowType +} func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) { graphics, err := g.newOpenGL() return graphics, GraphicsLibraryOpenGL, err } -func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { - return opengl.NewGraphics() +func (g *graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { + return opengl.NewGraphics(uintptr(g.nativeWindow)) } func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) { @@ -66,8 +68,6 @@ type userInterfaceImpl struct { inputState InputState nativeTouches []C.struct_Touch - egl egl - m sync.Mutex } @@ -76,34 +76,26 @@ func (u *UserInterface) init() error { } func (u *UserInterface) initOnMainThread(options *RunOptions) error { - g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{}, options.GraphicsLibrary) + n := C.ebitengine_Initialize() + g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{ + nativeWindow: n, + }, options.GraphicsLibrary) if err != nil { return err } u.graphicsDriver = g u.setGraphicsLibrary(lib) - n := C.ebitengine_Initialize() - if err := u.egl.init(n); err != nil { - return err - } - initializeProfiler() return nil } func (u *UserInterface) loopGame() error { - u.renderThread.Call(func() { - u.egl.makeContextCurrent() - }) - for { recordProfilerHeartbeat() - if err := u.context.updateFrame(u.graphicsDriver, float64(C.kScreenWidth), float64(C.kScreenHeight), deviceScaleFactor, u, func() { - u.egl.swapBuffers() - }); err != nil { + if err := u.context.updateFrame(u.graphicsDriver, float64(C.kScreenWidth), float64(C.kScreenHeight), deviceScaleFactor, u, nil); err != nil { return err } }