internal/graphicsdriver/opengl: move egl to the package opengl

Updates #2714
This commit is contained in:
Hajime Hoshi 2023-12-18 01:09:05 +09:00
parent 425b4dd99a
commit 6fd18150d8
7 changed files with 90 additions and 23 deletions

View File

@ -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 <EGL/egl.h>
// #include <EGL/eglext.h>
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")
}

View File

@ -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.

View File

@ -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).
}

View File

@ -45,3 +45,9 @@ func NewGraphics(canvas js.Value) (graphicsdriver.Graphics, error) {
return newGraphics(ctx), nil
}
func (g *Graphics) makeContextCurrent() {
}
func (g *Graphics) swapBuffers() {
}

View File

@ -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).
}

View File

@ -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()
}

View File

@ -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
}
}