Struct sfml::graphics::RenderWindow
source · pub struct RenderWindow { /* private fields */ }
Expand description
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.
Usage example
use sfml::window::{Event, Style};
use sfml::graphics::{RenderWindow, RenderTarget, Color};
// Create a new window
let mut window = RenderWindow::new((800, 600),
"SFML window",
Style::CLOSE,
&Default::default());
// Limit the framerate to 60 frames per second (this step is optional)
window.set_framerate_limit(60);
// The main loop - ends as soon as the window is closed
while window.is_open() {
// Event processing
while let Some(event) = window.poll_event() {
match event {
Event::Closed => window.close(),
_ => {}
}
}
window.clear(Color::BLACK);
// SFML drawing commands go here...
// End the current frame and display its contents on screen
window.display();
}
Implementations§
source§impl RenderWindow
impl RenderWindow
sourcepub fn new<V: Into<VideoMode>, S: SfStrConv>(
mode: V,
title: S,
style: Style,
settings: &ContextSettings
) -> RenderWindow
pub fn new<V: Into<VideoMode>, S: SfStrConv>(
mode: V,
title: S,
style: Style,
settings: &ContextSettings
) -> 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
Usage example
use sfml::window::Style;
use sfml::graphics::{RenderWindow};
// Create a new window
let mut window = RenderWindow::new((800, 600),
"SFML window",
Style::CLOSE,
&Default::default());
sourcepub unsafe fn from_handle(
handle: Handle,
settings: &ContextSettings
) -> RenderWindow
pub unsafe fn from_handle(
handle: Handle,
settings: &ContextSettings
) -> RenderWindow
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
sourcepub fn system_handle(&self) -> Handle
pub fn system_handle(&self) -> Handle
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.
sourcepub unsafe fn set_icon(&mut self, width: u32, height: u32, pixels: &[u8])
pub unsafe fn set_icon(&mut self, width: u32, height: u32, pixels: &[u8])
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).
Usage example
while window.is_open() {
// Creates a bright red window icon
let (width, height) = (1, 1);
let pixels: [u8; 4] = [255, 0, 0, 255];
unsafe { window.set_icon(width, height, &pixels); }
window.display();
}
sourcepub fn poll_event(&mut self) -> Option<Event>
pub fn poll_event(&mut self) -> Option<Event>
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
Usage example
while window.is_open() {
// Event processing
while let Some(event) = window.poll_event() {
match event {
Event::Closed => window.close(),
_ => {},
}
}
}
sourcepub fn wait_event(&mut self) -> Option<Event>
pub fn wait_event(&mut self) -> Option<Event>
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
Usage example
// The main loop - ends as soon as the window is closed
while window.is_open() {
// Event processing
match window.wait_event() { // Stops program from continuing until new event occurs
Some(Event::Closed) => window.close(),
_ => {},
}
}
sourcepub fn close(&mut self)
pub fn close(&mut self)
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.
Usage Example
// The main loop - ends as soon as the window is closed
while window.is_open() {
// Event processing
while let Some(event) = window.poll_event() {
match event {
Event::Closed => window.close(),
_ => {}
}
}
}
// Once window is closed, we can do other things.
sourcepub fn is_open(&self) -> bool
pub fn is_open(&self) -> bool
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.
Usage Example
use sfml::window::{Event, Style};
use sfml::graphics::RenderWindow;
// Create a new window
let mut window = RenderWindow::new((800, 600),
"SFML window",
Style::CLOSE,
&Default::default());
while window.is_open() {
// Do something
}
sourcepub fn display(&mut self)
pub fn display(&mut self)
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.
Usage Example
while window.is_open() {
window.clear(Color::BLACK);
// Draw something
window.display();
}
sourcepub fn set_framerate_limit(&mut self, limit: u32)
pub fn set_framerate_limit(&mut self, limit: u32)
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)
sourcepub fn settings(&self) -> &ContextSettings
pub fn settings(&self) -> &ContextSettings
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
sourcepub fn set_visible(&mut self, visible: bool)
pub fn set_visible(&mut self, visible: bool)
sourcepub fn set_mouse_cursor_visible(&mut self, visible: bool)
pub fn set_mouse_cursor_visible(&mut self, visible: bool)
sourcepub fn set_mouse_cursor_grabbed(&mut self, grabbed: bool)
pub fn set_mouse_cursor_grabbed(&mut self, grabbed: bool)
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.
sourcepub fn set_vertical_sync_enabled(&mut self, enabled: bool)
pub fn set_vertical_sync_enabled(&mut self, enabled: bool)
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
sourcepub fn set_key_repeat_enabled(&mut self, enabled: bool)
pub fn set_key_repeat_enabled(&mut self, enabled: bool)
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
sourcepub fn set_active(&mut self, enabled: bool) -> bool
pub fn set_active(&mut self, enabled: bool) -> bool
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
Return true if operation was successful, false otherwise
sourcepub fn set_joystick_threshold(&mut self, threshold: f32)
pub fn set_joystick_threshold(&mut self, threshold: f32)
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]
sourcepub fn set_position(&mut self, position: Vector2i)
pub fn set_position(&mut self, position: Vector2i)
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
Usage Example
window.set_position(Vector2::new(100, 400));
use std::{thread, time::Duration};
// You need to wait for the OS the set the window's position before checking
thread::sleep(Duration::from_millis(250));
assert_eq!(window.position(), Vector2::new(100, 400));
sourcepub fn set_size<S: Into<Vector2u>>(&mut self, size: S)
pub fn set_size<S: Into<Vector2u>>(&mut self, size: S)
Change the size of the rendering region of a window
Arguments
- size - New size, in pixels
Usage Example
window.set_size(Vector2::new(100, 400));
use std::{thread, time::Duration};
// You need to wait for the OS the set the window's size before checking
thread::sleep(Duration::from_millis(250));
assert_eq!(window.size(), Vector2::new(100, 400));
sourcepub fn mouse_position(&self) -> Vector2i
pub fn mouse_position(&self) -> Vector2i
Returns the current position of the mouse relative to the window.
sourcepub fn set_mouse_position(&mut self, position: Vector2i)
pub fn set_mouse_position(&mut self, position: Vector2i)
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
sourcepub unsafe fn set_mouse_cursor(&mut self, cursor: &Cursor)
pub unsafe fn set_mouse_cursor(&mut self, cursor: &Cursor)
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.
Usage Example
let cursor = Cursor::from_system(CursorType::Arrow);
if let Some(arrow_cursor) = &cursor {
unsafe { window.set_mouse_cursor(arrow_cursor); }
}
// You need to ensure the SFML window closes before the cursor's end of life.
// Doing it the other way around will cause undefined behavior.
window.close();
drop(cursor);
sourcepub fn touch_position(&self, finger: u32) -> Vector2i
pub fn touch_position(&self, finger: u32) -> Vector2i
Returns the current position of a touch in window coordinates.
sourcepub fn has_focus(&self) -> bool
pub fn has_focus(&self) -> bool
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.
sourcepub fn request_focus(&self)
pub fn request_focus(&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
.
Usage Example
window.request_focus();
use std::{thread, time::Duration};
// You need to wait for the OS the set the window's visibility before checking
thread::sleep(Duration::from_millis(250));
assert_eq!(window.has_focus(), true);
Trait Implementations§
source§impl Debug for RenderWindow
impl Debug for RenderWindow
source§impl Drop for RenderWindow
impl Drop for RenderWindow
source§impl RenderTarget for RenderWindow
impl RenderTarget for RenderWindow
source§fn push_gl_states(&mut self)
fn push_gl_states(&mut self)
source§fn pop_gl_states(&mut self)
fn pop_gl_states(&mut self)
source§fn reset_gl_states(&mut self)
fn reset_gl_states(&mut self)
source§fn default_view(&self) -> &View
fn default_view(&self) -> &View
source§fn map_pixel_to_coords(&self, point: Vector2i, view: &View) -> Vector2f
fn map_pixel_to_coords(&self, point: Vector2i, view: &View) -> Vector2f
source§fn map_pixel_to_coords_current_view(&self, point: Vector2i) -> Vector2f
fn map_pixel_to_coords_current_view(&self, point: Vector2i) -> Vector2f
source§fn map_coords_to_pixel(&self, point: Vector2f, view: &View) -> Vector2i
fn map_coords_to_pixel(&self, point: Vector2f, view: &View) -> Vector2i
source§fn map_coords_to_pixel_current_view(&self, point: Vector2f) -> Vector2i
fn map_coords_to_pixel_current_view(&self, point: Vector2f) -> Vector2i
source§fn draw(&mut self, object: &dyn Drawable)
fn draw(&mut self, object: &dyn Drawable)
source§fn draw_with_renderstates(
&mut self,
object: &dyn Drawable,
render_states: &RenderStates<'_, '_, '_>
)
fn draw_with_renderstates(
&mut self,
object: &dyn Drawable,
render_states: &RenderStates<'_, '_, '_>
)
RenderStates
Read moresource§fn draw_text(&self, text: &Text<'_>, render_states: &RenderStates<'_, '_, '_>)
fn draw_text(&self, text: &Text<'_>, render_states: &RenderStates<'_, '_, '_>)
source§fn draw_shape(
&self,
shape: &CustomShape<'_>,
render_states: &RenderStates<'_, '_, '_>
)
fn draw_shape(
&self,
shape: &CustomShape<'_>,
render_states: &RenderStates<'_, '_, '_>
)
source§fn draw_sprite(
&self,
sprite: &Sprite<'_>,
render_states: &RenderStates<'_, '_, '_>
)
fn draw_sprite(
&self,
sprite: &Sprite<'_>,
render_states: &RenderStates<'_, '_, '_>
)
source§fn draw_rc_sprite(
&self,
sprite: &RcSprite,
render_states: &RenderStates<'_, '_, '_>
)
fn draw_rc_sprite(
&self,
sprite: &RcSprite,
render_states: &RenderStates<'_, '_, '_>
)
RcSprite
source§fn draw_circle_shape(
&self,
circle_shape: &CircleShape<'_>,
render_states: &RenderStates<'_, '_, '_>
)
fn draw_circle_shape(
&self,
circle_shape: &CircleShape<'_>,
render_states: &RenderStates<'_, '_, '_>
)
CircleShape
source§fn draw_rectangle_shape(
&self,
rectangle_shape: &RectangleShape<'_>,
render_states: &RenderStates<'_, '_, '_>
)
fn draw_rectangle_shape(
&self,
rectangle_shape: &RectangleShape<'_>,
render_states: &RenderStates<'_, '_, '_>
)
RectangleShape
source§fn draw_convex_shape(
&self,
convex_shape: &ConvexShape<'_>,
render_states: &RenderStates<'_, '_, '_>
)
fn draw_convex_shape(
&self,
convex_shape: &ConvexShape<'_>,
render_states: &RenderStates<'_, '_, '_>
)
ConvexShape
source§fn draw_vertex_buffer(
&self,
vertex_buffer: &VertexBuffer,
render_states: &RenderStates<'_, '_, '_>
)
fn draw_vertex_buffer(
&self,
vertex_buffer: &VertexBuffer,
render_states: &RenderStates<'_, '_, '_>
)
VertexBuffer