2018-12-30 07:35:49 +01:00
|
|
|
// Copyright 2018 The Ebiten 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 glfw
|
|
|
|
|
|
|
|
import (
|
2022-05-18 15:27:18 +02:00
|
|
|
"errors"
|
2018-12-30 07:35:49 +01:00
|
|
|
"image"
|
2018-12-30 20:59:06 +01:00
|
|
|
"image/draw"
|
|
|
|
|
2022-05-18 15:27:18 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/glfwwin"
|
2018-12-30 07:35:49 +01:00
|
|
|
)
|
|
|
|
|
2022-05-18 15:27:18 +02:00
|
|
|
type Cursor glfwwin.Cursor
|
2021-04-11 07:21:38 +02:00
|
|
|
|
|
|
|
func CreateStandardCursor(shape StandardCursor) *Cursor {
|
2022-05-18 15:27:18 +02:00
|
|
|
c, err := glfwwin.CreateStandardCursor(glfwwin.StandardCursor(shape))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return (*Cursor)(c)
|
2021-04-11 07:21:38 +02:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:27:18 +02:00
|
|
|
type Monitor glfwwin.Monitor
|
2018-12-30 07:35:49 +01:00
|
|
|
|
2022-12-08 05:39:15 +01:00
|
|
|
func (m *Monitor) GetContentScale() (float32, float32, error) {
|
|
|
|
return (*glfwwin.Monitor)(m).GetContentScale()
|
2021-09-14 05:35:02 +02:00
|
|
|
}
|
|
|
|
|
2019-02-24 15:10:14 +01:00
|
|
|
func (m *Monitor) GetPos() (int, int) {
|
2022-05-18 15:27:18 +02:00
|
|
|
x, y, err := (*glfwwin.Monitor)(m).GetPos()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return x, y
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Monitor) GetVideoMode() *VidMode {
|
2022-05-18 15:27:18 +02:00
|
|
|
v, err := (*glfwwin.Monitor)(m).GetVideoMode()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
2022-05-18 15:27:18 +02:00
|
|
|
return (*VidMode)(v)
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:27:18 +02:00
|
|
|
type Window glfwwin.Window
|
2018-12-30 07:35:49 +01:00
|
|
|
|
|
|
|
func (w *Window) Destroy() {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).Destroy(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
2022-08-12 15:34:17 +02:00
|
|
|
func (w *Window) Focus() {
|
|
|
|
if err := (*glfwwin.Window)(w).Focus(); err != nil {
|
2022-05-18 15:27:18 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
2022-08-12 15:34:17 +02:00
|
|
|
func (w *Window) GetAttrib(attrib Hint) int {
|
|
|
|
r, err := (*glfwwin.Window)(w).GetAttrib(glfwwin.Hint(attrib))
|
|
|
|
if err != nil {
|
2022-05-18 15:27:18 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2022-08-12 15:34:17 +02:00
|
|
|
return r
|
2019-11-26 03:45:52 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 07:35:49 +01:00
|
|
|
func (w *Window) GetCursorPos() (x, y float64) {
|
2022-05-18 15:27:18 +02:00
|
|
|
x, y, err := (*glfwwin.Window)(w).GetCursorPos()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return x, y
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetInputMode(mode InputMode) int {
|
2022-05-18 15:27:18 +02:00
|
|
|
r, err := (*glfwwin.Window)(w).GetInputMode(glfwwin.InputMode(mode))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return r
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetKey(key Key) Action {
|
2022-05-18 15:27:18 +02:00
|
|
|
r, err := (*glfwwin.Window)(w).GetKey(glfwwin.Key(key))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
return Action(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetMonitor() *Monitor {
|
2022-05-18 15:27:18 +02:00
|
|
|
m, err := (*glfwwin.Window)(w).GetMonitor()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
2022-05-18 15:27:18 +02:00
|
|
|
return (*Monitor)(m)
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetMouseButton(button MouseButton) Action {
|
2022-05-18 15:27:18 +02:00
|
|
|
r, err := (*glfwwin.Window)(w).GetMouseButton(glfwwin.MouseButton(button))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
return Action(r)
|
|
|
|
}
|
|
|
|
|
2019-02-24 15:10:14 +01:00
|
|
|
func (w *Window) GetPos() (int, int) {
|
2022-05-18 15:27:18 +02:00
|
|
|
x, y, err := (*glfwwin.Window)(w).GetPos()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return x, y
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 15:10:14 +01:00
|
|
|
func (w *Window) GetSize() (int, int) {
|
2022-05-18 15:27:18 +02:00
|
|
|
width, height, err := (*glfwwin.Window)(w).GetSize()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return width, height
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
2021-09-24 19:46:18 +02:00
|
|
|
func (w *Window) Hide() {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).Hide(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-09-24 19:46:18 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 11:26:28 +01:00
|
|
|
func (w *Window) Iconify() {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).Iconify(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-21 11:26:28 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 07:35:49 +01:00
|
|
|
func (w *Window) MakeContextCurrent() {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).MakeContextCurrent(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 11:26:28 +01:00
|
|
|
func (w *Window) Maximize() {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).Maximize(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-21 11:26:28 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 14:16:23 +01:00
|
|
|
func (w *Window) Restore() {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).Restore(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-21 14:16:23 +01:00
|
|
|
}
|
|
|
|
|
2022-08-12 15:34:17 +02:00
|
|
|
func (w *Window) SetAttrib(attrib Hint, value int) {
|
|
|
|
if err := (*glfwwin.Window)(w).SetAttrib(glfwwin.Hint(attrib), value); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 07:35:49 +01:00
|
|
|
func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsCallback) {
|
2022-05-18 15:27:18 +02:00
|
|
|
f, err := (*glfwwin.Window)(w).SetCharModsCallback(cbfun)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return f
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
2021-06-13 13:52:14 +02:00
|
|
|
func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback) {
|
2022-05-18 15:27:18 +02:00
|
|
|
f, err := (*glfwwin.Window)(w).SetCloseCallback(cbfun)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return f
|
2021-06-13 13:52:14 +02:00
|
|
|
}
|
|
|
|
|
2021-04-11 07:21:38 +02:00
|
|
|
func (w *Window) SetCursor(cursor *Cursor) {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).SetCursor((*glfwwin.Cursor)(cursor)); err != nil {
|
|
|
|
panic(err)
|
2021-04-11 07:21:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 07:35:49 +01:00
|
|
|
func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) {
|
2022-05-18 15:27:18 +02:00
|
|
|
f, err := (*glfwwin.Window)(w).SetFramebufferSizeCallback(cbfun)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return f
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallback) {
|
2022-05-18 15:27:18 +02:00
|
|
|
f, err := (*glfwwin.Window)(w).SetScrollCallback(cbfun)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return f
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
2021-06-13 13:52:14 +02:00
|
|
|
func (w *Window) SetShouldClose(value bool) {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).SetShouldClose(value); err != nil {
|
|
|
|
panic(err)
|
2021-06-13 13:52:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 08:52:09 +01:00
|
|
|
func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback) {
|
2022-05-18 15:27:18 +02:00
|
|
|
f, err := (*glfwwin.Window)(w).SetSizeCallback(cbfun)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return f
|
2019-01-09 02:34:54 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 21:24:13 +02:00
|
|
|
func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).SetSizeLimits(minw, minh, maxw, maxh); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-04-16 21:24:13 +02:00
|
|
|
}
|
|
|
|
|
2022-02-09 11:36:42 +01:00
|
|
|
func (w *Window) SetAspectRatio(numer, denom int) {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).SetAspectRatio(numer, denom); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-02-09 11:11:25 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 07:35:49 +01:00
|
|
|
func (w *Window) SetIcon(images []image.Image) {
|
2022-05-18 15:27:18 +02:00
|
|
|
gimgs := make([]*glfwwin.Image, len(images))
|
2018-12-30 20:59:06 +01:00
|
|
|
for i, img := range images {
|
|
|
|
b := img.Bounds()
|
|
|
|
m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
|
|
|
|
draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
|
2022-05-18 15:27:18 +02:00
|
|
|
gimgs[i] = &glfwwin.Image{
|
|
|
|
Width: b.Dx(),
|
|
|
|
Height: b.Dy(),
|
|
|
|
Pixels: m.Pix,
|
|
|
|
}
|
2018-12-30 20:59:06 +01:00
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).SetIcon(gimgs); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetInputMode(mode InputMode, value int) {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).SetInputMode(glfwwin.InputMode(mode), value); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetMonitor(monitor *Monitor, xpos, ypos, width, height, refreshRate int) {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).SetMonitor((*glfwwin.Monitor)(monitor), xpos, ypos, width, height, refreshRate); err != nil {
|
|
|
|
panic(err)
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetPos(xpos, ypos int) {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).SetPos(xpos, ypos); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetSize(width, height int) {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).SetSize(width, height); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetTitle(title string) {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).SetTitle(title); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) ShouldClose() bool {
|
2022-05-18 15:27:18 +02:00
|
|
|
r, err := (*glfwwin.Window)(w).ShouldClose()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return r
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) Show() {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).Show(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SwapBuffers() {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := (*glfwwin.Window)(w).SwapBuffers(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateWindow(width, height int, title string, monitor *Monitor, share *Window) (*Window, error) {
|
2022-05-18 15:27:18 +02:00
|
|
|
w, err := glfwwin.CreateWindow(width, height, title, (*glfwwin.Monitor)(monitor), (*glfwwin.Window)(share))
|
|
|
|
// TODO: acceptError(APIUnavailable, VersionUnavailable)?
|
|
|
|
return (*Window)(w), err
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
2022-08-13 06:33:52 +02:00
|
|
|
func GetKeyName(key Key, scancode int) string {
|
|
|
|
name, err := glfwwin.GetKeyName(glfwwin.Key(key), scancode)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2018-12-30 07:35:49 +01:00
|
|
|
func GetMonitors() []*Monitor {
|
2022-05-18 15:27:18 +02:00
|
|
|
ms, err := glfwwin.GetMonitors()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
result := make([]*Monitor, 0, len(ms))
|
|
|
|
for _, m := range ms {
|
|
|
|
result = append(result, (*Monitor)(m))
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
2022-05-18 15:27:18 +02:00
|
|
|
return result
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetPrimaryMonitor() *Monitor {
|
2022-05-18 15:27:18 +02:00
|
|
|
m, err := glfwwin.GetPrimaryMonitor()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
2022-05-18 15:27:18 +02:00
|
|
|
return (*Monitor)(m)
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Init() error {
|
2021-07-14 16:52:27 +02:00
|
|
|
// InvalidValue can happen when specific joysticks are used. This issue
|
2021-07-14 18:02:54 +02:00
|
|
|
// will be fixed in GLFW 3.3.5. As a temporary fix, ignore this error.
|
2021-07-14 16:52:27 +02:00
|
|
|
// See go-gl/glfw#292, go-gl/glfw#324, and glfw/glfw#1763
|
|
|
|
// (#1229).
|
2022-05-18 15:27:18 +02:00
|
|
|
// TODO: acceptError(APIUnavailable, InvalidValue)?
|
|
|
|
err := glfwwin.Init()
|
|
|
|
if err != nil && !errors.Is(err, glfwwin.InvalidValue) {
|
|
|
|
return err
|
2021-07-14 18:02:54 +02:00
|
|
|
}
|
2022-05-18 15:27:18 +02:00
|
|
|
return nil
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:27:18 +02:00
|
|
|
func PollEvents() {
|
|
|
|
if err := glfwwin.PollEvents(); err != nil && !errors.Is(err, glfwwin.InvalidValue) {
|
2021-07-15 04:19:39 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:07:28 +02:00
|
|
|
func PostEmptyEvent() {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := glfwwin.PostEmptyEvent(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-07-22 18:07:28 +02:00
|
|
|
}
|
|
|
|
|
2022-05-18 18:49:21 +02:00
|
|
|
func SetMonitorCallback(cbfun MonitorCallback) MonitorCallback {
|
2022-05-18 15:27:18 +02:00
|
|
|
f, err := glfwwin.SetMonitorCallback(cbfun)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return f
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func SwapInterval(interval int) {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := glfwwin.SwapInterval(interval); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Terminate() {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := glfwwin.Terminate(); err != nil {
|
2018-12-30 07:35:49 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:07:28 +02:00
|
|
|
func WaitEvents() {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := glfwwin.WaitEvents(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-07-22 18:07:28 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 07:35:49 +01:00
|
|
|
func WindowHint(target Hint, hint int) {
|
2022-05-18 15:27:18 +02:00
|
|
|
if err := glfwwin.WindowHint(glfwwin.Hint(target), hint); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 07:35:49 +01:00
|
|
|
}
|