2016-11-23 16:12:23 -06:00
|
|
|
package pixel
|
|
|
|
|
|
|
|
import (
|
2016-11-24 08:13:05 -06:00
|
|
|
"image/color"
|
2016-11-23 17:43:00 -06:00
|
|
|
"sync"
|
|
|
|
|
2016-11-23 16:12:23 -06:00
|
|
|
"github.com/faiface/pixel/pixelgl"
|
|
|
|
"github.com/go-gl/glfw/v3.2/glfw"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2016-11-24 07:27:43 -06:00
|
|
|
// WindowConfig is convenience 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.
|
|
|
|
//
|
|
|
|
// 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 {
|
2016-11-24 09:16:00 -06:00
|
|
|
// 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
|
|
|
|
|
|
|
|
// 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
|
2016-11-24 09:16:00 -06:00
|
|
|
|
|
|
|
// 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 {
|
|
|
|
window *glfw.Window
|
|
|
|
config WindowConfig
|
|
|
|
}
|
|
|
|
|
2016-11-24 07:27:43 -06:00
|
|
|
// NewWindow creates a new window with it's properties specified in the provided config.
|
|
|
|
//
|
|
|
|
// If window creation fails, an error is returned.
|
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 := pixelgl.DoErr(func() error {
|
|
|
|
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)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
w.window, err = glfw.CreateWindow(int(config.Width), int(config.Height), config.Title, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "creating window failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2016-11-24 07:27:43 -06:00
|
|
|
// Clear clears the window with a color.
|
2016-11-24 08:13:05 -06:00
|
|
|
func (w *Window) Clear(c color.Color) {
|
2016-11-23 16:25:45 -06:00
|
|
|
w.Begin()
|
2016-11-24 08:13:05 -06:00
|
|
|
pixelgl.Clear(colorToRGBA(c))
|
2016-11-23 16:25:45 -06:00
|
|
|
w.End()
|
|
|
|
}
|
|
|
|
|
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() {
|
2016-11-23 16:25:45 -06:00
|
|
|
w.Begin()
|
2016-11-23 16:12:23 -06:00
|
|
|
pixelgl.Do(func() {
|
|
|
|
if w.config.VSync {
|
|
|
|
glfw.SwapInterval(1)
|
|
|
|
}
|
|
|
|
w.window.SwapBuffers()
|
|
|
|
glfw.PollEvents()
|
|
|
|
})
|
2016-11-23 16:25:45 -06:00
|
|
|
w.End()
|
2016-11-23 16:12:23 -06:00
|
|
|
}
|
|
|
|
|
2016-11-24 09:16:00 -06:00
|
|
|
var currentWindow struct {
|
|
|
|
sync.Mutex
|
|
|
|
handler *Window
|
|
|
|
}
|
2016-11-23 16:12:23 -06:00
|
|
|
|
2016-11-24 07:27:43 -06:00
|
|
|
// Begin makes the context of this window current.
|
2016-11-24 09:16:00 -06:00
|
|
|
//
|
|
|
|
// Note that you only need to use this function if you're designing a low-level technical plugin (such as an effect).
|
2016-11-23 16:12:23 -06:00
|
|
|
func (w *Window) Begin() {
|
2016-11-24 09:16:00 -06:00
|
|
|
currentWindow.Lock()
|
|
|
|
if currentWindow.handler != w {
|
2016-11-23 17:43:00 -06:00
|
|
|
pixelgl.Do(func() {
|
2016-11-23 16:25:45 -06:00
|
|
|
w.window.MakeContextCurrent()
|
|
|
|
pixelgl.Init()
|
2016-11-23 17:43:00 -06:00
|
|
|
})
|
2016-11-24 09:16:00 -06:00
|
|
|
currentWindow.handler = w
|
2016-11-23 17:43:00 -06:00
|
|
|
}
|
2016-11-23 16:12:23 -06:00
|
|
|
}
|
|
|
|
|
2016-11-24 07:27:43 -06:00
|
|
|
// End makes it possible for other windows to make their context current.
|
2016-11-24 09:16:00 -06:00
|
|
|
//
|
|
|
|
// Note that you only need to use this function if you're designing a low-level technical plugin (such as an effect).
|
2016-11-23 16:12:23 -06:00
|
|
|
func (w *Window) End() {
|
2016-11-24 09:16:00 -06:00
|
|
|
currentWindow.Unlock()
|
2016-11-23 16:12:23 -06:00
|
|
|
}
|