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
|
|
|
|
2016-12-18 13:16:27 -06:00
|
|
|
"runtime"
|
|
|
|
|
2017-01-20 10:45:19 -06:00
|
|
|
"github.com/faiface/mainthread"
|
2016-11-23 16:12:23 -06:00
|
|
|
"github.com/faiface/pixel/pixelgl"
|
|
|
|
"github.com/go-gl/glfw/v3.2/glfw"
|
2016-12-17 05:57:48 -06:00
|
|
|
"github.com/go-gl/mathgl/mgl32"
|
2016-11-23 16:12:23 -06:00
|
|
|
"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 {
|
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
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// If set to nil, a window will be windowed. Otherwise it will be fullscreen on the
|
|
|
|
// specified monitor.
|
2016-11-24 10:24:38 -06:00
|
|
|
Fullscreen *Monitor
|
|
|
|
|
2016-11-24 09:16:00 -06:00
|
|
|
// 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
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// VSync (vertical synchronization) synchronizes window's framerate with the framerate
|
|
|
|
// of the monitor.
|
2016-11-24 09:16:00 -06:00
|
|
|
VSync bool
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// 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-01-01 15:12:12 -06:00
|
|
|
enabled bool
|
|
|
|
window *glfw.Window
|
|
|
|
config WindowConfig
|
|
|
|
shader *pixelgl.Shader
|
2017-01-10 17:26:33 -06:00
|
|
|
|
2017-01-15 18:12:03 -06:00
|
|
|
// cache
|
|
|
|
width, height float64
|
|
|
|
|
2017-01-10 17:26:33 -06:00
|
|
|
// Target stuff, Picture, transformation matrix and color
|
|
|
|
pic *Picture
|
|
|
|
mat mgl32.Mat3
|
|
|
|
col mgl32.Vec4
|
2017-01-14 09:07:51 -06:00
|
|
|
bnd mgl32.Vec4
|
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
|
|
|
|
}
|
2016-12-21 13:18:38 -06:00
|
|
|
|
|
|
|
prevInp, tempInp, currInp struct {
|
|
|
|
buttons [KeyLast + 1]bool
|
|
|
|
scroll Vec
|
|
|
|
}
|
2017-01-20 10:45:19 -06:00
|
|
|
|
|
|
|
//DEBUG
|
|
|
|
Frame *pixelgl.Frame
|
2016-11-23 16:12:23 -06:00
|
|
|
}
|
|
|
|
|
2017-01-01 15:12:12 -06:00
|
|
|
var currentWindow *Window
|
|
|
|
|
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}
|
|
|
|
|
2017-01-20 10:45:19 -06:00
|
|
|
err := mainthread.CallErr(func() error {
|
2016-11-25 16:26:27 -06:00
|
|
|
err := glfw.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
}
|
2016-11-24 10:24:38 -06:00
|
|
|
|
2016-11-23 16:12:23 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "creating window failed")
|
|
|
|
}
|
|
|
|
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2017-01-04 17:19:45 -06:00
|
|
|
w.begin()
|
2017-01-10 18:05:24 -06:00
|
|
|
w.end()
|
2016-11-24 15:06:51 -06:00
|
|
|
|
2017-01-01 15:12:12 -06:00
|
|
|
w.shader, err = pixelgl.NewShader(
|
2016-12-16 13:46:24 -06:00
|
|
|
defaultVertexFormat,
|
|
|
|
defaultUniformFormat,
|
|
|
|
defaultVertexShader,
|
|
|
|
defaultFragmentShader,
|
|
|
|
)
|
2017-01-04 17:19:45 -06:00
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "NewWindow: failed to create shader"))
|
|
|
|
}
|
2016-12-16 13:46:24 -06:00
|
|
|
})
|
2016-12-01 18:20:54 -06:00
|
|
|
if err != nil {
|
2016-12-18 13:16:27 -06:00
|
|
|
w.Destroy()
|
2016-12-01 18:20:54 -06:00
|
|
|
return nil, errors.Wrap(err, "creating window failed")
|
|
|
|
}
|
|
|
|
|
2017-01-01 15:12:12 -06:00
|
|
|
w.initInput()
|
|
|
|
w.SetFullscreen(config.Fullscreen)
|
|
|
|
|
2017-01-10 17:26:33 -06:00
|
|
|
w.SetPicture(nil)
|
|
|
|
w.SetTransform()
|
|
|
|
w.SetMaskColor(NRGBA{1, 1, 1, 1})
|
|
|
|
|
2016-12-18 13:16:27 -06:00
|
|
|
runtime.SetFinalizer(w, (*Window).Destroy)
|
|
|
|
|
2016-11-23 16:12:23 -06:00
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2016-12-18 13:16:27 -06:00
|
|
|
// Destroy destroys a window. The window can't be used any further.
|
|
|
|
func (w *Window) Destroy() {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-16 13:27:40 -06:00
|
|
|
w.window.Destroy()
|
2016-11-24 10:24:38 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.CallNonBlock(func() {
|
2017-01-04 17:19:45 -06:00
|
|
|
w.begin()
|
2017-01-01 15:12:12 -06:00
|
|
|
c := NRGBAModel.Convert(c).(NRGBA)
|
2017-01-15 18:12:03 -06:00
|
|
|
pixelgl.Clear(float32(c.R), float32(c.G), float32(c.B), float32(c.A))
|
2017-01-20 10:45:19 -06:00
|
|
|
w.end()
|
2016-11-25 15:49:56 -06:00
|
|
|
})
|
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-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2017-01-04 17:19:45 -06:00
|
|
|
w.begin()
|
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()
|
|
|
|
|
2017-01-15 18:12:03 -06:00
|
|
|
w.width, w.height = w.Size()
|
2016-11-24 15:06:51 -06:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:18:38 -06:00
|
|
|
// SetClosed sets the closed flag of a window.
|
|
|
|
//
|
2016-12-30 10:43:26 -06:00
|
|
|
// This is usefull when overriding the user's attempt to close a window, or just to close a
|
|
|
|
// window from within a program.
|
2016-12-21 13:18:38 -06:00
|
|
|
func (w *Window) SetClosed(closed bool) {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-21 13:18:38 -06:00
|
|
|
w.window.SetShouldClose(closed)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closed returns the closed flag of a window, which reports whether the window should be closed.
|
|
|
|
//
|
|
|
|
// The closed flag is automatically set when a user attempts to close a window.
|
|
|
|
func (w *Window) Closed() bool {
|
2017-01-20 10:45:19 -06:00
|
|
|
return mainthread.CallVal(func() interface{} {
|
2016-12-21 13:18:38 -06:00
|
|
|
return w.window.ShouldClose()
|
|
|
|
}).(bool)
|
|
|
|
}
|
|
|
|
|
2016-11-24 15:06:51 -06:00
|
|
|
// SetTitle changes the title of a window.
|
|
|
|
func (w *Window) SetTitle(title string) {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-16 13:27:40 -06:00
|
|
|
w.window.SetTitle(title)
|
2016-11-24 15:06:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// SetSize resizes a 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) {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-16 13:27:40 -06:00
|
|
|
w.window.SetSize(int(width), int(height))
|
2016-11-24 15:06:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of the client area of a window (the part you can draw on).
|
|
|
|
func (w *Window) Size() (width, height float64) {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-16 13:27:40 -06:00
|
|
|
wi, hi := w.window.GetSize()
|
|
|
|
width = float64(wi)
|
|
|
|
height = float64(hi)
|
2016-11-24 15:06:51 -06:00
|
|
|
})
|
|
|
|
return width, height
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show makes a window visible if it was hidden.
|
|
|
|
func (w *Window) Show() {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-16 13:27:40 -06:00
|
|
|
w.window.Show()
|
2016-11-24 15:06:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide hides a window if it was visible.
|
|
|
|
func (w *Window) Hide() {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-16 13:27:40 -06:00
|
|
|
w.window.Hide()
|
2016-11-24 15:06:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// SetFullscreen sets a window fullscreen on a given monitor. If the monitor is nil, the window
|
|
|
|
// will be resored to windowed instead.
|
2016-11-24 15:06:51 -06:00
|
|
|
//
|
2016-12-30 10:43:26 -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.
|
2016-11-24 15:06:51 -06:00
|
|
|
func (w *Window) SetFullscreen(monitor *Monitor) {
|
|
|
|
if w.Monitor() != monitor {
|
|
|
|
if monitor == nil {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-16 13:27:40 -06:00
|
|
|
w.window.SetMonitor(
|
|
|
|
nil,
|
|
|
|
w.restore.xpos,
|
|
|
|
w.restore.ypos,
|
|
|
|
w.restore.width,
|
|
|
|
w.restore.height,
|
|
|
|
0,
|
|
|
|
)
|
2016-11-24 15:06:51 -06:00
|
|
|
})
|
|
|
|
} else {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-16 13:27:40 -06:00
|
|
|
w.restore.xpos, w.restore.ypos = w.window.GetPos()
|
|
|
|
w.restore.width, w.restore.height = w.window.GetSize()
|
|
|
|
|
|
|
|
width, height := monitor.Size()
|
|
|
|
refreshRate := monitor.RefreshRate()
|
|
|
|
w.window.SetMonitor(
|
|
|
|
monitor.monitor,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
int(width),
|
|
|
|
int(height),
|
|
|
|
int(refreshRate),
|
|
|
|
)
|
2016-11-24 15:06:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsFullscreen returns true if the window is in the fullscreen mode.
|
|
|
|
func (w *Window) IsFullscreen() bool {
|
|
|
|
return w.Monitor() != nil
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// Monitor returns a monitor a fullscreen window 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 {
|
2017-01-20 10:45:19 -06:00
|
|
|
monitor := mainthread.CallVal(func() interface{} {
|
2016-12-16 13:27:40 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-24 10:24:38 -06:00
|
|
|
// Focus brings a window to the front and sets input focus.
|
|
|
|
func (w *Window) Focus() {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-16 13:27:40 -06:00
|
|
|
w.window.Focus()
|
2016-11-24 10:24:38 -06:00
|
|
|
})
|
2016-11-24 15:06:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Focused returns true if a window has input focus.
|
|
|
|
func (w *Window) Focused() bool {
|
2017-01-20 10:45:19 -06:00
|
|
|
return mainthread.CallVal(func() interface{} {
|
2016-12-16 13:27:40 -06:00
|
|
|
return w.window.GetAttrib(glfw.Focused) == glfw.True
|
|
|
|
}).(bool)
|
2016-11-24 15:06:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Maximize puts a windowed window to a maximized state.
|
|
|
|
func (w *Window) Maximize() {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-16 13:27:40 -06:00
|
|
|
w.window.Maximize()
|
2016-11-24 15:06:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore restores a windowed window from a maximized state.
|
|
|
|
func (w *Window) Restore() {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.Call(func() {
|
2016-12-16 13:27:40 -06:00
|
|
|
w.window.Restore()
|
2016-11-24 15:06:51 -06:00
|
|
|
})
|
2016-11-24 10:24:38 -06:00
|
|
|
}
|
|
|
|
|
2017-01-10 16:54:35 -06:00
|
|
|
// Note: must be called inside the main thread.
|
2017-01-04 17:19:45 -06:00
|
|
|
func (w *Window) begin() {
|
2017-01-01 15:12:12 -06:00
|
|
|
if currentWindow != w {
|
|
|
|
w.window.MakeContextCurrent()
|
|
|
|
pixelgl.Init()
|
|
|
|
currentWindow = w
|
|
|
|
}
|
|
|
|
if w.shader != nil {
|
|
|
|
w.shader.Begin()
|
|
|
|
}
|
2017-01-20 10:45:19 -06:00
|
|
|
if w.Frame != nil {
|
|
|
|
w.Frame.Begin()
|
|
|
|
pixelgl.Viewport(0, 0, int32(w.Frame.Width()), int32(w.Frame.Height()))
|
|
|
|
} else {
|
|
|
|
pixelgl.Viewport(0, 0, int32(w.width), int32(w.height))
|
|
|
|
}
|
2016-11-24 09:16:00 -06:00
|
|
|
}
|
2016-11-23 16:12:23 -06:00
|
|
|
|
2017-01-10 16:54:35 -06:00
|
|
|
// Note: must be called inside the main thread.
|
2017-01-04 17:19:45 -06:00
|
|
|
func (w *Window) end() {
|
2017-01-20 10:45:19 -06:00
|
|
|
if w.Frame != nil {
|
|
|
|
w.Frame.End()
|
|
|
|
}
|
2017-01-01 15:12:12 -06:00
|
|
|
if w.shader != nil {
|
|
|
|
w.shader.End()
|
|
|
|
}
|
|
|
|
}
|
2016-12-08 08:25:00 -06:00
|
|
|
|
2017-01-20 11:40:11 -06:00
|
|
|
type trianglesPositionColorTexture interface {
|
|
|
|
Triangles
|
|
|
|
Position(i int) Vec
|
|
|
|
Color(i int) NRGBA
|
|
|
|
Texture(i int) Vec
|
2017-01-04 17:19:45 -06:00
|
|
|
}
|
2017-01-01 15:12:12 -06:00
|
|
|
|
2017-01-20 11:40:11 -06:00
|
|
|
type windowTriangles struct {
|
|
|
|
w *Window
|
|
|
|
trianglesPositionColorTexture
|
2017-01-04 17:19:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wt *windowTriangles) Draw() {
|
2017-01-14 09:20:31 -06:00
|
|
|
// avoid possible race condition
|
|
|
|
pic := wt.w.pic
|
|
|
|
mat := wt.w.mat
|
|
|
|
col := wt.w.col
|
2017-01-14 09:07:51 -06:00
|
|
|
bnd := wt.w.bnd
|
2017-01-10 17:35:16 -06:00
|
|
|
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.CallNonBlock(func() {
|
2017-01-04 17:19:45 -06:00
|
|
|
wt.w.begin()
|
2017-01-10 17:26:33 -06:00
|
|
|
|
2017-01-10 17:35:16 -06:00
|
|
|
wt.w.shader.SetUniformAttr(transformMat3, mat)
|
|
|
|
wt.w.shader.SetUniformAttr(maskColorVec4, col)
|
2017-01-14 09:07:51 -06:00
|
|
|
wt.w.shader.SetUniformAttr(boundsVec4, bnd)
|
2017-01-10 17:26:33 -06:00
|
|
|
|
2017-01-10 17:35:16 -06:00
|
|
|
if pic != nil {
|
|
|
|
pic.Texture().Begin()
|
2016-12-08 08:25:00 -06:00
|
|
|
}
|
2017-01-20 11:40:11 -06:00
|
|
|
wt.trianglesPositionColorTexture.Draw()
|
2017-01-10 17:35:16 -06:00
|
|
|
if pic != nil {
|
|
|
|
pic.Texture().End()
|
2017-01-04 17:19:45 -06:00
|
|
|
}
|
2017-01-10 17:26:33 -06:00
|
|
|
|
2017-01-04 17:19:45 -06:00
|
|
|
wt.w.end()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 Triangles) Triangles {
|
2017-01-20 11:40:11 -06:00
|
|
|
tpcs := NewGLTriangles(w.shader, t).(trianglesPositionColorTexture)
|
2017-01-04 17:19:45 -06:00
|
|
|
wt := &windowTriangles{
|
2017-01-20 11:40:11 -06:00
|
|
|
w: w,
|
|
|
|
trianglesPositionColorTexture: tpcs,
|
2017-01-04 17:19:45 -06:00
|
|
|
}
|
|
|
|
return wt
|
|
|
|
}
|
|
|
|
|
2017-01-10 17:26:33 -06:00
|
|
|
// SetPicture sets a Picture that will be used in subsequent drawings onto the window.
|
|
|
|
func (w *Window) SetPicture(p *Picture) {
|
2017-01-14 09:07:51 -06:00
|
|
|
if p != nil {
|
|
|
|
min := pictureBounds(p, V(0, 0))
|
|
|
|
max := pictureBounds(p, V(1, 1))
|
|
|
|
w.bnd = mgl32.Vec4{
|
|
|
|
float32(min.X()), float32(min.Y()),
|
|
|
|
float32(max.X()), float32(max.Y()),
|
|
|
|
}
|
|
|
|
}
|
2017-01-10 17:26:33 -06:00
|
|
|
w.pic = p
|
|
|
|
}
|
|
|
|
|
2017-01-04 17:19:45 -06:00
|
|
|
// SetTransform sets a global transformation matrix for the Window.
|
|
|
|
//
|
|
|
|
// Transforms are applied right-to-left.
|
|
|
|
func (w *Window) SetTransform(t ...Transform) {
|
2017-01-12 04:44:54 -06:00
|
|
|
w.mat = transformToMat(t...)
|
2017-01-04 17:19:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetMaskColor sets a global mask color for the Window.
|
|
|
|
func (w *Window) SetMaskColor(c color.Color) {
|
2017-01-05 19:50:03 -06:00
|
|
|
if c == nil {
|
|
|
|
c = NRGBA{1, 1, 1, 1}
|
|
|
|
}
|
2017-01-04 17:19:45 -06:00
|
|
|
nrgba := NRGBAModel.Convert(c).(NRGBA)
|
|
|
|
r := float32(nrgba.R)
|
|
|
|
g := float32(nrgba.G)
|
|
|
|
b := float32(nrgba.B)
|
|
|
|
a := float32(nrgba.A)
|
2017-01-10 17:26:33 -06:00
|
|
|
w.col = mgl32.Vec4{r, g, b, a}
|
2016-12-02 11:17:40 -06:00
|
|
|
}
|
|
|
|
|
2017-01-13 17:47:49 -06:00
|
|
|
const (
|
|
|
|
positionVec2 int = iota
|
|
|
|
colorVec4
|
|
|
|
textureVec2
|
|
|
|
)
|
|
|
|
|
2016-12-15 17:28:52 -06:00
|
|
|
var defaultVertexFormat = pixelgl.AttrFormat{
|
2017-01-13 17:47:49 -06:00
|
|
|
positionVec2: {Name: "position", Type: pixelgl.Vec2},
|
|
|
|
colorVec4: {Name: "color", Type: pixelgl.Vec4},
|
|
|
|
textureVec2: {Name: "texture", Type: pixelgl.Vec2},
|
2016-11-23 16:12:23 -06:00
|
|
|
}
|
2016-12-01 18:20:54 -06:00
|
|
|
|
2017-01-13 17:47:49 -06:00
|
|
|
const (
|
|
|
|
maskColorVec4 int = iota
|
|
|
|
transformMat3
|
2017-01-14 09:07:51 -06:00
|
|
|
boundsVec4
|
2017-01-13 17:47:49 -06:00
|
|
|
)
|
|
|
|
|
2016-12-15 17:28:52 -06:00
|
|
|
var defaultUniformFormat = pixelgl.AttrFormat{
|
2017-01-13 17:47:49 -06:00
|
|
|
{Name: "maskColor", Type: pixelgl.Vec4},
|
|
|
|
{Name: "transform", Type: pixelgl.Mat3},
|
2017-01-14 09:07:51 -06:00
|
|
|
{Name: "bounds", Type: pixelgl.Vec4},
|
2016-12-01 18:20:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var defaultVertexShader = `
|
|
|
|
#version 330 core
|
|
|
|
|
2016-12-15 17:28:52 -06:00
|
|
|
in vec2 position;
|
|
|
|
in vec4 color;
|
2017-01-04 17:19:45 -06:00
|
|
|
in vec2 texture;
|
2016-12-01 18:20:54 -06:00
|
|
|
|
|
|
|
out vec4 Color;
|
2017-01-04 17:19:45 -06:00
|
|
|
out vec2 Texture;
|
2016-12-01 18:20:54 -06:00
|
|
|
|
|
|
|
uniform mat3 transform;
|
|
|
|
|
|
|
|
void main() {
|
2016-12-02 11:17:40 -06:00
|
|
|
gl_Position = vec4((transform * vec3(position.x, position.y, 1.0)).xy, 0.0, 1.0);
|
2016-12-01 18:20:54 -06:00
|
|
|
Color = color;
|
2017-01-04 17:19:45 -06:00
|
|
|
Texture = texture;
|
2016-12-01 18:20:54 -06:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var defaultFragmentShader = `
|
|
|
|
#version 330 core
|
|
|
|
|
|
|
|
in vec4 Color;
|
2017-01-04 17:19:45 -06:00
|
|
|
in vec2 Texture;
|
2016-12-01 18:20:54 -06:00
|
|
|
|
|
|
|
out vec4 color;
|
|
|
|
|
2016-12-03 08:48:40 -06:00
|
|
|
uniform vec4 maskColor;
|
2017-01-14 09:07:51 -06:00
|
|
|
uniform vec4 bounds;
|
2016-12-01 18:20:54 -06:00
|
|
|
uniform sampler2D tex;
|
|
|
|
|
|
|
|
void main() {
|
2017-01-14 09:07:51 -06:00
|
|
|
vec2 boundsMin = bounds.xy;
|
|
|
|
vec2 boundsMax = bounds.zw;
|
|
|
|
|
2017-01-04 17:19:45 -06:00
|
|
|
if (Texture == vec2(-1, -1)) {
|
2016-12-03 08:48:40 -06:00
|
|
|
color = maskColor * Color;
|
2016-12-09 13:13:32 -06:00
|
|
|
} else {
|
2017-01-20 10:45:19 -06:00
|
|
|
float tx = boundsMin.x * (1 - Texture.x) + boundsMax.x * Texture.x;
|
|
|
|
float ty = boundsMin.y * (1 - Texture.y) + boundsMax.y * Texture.y;
|
2017-01-14 09:07:51 -06:00
|
|
|
color = maskColor * Color * texture(tex, vec2(tx, 1 - ty));
|
2016-12-01 18:20:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|