Skip to content
26 changes: 26 additions & 0 deletions src/sdl2/controller.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use libc::c_char;
use std::error;
use std::ffi::{CString, CStr, NulError};
use std::fmt;
use std::path::Path;
use rwops::RWops;

Expand All @@ -18,6 +20,30 @@ pub enum AddMappingError {
SdlError(String),
}

impl fmt::Display for AddMappingError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::AddMappingError::*;

match *self {
InvalidMapping(ref e) => write!(f, "Null error: {}", e),
InvalidFilePath(ref value) => write!(f, "Invalid file path ({})", value),
SdlError(ref e) => write!(f, "SDL error: {}", e)
}
}
}

impl error::Error for AddMappingError {
fn description(&self) -> &str {
use self::AddMappingError::*;

match *self {
InvalidMapping(_) => "invalid mapping",
InvalidFilePath(_) => "invalid file path",
SdlError(ref e) => e,
}
}
}

impl GameControllerSubsystem {
/// Retreive the total number of attached joysticks *and* controllers identified by SDL.
pub fn num_joysticks(&self) -> Result<u32, String> {
Expand Down
26 changes: 26 additions & 0 deletions src/sdl2/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::error;
use std::ffi::{CStr, CString, NulError};
use std::fmt;
use get_error;
use libc::c_char;

Expand All @@ -24,6 +26,30 @@ pub enum PrefPathError {
SdlError(String),
}

impl fmt::Display for PrefPathError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::PrefPathError::*;

match *self {
InvalidOrganizationName(ref e) => write!(f, "Invalid organization name: {}", e),
InvalidApplicationName(ref e) => write!(f, "Invalid application name: {}", e),
SdlError(ref e) => write!(f, "SDL error: {}", e)
}
}
}

impl error::Error for PrefPathError {
fn description(&self) -> &str {
use self::PrefPathError::*;

match *self {
InvalidOrganizationName(_) => "invalid organization name",
InvalidApplicationName(_) => "invalid application name",
SdlError(ref e) => e,
}
}
}

// TODO: Change to OsStr or something?
/// Return the preferred directory for the application to write files on this
/// system, based on the given organization and application name.
Expand Down
29 changes: 29 additions & 0 deletions src/sdl2/messagebox.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::error;
use std::ffi::{CString, NulError};
use std::fmt;
use std::ptr;
use std::os::raw::{c_char,c_int};

Expand Down Expand Up @@ -81,6 +83,33 @@ pub enum ShowMessageError {
SdlError(String),
}

impl fmt::Display for ShowMessageError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::ShowMessageError::*;

match *self {
InvalidTitle(ref e) => write!(f, "Invalid title: {}", e),
InvalidMessage(ref e) => write!(f, "Invalid message: {}", e),
InvalidButton(ref e, value) => write!(f,
"Invalid button ({}): {}", value, e),
SdlError(ref e) => write!(f, "SDL error: {}", e)
}
}
}

impl error::Error for ShowMessageError {
fn description(&self) -> &str {
use self::ShowMessageError::*;

match *self {
InvalidTitle(_) => "invalid title",
InvalidMessage(_) => "invalid message",
InvalidButton(..) => "invalid button",
SdlError(ref e) => e
}
}
}

/// Show a simple message box, meant to be informative only.
///
/// There is no way to know if the user clicked "Ok" or closed the message box,
Expand Down
83 changes: 83 additions & 0 deletions src/sdl2/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,48 @@ pub enum UpdateTextureError {
SdlError(String),
}

impl fmt::Display for UpdateTextureError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::UpdateTextureError::*;

match *self {
PitchOverflows(value) => write!(f, "Pitch overflows ({})", value),
PitchMustBeMultipleOfTwoForFormat(value, format) => write!(f,
"Pitch must be multiple of two for pixel format '{:?}' ({})",
format, value),
XMustBeMultipleOfTwoForFormat(value, format) => write!(f,
"X must be multiple of two for pixel format '{:?}' ({})",
format, value),
YMustBeMultipleOfTwoForFormat(value, format) => write!(f,
"Y must be multiple of two for pixel format '{:?}' ({})",
format, value),
WidthMustBeMultipleOfTwoForFormat(value, format) => write!(f,
"Width must be multiple of two for pixel format '{:?}' ({})",
format, value),
HeightMustBeMultipleOfTwoForFormat(value, format) => write!(f,
"Height must be multiple of two for pixel format '{:?}' ({})",
format, value),
SdlError(ref e) => write!(f, "SDL error: {}", e)
}
}
}

impl Error for UpdateTextureError {
fn description(&self) -> &str {
use self::UpdateTextureError::*;

match *self {
PitchOverflows(_) => "pitch overflow",
PitchMustBeMultipleOfTwoForFormat(..) => "pitch must be multiple of two",
XMustBeMultipleOfTwoForFormat(..) => "x must be multiple of two",
YMustBeMultipleOfTwoForFormat(..) => "y must be multiple of two",
WidthMustBeMultipleOfTwoForFormat(..) => "width must be multiple of two",
HeightMustBeMultipleOfTwoForFormat(..) => "height must be multiple of two",
SdlError(ref e) => e,
}
}
}

#[derive(Debug)]
pub enum UpdateTextureYUVError {
PitchOverflows { plane: &'static str, value: usize },
Expand All @@ -1055,6 +1097,47 @@ pub enum UpdateTextureYUVError {
SdlError(String),
}

impl fmt::Display for UpdateTextureYUVError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::UpdateTextureYUVError::*;

match *self {
PitchOverflows { plane, value } => write!(f,
"Pitch overflows on {} plane ({})", plane, value),
InvalidPlaneLength { plane, length, pitch, height } => write!(f,
"The {} plane is wrong length ({}, should be {} * {})",
plane, length, pitch, height),
XMustBeMultipleOfTwoForFormat(value) => write!(f,
"X must be multiple of two ({})", value),
YMustBeMultipleOfTwoForFormat(value) => write!(f,
"Y must be multiple of two ({})", value),
WidthMustBeMultipleOfTwoForFormat(value) => write!(f,
"Width must be multiple of two ({})", value),
HeightMustBeMultipleOfTwoForFormat(value) => write!(f,
"Height must be multiple of two ({})", value),
RectNotInsideTexture(_) => write!(f, "Rect must be inside texture"),
SdlError(ref e) => write!(f, "SDL error: {}", e)
}
}
}

impl Error for UpdateTextureYUVError {
fn description(&self) -> &str {
use self::UpdateTextureYUVError::*;

match *self {
PitchOverflows {..} => "pitch overflow",
InvalidPlaneLength {..} => "invalid plane length",
XMustBeMultipleOfTwoForFormat(_) => "x must be multiple of two",
YMustBeMultipleOfTwoForFormat(_) => "y must be multiple of two",
WidthMustBeMultipleOfTwoForFormat(_) => "width must be multiple of two",
HeightMustBeMultipleOfTwoForFormat(_) => "height must be multiple of two",
RectNotInsideTexture(_) => "rect must be inside texture",
SdlError(ref e) => e,
}
}
}

impl Texture {
#[inline]
fn check_renderer(&self) {
Expand Down
30 changes: 30 additions & 0 deletions src/sdl2/sdl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::error;
use std::ffi::{CStr, CString, NulError};
use std::fmt;
use std::rc::Rc;
use libc::c_char;

Expand All @@ -13,6 +15,34 @@ pub enum Error {
UnsupportedError = ll::SDL_UNSUPPORTED as isize
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;

match *self {
NoMemError => write!(f, "Out of memory"),
ReadError => write!(f, "Error reading from datastream"),
WriteError => write!(f, "Error writing to datastream"),
SeekError => write!(f, "Error seeking in datastream"),
UnsupportedError => write!(f, "Unknown SDL error")
}
}
}

impl error::Error for Error {
fn description(&self) -> &str {
use self::Error::*;

match *self {
NoMemError => "out of memory",
ReadError => "error reading from datastream",
WriteError => "error writing to datastream",
SeekError => "error seeking in datastream",
UnsupportedError => "unknown SDL error"
}
}
}

use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT};
/// Only one Sdl context can be alive at a time.
/// Set to false by default (not alive).
Expand Down