Skip to content

Commit

Permalink
Refer to enum variants with their full name (see rust-lang/rust#18973).
Browse files Browse the repository at this point in the history
  • Loading branch information
reima authored and caryhaynie committed Nov 25, 2014
1 parent 0533a57 commit ac9b2bf
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 354 deletions.
8 changes: 4 additions & 4 deletions src/sdl2/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ pub enum AudioDevice{
impl AudioDevice {
fn to_id(self) -> AudioDeviceID {
match self {
PlaybackDevice(id) => id,
RecordingDevice(id) => id
AudioDevice::PlaybackDevice(id) => id,
AudioDevice::RecordingDevice(id) => id
}
}

Expand All @@ -282,9 +282,9 @@ impl AudioDevice {
Err(get_error())
} else {
if iscapture == 0 { // plaback device
Ok((PlaybackDevice(ret as AudioDeviceID), obtained))
Ok((AudioDevice::PlaybackDevice(ret as AudioDeviceID), obtained))
} else {
Ok((RecordingDevice(ret as AudioDeviceID), obtained))
Ok((AudioDevice::RecordingDevice(ret as AudioDeviceID), obtained))
}
}
}
Expand Down
42 changes: 21 additions & 21 deletions src/sdl2/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ pub enum ControllerAxis {

pub fn wrap_controller_axis(bitflags: u8) -> ControllerAxis {
match bitflags as c_int {
ll::SDL_CONTROLLER_AXIS_LEFTX => LeftXAxis,
ll::SDL_CONTROLLER_AXIS_LEFTY => LeftYAxis,
ll::SDL_CONTROLLER_AXIS_RIGHTX => RightXAxis,
ll::SDL_CONTROLLER_AXIS_RIGHTY => RightYAxis,
ll::SDL_CONTROLLER_AXIS_TRIGGERLEFT => TriggerLeftAxis,
ll::SDL_CONTROLLER_AXIS_TRIGGERRIGHT => TriggerRightAxis,
ll::SDL_CONTROLLER_AXIS_LEFTX => ControllerAxis::LeftXAxis,
ll::SDL_CONTROLLER_AXIS_LEFTY => ControllerAxis::LeftYAxis,
ll::SDL_CONTROLLER_AXIS_RIGHTX => ControllerAxis::RightXAxis,
ll::SDL_CONTROLLER_AXIS_RIGHTY => ControllerAxis::RightYAxis,
ll::SDL_CONTROLLER_AXIS_TRIGGERLEFT => ControllerAxis::TriggerLeftAxis,
ll::SDL_CONTROLLER_AXIS_TRIGGERRIGHT => ControllerAxis::TriggerRightAxis,
_ => panic!("unhandled controller axis")
}
}
Expand Down Expand Up @@ -173,21 +173,21 @@ pub enum ControllerButton {

pub fn wrap_controller_button(bitflags: u8) -> ControllerButton {
match bitflags as c_int {
ll::SDL_CONTROLLER_BUTTON_A => AButton,
ll::SDL_CONTROLLER_BUTTON_B => BButton,
ll::SDL_CONTROLLER_BUTTON_X => XButton,
ll::SDL_CONTROLLER_BUTTON_Y => YButton,
ll::SDL_CONTROLLER_BUTTON_BACK => BackButton,
ll::SDL_CONTROLLER_BUTTON_GUIDE => GuideButton,
ll::SDL_CONTROLLER_BUTTON_START => StartButton,
ll::SDL_CONTROLLER_BUTTON_LEFTSTICK => LeftStickButton,
ll::SDL_CONTROLLER_BUTTON_RIGHTSTICK => RightStickButton,
ll::SDL_CONTROLLER_BUTTON_LEFTSHOULDER => LeftShoulderButton,
ll::SDL_CONTROLLER_BUTTON_RIGHTSHOULDER => RightShoulderButton,
ll::SDL_CONTROLLER_BUTTON_DPAD_UP => DPadUpButton,
ll::SDL_CONTROLLER_BUTTON_DPAD_DOWN => DPadDownButton,
ll::SDL_CONTROLLER_BUTTON_DPAD_LEFT => DPadLeftButton,
ll::SDL_CONTROLLER_BUTTON_DPAD_RIGHT => DPadRightButton,
ll::SDL_CONTROLLER_BUTTON_A => ControllerButton::AButton,
ll::SDL_CONTROLLER_BUTTON_B => ControllerButton::BButton,
ll::SDL_CONTROLLER_BUTTON_X => ControllerButton::XButton,
ll::SDL_CONTROLLER_BUTTON_Y => ControllerButton::YButton,
ll::SDL_CONTROLLER_BUTTON_BACK => ControllerButton::BackButton,
ll::SDL_CONTROLLER_BUTTON_GUIDE => ControllerButton::GuideButton,
ll::SDL_CONTROLLER_BUTTON_START => ControllerButton::StartButton,
ll::SDL_CONTROLLER_BUTTON_LEFTSTICK => ControllerButton::LeftStickButton,
ll::SDL_CONTROLLER_BUTTON_RIGHTSTICK => ControllerButton::RightStickButton,
ll::SDL_CONTROLLER_BUTTON_LEFTSHOULDER => ControllerButton::LeftShoulderButton,
ll::SDL_CONTROLLER_BUTTON_RIGHTSHOULDER => ControllerButton::RightShoulderButton,
ll::SDL_CONTROLLER_BUTTON_DPAD_UP => ControllerButton::DPadUpButton,
ll::SDL_CONTROLLER_BUTTON_DPAD_DOWN => ControllerButton::DPadDownButton,
ll::SDL_CONTROLLER_BUTTON_DPAD_LEFT => ControllerButton::DPadLeftButton,
ll::SDL_CONTROLLER_BUTTON_DPAD_RIGHT => ControllerButton::DPadRightButton,
_ => panic!("unhandled controller button")
}
}
433 changes: 221 additions & 212 deletions src/sdl2/event.rs

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/sdl2/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::ptr;
use std::string;
use std::vec;

use keycode::{KeyCode, UnknownKey};
use keycode::KeyCode;
use rect::Rect;
use scancode::{ScanCode, UnknownScanCode};
use scancode::ScanCode;
use video::Window;

#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn get_keyboard_state() -> HashMap<ScanCode, bool> {
let mut current = 0;
while current < raw.len() {
state.insert(FromPrimitive::from_int(current as int)
.unwrap_or(UnknownScanCode),
.unwrap_or(ScanCode::UnknownScanCode),
raw[current] == 1);
current += 1;
}
Expand All @@ -106,14 +106,14 @@ pub fn set_mod_state(flags: Mod) {
pub fn get_key_from_scancode(scancode: ScanCode) -> KeyCode {
unsafe {
FromPrimitive::from_int(ll::SDL_GetKeyFromScancode(scancode as u32) as int)
.unwrap_or(UnknownKey)
.unwrap_or(KeyCode::UnknownKey)
}
}

pub fn get_scancode_from_key(key: KeyCode) -> ScanCode {
unsafe {
FromPrimitive::from_int(ll::SDL_GetScancodeFromKey(key as i32) as int)
.unwrap_or(UnknownScanCode)
.unwrap_or(ScanCode::UnknownScanCode)
}
}

Expand All @@ -128,7 +128,7 @@ pub fn get_scancode_from_name(name: &str) -> ScanCode {
unsafe {
name.with_c_str(|name| {
FromPrimitive::from_int(ll::SDL_GetScancodeFromName(name) as int)
.unwrap_or(UnknownScanCode)
.unwrap_or(ScanCode::UnknownScanCode)
})
}
}
Expand All @@ -144,7 +144,7 @@ pub fn get_key_from_name(name: &str) -> KeyCode {
unsafe {
name.with_c_str(|name| {
FromPrimitive::from_int(ll::SDL_GetKeyFromName(name) as int)
.unwrap_or(UnknownKey)
.unwrap_or(KeyCode::UnknownKey)
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/sdl2/messagebox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pub mod ll {

bitflags! {
flags MessageBoxFlag: u32 {
const MESSAGEBOX_ERROR = ll::SDL_MESSAGEBOX_ERROR as u32,
const MESSAGEBOX_WARNING = ll::SDL_MESSAGEBOX_WARNING as u32,
const MESSAGEBOX_INFORMATION = ll::SDL_MESSAGEBOX_INFORMATION as u32
const MESSAGEBOX_ERROR = ll::SDL_MessageBoxFlags::SDL_MESSAGEBOX_ERROR as u32,
const MESSAGEBOX_WARNING = ll::SDL_MessageBoxFlags::SDL_MESSAGEBOX_WARNING as u32,
const MESSAGEBOX_INFORMATION = ll::SDL_MessageBoxFlags::SDL_MESSAGEBOX_INFORMATION as u32
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/sdl2/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ bitflags! {

pub fn wrap_mouse(bitflags: u8) -> Mouse {
match bitflags {
1 => LeftMouse,
2 => MiddleMouse,
3 => RightMouse,
4 => X1Mouse,
5 => X2Mouse,
_ => UnknownMouse(bitflags)
1 => Mouse::LeftMouse,
2 => Mouse::MiddleMouse,
3 => Mouse::RightMouse,
4 => Mouse::X1Mouse,
5 => Mouse::X2Mouse,
_ => Mouse::UnknownMouse(bitflags)
}
}

Expand Down
78 changes: 49 additions & 29 deletions src/sdl2/pixels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ pub enum Color {
impl Color {
pub fn to_u32(&self, format: &PixelFormat) -> u32 {
match self {
&RGB(r, g, b) => {
&Color::RGB(r, g, b) => {
unsafe { ll::SDL_MapRGB(format.raw, r, g, b) }
}
&RGBA(r, g, b, a) => {
&Color::RGBA(r, g, b, a) => {
unsafe { ll::SDL_MapRGBA(format.raw, r, g, b, a) }
}
}
Expand All @@ -124,21 +124,21 @@ impl Color {
unsafe {
ll::SDL_GetRGBA(pixel, format.raw, &r, &g, &b, &a)
};
RGBA(r, g, b, a)
Color::RGBA(r, g, b, a)
}

pub fn get_rgb(&self) -> (u8, u8, u8) {
match self {
&RGB(r, g, b) => (r, g, b),
&RGBA(r, g, b, _) => (r, g, b)
&Color::RGB(r, g, b) => (r, g, b),
&Color::RGBA(r, g, b, _) => (r, g, b)
}
}
}

impl rand::Rand for Color {
fn rand<R: rand::Rng>(rng: &mut R) -> Color {
if rng.gen() { RGBA(rng.gen(), rng.gen(), rng.gen(), rng.gen()) }
else { RGB(rng.gen(), rng.gen(), rng.gen()) }
if rng.gen() { Color::RGBA(rng.gen(), rng.gen(), rng.gen(), rng.gen()) }
else { Color::RGB(rng.gen(), rng.gen(), rng.gen()) }
}
}

Expand Down Expand Up @@ -193,53 +193,73 @@ pub enum PixelFormatFlag {
impl PixelFormatFlag {
pub fn byte_size_of_pixels(&self, num_of_pixels: uint) -> uint {
match *self {
RGB332
PixelFormatFlag::RGB332
=> num_of_pixels * 1,
RGB444 | RGB555 | BGR555 | ARGB4444 | RGBA4444 | ABGR4444 |
BGRA4444 | ARGB1555 | RGBA5551 | ABGR1555 | BGRA5551 | RGB565 |
BGR565
PixelFormatFlag::RGB444 | PixelFormatFlag::RGB555 |
PixelFormatFlag::BGR555 | PixelFormatFlag::ARGB4444 |
PixelFormatFlag::RGBA4444 | PixelFormatFlag::ABGR4444 |
PixelFormatFlag::BGRA4444 | PixelFormatFlag::ARGB1555 |
PixelFormatFlag::RGBA5551 | PixelFormatFlag::ABGR1555 |
PixelFormatFlag::BGRA5551 | PixelFormatFlag::RGB565 |
PixelFormatFlag::BGR565
=> num_of_pixels * 2,
RGB24 | BGR24
PixelFormatFlag::RGB24 | PixelFormatFlag::BGR24
=> num_of_pixels * 3,
RGB888 | RGBX8888 | BGR888 | BGRX8888 | ARGB8888 | RGBA8888 |
ABGR8888 | BGRA8888 | ARGB2101010
PixelFormatFlag::RGB888 | PixelFormatFlag::RGBX8888 |
PixelFormatFlag::BGR888 | PixelFormatFlag::BGRX8888 |
PixelFormatFlag::ARGB8888 | PixelFormatFlag::RGBA8888 |
PixelFormatFlag::ABGR8888 | PixelFormatFlag::BGRA8888 |
PixelFormatFlag::ARGB2101010
=> num_of_pixels * 4,
// YUV formats
// FIXME: rounding error here?
YV12 | IYUV
PixelFormatFlag::YV12 | PixelFormatFlag::IYUV
=> num_of_pixels / 2 * 3,
YUY2 | UYVY | YVYU
PixelFormatFlag::YUY2 | PixelFormatFlag::UYVY |
PixelFormatFlag::YVYU
=> num_of_pixels * 2,
// Unsupported formats
Index8
PixelFormatFlag::Index8
=> num_of_pixels * 1,
Unknown | Index1LSB | Index1MSB | Index4LSB | Index4MSB
PixelFormatFlag::Unknown | PixelFormatFlag::Index1LSB |
PixelFormatFlag::Index1MSB | PixelFormatFlag::Index4LSB |
PixelFormatFlag::Index4MSB
=> panic!("not supported format: {}", *self),
}
}

pub fn byte_size_per_pixel(&self) -> uint {
match *self {
RGB332
PixelFormatFlag::RGB332
=> 1,
RGB444 | RGB555 | BGR555 | ARGB4444 | RGBA4444 | ABGR4444 |
BGRA4444 | ARGB1555 | RGBA5551 | ABGR1555 | BGRA5551 | RGB565 |
BGR565
PixelFormatFlag::RGB444 | PixelFormatFlag::RGB555 |
PixelFormatFlag::BGR555 | PixelFormatFlag::ARGB4444 |
PixelFormatFlag::RGBA4444 | PixelFormatFlag::ABGR4444 |
PixelFormatFlag::BGRA4444 | PixelFormatFlag::ARGB1555 |
PixelFormatFlag::RGBA5551 | PixelFormatFlag::ABGR1555 |
PixelFormatFlag::BGRA5551 | PixelFormatFlag::RGB565 |
PixelFormatFlag::BGR565
=> 2,
RGB24 | BGR24
PixelFormatFlag::RGB24 | PixelFormatFlag::BGR24
=> 3,
RGB888 | RGBX8888 | BGR888 | BGRX8888 | ARGB8888 | RGBA8888 |
ABGR8888 | BGRA8888 | ARGB2101010
PixelFormatFlag::RGB888 | PixelFormatFlag::RGBX8888 |
PixelFormatFlag::BGR888 | PixelFormatFlag::BGRX8888 |
PixelFormatFlag::ARGB8888 | PixelFormatFlag::RGBA8888 |
PixelFormatFlag::ABGR8888 | PixelFormatFlag::BGRA8888 |
PixelFormatFlag::ARGB2101010
=> 4,
// YUV formats
YV12 | IYUV
PixelFormatFlag::YV12 | PixelFormatFlag::IYUV
=> 2,
YUY2 | UYVY | YVYU
PixelFormatFlag::YUY2 | PixelFormatFlag::UYVY |
PixelFormatFlag::YVYU
=> 2,
// Unsupported formats
Index8
PixelFormatFlag::Index8
=> 1,
Unknown | Index1LSB | Index1MSB | Index4LSB | Index4MSB
PixelFormatFlag::Unknown | PixelFormatFlag::Index1LSB |
PixelFormatFlag::Index1MSB | PixelFormatFlag::Index4LSB |
PixelFormatFlag::Index4MSB
=> panic!("not supported format: {}", *self),
}
}
Expand Down
Loading

0 comments on commit ac9b2bf

Please sign in to comment.