ebiten/internal/ui/egl_nintendosdk.go
Hajime Hoshi 312dce950c internal/ui: bug fix: compile failures with -tags=nintendosdk
Needed to use NativeDisplayType for eglGetDisplay.
2023-01-03 20:38:45 +09:00

94 lines
2.7 KiB
Go

// 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.NativeDisplayType(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.EGLSurface(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.EGLContext(C.EGL_NO_CONTEXT), &contextAttribs[0])
if e.context == C.EGLContext(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)
}