2022-06-16 19:02:29 +02:00
|
|
|
// Copyright 2022 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.
|
|
|
|
|
2024-10-12 07:51:20 +02:00
|
|
|
//go:build (freebsd || linux || netbsd || openbsd) && !android && !nintendosdk && !playstation5
|
2022-06-16 19:02:29 +02:00
|
|
|
|
2022-11-13 18:46:09 +01:00
|
|
|
package opengl
|
2022-06-16 19:02:29 +02:00
|
|
|
|
2022-09-12 16:28:43 +02:00
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
2023-12-19 17:52:03 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
|
2022-09-12 16:28:43 +02:00
|
|
|
)
|
|
|
|
|
2023-12-23 11:17:50 +01:00
|
|
|
type graphicsPlatform struct {
|
|
|
|
window *glfw.Window
|
|
|
|
}
|
|
|
|
|
2023-12-19 17:52:03 +01:00
|
|
|
// 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) {
|
|
|
|
ctx, err := gl.NewDefaultContext()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-19 19:06:58 +01:00
|
|
|
if err := setGLFWClientAPI(ctx.IsES()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-19 18:38:09 +01:00
|
|
|
|
2023-12-19 18:46:31 +01:00
|
|
|
return newGraphics(ctx), nil
|
2023-12-19 17:52:03 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 18:46:31 +01:00
|
|
|
func setGLFWClientAPI(isES bool) error {
|
|
|
|
if isES {
|
2023-10-03 16:07:53 +02:00
|
|
|
if err := glfw.WindowHint(glfw.ClientAPI, glfw.OpenGLESAPI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := glfw.WindowHint(glfw.ContextVersionMajor, 3); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := glfw.WindowHint(glfw.ContextVersionMinor, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := glfw.WindowHint(glfw.ContextCreationAPI, glfw.EGLContextAPI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-11-13 18:46:09 +01:00
|
|
|
}
|
|
|
|
|
2023-10-03 16:07:53 +02:00
|
|
|
if err := glfw.WindowHint(glfw.ClientAPI, glfw.OpenGLAPI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := glfw.WindowHint(glfw.ContextVersionMajor, 3); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := glfw.WindowHint(glfw.ContextVersionMinor, 2); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-06-16 19:02:29 +02:00
|
|
|
}
|
2023-12-19 18:46:31 +01:00
|
|
|
|
2023-12-23 11:17:50 +01:00
|
|
|
func (g *Graphics) SetGLFWWindow(window *glfw.Window) {
|
|
|
|
g.window = window
|
|
|
|
}
|
|
|
|
|
2023-12-23 11:28:17 +01:00
|
|
|
func (g *Graphics) makeContextCurrent() error {
|
2023-12-23 11:17:50 +01:00
|
|
|
return g.window.MakeContextCurrent()
|
2023-12-19 18:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) swapBuffers() error {
|
|
|
|
// Call SwapIntervals even though vsync is not changed.
|
|
|
|
// When toggling to fullscreen, vsync state might be reset unexpectedly (#1787).
|
|
|
|
|
|
|
|
// SwapInterval is affected by the current monitor of the window.
|
|
|
|
// This needs to be called at least after SetMonitor.
|
|
|
|
// Without SwapInterval after SetMonitor, vsynch doesn't work (#375).
|
|
|
|
if g.vsync {
|
2024-10-12 16:01:59 +02:00
|
|
|
if err := g.window.SwapInterval(1); err != nil {
|
2023-12-19 18:46:31 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2024-10-12 16:01:59 +02:00
|
|
|
if err := g.window.SwapInterval(0); err != nil {
|
2023-12-19 18:46:31 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 11:17:50 +01:00
|
|
|
if err := g.window.SwapBuffers(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-19 18:46:31 +01:00
|
|
|
return nil
|
|
|
|
}
|