use libc::{c_uint, c_float};
use std::vec::Vec;
use std::ffi::CString;
use traits::Wrappable;
use window::{event, VideoMode, ContextSettings, WindowStyle};
use sfml_types::{Vector2i, Vector2u};
use sfml_types::sfBool;
use csfml_window_sys as ffi;
pub struct Window {
window: *mut ffi::sfWindow,
}
pub struct Events {
window: *mut ffi::sfWindow,
}
impl Window {
pub fn new(mode: VideoMode,
title: &str,
style: WindowStyle,
settings: &ContextSettings) -> Option<Window> {
let c_str = CString::new(title.as_bytes()).unwrap().as_ptr();
let sf_win: *mut ffi::sfWindow = unsafe {
ffi::sfWindow_create(mode.unwrap(), c_str, style.bits(), &settings.0)
};
if sf_win.is_null() {
None
} else {
Some (Window {
window: sf_win,
})
}
}
pub fn new_with_unicode(mode: VideoMode,
title: Vec<u32>,
style: WindowStyle,
settings: &ContextSettings) -> Option<Window> {
let sf_win =
unsafe { ffi::sfWindow_createUnicode(mode.unwrap(),
title.as_ptr(),
style.bits(), &settings.0) };
if sf_win.is_null() {
None
} else {
Some (Window {
window: sf_win,
})
}
}
pub fn events(&self) -> Events {
Events {
window: self.window,
}
}
pub fn poll_event(&mut self) -> event::Event {
let mut event = ffi::sfEvent { data: [032; 6] };
let have_event = unsafe {
ffi::sfWindow_pollEvent(self.window, &mut event).to_bool()
};
if !have_event {
event::NoEvent
} else {
event::raw::get_wrapped_event(&mut event)
}
}
pub fn wait_event(&mut self) -> event::Event {
let mut event = ffi::sfEvent { data: [032; 6] };
let have_event = unsafe {
ffi::sfWindow_waitEvent(self.window, &mut event).to_bool()
};
if !have_event {
event::NoEvent
} else {
event::raw::get_wrapped_event(&mut event)
}
}
pub fn set_unicode_title(&mut self, title: Vec<u32>) {
unsafe {
ffi::sfWindow_setUnicodeTitle(self.window, title.as_ptr())
}
}
pub fn set_icon(&mut self, width: u32, height: u32, pixels: Vec<u8>) {
unsafe {
ffi::sfWindow_setIcon(self.window, width as c_uint, height as c_uint, pixels.as_ptr())
}
}
pub fn close(&mut self) {
unsafe {
ffi::sfWindow_close(self.window);
}
}
pub fn is_open(&self) -> bool {
unsafe { ffi::sfWindow_isOpen(self.window) }.to_bool()
}
pub fn get_settings(&self) -> ContextSettings {
ContextSettings(unsafe {ffi::sfWindow_getSettings(self.window)})
}
pub fn set_title(&mut self, title: &str) {
let c_str = CString::new(title.as_bytes()).unwrap().as_ptr();
unsafe {
ffi::sfWindow_setTitle(self.window, c_str)
}
}
pub fn set_visible(&mut self, visible: bool) {
unsafe {
ffi::sfWindow_setVisible(self.window, sfBool::from_bool(visible))
}
}
pub fn set_mouse_cursor_visible(&mut self, visible: bool) {
unsafe {
ffi::sfWindow_setMouseCursorVisible(self.window, sfBool::from_bool(visible))
}
}
pub fn set_vertical_sync_enabled(&mut self, enabled: bool) {
unsafe {
ffi::sfWindow_setVerticalSyncEnabled(self.window, sfBool::from_bool(enabled))
}
}
pub fn set_key_repeat_enabled(&mut self, enabled: bool) {
unsafe {
ffi::sfWindow_setKeyRepeatEnabled(self.window, sfBool::from_bool(enabled))
}
}
pub fn set_active(&mut self, enabled: bool) -> bool {
unsafe { ffi::sfWindow_setActive(self.window, sfBool::from_bool(enabled)) }.to_bool()
}
pub fn display(&mut self) {
unsafe {
ffi::sfWindow_display(self.window)
}
}
pub fn set_framerate_limit(&mut self, limit: u32) {
unsafe {
ffi::sfWindow_setFramerateLimit(self.window, limit as c_uint)
}
}
pub fn set_joystick_threshold(&mut self, threshold: f32) {
unsafe {
ffi::sfWindow_setJoystickThreshold(self.window, threshold as c_float)
}
}
pub fn get_position(&self) -> Vector2i {
unsafe {
ffi::sfWindow_getPosition(self.window)
}
}
pub fn set_position(&mut self, position: &Vector2i) {
unsafe {
ffi::sfWindow_setPosition(self.window, *position)
}
}
pub fn get_size(&self) -> Vector2u {
unsafe {
ffi::sfWindow_getSize(self.window)
}
}
pub fn set_size(&mut self, size: &Vector2u) {
unsafe {
ffi::sfWindow_setSize(self.window, *size)
}
}
pub fn get_mouse_position(&self) -> Vector2i {
unsafe {
ffi::sfMouse_getPosition(self.window)
}
}
pub fn set_mouse_position(&mut self, position: &Vector2i) {
unsafe {
ffi::sfMouse_setPosition(*position, self.window)
}
}
#[doc(hidden)]
pub fn unwrap(&self) -> *mut ffi::sfWindow {
self.window
}
}
impl Iterator for Events {
type Item = event::Event;
fn next(&mut self) -> Option<event::Event> {
let mut event = ffi::sfEvent { data: [032; 6] };
if unsafe { ffi::sfWindow_pollEvent(self.window, &mut event) }.to_bool() {
Some(event::raw::get_wrapped_event(&mut event))
} else {
None
}
}
}
impl Drop for Window {
fn drop(&mut self) {
unsafe {
ffi::sfWindow_destroy(self.window);
}
}
}