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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use crate::{
IntoSfResult, SfError, SfResult,
cpp::FBox,
ffi::graphics as ffi,
graphics::{
CircleShape, Color, ConvexShape, CustomShape, Drawable, IntRect, PrimitiveType, RcSprite,
RcText, RectangleShape, RenderStates, RenderTarget, Sprite, Text, Vertex, VertexBuffer,
View,
},
system::{SfStrConv, Vector2f, Vector2i, Vector2u},
window::{ContextSettings, Cursor, Event, Handle, Style, VideoMode, thread_safety},
};
decl_opaque! {
/// [`Window`] that can serve as a target for 2D drawing.
///
/// `RenderWindow` is the main type of the graphics module.
/// It defines an OS window that can be painted using the other classes
/// of the graphics module.
///
/// [`Window`]: crate::window::Window
pub RenderWindow;
}
/// Creation
impl RenderWindow {
/// Construct a new render window
///
/// This function creates the render window with the size and pixel
/// depth defined in mode. An optional style can be passed to
/// customize the look and behaviour of the window (borders,
/// title bar, resizable, closable, ...). If style contains
/// [`Style::FULLSCREEN`], then mode must be a valid video mode.
///
/// The fourth parameter is a pointer to a structure specifying
/// advanced OpenGL context settings such as antialiasing,
/// depth-buffer bits, etc.
///
/// # Arguments
/// * mode - Video mode to use (defines the width, height and depth of the
/// rendering area of the render window)
/// * title - Title of the render window
/// * style - Window style
/// * settings - Additional settings for the underlying OpenGL context
pub fn new<V: Into<VideoMode>, S: SfStrConv>(
mode: V,
title: S,
style: Style,
settings: &ContextSettings,
) -> SfResult<FBox<Self>> {
thread_safety::set_window_thread();
title.with_as_sfstr(|sfstr| {
let ptr = unsafe {
ffi::sfRenderWindow_new_mtss(mode.into(), sfstr.as_ptr(), style.bits(), settings)
};
FBox::new(ptr).ok_or(SfError::CallFailed)
})
}
/// Recreate with new settings. See [`Self::new`] for more information.
pub fn recreate<V: Into<VideoMode>, S: SfStrConv>(
&mut self,
mode: V,
title: S,
style: Style,
settings: &ContextSettings,
) {
thread_safety::set_window_thread();
title.with_as_sfstr(|sfstr| unsafe {
ffi::sfRenderWindow_create_mtss(
self,
mode.into(),
sfstr.as_ptr(),
style.bits(),
settings,
);
});
}
/// Create a render window from an existing platform-specific window handle
///
/// This function creates a render window based on an existing platform
/// specific window handle which has been allocated outside of SFML. This is
/// only intended to be used in cases where you need to integrate SFML with
/// some other windowing library.
///
/// # Safety
///
/// It is the caller's responsibility to ensure that it is called with a valid window handle.
///
/// # Arguments
/// * handle - The handle to the platform-specific window handle to use for
/// the window.
/// * settings - Additional settings for the underlying OpenGL context
pub unsafe fn from_handle(handle: Handle, settings: &ContextSettings) -> SfResult<FBox<Self>> {
thread_safety::set_window_thread();
let ptr = unsafe { ffi::sfRenderWindow_new_handle_settings(handle, settings) };
FBox::new(ptr).ok_or(SfError::CallFailed)
}
}
/// Event handling
impl RenderWindow {
/// Pop the event on top of event queue, if any, and return it
///
/// This function is not blocking: if there's no pending event then
/// it will return `None`.
/// Note that more than one event may be present in the event queue,
/// thus you should always call this function in a loop
/// to make sure that you process every pending event.
///
/// Returns `Some(event)` if an event was returned, or `None` if the event queue was empty
pub fn poll_event(&mut self) -> Option<Event> {
let mut event = std::mem::MaybeUninit::uninit();
let have_event = unsafe { ffi::sfRenderWindow_pollEvent(self, event.as_mut_ptr()) };
if have_event {
unsafe { Event::from_raw(&event.assume_init()) }
} else {
None
}
}
/// Wait for an event and return it
///
/// This function is blocking: if there's no pending event then
/// it will wait until an event is received.
///
/// This function is typically used when you have a thread that
/// is dedicated to events handling: you want to make this thread
/// sleep as long as no new event is received.
///
/// Returns `Some(event)` or `None` if an error has occured
pub fn wait_event(&mut self) -> Option<Event> {
let mut event = std::mem::MaybeUninit::uninit();
let have_event = unsafe { ffi::sfRenderWindow_waitEvent(self, event.as_mut_ptr()) };
if have_event {
unsafe { Event::from_raw(&event.assume_init()) }
} else {
None
}
}
}
/// Rendering. See also [`RenderTarget`], which `RenderWindow` implements.
impl RenderWindow {
/// Display on screen what has been rendered to the window so far
///
/// This function is typically called after all OpenGL rendering
/// has been done for the current frame, in order to show
/// it on screen.
pub fn display(&mut self) {
unsafe { ffi::sfRenderWindow_display(self) }
}
/// Limit the framerate to a maximum fixed frequency
///
/// If a limit is set, the window will use a small delay after
/// each call to [`RenderWindow::display`] to ensure that the current frame
/// lasted long enough to match the framerate limit.
///
/// # Arguments
/// * limit - Framerate limit, in frames per seconds (use 0 to disable limit)
pub fn set_framerate_limit(&mut self, limit: u32) {
unsafe { ffi::sfRenderWindow_setFramerateLimit(self, limit) }
}
/// Get the settings of the OpenGL context of a window
///
/// Note that these settings may be different from what was
/// passed to the [`RenderWindow::new`] function,
/// if one or more settings were not supported. In this case,
/// SFML chose the closest match.
///
/// Return a structure containing the OpenGL context settings
#[must_use]
pub fn settings(&self) -> &ContextSettings {
unsafe { &*ffi::sfRenderWindow_getSettings(self) }
}
/// Tell if the render texture will use sRGB encoding when drawing it
///
/// Returns true if sRGB encoding is enabled, false if sRGB encoding is disabled
#[must_use]
pub fn is_srgb(&self) -> bool {
unsafe { ffi::sfRenderWindow_isSrgb(self) }
}
/// Enable or disable vertical synchronization
///
/// Activating vertical synchronization will limit the number
/// of frames displayed to the refresh rate of the monitor.
/// This can avoid some visual artifacts, and limit the framerate
/// to a good value (but not constant across different computers).
///
/// # Arguments
/// * enabled - true to enable v-sync, false to deactivate
pub fn set_vertical_sync_enabled(&mut self, enabled: bool) {
unsafe {
ffi::sfRenderWindow_setVerticalSyncEnabled(self, enabled);
}
}
/// Activate or deactivate a render window as the current target for OpenGL rendering
///
/// A window is active only on the current thread, if you want to
/// make it active on another thread you have to deactivate it
/// on the previous thread first if it was active.
/// Only one window can be active on a thread at a time, thus
/// the window previously active (if any) automatically gets deactivated.
///
/// # Arguments
/// * active - true to activate, false to deactivate
pub fn set_active(&mut self, enabled: bool) -> SfResult<()> {
unsafe { ffi::sfRenderWindow_setActive(self, enabled) }.into_sf_result()
}
}
/// Input
impl RenderWindow {
/// Show or hide the mouse cursor
///
/// # Arguments
/// * visible - true to false to hide
pub fn set_mouse_cursor_visible(&mut self, visible: bool) {
unsafe {
ffi::sfRenderWindow_setMouseCursorVisible(self, visible);
}
}
/// Grab or release the mouse cursor.
///
/// If set, grabs the mouse cursor inside this window's client area so it may no longer be
/// moved outside its bounds. Note that grabbing is only active while the window has focus.
pub fn set_mouse_cursor_grabbed(&mut self, grabbed: bool) {
unsafe { ffi::sfRenderWindow_setMouseCursorGrabbed(self, grabbed) }
}
/// Enable or disable automatic key-repeat
///
/// If key repeat is enabled, you will receive repeated
/// [`crate::window::Event::KeyPressed`] 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.
///
/// # Arguments
/// * enabled - true to enable, false to disable
pub fn set_key_repeat_enabled(&mut self, enabled: bool) {
unsafe {
ffi::sfRenderWindow_setKeyRepeatEnabled(self, enabled);
}
}
/// Change the joystick threshold
///
/// The joystick threshold is the value below which
/// no [`crate::window::Event::JoystickMoved`] event will be generated.
///
/// # Arguments
/// * threshold - New threshold, in the range [0, 100]
pub fn set_joystick_threshold(&mut self, threshold: f32) {
unsafe { ffi::sfRenderWindow_setJoystickThreshold(self, threshold) }
}
/// Returns the current position of the mouse relative to the window.
#[must_use]
pub fn mouse_position(&self) -> Vector2i {
unsafe { crate::ffi::graphics::sfMouse_getPositionRenderWindow(self) }
}
/// Set the current position of the mouse relatively to a render window
///
/// This function sets the current position of the mouse cursor relative
/// to the given render window
///
/// # Arguments
/// * `position` - the positon to set
pub fn set_mouse_position(&mut self, position: Vector2i) {
unsafe { crate::ffi::graphics::sfMouse_setPositionRenderWindow(position, self) }
}
/// Returns the current position of a touch in window coordinates.
#[must_use]
pub fn touch_position(&self, finger: u32) -> Vector2i {
unsafe { crate::ffi::graphics::sfTouch_getPositionRenderWindow(finger, self) }
}
}
/// Window operations
impl RenderWindow {
/// Change a render window's icon
/// pixels must be an array of width x height pixels in 32-bits RGBA format.
///
/// # Arguments
/// * width - Icon's width, in pixels
/// * height - Icon's height, in pixels
/// * pixels - Vector of pixels
///
/// # Safety
///
/// `pixels` not being at least `width * height * 4` will likely cause undefined behavior.
///
/// Platform-specific behavior is also unclear (limits on max size, etc).
pub unsafe fn set_icon(&mut self, width: u32, height: u32, pixels: &[u8]) {
unsafe { ffi::sfRenderWindow_setIcon(self, width, height, pixels.as_ptr()) }
}
/// Close a render window and destroy all the attached resources
///
/// After calling this method, the Window object remains
/// valid.
/// All other functions such as `poll_event` or display
/// will still work (i.e. you don't have to test `is_open`
/// every time), and will have no effect on closed windows.
pub fn close(&mut self) {
unsafe {
ffi::sfRenderWindow_close(self);
}
}
/// Tell whether or not a window is opened
///
/// This function returns whether or not the window exists.
/// Note that a hidden window `(set_visible(false))` will return
/// true.
#[must_use]
pub fn is_open(&self) -> bool {
unsafe { ffi::sfRenderWindow_isOpen(self) }
}
/// Change the title of a window
///
/// # Arguments
/// * title - New title
pub fn set_title<S: SfStrConv>(&mut self, title: S) {
title.with_as_sfstr(|sfstr| unsafe {
ffi::sfRenderWindow_setUnicodeTitle(self, sfstr.as_ptr());
})
}
/// Show or hide a window.
///
/// # Arguments
/// * visible - true to show the window, false to hide it
pub fn set_visible(&mut self, visible: bool) {
unsafe {
ffi::sfRenderWindow_setVisible(self, visible);
}
}
/// Get the position of a window
///
/// Return the position in pixels
#[must_use]
pub fn position(&self) -> Vector2i {
unsafe { ffi::sfRenderWindow_getPosition(self) }
}
/// Change the position of a window on screen
///
/// This function only works for top-level windows
/// (i.e. it will be ignored for windows created from
/// the handle of a child window/control).
///
/// # Arguments
/// * position - New position of the window, in pixels
pub fn set_position(&mut self, position: Vector2i) {
unsafe { ffi::sfRenderWindow_setPosition(self, position) }
}
/// Change the size of the rendering region of a window
///
/// # Arguments
/// * size - New size, in pixels
pub fn set_size<S: Into<Vector2u>>(&mut self, size: S) {
unsafe { ffi::sfRenderWindow_setSize(self, size.into()) }
}
/// Set the displayed cursor to a native system cursor.
///
/// Upon window creation, the arrow cursor is used by default.
///
/// # Safety
///
/// The cursor can not be destroyed while in use by the window.
pub unsafe fn set_mouse_cursor(&mut self, cursor: &Cursor) {
unsafe { ffi::sfRenderWindow_setMouseCursor(self, cursor) }
}
/// 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.
#[must_use]
pub fn has_focus(&self) -> bool {
unsafe { ffi::sfRenderWindow_hasFocus(self) }
}
/// 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 [`RenderWindow::set_active`].
pub fn request_focus(&mut self) {
unsafe { ffi::sfRenderWindow_requestFocus(self) }
}
}
/// System integration
impl RenderWindow {
/// Get the OS-specific handle of the window.
///
/// The type of the returned handle is Handle, which is a typedef to the handle type defined by the OS.
/// You shouldn't need to use this function, unless you have very specific stuff to implement that SFML
/// doesn't support, or implement a temporary workaround until a bug is fixed.
#[must_use]
pub fn system_handle(&self) -> Handle {
unsafe { ffi::sfRenderWindow_getSystemHandle(self) }
}
}
impl RenderTarget for RenderWindow {
fn push_gl_states(&mut self) {
unsafe { ffi::sfRenderWindow_pushGLStates(self) }
}
fn pop_gl_states(&mut self) {
unsafe { ffi::sfRenderWindow_popGLStates(self) }
}
fn reset_gl_states(&mut self) {
unsafe { ffi::sfRenderWindow_resetGLStates(self) }
}
fn set_view(&mut self, view: &View) {
unsafe { ffi::sfRenderWindow_setView(self, view) }
}
fn view(&self) -> &View {
unsafe { &*(ffi::sfRenderWindow_getView(self)) }
}
fn default_view(&self) -> &View {
unsafe { &*(ffi::sfRenderWindow_getDefaultView(self)) }
}
fn map_pixel_to_coords(&self, point: Vector2i, view: &View) -> Vector2f {
unsafe { ffi::sfRenderWindow_mapPixelToCoords_View(self, point, view) }
}
fn map_pixel_to_coords_current_view(&self, point: Vector2i) -> Vector2f {
unsafe { ffi::sfRenderWindow_mapPixelToCoords(self, point) }
}
fn map_coords_to_pixel(&self, point: Vector2f, view: &View) -> Vector2i {
unsafe { ffi::sfRenderWindow_mapCoordsToPixel_View(self, point, view) }
}
fn map_coords_to_pixel_current_view(&self, point: Vector2f) -> Vector2i {
unsafe { ffi::sfRenderWindow_mapCoordsToPixel(self, point) }
}
fn viewport(&self, view: &View) -> IntRect {
unsafe { ffi::sfRenderWindow_getViewport(self, view) }
}
fn size(&self) -> Vector2u {
unsafe { ffi::sfRenderWindow_getSize(self) }
}
fn draw(&mut self, object: &dyn Drawable) {
object.draw(self, &RenderStates::DEFAULT);
}
fn draw_with_renderstates(&mut self, object: &dyn Drawable, render_states: &RenderStates) {
object.draw(self, render_states);
}
fn draw_text(&mut self, text: &Text, render_states: &RenderStates) {
unsafe { ffi::sfRenderWindow_drawText(self, text.raw(), render_states) }
}
fn draw_rc_text(&mut self, text: &RcText, render_states: &RenderStates) {
unsafe { ffi::sfRenderWindow_drawText(self, text.raw(), render_states) }
}
fn draw_shape(&mut self, shape: &CustomShape, render_states: &RenderStates) {
unsafe { ffi::sfRenderWindow_drawShape(self, shape.raw().cast(), render_states) }
}
fn draw_sprite(&mut self, sprite: &Sprite, render_states: &RenderStates) {
unsafe { ffi::sfRenderWindow_drawSprite(self, sprite.raw(), render_states) }
}
fn draw_rc_sprite(&mut self, sprite: &RcSprite, render_states: &RenderStates) {
unsafe { ffi::sfRenderWindow_drawSprite(self, sprite.raw(), render_states) }
}
fn draw_circle_shape(&mut self, circle_shape: &CircleShape, render_states: &RenderStates) {
unsafe { ffi::sfRenderWindow_drawCircleShape(self, circle_shape.raw(), render_states) }
}
fn draw_rectangle_shape(
&mut self,
rectangle_shape: &RectangleShape,
render_states: &RenderStates,
) {
unsafe {
ffi::sfRenderWindow_drawRectangleShape(self, rectangle_shape.raw(), render_states)
}
}
fn draw_convex_shape(&mut self, convex_shape: &ConvexShape, render_states: &RenderStates) {
unsafe { ffi::sfRenderWindow_drawConvexShape(self, convex_shape.raw(), render_states) }
}
fn draw_vertex_buffer(&mut self, vertex_buffer: &VertexBuffer, render_states: &RenderStates) {
unsafe { ffi::sfRenderWindow_drawVertexBuffer(self, vertex_buffer, render_states) }
}
fn draw_primitives(&mut self, vertices: &[Vertex], ty: PrimitiveType, rs: &RenderStates) {
unsafe {
ffi::sfRenderWindow_drawPrimitives(
self,
vertices.as_ptr().cast(),
vertices.len(),
ty.0,
rs,
);
}
}
fn clear(&mut self, color: Color) {
unsafe { ffi::sfRenderWindow_clear(self, color) }
}
}
impl Drop for RenderWindow {
fn drop(&mut self) {
unsafe {
ffi::sfRenderWindow_del(self);
}
}
}