Skip to content
window.go 9.71 KiB
Newer Older
faiface's avatar
faiface committed

import (
faiface's avatar
faiface committed
	"image/color"
	"math"
faiface's avatar
faiface committed
	"github.com/faiface/glhf"
	"github.com/faiface/mainthread"
	"github.com/faiface/pixel"
faiface's avatar
faiface committed
	"github.com/go-gl/glfw/v3.2/glfw"
	"github.com/pkg/errors"
)

faiface's avatar
faiface committed
// 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.
faiface's avatar
faiface committed
//
// Note that you always need to set the Bounds of the Window.
faiface's avatar
faiface committed
type WindowConfig struct {
	// Title at the top of the Window
	// Bounds specify the bounds of the Window in pixels.
	Bounds pixel.Rect
	// If set to nil, a window will be windowed. Otherwise it will be fullscreen on the
	// specified 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.).
faiface's avatar
faiface committed
	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.
	// 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).
faiface's avatar
faiface committed
	MSAASamples int
}

faiface's avatar
faiface committed
// Window is a window handler. Use this type to manipulate a window (input, drawing, ...).
faiface's avatar
faiface committed
type Window struct {
faiface's avatar
faiface committed
	window *glfw.Window
	bounds pixel.Rect
	canvas *Canvas
	vsync  bool
faiface's avatar
faiface committed

	// 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
faiface's avatar
faiface committed
}

faiface's avatar
faiface committed
var currentWindow *Window

faiface's avatar
faiface committed
// NewWindow creates a new Window with it's properties specified in the provided config.
faiface's avatar
faiface committed
//
faiface's avatar
faiface committed
// If Window creation fails, an error is returned (e.g. due to unavailable graphics device).
func NewWindow(cfg WindowConfig) (*Window, error) {
faiface's avatar
faiface committed
	bool2int := map[bool]int{
		true:  glfw.True,
		false: glfw.False,
	}

	w := &Window{
		bounds: cfg.Bounds,
	}
faiface's avatar
faiface committed

	err := mainthread.CallErr(func() error {
faiface's avatar
faiface committed
		var err error
faiface's avatar
faiface committed
		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[cfg.Resizable])
		glfw.WindowHint(glfw.Visible, bool2int[!cfg.Hidden])
		glfw.WindowHint(glfw.Decorated, bool2int[!cfg.Undecorated])
		glfw.WindowHint(glfw.Focused, bool2int[!cfg.Unfocused])
		glfw.WindowHint(glfw.Maximized, bool2int[cfg.Maximized])
		glfw.WindowHint(glfw.Samples, cfg.MSAASamples)
faiface's avatar
faiface committed

faiface's avatar
faiface committed
		var share *glfw.Window
		if currentWindow != nil {
			share = currentWindow.window
		}
		_, _, width, height := discreteBounds(cfg.Bounds)
		w.window, err = glfw.CreateWindow(
			width,
			height,
			cfg.Title,
faiface's avatar
faiface committed
		if err != nil {
			return err
		}
		// enter the OpenGL context
faiface's avatar
faiface committed
		w.end()
faiface's avatar
faiface committed
	})
faiface's avatar
faiface committed
	if err != nil {
		return nil, errors.Wrap(err, "creating window failed")
	}

	w.SetVSync(cfg.VSync)

faiface's avatar
faiface committed
	w.initInput()
	w.SetMonitor(cfg.Fullscreen)
faiface's avatar
faiface committed

	w.canvas = NewCanvas(cfg.Bounds, false)
	runtime.SetFinalizer(w, (*Window).Destroy)

faiface's avatar
faiface committed
	return w, nil
}

faiface's avatar
faiface committed
// Destroy destroys the Window. The Window can't be used any further.
func (w *Window) Destroy() {
	mainthread.Call(func() {
		w.window.Destroy()
faiface's avatar
faiface committed
// Update swaps buffers and polls events.
faiface's avatar
faiface committed
func (w *Window) Update() {
	mainthread.Call(func() {
		wi, hi := w.window.GetSize()
		w.bounds.Size = pixel.V(float64(wi), float64(hi))
		// fractional positions end up covering more pixels with less size
		if w.bounds.X() != math.Floor(w.bounds.X()) {
			w.bounds.Size -= pixel.V(1, 0)
		}
		if w.bounds.Y() != math.Floor(w.bounds.Y()) {
			w.bounds.Size -= pixel.V(0, 1)
		}
	})

	w.canvas.SetBounds(w.Bounds())
	mainthread.Call(func() {
		glhf.Bounds(0, 0, w.canvas.f.Width(), w.canvas.f.Height())

faiface's avatar
faiface committed
		glhf.Clear(0, 0, 0, 0)
		w.canvas.f.Begin()
		w.canvas.f.Blit(
			nil,
			0, 0, w.canvas.f.Width(), w.canvas.f.Height(),
			0, 0, w.canvas.f.Width(), w.canvas.f.Height(),
		)
		w.canvas.f.End()

		if w.vsync {
faiface's avatar
faiface committed
			glfw.SwapInterval(1)
		} else {
			glfw.SwapInterval(0)
faiface's avatar
faiface committed
		}
		w.window.SwapBuffers()
faiface's avatar
faiface committed
		w.end()
faiface's avatar
faiface committed
	})

	w.updateInput()
faiface's avatar
faiface committed
// SetClosed sets the closed flag of the Window.
faiface's avatar
faiface committed
// 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)
	})
}

faiface's avatar
faiface committed
// Closed returns the closed flag of the Window, which reports whether the Window should be closed.
faiface's avatar
faiface committed
// 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)
}

faiface's avatar
faiface committed
// SetTitle changes the title of the Window.
faiface's avatar
faiface committed
func (w *Window) SetTitle(title string) {
	mainthread.Call(func() {
		w.window.SetTitle(title)
// SetBounds sets the bounds of the Window in pixels. Bounds can be fractional, but the size will be
// changed in the next Update to a real possible size of the Window.
func (w *Window) SetBounds(bounds pixel.Rect) {
	w.bounds = bounds
	mainthread.Call(func() {
		_, _, width, height := discreteBounds(bounds)
		w.window.SetSize(width, height)
// Bounds returns the current bounds of the Window.
func (w *Window) Bounds() pixel.Rect {
	return w.bounds
faiface's avatar
faiface committed
// Show makes the Window visible if it was hidden.
faiface's avatar
faiface committed
func (w *Window) Show() {
	mainthread.Call(func() {
		w.window.Show()
faiface's avatar
faiface committed
// Hide hides the Window if it was visible.
faiface's avatar
faiface committed
func (w *Window) Hide() {
	mainthread.Call(func() {
		w.window.Hide()
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,
		)
	})
}

faiface's avatar
faiface committed
// SetMonitor sets the Window fullscreen on the given Monitor. If the Monitor is nil, the Window
// will be resored to windowed state instead.
faiface's avatar
faiface committed
// 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) {
faiface's avatar
faiface committed
	if w.Monitor() != monitor {
		if monitor != nil {
			w.setFullscreen(monitor)
faiface's avatar
faiface committed
		} else {
			w.setWindowed()
faiface's avatar
faiface committed
// IsFullscreen returns true if the Window is in fullscreen mode.
faiface's avatar
faiface committed
func (w *Window) IsFullscreen() bool {
	return w.Monitor() != nil
}

faiface's avatar
faiface committed
// Monitor returns a monitor the Window is fullscreen is on. If the Window is not fullscreen, this
// function returns nil.
faiface's avatar
faiface committed
func (w *Window) Monitor() *Monitor {
	monitor := mainthread.CallVal(func() interface{} {
		return w.window.GetMonitor()
	}).(*glfw.Monitor)
faiface's avatar
faiface committed
	if monitor == nil {
		return nil
	}
	return &Monitor{
		monitor: monitor,
	}
faiface's avatar
faiface committed
}

faiface's avatar
faiface committed
// Focus brings the Window to the front and sets input focus.
func (w *Window) Focus() {
	mainthread.Call(func() {
		w.window.Focus()
faiface's avatar
faiface committed
// Focused returns true if the Window has input focus.
faiface's avatar
faiface committed
func (w *Window) Focused() bool {
	return mainthread.CallVal(func() interface{} {
		return w.window.GetAttrib(glfw.Focused) == glfw.True
	}).(bool)
faiface's avatar
faiface committed
// Maximize puts the Window window to the maximized state.
faiface's avatar
faiface committed
func (w *Window) Maximize() {
	mainthread.Call(func() {
		w.window.Maximize()
faiface's avatar
faiface committed
// Restore restores the Window window from the maximized state.
faiface's avatar
faiface committed
func (w *Window) Restore() {
	mainthread.Call(func() {
		w.window.Restore()
// SetVSync sets whether the Window should synchronize with the monitor refresh rate.
func (w *Window) SetVSync(vsync bool) {
	w.vsync = vsync
}

// VSync returns whether the Window is set to synchronize with the monitor refresh rate.
func (w *Window) VSync() bool {
	return w.vsync
}

// Note: must be called inside the main thread.
func (w *Window) begin() {
faiface's avatar
faiface committed
	if currentWindow != w {
		w.window.MakeContextCurrent()
faiface's avatar
faiface committed
		glhf.Init()
faiface's avatar
faiface committed
		currentWindow = w
	}
faiface's avatar
faiface committed

// Note: must be called inside the main thread.
func (w *Window) end() {
faiface's avatar
faiface committed
	// 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)
// MakePicture generates a specialized copy of the supplied Picture that will draw onto this Window.
//
// Window support PictureColor.
func (w *Window) MakePicture(p pixel.Picture) pixel.TargetPicture {
	return w.canvas.MakePicture(p)
// SetMatrix sets a Matrix that every point will be projected by.
func (w *Window) SetMatrix(m pixel.Matrix) {
	w.canvas.SetMatrix(m)
// SetColorMask sets a global color mask for the Window.
func (w *Window) SetColorMask(c color.Color) {
	w.canvas.SetColorMask(c)
// SetSmooth sets whether the stretched Pictures drawn onto this Window should be drawn smooth or
// pixely.
func (w *Window) SetSmooth(smooth bool) {
	w.canvas.SetSmooth(smooth)
faiface's avatar
faiface committed
}
faiface's avatar
faiface committed

// Smooth returns whether the stretched Pictures drawn onto this Window are set to be drawn smooth
// or pixely.
func (w *Window) Smooth() bool {
	return w.canvas.Smooth()
faiface's avatar
faiface committed
}

// Clear clears the Window with a color.
func (w *Window) Clear(c color.Color) {
	w.canvas.Clear(c)
faiface's avatar
faiface committed
}