internal/ui: use -overlay to provide the implementation for Nintendo Switch

Closes #2372
This commit is contained in:
Hajime Hoshi 2023-01-02 00:55:35 +09:00
parent edf35b02cb
commit 0e0205b2d0
7 changed files with 195 additions and 25 deletions

2
doc.go
View File

@ -104,4 +104,6 @@
// `microsoftgdk` is for Microsoft GDK (e.g. Xbox). // `microsoftgdk` is for Microsoft GDK (e.g. Xbox).
// //
// `nintendosdk` is for NintendoSDK (e.g. Nintendo Switch). // `nintendosdk` is for NintendoSDK (e.g. Nintendo Switch).
//
// `nintendosdkprofile` enables a profiler for NintendoSDK.
package ebiten package ebiten

View File

@ -0,0 +1,93 @@
// 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 ui
// #include <EGL/egl.h>
// #include <EGL/eglext.h>
import "C"
import (
"fmt"
)
type egl struct {
display C.EGLDisplay
surface C.EGLSurface
context C.EGLContext
}
func (e *egl) init(nativeWindowHandle C.NativeWindowType) error {
// Initialize EGL
e.display = C.eglGetDisplay(C.EGL_DEFAULT_DISPLAY)
if e.display == 0 {
return fmt.Errorf("ui: eglGetDisplay failed")
}
if r := C.eglInitialize(e.display, nil, nil); r == 0 {
return fmt.Errorf("ui: eglInitialize failed")
}
configAttribs := []C.EGLint{
C.EGL_RENDERABLE_TYPE, C.EGL_OPENGL_BIT,
C.EGL_SURFACE_TYPE, C.EGL_WINDOW_BIT,
C.EGL_RED_SIZE, 8,
C.EGL_GREEN_SIZE, 8,
C.EGL_BLUE_SIZE, 8,
C.EGL_ALPHA_SIZE, 8,
C.EGL_NONE}
var numConfigs C.EGLint
var config C.EGLConfig
if r := C.eglChooseConfig(e.display, &configAttribs[0], &config, 1, &numConfigs); r == 0 {
return fmt.Errorf("ui: eglChooseConfig failed")
}
if numConfigs != 1 {
return fmt.Errorf("ui: eglChooseConfig failed: numConfigs must be 1 but %d", numConfigs)
}
e.surface = C.eglCreateWindowSurface(e.display, config, nativeWindowHandle, nil)
if e.surface == C.EGL_NO_SURFACE {
return fmt.Errorf("ui: eglCreateWindowSurface failed")
}
// Set the current rendering API.
if r := C.eglBindAPI(C.EGL_OPENGL_API); r == 0 {
return fmt.Errorf("ui: eglBindAPI failed")
}
// Create new context and set it as current.
contextAttribs := []C.EGLint{
// Set target garaphics api version.
C.EGL_CONTEXT_MAJOR_VERSION, 2,
C.EGL_CONTEXT_MINOR_VERSION, 1,
// For debug callback
C.EGL_CONTEXT_FLAGS_KHR, C.EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR,
C.EGL_NONE}
e.context = C.eglCreateContext(e.display, config, C.EGL_NO_CONTEXT, &contextAttribs[0])
if e.context == C.EGL_NO_CONTEXT {
return fmt.Errorf("ui: eglCreateContext failed: error: %d", C.eglGetError())
}
if r := C.eglMakeCurrent(e.display, e.surface, e.surface, e.context); r == 0 {
return fmt.Errorf("ui: eglMakeCurrent failed")
}
return nil
}
func (e *egl) swapBuffers() {
C.eglSwapBuffers(e.display, e.surface)
}

View File

@ -1,4 +1,4 @@
// Copyright 2021 The Ebiten Authors // Copyright 2023 The Ebitengine Authors
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -14,27 +14,12 @@
//go:build nintendosdk //go:build nintendosdk
package nintendosdk // The actual implementaiton will be provided by -overlay.
// #cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all #include "init_nintendosdk.h"
// #cgo darwin LDFLAGS: -Wl,-undefined,dynamic_lookup
//
// #include <stdint.h>
//
// // UI
// void EbitenInitializeGame();
// void EbitenBeginFrame();
// void EbitenEndFrame();
import "C"
func InitializeGame() { extern "C" NativeWindowType ebitengine_Init() {}
C.EbitenInitializeGame()
}
func BeginFrame() { extern "C" void ebitengine_InitializeProfiler() {}
C.EbitenBeginFrame()
}
func EndFrame() { extern "C" void ebitengine_RecordProfilerHeartbeat() {}
C.EbitenEndFrame()
}

View File

@ -0,0 +1,29 @@
// 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
#include <EGL/egl.h>
#ifdef __cplusplus
extern "C" {
#endif
NativeWindowType ebitengine_Init();
void ebitengine_InitializeProfiler();
void ebitengine_RecordProfilerHeartbeat();
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -0,0 +1,23 @@
// 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 && !nintendosdkprofile
package ui
func initializeProfiler() {
}
func recordProfilerHeartbeat() {
}

View File

@ -0,0 +1,28 @@
// 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 && nintendosdkprofile
package ui
// #include "init_nintendosdk.h"
import "C"
func initializeProfiler() {
C.ebitengine_InitializeProfiler()
}
func recordProfilerHeartbeat() {
C.ebitengine_RecordProfilerHeartbeat()
}

View File

@ -16,6 +16,7 @@
package ui package ui
// #include "init_nintendosdk.h"
// #include "input_nintendosdk.h" // #include "input_nintendosdk.h"
import "C" import "C"
@ -25,7 +26,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/gamepad" "github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
"github.com/hajimehoshi/ebiten/v2/internal/nintendosdk"
) )
type graphicsDriverCreatorImpl struct{} type graphicsDriverCreatorImpl struct{}
@ -59,6 +59,8 @@ type userInterfaceImpl struct {
context *context context *context
inputState InputState inputState InputState
nativeTouches []C.struct_Touch nativeTouches []C.struct_Touch
egl egl
} }
func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error { func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
@ -68,10 +70,18 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
return err return err
} }
u.graphicsDriver = g u.graphicsDriver = g
nintendosdk.InitializeGame()
n := C.ebitengine_Init()
if err := u.egl.init(n); err != nil {
return err
}
initializeProfiler()
for { for {
recordProfilerHeartbeat()
// TODO: Make a separate thread for rendering (#2512). // TODO: Make a separate thread for rendering (#2512).
nintendosdk.BeginFrame()
gamepad.Update() gamepad.Update()
u.updateInputState() u.updateInputState()
@ -79,7 +89,7 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
return err return err
} }
nintendosdk.EndFrame() u.egl.swapBuffers()
} }
} }