Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Conversion fn prefixes: change to fit Rust's convention #1037

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/sdl2/controller.rs
Original file line number Diff line number Diff line change
@@ -224,7 +224,12 @@ impl Axis {
})
}

#[deprecated(since="0.34.4", note="please, use `into_ll()` instead")]
pub fn to_ll(self) -> sys::SDL_GameControllerAxis {
self.into_ll()
}

pub fn into_ll(self) -> sys::SDL_GameControllerAxis {
match self {
Axis::LeftX => sys::SDL_GameControllerAxis::SDL_CONTROLLER_AXIS_LEFTX,
Axis::LeftY => sys::SDL_GameControllerAxis::SDL_CONTROLLER_AXIS_LEFTY,
@@ -302,7 +307,12 @@ impl Button {
})
}

#[deprecated(since="0.34.4", note="please, use `into_ll()` instead")]
pub fn to_ll(self) -> sys::SDL_GameControllerButton {
self.into_ll()
}

pub fn into_ll(self) -> sys::SDL_GameControllerButton {
match self {
Button::A => sys::SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_A,
Button::B => sys::SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_B,
12 changes: 6 additions & 6 deletions src/sdl2/event.rs
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ impl CustomEventTypeMaps {
}

lazy_static! {
static ref CUSTOM_EVENT_TYPES : Mutex<CustomEventTypeMaps> = { Mutex::new(CustomEventTypeMaps::new()) };
static ref CUSTOM_EVENT_TYPES : Mutex<CustomEventTypeMaps> = Mutex::new(CustomEventTypeMaps::new());
}

impl crate::EventSubsystem {
@@ -905,7 +905,7 @@ impl Event {
xrel,
yrel
} => {
let state = mousestate.to_sdl_state();
let state = mousestate.as_sdl_state();
let event = sys::SDL_MouseMotionEvent {
type_: SDL_EventType::SDL_MOUSEMOTION as u32,
timestamp,
@@ -1051,7 +1051,7 @@ impl Event {
hat_idx,
state,
} => {
let hatvalue = state.to_raw();
let hatvalue = state.as_raw();
let event = sys::SDL_JoyHatEvent {
type_: SDL_EventType::SDL_JOYHATMOTION as u32,
timestamp,
@@ -1145,7 +1145,7 @@ impl Event {
axis,
value,
} => {
let axisval = axis.to_ll();
let axisval = axis.into_ll();
let event = sys::SDL_ControllerAxisEvent {
type_: SDL_EventType::SDL_CONTROLLERAXISMOTION as u32,
timestamp,
@@ -1167,7 +1167,7 @@ impl Event {
which,
button,
} => {
let buttonval = button.to_ll();
let buttonval = button.into_ll();
let event = sys::SDL_ControllerButtonEvent {
type_: SDL_EventType::SDL_CONTROLLERBUTTONDOWN as u32,
timestamp,
@@ -1190,7 +1190,7 @@ impl Event {
which,
button,
} => {
let buttonval = button.to_ll();
let buttonval = button.into_ll();
let event = sys::SDL_ControllerButtonEvent {
type_: SDL_EventType::SDL_CONTROLLERBUTTONUP as u32,
timestamp,
5 changes: 5 additions & 0 deletions src/sdl2/joystick.rs
Original file line number Diff line number Diff line change
@@ -497,7 +497,12 @@ impl HatState {
}
}

#[deprecated(since="0.34.4", note="please, use `as_raw()` instead")]
pub fn to_raw(self) -> u8 {
self.as_raw()
}

pub fn as_raw(self) -> u8 {
match self {
HatState::Centered => 0,
HatState::Up => 1,
5 changes: 5 additions & 0 deletions src/sdl2/mouse/mod.rs
Original file line number Diff line number Diff line change
@@ -177,9 +177,14 @@ impl MouseState {
pub fn from_sdl_state(state: u32) -> MouseState {
MouseState { mouse_state : state, x: 0, y: 0 }
}

#[deprecated(since="0.34.4", note="please, use `as_sdl_state()` instead")]
pub fn to_sdl_state(&self) -> u32 {
self.mouse_state
}
pub fn as_sdl_state(&self) -> u32 {
self.mouse_state
}

fn button_mask(&self, button: u32) -> u32 {
1 << (button - 1)
5 changes: 5 additions & 0 deletions src/sdl2/mouse/relative.rs
Original file line number Diff line number Diff line change
@@ -30,9 +30,14 @@ impl RelativeMouseState {
pub fn from_sdl_state(state: u32) -> RelativeMouseState {
RelativeMouseState { mouse_state : state, x: 0, y: 0 }
}

#[deprecated(since="0.34.4", note="please, use `as_sdl_state()` instead")]
pub fn to_sdl_state(&self) -> u32 {
self.mouse_state
}
pub fn as_sdl_state(&self) -> u32 {
self.mouse_state
}

fn button_mask(&self, button: u32) -> u32 {
1 << (button - 1)
4 changes: 4 additions & 0 deletions src/sdl2/pixels.rs
Original file line number Diff line number Diff line change
@@ -111,9 +111,13 @@ impl Color {
Color { r, g, b, a }
}

#[deprecated(since="0.34.4", note="please, use `as_u32()`")]
pub fn to_u32(self, format: &PixelFormat) -> u32 {
unsafe { sys::SDL_MapRGBA(format.raw, self.r, self.g, self.b, self.a) }
}
pub fn as_u32(self, format: &PixelFormat) -> u32 {
unsafe { sys::SDL_MapRGBA(format.raw, self.r, self.g, self.b, self.a) }
}

pub fn from_u32(format: &PixelFormat, pixel: u32) -> Color {
let (mut r, mut g, mut b, mut a) = (0, 0, 0, 0);
26 changes: 25 additions & 1 deletion src/sdl2/render.rs
Original file line number Diff line number Diff line change
@@ -355,15 +355,27 @@ impl<'s> Canvas<Surface<'s>> {

/// Gets a reference to the associated surface of the Canvas
#[inline]
#[deprecated(since="0.34.4", note="please, use `as_surface()` instead")]
pub fn surface(&self) -> &SurfaceRef {
&self.target
}
/// Gets a reference to the associated surface of the Canvas
#[inline]
pub fn as_surface(&self) -> &SurfaceRef {
&self.target
}

/// Gets a mutable reference to the associated surface of the Canvas
#[inline]
#[deprecated(since="0.34.4", note="please, use `as_surface_mut()` instead")]
pub fn surface_mut(&mut self) -> &mut SurfaceRef {
&mut self.target
}
/// Gets a mutable reference to the associated surface of the Canvas
#[inline]
pub fn as_surface_mut(&mut self) -> &mut SurfaceRef {
&mut self.target
}

/// Gets the associated surface of the Canvas and destroys the Canvas
#[inline]
@@ -395,15 +407,27 @@ impl RenderTarget for Window {
impl Canvas<Window> {
/// Gets a reference to the associated window of the Canvas
#[inline]
#[deprecated(since="0.34.4", note="please, use `as_window()` instead")]
pub fn window(&self) -> &Window {
&self.target
}
/// Gets a reference to the associated window of the Canvas
#[inline]
pub fn as_window(&self) -> &Window {
&self.target
}

/// Gets a mutable reference to the associated window of the Canvas
#[inline]
#[deprecated(since="0.34.4", note="please, use `as_window_mut()` instead")]
pub fn window_mut(&mut self) -> &mut Window {
&mut self.target
}
/// Gets a mutable reference to the associated window of the Canvas
#[inline]
pub fn as_window_mut(&mut self) -> &mut Window {
&mut self.target
}

/// Gets the associated window of the Canvas and destroys the Canvas
#[inline]
@@ -413,7 +437,7 @@ impl Canvas<Window> {

#[inline]
pub fn default_pixel_format(&self) -> PixelFormatEnum {
self.window().window_pixel_format()
self.as_window().window_pixel_format()
}

/// Returns a `TextureCreator` that can create Textures to be drawn on this `Canvas`
28 changes: 22 additions & 6 deletions src/sdl2/surface.rs
Original file line number Diff line number Diff line change
@@ -174,6 +174,14 @@ impl<'a> Surface<'a> {
}
}

/// A convenience function for [`TextureCreator::create_texture_from_surface`].
/// Deprecated: use `to_texture()` instead.
#[cfg(not(feature = "unsafe_textures"))]
#[deprecated(since="0.34.4", note="please, use `to_texture()` instead")]
pub fn as_texture<'b, T>(&self, texture_creator: &'b TextureCreator<T>) -> Result<Texture<'b>, TextureValueError> {
texture_creator.create_texture_from_surface(self)
}

/// A convenience function for [`TextureCreator::create_texture_from_surface`].
///
/// ```no_run
@@ -198,10 +206,18 @@ impl<'a> Surface<'a> {
/// let texture_creator = canvas.texture_creator();
///
/// let surface = Surface::new(512, 512, PixelFormatEnum::RGB24).unwrap();
/// let texture = surface.as_texture(&texture_creator).unwrap();
/// let texture = surface.to_texture(&texture_creator).unwrap();
/// ```
#[cfg(not(feature = "unsafe_textures"))]
pub fn as_texture<'b, T>(&self, texture_creator: &'b TextureCreator<T>) -> Result<Texture<'b>, TextureValueError> {
pub fn to_texture<'b, T>(&self, texture_creator: &'b TextureCreator<T>) -> Result<Texture<'b>, TextureValueError> {
texture_creator.create_texture_from_surface(self)
}

/// A convenience function for [`TextureCreator::create_texture_from_surface`].
/// Deprecated: use `to_texture()` instead.
#[cfg(feature = "unsafe_textures")]
#[deprecated(since="0.34.4", note="please, use `to_texture()` instead")]
pub fn as_texture<T>(&self, texture_creator: &TextureCreator<T>) -> Result<Texture, TextureValueError> {
texture_creator.create_texture_from_surface(self)
}

@@ -229,10 +245,10 @@ impl<'a> Surface<'a> {
/// let texture_creator = canvas.texture_creator();
///
/// let surface = Surface::new(512, 512, PixelFormatEnum::RGB24).unwrap();
/// let texture = surface.as_texture(&texture_creator).unwrap();
/// let texture = surface.to_texture(&texture_creator).unwrap();
/// ```
#[cfg(feature = "unsafe_textures")]
pub fn as_texture<T>(&self, texture_creator: &TextureCreator<T>) -> Result<Texture, TextureValueError> {
pub fn to_texture<T>(&self, texture_creator: &TextureCreator<T>) -> Result<Texture, TextureValueError> {
texture_creator.create_texture_from_surface(self)
}

@@ -432,7 +448,7 @@ impl SurfaceRef {
}

pub fn set_color_key(&mut self, enable: bool, color: pixels::Color) -> Result<(), String> {
let key = color.to_u32(&self.pixel_format());
let key = color.as_u32(&self.pixel_format());
let result = unsafe {
sys::SDL_SetColorKey(self.raw(), if enable { 1 } else { 0 }, key)
};
@@ -497,7 +513,7 @@ impl SurfaceRef {
let rect_ptr = mem::transmute(rect.as_ref()); // TODO find a better way to transform
// Option<&...> into a *const _
let format = self.pixel_format();
let result = sys::SDL_FillRect(self.raw(), rect_ptr, color.to_u32(&format) );
let result = sys::SDL_FillRect(self.raw(), rect_ptr, color.as_u32(&format) );
match result {
0 => Ok(()),
_ => Err(get_error())