Newer
Older
"github.com/faiface/pixel/pixelgl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/pkg/errors"
)
// 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.
// 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.).
// 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).
// Window is a window handler. Use this type to manipulate a window (input, drawing, ...).
type Window struct {
window *glfw.Window
config WindowConfig
// need to save these to correctly restore a fullscreen window
restore struct {
xpos, ypos, width, height int
}
// NewWindow creates a new window with it's properties specified in the provided config.
//
// If window creation fails, an error is returned.
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)
return nil
})
if err != nil {
return nil, errors.Wrap(err, "creating window failed")
}
// Delete destroys a window. The window can't be used any further.
func (w *Window) Delete() {
w.Begin()
pixelgl.Do(func() {
w.window.Destroy()
})
}
pixelgl.Do(func() {
if w.config.VSync {
glfw.SwapInterval(1)
}
w.window.SwapBuffers()
glfw.PollEvents()
})
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
}
// SetTitle changes the title of a window.
func (w *Window) SetTitle(title string) {
w.Begin()
defer w.End()
pixelgl.Do(func() {
w.window.SetTitle(title)
})
}
// SetSize resizes a window to the specified size in pixels.
// In case of a fullscreen window, it changes the resolution of that window.
func (w *Window) SetSize(width, height float64) {
w.Begin()
defer w.End()
pixelgl.Do(func() {
w.window.SetSize(int(width), int(height))
})
}
// Size returns the size of the client area of a window (the part you can draw on).
func (w *Window) Size() (width, height float64) {
w.Begin()
defer w.End()
pixelgl.Do(func() {
wi, hi := w.window.GetSize()
width = float64(wi)
height = float64(hi)
})
return width, height
}
// Show makes a window visible if it was hidden.
func (w *Window) Show() {
w.Begin()
defer w.End()
pixelgl.Do(func() {
w.window.Show()
})
}
// Hide hides a window if it was visible.
func (w *Window) Hide() {
w.Begin()
defer w.End()
pixelgl.Do(func() {
w.window.Hide()
})
}
// SetFullscreen sets a window fullscreen on a given monitor. If the monitor is nil, the window will be resored to windowed instead.
//
// 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) SetFullscreen(monitor *Monitor) {
if w.Monitor() != monitor {
if monitor == nil {
w.Begin()
defer w.End()
pixelgl.Do(func() {
w.window.SetMonitor(
nil,
w.restore.xpos,
w.restore.ypos,
w.restore.width,
w.restore.height,
0,
)
})
} else {
w.Begin()
defer w.End()
pixelgl.Do(func() {
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),
)
})
}
}
}
// IsFullscreen returns true if the window is in the fullscreen mode.
func (w *Window) IsFullscreen() bool {
return w.Monitor() != nil
}
// Monitor returns a monitor a fullscreen window is on. If the window is not fullscreen, this function returns nil.
func (w *Window) Monitor() *Monitor {
w.Begin()
defer w.End()
monitor := pixelgl.DoVal(func() interface{} {
return w.window.GetMonitor()
}).(*glfw.Monitor)
if monitor == nil {
return nil
}
return &Monitor{
monitor: monitor,
}
// Focus brings a window to the front and sets input focus.
func (w *Window) Focus() {
w.Begin()
pixelgl.Do(func() {
w.window.Focus()
})
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
}
// Focused returns true if a window has input focus.
func (w *Window) Focused() bool {
w.Begin()
defer w.End()
return pixelgl.DoVal(func() interface{} {
return w.window.GetAttrib(glfw.Focused) == glfw.True
}).(bool)
}
// Maximize puts a windowed window to a maximized state.
func (w *Window) Maximize() {
w.Begin()
defer w.End()
pixelgl.Do(func() {
w.window.Maximize()
})
}
// Restore restores a windowed window from a maximized state.
func (w *Window) Restore() {
w.Begin()
defer w.End()
pixelgl.Do(func() {
w.window.Restore()
})
var currentWindow struct {
sync.Mutex
handler *Window
}
//
// Note that you only need to use this function if you're designing a low-level technical plugin (such as an effect).
currentWindow.Lock()
if currentWindow.handler != w {
// End makes it possible for other windows to make their context current.
//
// Note that you only need to use this function if you're designing a low-level technical plugin (such as an effect).