2023-08-03 16:51:17 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2023-07-07 19:15:47 +02:00
|
|
|
// SPDX-FileCopyrightText: 2012 The glfw3-go Authors
|
|
|
|
// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
|
|
|
|
|
2023-07-08 06:59:51 +02:00
|
|
|
//go:build darwin || freebsd || linux || netbsd || openbsd
|
2023-07-05 03:51:55 +02:00
|
|
|
|
2023-10-07 16:21:29 +02:00
|
|
|
package glfw
|
2023-07-05 03:51:55 +02:00
|
|
|
|
|
|
|
//#include <stdlib.h>
|
|
|
|
//#define GLFW_INCLUDE_NONE
|
2023-10-07 09:36:55 +02:00
|
|
|
//#include "glfw3_unix.h"
|
2023-07-05 03:51:55 +02:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MakeContextCurrent makes the context of the window current.
|
|
|
|
// Originally GLFW 3 passes a null pointer to detach the context.
|
2023-12-17 14:42:34 +01:00
|
|
|
// But since we're using receivers, DetachCurrentContext should
|
2023-07-05 03:51:55 +02:00
|
|
|
// be used instead.
|
2023-10-07 11:47:41 +02:00
|
|
|
func (w *Window) MakeContextCurrent() error {
|
2023-07-05 03:51:55 +02:00
|
|
|
C.glfwMakeContextCurrent(w.data)
|
2023-10-07 11:47:41 +02:00
|
|
|
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-05 03:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DetachCurrentContext detaches the current context.
|
2023-10-07 11:47:41 +02:00
|
|
|
func DetachCurrentContext() error {
|
2023-07-05 03:51:55 +02:00
|
|
|
C.glfwMakeContextCurrent(nil)
|
2023-10-07 11:47:41 +02:00
|
|
|
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-05 03:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentContext returns the window whose context is current.
|
2023-10-07 11:47:41 +02:00
|
|
|
func GetCurrentContext() (*Window, error) {
|
2023-07-05 03:51:55 +02:00
|
|
|
w := C.glfwGetCurrentContext()
|
2023-10-07 11:47:41 +02:00
|
|
|
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-05 03:51:55 +02:00
|
|
|
if w == nil {
|
2023-10-07 11:47:41 +02:00
|
|
|
return nil, nil
|
2023-07-05 03:51:55 +02:00
|
|
|
}
|
2023-10-07 11:47:41 +02:00
|
|
|
return windows.get(w), nil
|
2023-07-05 03:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SwapBuffers swaps the front and back buffers of the window. If the
|
|
|
|
// swap interval is greater than zero, the GPU driver waits the specified number
|
|
|
|
// of screen updates before swapping the buffers.
|
2023-10-07 11:47:41 +02:00
|
|
|
func (w *Window) SwapBuffers() error {
|
2023-07-05 03:51:55 +02:00
|
|
|
C.glfwSwapBuffers(w.data)
|
2023-10-07 11:47:41 +02:00
|
|
|
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-05 03:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SwapInterval sets the swap interval for the current context, i.e. the number
|
|
|
|
// of screen updates to wait before swapping the buffers of a window and
|
|
|
|
// returning from SwapBuffers. This is sometimes called
|
|
|
|
// 'vertical synchronization', 'vertical retrace synchronization' or 'vsync'.
|
|
|
|
//
|
|
|
|
// Contexts that support either of the WGL_EXT_swap_control_tear and
|
|
|
|
// GLX_EXT_swap_control_tear extensions also accept negative swap intervals,
|
|
|
|
// which allow the driver to swap even if a frame arrives a little bit late.
|
|
|
|
// You can check for the presence of these extensions using
|
|
|
|
// ExtensionSupported. For more information about swap tearing,
|
|
|
|
// see the extension specifications.
|
|
|
|
//
|
|
|
|
// Some GPU drivers do not honor the requested swap interval, either because of
|
|
|
|
// user settings that override the request or due to bugs in the driver.
|
2023-10-07 11:47:41 +02:00
|
|
|
func SwapInterval(interval int) error {
|
2023-07-05 03:51:55 +02:00
|
|
|
C.glfwSwapInterval(C.int(interval))
|
2023-10-07 11:47:41 +02:00
|
|
|
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-05 03:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExtensionSupported reports whether the specified OpenGL or context creation
|
|
|
|
// API extension is supported by the current context. For example, on Windows
|
|
|
|
// both the OpenGL and WGL extension strings are checked.
|
|
|
|
//
|
|
|
|
// As this functions searches one or more extension strings on each call, it is
|
|
|
|
// recommended that you cache its results if it's going to be used frequently.
|
|
|
|
// The extension strings will not change during the lifetime of a context, so
|
|
|
|
// there is no danger in doing this.
|
2023-10-07 11:47:41 +02:00
|
|
|
func ExtensionSupported(extension string) (bool, error) {
|
2023-07-05 03:51:55 +02:00
|
|
|
e := C.CString(extension)
|
|
|
|
defer C.free(unsafe.Pointer(e))
|
2023-07-07 19:34:56 +02:00
|
|
|
ret := C.glfwExtensionSupported(e) != 0
|
2023-10-07 11:47:41 +02:00
|
|
|
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return ret, nil
|
2023-07-05 03:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetProcAddress returns the address of the specified OpenGL or OpenGL ES core
|
|
|
|
// or extension function, if it is supported by the current context.
|
|
|
|
//
|
|
|
|
// A context must be current on the calling thread. Calling this function
|
|
|
|
// without a current context will cause a GLFW_NO_CURRENT_CONTEXT error.
|
|
|
|
//
|
|
|
|
// This function is used to provide GL proc resolving capabilities to an
|
|
|
|
// external C library.
|
2023-10-07 11:47:41 +02:00
|
|
|
func GetProcAddress(procname string) (unsafe.Pointer, error) {
|
2023-07-05 03:51:55 +02:00
|
|
|
p := C.CString(procname)
|
|
|
|
defer C.free(unsafe.Pointer(p))
|
|
|
|
ret := unsafe.Pointer(C.glfwGetProcAddress(p))
|
2023-10-07 11:47:41 +02:00
|
|
|
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ret, nil
|
2023-07-05 03:51:55 +02:00
|
|
|
}
|