Merge pull request #103 from jcmoyer/bitflags

Replace flags.rs with stdlib bitflags macro
This commit is contained in:
Tony Aldridge
2014-05-15 09:10:27 +01:00
10 changed files with 94 additions and 222 deletions
+4 -4
View File
@@ -760,7 +760,7 @@ impl Event {
KeyDownEvent(event.timestamp as uint, window,
FromPrimitive::from_int(event.keysym.sym as int).unwrap(),
FromPrimitive::from_int(event.keysym.scancode as int).unwrap(),
keyboard::Mod::new(event.keysym._mod as SDL_Keymod))
keyboard::Mod::from_bits(event.keysym._mod as SDL_Keymod))
}
KeyUpEventType => {
let event = *raw.key();
@@ -774,7 +774,7 @@ impl Event {
KeyUpEvent(event.timestamp as uint, window,
FromPrimitive::from_int(event.keysym.sym as int).unwrap(),
FromPrimitive::from_int(event.keysym.scancode as int).unwrap(),
keyboard::Mod::new(event.keysym._mod as SDL_Keymod))
keyboard::Mod::from_bits(event.keysym._mod as SDL_Keymod))
}
TextEditingEventType => {
let event = *raw.edit();
@@ -813,7 +813,7 @@ impl Event {
MouseMotionEvent(event.timestamp as uint, window,
event.which as uint,
mouse::MouseState::new(event.state),
mouse::MouseState::from_bits(event.state),
event.x as int, event.y as int,
event.xrel as int, event.yrel as int)
}
@@ -874,7 +874,7 @@ impl Event {
let event = *raw.jhat();
JoyHatMotionEvent(event.timestamp as uint, event.which as int,
event.hat as int,
joystick::HatState::new(event.value))
joystick::HatState::from_bits(event.value))
}
JoyButtonDownEventType => {
let event = *raw.jbutton();
-133
View File
@@ -1,133 +0,0 @@
// Loosely based on zlib-licensed code from RustAllegro
// https://github.com/SiegeLord/RustAllegro
//
//! Implements efficient, type-safe flags with support for bitwise operators.
//
// The macros defined in this module are for internal usage only.
//
// Example:
//
// flag_type!(FlagTypeName {
// FlagName1 = FlagValue1,
// FlagName2 = FlagValue2,
// ...
// FlagNameN = FlagValueN
// })
//
// fn foo(flag: FlagTypeName) {
// let raw = (flag | FlagNameN).get();
// bar(raw);
// }
#![macro_escape]
/// The `Flags` trait is used to define values for flag types where either none
/// or all of the flags are set.
pub trait Flags: ::std::ops::Not<Self> {
/// Returns a value representing an empty bitset. Implementors should ensure
/// this function returns the equivalent of `0` (no bits set) for a flag
/// type.
fn none() -> Self;
/// By default, this function will negate the result of `none()`. For sanely
/// implemented types, this should be equivalent to having all flags set.
fn all() -> Self {
let none: Self = Flags::none();
!none
}
}
macro_rules! flag_type(
($typename:ident : $supertype:ident { $($name:ident = $value:expr),* }) => {
pub struct $typename {
bits: $supertype
}
impl $typename {
#[inline]
pub fn new(bits: $supertype) -> $typename {
$typename { bits: bits }
}
#[inline]
pub fn get(self) -> $supertype {
self.bits
}
}
impl ::std::default::Default for $typename {
fn default() -> $typename {
$typename::new(0)
}
}
impl ::std::cmp::Eq for $typename {
fn eq(&self, other: &$typename) -> bool {
self.bits == other.bits
}
}
impl ::std::cmp::TotalEq for $typename {}
impl ::std::cmp::Ord for $typename {
fn lt(&self, other: &$typename) -> bool {
self.bits < other.bits
}
}
impl ::std::cmp::TotalOrd for $typename {
fn cmp(&self, other: &$typename) -> Ordering {
self.bits.cmp(&other.bits)
}
}
impl ::std::ops::Not<$typename> for $typename {
fn not(&self) -> $typename {
$typename { bits: !self.bits }
}
}
impl ::std::ops::BitAnd<$typename, $typename> for $typename {
fn bitand(&self, rhs: &$typename) -> $typename {
$typename { bits: self.bits & rhs.bits }
}
}
impl ::std::ops::BitOr<$typename, $typename> for $typename {
fn bitor(&self, rhs: &$typename) -> $typename {
$typename { bits: self.bits | rhs.bits }
}
}
impl ::std::ops::BitXor<$typename, $typename> for $typename {
fn bitxor(&self, rhs: &$typename) -> $typename {
$typename { bits: self.bits ^ rhs.bits }
}
}
impl ::std::ops::Shl<$supertype, $typename> for $typename {
fn shl(&self, rhs: &$supertype) -> $typename {
$typename { bits: self.bits << *rhs }
}
}
impl ::std::ops::Shr<$supertype, $typename> for $typename {
fn shr(&self, rhs: &$supertype) -> $typename {
$typename { bits: self.bits >> *rhs }
}
}
impl ::flags::Flags for $typename {
fn none() -> $typename {
$typename { bits: 0 as $supertype }
}
}
$(
pub static $name: $typename = $typename { bits: $value as $supertype };
)+
};
($typename:ident { $($name:ident = $value:expr),* }) => {
flag_type!($typename : u32 { $($name = $value),* })
}
)
+10 -10
View File
@@ -43,14 +43,14 @@ pub mod ll {
}
}
flag_type!(HatState: u8 {
CenteredHatState = 0,
UpHatState = 0x01,
RightHatState = 0x02,
DownHatState = 0x04,
LeftHatState = 0x08,
RightUpHatState = 0x02 | 0x01, // RightHatState | UpHatState
RightDownHatState = 0x02 | 0x04, // RightHatState | DownHatState,
LeftUpHatState = 0x08 | 0x01, // LeftHatState | UpHatState,
LeftDownHatState = 0x08 | 0x04 // LeftHatState | DownHatState
bitflags!(flags HatState: u8 {
static CenteredHatState = 0,
static UpHatState = 0x01,
static RightHatState = 0x02,
static DownHatState = 0x04,
static LeftHatState = 0x08,
static RightUpHatState = 0x02 | 0x01, // RightHatState | UpHatState
static RightDownHatState = 0x02 | 0x04, // RightHatState | DownHatState,
static LeftUpHatState = 0x08 | 0x01, // LeftHatState | UpHatState,
static LeftDownHatState = 0x08 | 0x04 // LeftHatState | DownHatState
})
+16 -16
View File
@@ -50,20 +50,20 @@ pub mod ll {
}
}
flag_type!(Mod {
NoMod = 0x0000,
LShiftMod = 0x0001,
RShiftMod = 0x0002,
LCtrlMod = 0x0040,
RCtrlMod = 0x0080,
LAltMod = 0x0100,
RAltMod = 0x0200,
LGuiMod = 0x0400,
RGuiMod = 0x0800,
NumMod = 0x1000,
CapsMod = 0x2000,
ModeMod = 0x4000,
ReservedMod = 0x8000
bitflags!(flags Mod: u32 {
static NoMod = 0x0000,
static LShiftMod = 0x0001,
static RShiftMod = 0x0002,
static LCtrlMod = 0x0040,
static RCtrlMod = 0x0080,
static LAltMod = 0x0100,
static RAltMod = 0x0200,
static LGuiMod = 0x0400,
static RGuiMod = 0x0800,
static NumMod = 0x1000,
static CapsMod = 0x2000,
static ModeMod = 0x4000,
static ReservedMod = 0x8000
})
pub fn get_keyboard_focus() -> Option<Window> {
@@ -93,11 +93,11 @@ pub fn get_keyboard_state() -> HashMap<ScanCode, bool> {
}
pub fn get_mod_state() -> Mod {
unsafe { Mod::new(ll::SDL_GetModState()) }
unsafe { Mod::from_bits(ll::SDL_GetModState()) }
}
pub fn set_mod_state(flags: Mod) {
unsafe { ll::SDL_SetModState(flags.get()); }
unsafe { ll::SDL_SetModState(flags.bits()); }
}
pub fn get_key_from_scancode(scancode: ScanCode) -> KeyCode {
-1
View File
@@ -16,7 +16,6 @@ pub mod keycode;
#[path = "generated/scancode.rs"]
pub mod scancode;
pub mod flags;
pub mod event;
pub mod gesture;
pub mod touch;
+14 -12
View File
@@ -142,12 +142,12 @@ pub enum Mouse {
UnknownMouse(u8)
}
flag_type!(MouseState {
LeftMouseState = 0x01,
MiddleMouseState = 0x02,
RightMouseState = 0x04,
X1MouseState = 0x08,
X2MouseState = 0x10
bitflags!(flags MouseState: u32 {
static LeftMouseState = 0x01,
static MiddleMouseState = 0x02,
static RightMouseState = 0x04,
static X1MouseState = 0x08,
static X2MouseState = 0x10
})
pub fn wrap_mouse(bitflags: u8) -> Mouse {
@@ -173,17 +173,19 @@ pub fn get_mouse_focus() -> Option<video::Window> {
pub fn get_mouse_state() -> (MouseState, int, int) {
let x = 0;
let y = 0;
let raw = unsafe { ll::SDL_GetMouseState(&x, &y) };
return (MouseState::new(raw), x as int, y as int);
unsafe {
let raw = ll::SDL_GetMouseState(&x, &y);
return (MouseState::from_bits(raw), x as int, y as int);
}
}
pub fn get_relative_mouse_state() -> (MouseState, int, int) {
let x = 0;
let y = 0;
let raw = unsafe { ll::SDL_GetRelativeMouseState(&x, &y) };
return (MouseState::new(raw), x as int, y as int);
unsafe {
let raw = ll::SDL_GetRelativeMouseState(&x, &y);
return (MouseState::from_bits(raw), x as int, y as int);
}
}
pub fn warp_mouse_in_window(window: &video::Window, x: i32, y: i32) {
+8 -8
View File
@@ -147,11 +147,11 @@ pub enum TextureAccess {
AccessTarget = ll::SDL_TEXTUREACCESS_TARGET as int
}
flag_type!(RendererFlags {
Software = ll::SDL_RENDERER_SOFTWARE,
Accelerated = ll::SDL_RENDERER_ACCELERATED,
PresentVSync = ll::SDL_RENDERER_PRESENTVSYNC,
TargetTexture = ll::SDL_RENDERER_TARGETTEXTURE
bitflags!(flags RendererFlags: u32 {
static Software = ll::SDL_RENDERER_SOFTWARE as u32,
static Accelerated = ll::SDL_RENDERER_ACCELERATED as u32,
static PresentVSync = ll::SDL_RENDERER_PRESENTVSYNC as u32,
static TargetTexture = ll::SDL_RENDERER_TARGETTEXTURE as u32
})
#[deriving(Eq)]
@@ -180,7 +180,7 @@ pub enum RendererFlip {
impl RendererInfo {
pub fn from_ll(info: &ll::SDL_RendererInfo) -> RendererInfo {
let actual_flags = RendererFlags::new(info.flags);
let actual_flags = unsafe { RendererFlags::from_bits(info.flags) };
unsafe {
let texture_formats: Vec<pixels::PixelFormatFlag> = info.texture_formats.slice(0, info.num_texture_formats as uint).iter().map(|&format| {
@@ -229,7 +229,7 @@ impl Renderer {
};
let raw = unsafe {
ll::SDL_CreateRenderer(window.raw, index as c_int, renderer_flags.get())
ll::SDL_CreateRenderer(window.raw, index as c_int, renderer_flags.bits())
};
if raw == ptr::null() {
@@ -242,7 +242,7 @@ impl Renderer {
pub fn new_with_window(width: int, height: int, window_flags: video::WindowFlags) -> Result<Renderer, ~str> {
let raw_window: *video::ll::SDL_Window = ptr::null();
let raw_renderer: *ll::SDL_Renderer = ptr::null();
let result = unsafe { ll::SDL_CreateWindowAndRenderer(width as c_int, height as c_int, window_flags.get(), &raw_window, &raw_renderer) == 0};
let result = unsafe { ll::SDL_CreateWindowAndRenderer(width as c_int, height as c_int, window_flags.bits(), &raw_window, &raw_renderer) == 0};
if result {
let window = video::Window {
raw: raw_window,
+17 -15
View File
@@ -63,16 +63,16 @@ pub mod ll {
}
}
flag_type!(InitFlag {
InitTimer = ll::SDL_INIT_TIMER,
InitAudio = ll::SDL_INIT_AUDIO,
InitVideo = ll::SDL_INIT_VIDEO,
InitJoystick = ll::SDL_INIT_JOYSTICK,
InitHaptic = ll::SDL_INIT_HAPTIC,
InitGameController = ll::SDL_INIT_GAMECONTROLLER,
InitEvents = ll::SDL_INIT_EVENTS,
InitNoParachute = ll::SDL_INIT_NOPARACHUTE,
InitEverything = ll::SDL_INIT_EVERYTHING
bitflags!(flags InitFlag: u32 {
static InitTimer = ll::SDL_INIT_TIMER,
static InitAudio = ll::SDL_INIT_AUDIO,
static InitVideo = ll::SDL_INIT_VIDEO,
static InitJoystick = ll::SDL_INIT_JOYSTICK,
static InitHaptic = ll::SDL_INIT_HAPTIC,
static InitGameController = ll::SDL_INIT_GAMECONTROLLER,
static InitEvents = ll::SDL_INIT_EVENTS,
static InitNoParachute = ll::SDL_INIT_NOPARACHUTE,
static InitEverything = ll::SDL_INIT_EVERYTHING
})
#[deriving(Eq)]
@@ -86,18 +86,18 @@ pub enum Error {
pub fn init(flags: InitFlag) -> bool {
unsafe {
ll::SDL_Init(flags.get()) == 0
ll::SDL_Init(flags.bits()) == 0
}
}
pub fn init_subsystem(flags: InitFlag) -> bool {
unsafe {
ll::SDL_InitSubSystem(flags.get()) == 0
ll::SDL_InitSubSystem(flags.bits()) == 0
}
}
pub fn quit_subsystem(flags: InitFlag) {
unsafe { ll::SDL_QuitSubSystem(flags.get()); }
unsafe { ll::SDL_QuitSubSystem(flags.bits()); }
}
pub fn quit() {
@@ -105,8 +105,10 @@ pub fn quit() {
}
pub fn was_inited(flags: InitFlag) -> InitFlag {
let raw = unsafe { ll::SDL_WasInit(flags.get()) };
flags & InitFlag::new(raw)
unsafe {
let raw = ll::SDL_WasInit(flags.bits());
flags & InitFlag::from_bits(raw)
}
}
pub fn get_error() -> ~str {
+6 -6
View File
@@ -75,11 +75,11 @@ pub mod ll {
}
}
flag_type!(SurfaceFlag {
SWSurface = ll::SDL_SWSURFACE,
PreAlloc = ll::SDL_PREALLOC,
RLEAccel = ll::SDL_RLEACCEL,
DontFree = ll::SDL_DONTFREE
bitflags!(flags SurfaceFlag: u32 {
static SWSurface = ll::SDL_SWSURFACE as u32,
static PreAlloc = ll::SDL_PREALLOC as u32,
static RLEAccel = ll::SDL_RLEACCEL as u32,
static DontFree = ll::SDL_DONTFREE as u32
})
#[deriving(Eq)] #[allow(raw_pointer_deriving)]
@@ -102,7 +102,7 @@ impl Surface {
pub fn new(surface_flags: SurfaceFlag, width: int, height: int, bpp: int,
rmask: u32, gmask: u32, bmask: u32, amask: u32) -> Result<Surface, ~str> {
unsafe {
let raw = ll::SDL_CreateRGBSurface(surface_flags.get(), width as c_int, height as c_int, bpp as c_int,
let raw = ll::SDL_CreateRGBSurface(surface_flags.bits(), width as c_int, height as c_int, bpp as c_int,
rmask, gmask, bmask, amask);
if raw == ptr::null() {
+19 -17
View File
@@ -257,20 +257,20 @@ impl DisplayMode {
}
}
flag_type!(WindowFlags {
Fullscreen = ll::SDL_WINDOW_FULLSCREEN,
OpenGL = ll::SDL_WINDOW_OPENGL,
Shown = ll::SDL_WINDOW_SHOWN,
Hidden = ll::SDL_WINDOW_HIDDEN,
Borderless = ll::SDL_WINDOW_BORDERLESS,
Resizable = ll::SDL_WINDOW_RESIZABLE,
Minimized = ll::SDL_WINDOW_MINIMIZED,
Maximized = ll::SDL_WINDOW_MAXIMIZED,
InputGrabbed = ll::SDL_WINDOW_INPUT_GRABBED,
InputFocus = ll::SDL_WINDOW_INPUT_FOCUS,
MouseFocus = ll::SDL_WINDOW_MOUSE_FOCUS,
FullscreenDesktop = ll::SDL_WINDOW_FULLSCREEN_DESKTOP,
Foreign = ll::SDL_WINDOW_FOREIGN
bitflags!(flags WindowFlags: u32 {
static Fullscreen = ll::SDL_WINDOW_FULLSCREEN as u32,
static OpenGL = ll::SDL_WINDOW_OPENGL as u32,
static Shown = ll::SDL_WINDOW_SHOWN as u32,
static Hidden = ll::SDL_WINDOW_HIDDEN as u32,
static Borderless = ll::SDL_WINDOW_BORDERLESS as u32,
static Resizable = ll::SDL_WINDOW_RESIZABLE as u32,
static Minimized = ll::SDL_WINDOW_MINIMIZED as u32,
static Maximized = ll::SDL_WINDOW_MAXIMIZED as u32,
static InputGrabbed = ll::SDL_WINDOW_INPUT_GRABBED as u32,
static InputFocus = ll::SDL_WINDOW_INPUT_FOCUS as u32,
static MouseFocus = ll::SDL_WINDOW_MOUSE_FOCUS as u32,
static FullscreenDesktop = ll::SDL_WINDOW_FULLSCREEN_DESKTOP as u32,
static Foreign = ll::SDL_WINDOW_FOREIGN as u32
})
#[deriving(Eq)]
@@ -338,7 +338,7 @@ impl Window {
unwrap_windowpos(y),
width as c_int,
height as c_int,
window_flags.get()
window_flags.bits()
)
});
@@ -406,8 +406,10 @@ impl Window {
}
pub fn get_flags(&self) -> WindowFlags {
let raw = unsafe { ll::SDL_GetWindowFlags(self.raw) };
WindowFlags::new(raw)
unsafe {
let raw = ll::SDL_GetWindowFlags(self.raw);
WindowFlags::from_bits(raw)
}
}
pub fn set_title(&self, title: &str) {