-
Notifications
You must be signed in to change notification settings - Fork 1
/
window.go
274 lines (231 loc) · 7.51 KB
/
window.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright (C) 2012-2014 by krepa098. All rights reserved.
// Use of this source code is governed by a zlib-style
// license that can be found in the license.txt file.
package gosfml2
// #include <SFML/Window/Window.h>
// #include <stdlib.h>
import "C"
import (
"errors"
"runtime"
"unsafe"
)
/////////////////////////////////////
/// CONSTS
/////////////////////////////////////
const (
StyleNone WindowStyle = C.sfNone ///< No border / title bar (this flag and all others are mutually exclusive)
StyleTitlebar WindowStyle = C.sfTitlebar ///< Title bar + fixed border
StyleResize WindowStyle = C.sfResize ///< Titlebar + resizable border + maximize button
StyleClose WindowStyle = C.sfClose ///< Titlebar + close button
StyleFullscreen WindowStyle = C.sfFullscreen ///< Fullscreen mode (this flag and all others are mutually exclusive)
StyleDefault WindowStyle = C.sfDefaultStyle ///< Default window style
)
type WindowStyle int
/////////////////////////////////////
/// STRUCTS
/////////////////////////////////////
type Window struct {
cptr *C.sfWindow
}
/////////////////////////////////////
/// INTERFACES
/////////////////////////////////////
// Window and RenderWindow are SystemWindows
type SystemWindow interface {
SetVSyncEnabled(bool)
SetFramerateLimit(uint)
SetJoystickThreshold(float32)
SetKeyRepeatEnabled(bool)
Display()
IsOpen() bool
Close()
SetTitle(string)
SetIcon(uint, uint, []byte) error
SetMouseCursorVisible(bool)
SetActive(bool) bool
}
//TEST
var _ SystemWindow = (*RenderWindow)(nil)
var _ SystemWindow = (*Window)(nil)
/////////////////////////////////////
/// FUNCTIONS
/////////////////////////////////////
// Construct a new window
//
// videoMode: Video mode to use
// title: Title of the window
// style: Window style
// contextSettings: Creation settings (pass nil to use default values)
func NewWindow(videoMode VideoMode, title string, style WindowStyle, contextSettings ContextSettings) (window *Window) {
//string conversion
utf32 := strToRunes(title)
//convert contextSettings to C
cs := contextSettings.toC()
//create the window
window = &Window{C.sfWindow_createUnicode(videoMode.toC(), (*C.sfUint32)(unsafe.Pointer(&utf32[0])), C.sfUint32(style), &cs)}
//GC cleanup
runtime.SetFinalizer(window, (*Window).destroy)
return window
}
// Get the creation settings of a window
func (this *Window) GetSettings() (settings ContextSettings) {
settings.fromC(C.sfWindow_getSettings(this.cptr))
return
}
// Change the size of the rendering region of a window
//
// size: New size, in pixels
func (this *Window) SetSize(size Vector2u) {
C.sfWindow_setSize(this.cptr, size.toC())
}
// Get the size of the rendering region of a window
func (this *Window) GetSize() Vector2u {
size := C.sfWindow_getSize(this.cptr)
return Vector2u{uint(size.x), uint(size.y)}
}
// Change the position of a window on screen
//
// Only works for top-level windows
//
// pos: New position, in pixels
func (this *Window) SetPosition(pos Vector2i) {
C.sfWindow_setPosition(this.cptr, pos.toC())
}
// Get the position of a render window
func (this *Window) GetPosition() (pos Vector2i) {
pos.fromC(C.sfWindow_getPosition(this.cptr))
return
}
// Tell whether or not a window is opened
func (this *Window) IsOpen() bool {
return sfBool2Go(C.sfWindow_isOpen(this.cptr))
}
// Close a window (but doesn't destroy the internal data)
func (this *Window) Close() {
C.sfWindow_close(this.cptr)
}
// Destroy an existing window
func (this *Window) destroy() {
globalMutex.Lock()
C.sfWindow_destroy(this.cptr)
globalMutex.Unlock()
}
// Get the event on top of event queue of a window, if any, and pop it
//
// returns nil if there are no events left.
func (this *Window) PollEvent() Event {
cEvent := C.sfEvent{}
globalMutex.Lock()
hasEvent := C.sfWindow_pollEvent(this.cptr, &cEvent)
globalMutex.Unlock()
if hasEvent != 0 {
return handleEvent(&cEvent)
}
return nil
}
// Wait for an event and return it
func (this *Window) WaitEvent() Event {
cEvent := C.sfEvent{}
globalMutex.Lock()
hasError := C.sfWindow_waitEvent(this.cptr, &cEvent)
globalMutex.Unlock()
if hasError != 0 {
return handleEvent(&cEvent)
}
return nil
}
// Change the title of a window
//
// title: New title
func (this *Window) SetTitle(title string) {
utf32 := strToRunes(title)
C.sfWindow_setUnicodeTitle(this.cptr, (*C.sfUint32)(unsafe.Pointer(&utf32[0])))
}
// Change a window's icon
//
// width: Icon's width, in pixels
// height: Icon's height, in pixels
// pixels: Slice of pixels, format must be RGBA 32 bits
func (this *Window) SetIcon(width, height uint, data []byte) error {
if len(data) >= int(width*height*4) {
C.sfWindow_setIcon(this.cptr, C.uint(width), C.uint(height), (*C.sfUint8)(&data[0]))
return nil
}
return errors.New("SetIcon: Slice length does not match specified dimensions")
}
// Limit the framerate to a maximum fixed frequency for a window
//
// limit: Framerate limit, in frames per seconds (use 0 to disable limit)
func (this *Window) SetFramerateLimit(limit uint) {
C.sfWindow_setFramerateLimit(this.cptr, C.uint(limit))
}
///Change the joystick threshold, ie. the value below which no move event will be generated
//
// threshold: New threshold, in range [0, 100]
func (this *Window) SetJoystickThreshold(threshold float32) {
C.sfWindow_setJoystickThreshold(this.cptr, C.float(threshold))
}
// Enable or disable automatic key-repeat
//
// If key repeat is enabled, you will receive repeated
// KeyPress events while keeping a key pressed. If it is disabled,
// you will only get a single event when the key is pressed.
//
// Key repeat is enabled by default.
func (this *Window) SetKeyRepeatEnabled(enabled bool) {
C.sfWindow_setKeyRepeatEnabled(this.cptr, goBool2C(enabled))
}
// Display a window on screen
func (this *Window) Display() {
globalMutex.Lock()
C.sfWindow_display(this.cptr)
globalMutex.Unlock()
}
// Enable / disable vertical synchronization on a window
//
// enabled: true to enable v-sync, false to deactivate
func (this *Window) SetVSyncEnabled(enabled bool) {
globalMutex.Lock()
C.sfWindow_setVerticalSyncEnabled(this.cptr, goBool2C(enabled))
globalMutex.Unlock()
}
// Activate or deactivate a window as the current target for rendering
//
// active: true to activate, false to deactivate
//
// return True if operation was successful, false otherwise
func (this *Window) SetActive(active bool) bool {
globalMutex.Lock()
success := sfBool2Go(C.sfWindow_setActive(this.cptr, goBool2C(active)))
globalMutex.Unlock()
return success
}
// Show or hide the mouse cursor on a window
//
// visible: true to show, false to hide
func (this *Window) SetMouseCursorVisible(visible bool) {
C.sfWindow_setMouseCursorVisible(this.cptr, goBool2C(visible))
}
// Check whether the window has the input focus
//
// At any given time, only one window may have the input focus
// to receive input events such as keystrokes or most mouse
// events.
//
// True if window has focus, false otherwise
func (this *Window) HasFocus() bool {
return sfBool2Go(C.sfWindow_hasFocus(this.cptr))
}
// Request the current window to be made the active
// foreground window
//
// At any given time, only one window may have the input focus
// to receive input events such as keystrokes or mouse events.
// If a window requests focus, it only hints to the operating
// system, that it would like to be focused. The operating system
// is free to deny the request.
// This is not to be confused with Window.SetActive().
func (this *Window) RequestFocus() {
C.sfWindow_requestFocus(this.cptr)
}