go-opengl-pixel/pixelgl/window.go

432 lines
9.7 KiB
Go
Raw Normal View History

package pixelgl
2016-11-23 16:12:23 -06:00
import (
2016-11-24 08:13:05 -06:00
"image/color"
2016-11-23 17:43:00 -06:00
"runtime"
2017-02-11 07:09:47 -06:00
"github.com/faiface/glhf"
"github.com/faiface/mainthread"
"github.com/faiface/pixel"
2016-11-23 16:12:23 -06:00
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/pkg/errors"
)
2017-01-10 17:36:54 -06:00
// WindowConfig is a structure for specifying all possible properties of a window. Properties are
// chosen in such a way, that you usually only need to set a few of them - defaults (zeros) should
// usually be sensible.
2016-11-24 07:27:43 -06:00
//
// Note that you always need to set the width and the height of a window.
2016-11-23 16:12:23 -06:00
type WindowConfig struct {
// Title at the top of a window.
Title string
// Width of a window in pixels.
Width float64
// Height of a window in pixels.
Height float64
// If set to nil, a window will be windowed. Otherwise it will be fullscreen on the
// specified monitor.
Fullscreen *Monitor
// Whether a window is resizable.
Resizable bool
// If set to true, the window will be initially invisible.
Hidden bool
// Undecorated window ommits the borders and decorations (close button, etc.).
2016-11-23 16:12:23 -06:00
Undecorated bool
// If set to true, a window will not get focused upon showing up.
Unfocused bool
// Whether a window is maximized.
Maximized bool
// VSync (vertical synchronization) synchronizes window's framerate with the framerate
// of the monitor.
VSync bool
// Number of samples for multi-sample anti-aliasing (edge-smoothing). Usual values
// are 0, 2, 4, 8 (powers of 2 and not much more than this).
2016-11-23 16:12:23 -06:00
MSAASamples int
}
2016-11-24 07:27:43 -06:00
// Window is a window handler. Use this type to manipulate a window (input, drawing, ...).
2016-11-23 16:12:23 -06:00
type Window struct {
2017-02-11 07:09:47 -06:00
window *glfw.Window
config WindowConfig
canvas *Canvas
2017-02-11 07:09:47 -06:00
canvasVs *glhf.VertexSlice
shader *glhf.Shader
2016-11-24 15:06:51 -06:00
// need to save these to correctly restore a fullscreen window
restore struct {
xpos, ypos, width, height int
}
prevInp, tempInp, currInp struct {
buttons [KeyLast + 1]bool
scroll pixel.Vec
}
2016-11-23 16:12:23 -06:00
}
2017-01-01 15:12:12 -06:00
var currentWindow *Window
2017-01-25 11:55:17 -06:00
// NewWindow creates a new Window with it's properties specified in the provided config.
2016-11-24 07:27:43 -06:00
//
2017-01-25 11:55:17 -06:00
// If Window creation fails, an error is returned (e.g. due to unavailable graphics device).
2016-11-23 16:12:23 -06:00
func NewWindow(config WindowConfig) (*Window, error) {
bool2int := map[bool]int{
true: glfw.True,
false: glfw.False,
}
w := &Window{config: config}
err := mainthread.CallErr(func() error {
2017-01-25 20:33:21 -06:00
var err error
2016-11-25 16:12:01 -06:00
2016-11-23 16:12:23 -06:00
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
glfw.WindowHint(glfw.Resizable, bool2int[config.Resizable])
glfw.WindowHint(glfw.Visible, bool2int[!config.Hidden])
glfw.WindowHint(glfw.Decorated, bool2int[!config.Undecorated])
glfw.WindowHint(glfw.Focused, bool2int[!config.Unfocused])
glfw.WindowHint(glfw.Maximized, bool2int[config.Maximized])
glfw.WindowHint(glfw.Samples, config.MSAASamples)
2017-01-01 15:12:12 -06:00
var share *glfw.Window
if currentWindow != nil {
share = currentWindow.window
}
w.window, err = glfw.CreateWindow(
int(config.Width),
int(config.Height),
config.Title,
nil,
share,
)
2016-11-23 16:12:23 -06:00
if err != nil {
return err
}
// enter the OpenGL context
w.begin()
2017-01-10 18:05:24 -06:00
w.end()
2016-11-24 15:06:51 -06:00
2017-02-11 07:09:47 -06:00
w.shader, err = glhf.NewShader(
windowVertexFormat,
windowUniformFormat,
windowVertexShader,
windowFragmentShader,
2016-12-16 13:46:24 -06:00
)
if err != nil {
return err
}
2017-02-11 07:09:47 -06:00
w.canvasVs = glhf.MakeVertexSlice(w.shader, 6, 6)
w.canvasVs.Begin()
w.canvasVs.SetVertexData([]float32{
-1, -1, 0, 0,
1, -1, 1, 0,
1, 1, 1, 1,
-1, -1, 0, 0,
1, 1, 1, 1,
-1, 1, 0, 1,
})
w.canvasVs.End()
return nil
2016-12-16 13:46:24 -06:00
})
2016-12-01 18:20:54 -06:00
if err != nil {
return nil, errors.Wrap(err, "creating window failed")
}
2017-01-01 15:12:12 -06:00
w.initInput()
w.SetMonitor(config.Fullscreen)
2017-01-21 20:05:59 -06:00
w.canvas = NewCanvas(config.Width, config.Height, false)
w.Update()
runtime.SetFinalizer(w, (*Window).Destroy)
2016-11-23 16:12:23 -06:00
return w, nil
}
2017-01-25 11:55:17 -06:00
// Destroy destroys the Window. The Window can't be used any further.
func (w *Window) Destroy() {
mainthread.Call(func() {
w.window.Destroy()
})
}
2017-01-25 11:55:17 -06:00
// Clear clears the Window with a color.
2016-11-24 08:13:05 -06:00
func (w *Window) Clear(c color.Color) {
w.canvas.Clear(c)
2016-11-23 16:25:45 -06:00
}
2016-11-24 07:27:43 -06:00
// Update swaps buffers and polls events.
2016-11-23 16:12:23 -06:00
func (w *Window) Update() {
2017-01-21 20:05:59 -06:00
w.canvas.SetSize(w.Size())
mainthread.Call(func() {
w.begin()
2017-02-11 07:09:47 -06:00
glhf.Clear(0, 0, 0, 0)
w.shader.Begin()
w.canvas.f.Texture().Begin()
w.canvasVs.Begin()
w.canvasVs.Draw()
w.canvasVs.End()
w.canvas.f.Texture().End()
w.shader.End()
2017-01-01 15:12:12 -06:00
if w.config.VSync {
glfw.SwapInterval(1)
}
w.window.SwapBuffers()
2017-01-15 18:12:03 -06:00
w.end()
2017-01-01 15:12:12 -06:00
})
w.updateInput()
2016-11-24 15:06:51 -06:00
}
2017-01-25 11:55:17 -06:00
// SetClosed sets the closed flag of the Window.
//
2017-01-25 11:55:17 -06:00
// This is usefull when overriding the user's attempt to close the Window, or just to close the
// Window from within the program.
func (w *Window) SetClosed(closed bool) {
mainthread.Call(func() {
w.window.SetShouldClose(closed)
})
}
2017-01-25 11:55:17 -06:00
// Closed returns the closed flag of the Window, which reports whether the Window should be closed.
//
2017-01-25 11:55:17 -06:00
// The closed flag is automatically set when a user attempts to close the Window.
func (w *Window) Closed() bool {
return mainthread.CallVal(func() interface{} {
return w.window.ShouldClose()
}).(bool)
}
2017-01-25 11:55:17 -06:00
// SetTitle changes the title of the Window.
2016-11-24 15:06:51 -06:00
func (w *Window) SetTitle(title string) {
mainthread.Call(func() {
w.window.SetTitle(title)
2016-11-24 15:06:51 -06:00
})
}
2017-01-25 11:55:17 -06:00
// SetSize resizes the client area of the Window to the specified size in pixels. In case of a
// fullscreen Window, it changes the resolution of that Window.
2016-11-24 15:06:51 -06:00
func (w *Window) SetSize(width, height float64) {
mainthread.Call(func() {
w.window.SetSize(int(width), int(height))
2016-11-24 15:06:51 -06:00
})
}
2017-01-25 11:55:17 -06:00
// Size returns the size of the client area of the Window (the part you can draw on).
2016-11-24 15:06:51 -06:00
func (w *Window) Size() (width, height float64) {
mainthread.Call(func() {
wi, hi := w.window.GetSize()
width = float64(wi)
height = float64(hi)
2016-11-24 15:06:51 -06:00
})
return width, height
}
2017-01-25 11:55:17 -06:00
// Show makes the Window visible if it was hidden.
2016-11-24 15:06:51 -06:00
func (w *Window) Show() {
mainthread.Call(func() {
w.window.Show()
2016-11-24 15:06:51 -06:00
})
}
2017-01-25 11:55:17 -06:00
// Hide hides the Window if it was visible.
2016-11-24 15:06:51 -06:00
func (w *Window) Hide() {
mainthread.Call(func() {
w.window.Hide()
2016-11-24 15:06:51 -06:00
})
}
func (w *Window) setFullscreen(monitor *Monitor) {
mainthread.Call(func() {
w.restore.xpos, w.restore.ypos = w.window.GetPos()
w.restore.width, w.restore.height = w.window.GetSize()
mode := monitor.monitor.GetVideoMode()
w.window.SetMonitor(
monitor.monitor,
0,
0,
mode.Width,
mode.Height,
mode.RefreshRate,
)
})
}
func (w *Window) setWindowed() {
mainthread.Call(func() {
w.window.SetMonitor(
nil,
w.restore.xpos,
w.restore.ypos,
w.restore.width,
w.restore.height,
0,
)
})
}
2017-01-25 11:55:17 -06:00
// SetMonitor sets the Window fullscreen on the given Monitor. If the Monitor is nil, the Window
// will be resored to windowed state instead.
2016-11-24 15:06:51 -06:00
//
2017-01-25 11:55:17 -06:00
// Note, that there is nothing about the resolution of the fullscreen Window. The Window is
// automatically set to the Monitor's resolution. If you want a different resolution, you need
// to set it manually with SetSize method.
func (w *Window) SetMonitor(monitor *Monitor) {
2016-11-24 15:06:51 -06:00
if w.Monitor() != monitor {
if monitor != nil {
w.setFullscreen(monitor)
2016-11-24 15:06:51 -06:00
} else {
w.setWindowed()
2016-11-24 15:06:51 -06:00
}
}
}
2017-01-25 11:55:17 -06:00
// IsFullscreen returns true if the Window is in fullscreen mode.
2016-11-24 15:06:51 -06:00
func (w *Window) IsFullscreen() bool {
return w.Monitor() != nil
}
2017-01-25 11:55:17 -06:00
// Monitor returns a monitor the Window is fullscreen is on. If the Window is not fullscreen, this
// function returns nil.
2016-11-24 15:06:51 -06:00
func (w *Window) Monitor() *Monitor {
monitor := mainthread.CallVal(func() interface{} {
return w.window.GetMonitor()
}).(*glfw.Monitor)
2016-11-24 15:06:51 -06:00
if monitor == nil {
return nil
}
return &Monitor{
monitor: monitor,
}
2016-11-23 16:12:23 -06:00
}
2017-01-25 11:55:17 -06:00
// Focus brings the Window to the front and sets input focus.
func (w *Window) Focus() {
mainthread.Call(func() {
w.window.Focus()
})
2016-11-24 15:06:51 -06:00
}
2017-01-25 11:55:17 -06:00
// Focused returns true if the Window has input focus.
2016-11-24 15:06:51 -06:00
func (w *Window) Focused() bool {
return mainthread.CallVal(func() interface{} {
return w.window.GetAttrib(glfw.Focused) == glfw.True
}).(bool)
2016-11-24 15:06:51 -06:00
}
2017-01-25 11:55:17 -06:00
// Maximize puts the Window window to the maximized state.
2016-11-24 15:06:51 -06:00
func (w *Window) Maximize() {
mainthread.Call(func() {
w.window.Maximize()
2016-11-24 15:06:51 -06:00
})
}
2017-01-25 11:55:17 -06:00
// Restore restores the Window window from the maximized state.
2016-11-24 15:06:51 -06:00
func (w *Window) Restore() {
mainthread.Call(func() {
w.window.Restore()
2016-11-24 15:06:51 -06:00
})
}
// Note: must be called inside the main thread.
func (w *Window) begin() {
2017-01-01 15:12:12 -06:00
if currentWindow != w {
w.window.MakeContextCurrent()
2017-02-11 07:09:47 -06:00
glhf.Init()
2017-01-01 15:12:12 -06:00
currentWindow = w
}
}
2016-11-23 16:12:23 -06:00
// Note: must be called inside the main thread.
func (w *Window) end() {
2017-01-25 11:55:17 -06:00
// nothing, really
}
// MakeTriangles generates a specialized copy of the supplied triangles that will draw onto this
// Window.
//
// Window supports TrianglesPosition, TrianglesColor and TrianglesTexture.
func (w *Window) MakeTriangles(t pixel.Triangles) pixel.TargetTriangles {
return w.canvas.MakeTriangles(t)
}
2017-01-25 11:55:17 -06:00
// SetPicture sets a Picture that will be used in subsequent drawings onto the Window.
func (w *Window) SetPicture(p *pixel.Picture) {
w.canvas.SetPicture(p)
}
// SetTransform sets a global transformation matrix for the Window.
//
// Transforms are applied right-to-left.
func (w *Window) SetTransform(t ...pixel.Transform) {
w.canvas.SetTransform(t...)
}
// SetMaskColor sets a global mask color for the Window.
func (w *Window) SetMaskColor(c color.Color) {
w.canvas.SetMaskColor(c)
2016-12-02 11:17:40 -06:00
}
const (
windowPositionVec2 = iota
windowTextureVec2
)
2017-02-11 07:09:47 -06:00
var windowVertexFormat = glhf.AttrFormat{
windowPositionVec2: {Name: "position", Type: glhf.Vec2},
windowTextureVec2: {Name: "texture", Type: glhf.Vec2},
2016-11-23 16:12:23 -06:00
}
2016-12-01 18:20:54 -06:00
2017-02-11 07:09:47 -06:00
var windowUniformFormat = glhf.AttrFormat{}
2016-12-01 18:20:54 -06:00
var windowVertexShader = `
2016-12-01 18:20:54 -06:00
#version 330 core
2016-12-15 17:28:52 -06:00
in vec2 position;
in vec2 texture;
2016-12-01 18:20:54 -06:00
out vec2 Texture;
2016-12-01 18:20:54 -06:00
void main() {
gl_Position = vec4(position, 0.0, 1.0);
Texture = texture;
2016-12-01 18:20:54 -06:00
}
`
var windowFragmentShader = `
2016-12-01 18:20:54 -06:00
#version 330 core
in vec2 Texture;
2016-12-01 18:20:54 -06:00
out vec4 color;
uniform sampler2D tex;
void main() {
color = texture(tex, Texture);
2016-12-01 18:20:54 -06:00
}
`