Skip to content

Commit

Permalink
internal/graphicsdriver/opengl: refactoring: separate graphcis_*.go
Browse files Browse the repository at this point in the history
Updates #2714
  • Loading branch information
hajimehoshi committed Oct 12, 2024
1 parent 027ef33 commit 201bb36
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !android && !darwin && !js && !nintendosdk && !playstation5
//go:build (freebsd || linux || netbsd || openbsd) && !android && !nintendosdk && !playstation5

package opengl

import (
"fmt"

"github.com/hajimehoshi/ebiten/v2/internal/glfw"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
)

type graphicsPlatform struct {
Expand All @@ -32,10 +29,6 @@ type graphicsPlatform struct {
// 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) {
if microsoftgdk.IsXbox() {
return nil, fmt.Errorf("opengl: OpenGL is not supported on Xbox")
}

ctx, err := gl.NewDefaultContext()
if err != nil {
return nil, err
Expand Down
84 changes: 84 additions & 0 deletions internal/graphicsdriver/opengl/graphics_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2024 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.

package opengl

import (
"fmt"

"github.com/hajimehoshi/ebiten/v2/internal/glfw"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
)

type graphicsPlatform struct {
window *glfw.Window
}

// 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) {
if microsoftgdk.IsXbox() {
return nil, fmt.Errorf("opengl: OpenGL is not supported on Xbox")
}

ctx, err := gl.NewDefaultContext()
if err != nil {
return nil, err
}

if err := glfw.WindowHint(glfw.ClientAPI, glfw.OpenGLAPI); err != nil {
return nil, err
}
if err := glfw.WindowHint(glfw.ContextVersionMajor, 3); err != nil {
return nil, err
}
if err := glfw.WindowHint(glfw.ContextVersionMinor, 2); err != nil {
return nil, err
}

return newGraphics(ctx), nil
}

func (g *Graphics) SetGLFWWindow(window *glfw.Window) {
g.window = window
}

func (g *Graphics) makeContextCurrent() error {
return g.window.MakeContextCurrent()
}

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 {
if err := glfw.SwapInterval(1); err != nil {
return err
}
} else {
if err := glfw.SwapInterval(0); err != nil {
return err
}
}

if err := g.window.SwapBuffers(); err != nil {
return err
}
return nil
}

0 comments on commit 201bb36

Please sign in to comment.