diff --git a/nitro/src/app.rs b/nitro/src/app.rs index 89bba905..55452e66 100644 --- a/nitro/src/app.rs +++ b/nitro/src/app.rs @@ -7,7 +7,7 @@ use sdl2::rect::Rect; use sdl2::image::LoadTexture; use sdl2::render::Texture as SdlTexture; use sdl2::mixer; -use OptionAway; +use OptionLoaned; use input_private; use input::Input; use game_object::GameObject; @@ -17,11 +17,11 @@ use graphics::Texture; use graphics_private::texture; use graphics::Sprite; use graphics_private::sprite_sheet; -use PolarCoords; -use Vector; +use math::PolarCoords; +use math::Vector; use audio::Audio; use audio; -use transform::Transform; +use math::Transform; use Canvas; use camera::Camera; use nphysics2d::world::World; @@ -165,7 +165,7 @@ impl App { for game_obj in game_objs.values() { if let Some(ref game_obj) = *game_obj { for key in game_obj.component_keys() { - if let OptionAway::Some(component) = game_obj.component(key) { + if let OptionLoaned::Some(component) = game_obj.component(key) { component.render_gui(&mut canvas, game_obj); } } @@ -232,7 +232,6 @@ impl App { /// /// This function won't return until the game has been quit. pub fn run(&mut self) { - mixer::allocate_channels(128); let mut last_frame_instant = Instant::now(); while !self.exit { while let Some(e) = self.event_pump.poll_event() { @@ -289,8 +288,8 @@ impl App { /// /// Hint: That borrow is most likely in the game_object parameter of the receive_message /// function. - pub fn game_object_by_id(&self, id: u64) -> OptionAway<&GameObject> { - OptionAway::from(self.game_objects.get(&id)) + pub fn game_object_by_id(&self, id: u64) -> OptionLoaned<&GameObject> { + OptionLoaned::from(self.game_objects.get(&id)) } /// Retrieves a mutable reference to an existing GameObject by the GameObject's id. @@ -300,8 +299,8 @@ impl App { /// /// Hint: That borrow is most likely in the game_object parameter of the receive_message /// function. - pub fn game_object_by_id_mut(&mut self, id: u64) -> OptionAway<&mut GameObject> { - OptionAway::from(self.game_objects.get_mut(&id)) + pub fn game_object_by_id_mut(&mut self, id: u64) -> OptionLoaned<&mut GameObject> { + OptionLoaned::from(self.game_objects.get_mut(&id)) } /// Loads a texture and returns it for use. diff --git a/nitro/src/camera.rs b/nitro/src/camera.rs index 79e9e1e2..c4c4d7a9 100644 --- a/nitro/src/camera.rs +++ b/nitro/src/camera.rs @@ -1,4 +1,4 @@ -use transform::Transform; +use math::Transform; pub struct Camera { pub transform: Transform, diff --git a/nitro/src/canvas.rs b/nitro/src/canvas.rs index d211ce06..34ed7a3a 100644 --- a/nitro/src/canvas.rs +++ b/nitro/src/canvas.rs @@ -3,7 +3,7 @@ use graphics_private::texture::Texture; use rect::Rect; use sdl2::rect::Rect as SdlRect; use sdl2::rect::Point; -use Vector; +use math::Vector; use sdl2::render::Renderer; pub struct Canvas<'a> { diff --git a/nitro/src/game_object.rs b/nitro/src/game_object.rs index b399c263..6acd3de7 100644 --- a/nitro/src/game_object.rs +++ b/nitro/src/game_object.rs @@ -1,8 +1,8 @@ use app; use app::App; -use OptionAway; +use OptionLoaned; use graphics::Sprite; -use transform::Transform; +use math::Transform; use component::Component; use component::ComponentAny; use component::Message; @@ -74,20 +74,20 @@ impl GameObject { })) } - pub fn remove_component(&mut self, app: &mut App, index: i32) -> OptionAway> { + pub fn remove_component(&mut self, app: &mut App, index: i32) -> OptionLoaned> { let mut component_result = self.components.remove(&index); if let Some(Some(ref mut component)) = component_result { component.receive_message(app, self, &Message::OnDetach); } - OptionAway::from(component_result) + OptionLoaned::from(component_result) } - pub fn component(&self, index: i32) -> OptionAway<&Box> { - OptionAway::from(self.components.get(&index)) + pub fn component(&self, index: i32) -> OptionLoaned<&Box> { + OptionLoaned::from(self.components.get(&index)) } - pub fn component_mut(&mut self, index: i32) -> OptionAway<&mut Box> { - OptionAway::from(self.components.get_mut(&index)) + pub fn component_mut(&mut self, index: i32) -> OptionLoaned<&mut Box> { + OptionLoaned::from(self.components.get_mut(&index)) } pub fn component_with_type(&self, index: i32) -> Option<&T> diff --git a/nitro/src/lib.rs b/nitro/src/lib.rs index a5b01dcc..944fb8f7 100644 --- a/nitro/src/lib.rs +++ b/nitro/src/lib.rs @@ -37,11 +37,12 @@ pub use canvas::Canvas; mod rect; pub use rect::Rect; -mod transform; -pub use transform::Transform; - -mod polar_coords; -pub use polar_coords::PolarCoords; +mod math_private; +pub mod math { + pub use math_private::vector::Vector; + pub use math_private::transform::Transform; + pub use math_private::polar_coords::PolarCoords; +} mod input_private; pub mod input { @@ -52,80 +53,8 @@ pub mod input { pub use input_private::Button; } - mod camera; pub use camera::Camera; -pub type Vector = nphysics2d::math::Vector; - -/// Special variant of Option. -/// -/// TL; DR if a function returns Away that indicates the object is already loaned out -/// and is likely already available to you in the arguments of the function you're working in. -/// -/// In order to satisfy Rust's rule that there cannot be multiple mutable aliases -/// to a struct and still maintain a convenient API items are removed from their -/// parent containers before they are passed to the user of Nitro, they are then -/// reinserted into their parent containers after the user is done with it. -/// In order to signal that an item still exists but is merely not in the queried -/// container at this instant the Away option may be returned by the API. -pub enum OptionAway { - Some(T), - Away, - None, -} - -use std::borrow::{Borrow, BorrowMut}; - -impl From>> for OptionAway { - fn from(result: Option>) -> OptionAway { - match result { - Some(Some(result)) => OptionAway::Some(result), - Some(None) => OptionAway::Away, - None => OptionAway::None, - } - } -} - -impl<'a, T, M> From>> for OptionAway<&'a T> - where M: Borrow -{ - fn from(result: Option<&'a Option>) -> OptionAway<&'a T> { - match result { - Some(inner) => { - match inner.as_ref() { - Some(inner) => OptionAway::Some(inner.borrow()), - None => OptionAway::Away, - } - } - None => OptionAway::None, - } - } -} - -impl<'a, T, M> From>> for OptionAway<&'a mut T> - where M: BorrowMut -{ - fn from(result: Option<&'a mut Option>) -> OptionAway<&'a mut T> { - match result { - Some(inner) => { - match inner.as_mut() { - Some(inner) => OptionAway::Some(inner.borrow_mut()), - None => OptionAway::Away, - } - } - None => OptionAway::None, - } - } -} -use std::ops::SubAssign; -pub fn check_and_use(resource: &mut T, cost: K) -> bool - where T: SubAssign + PartialOrd -{ - if *resource >= cost { - *resource -= cost; - true - } else { - false - } -} +mod option_loaned; +pub use option_loaned::OptionLoaned; diff --git a/nitro/src/math_private/check_and_use.rs b/nitro/src/math_private/check_and_use.rs new file mode 100644 index 00000000..417c0da3 --- /dev/null +++ b/nitro/src/math_private/check_and_use.rs @@ -0,0 +1,11 @@ +use std::ops::SubAssign; +pub fn check_and_use(resource: &mut T, cost: K) -> bool + where T: SubAssign + PartialOrd +{ + if *resource >= cost { + *resource -= cost; + true + } else { + false + } +} diff --git a/nitro/src/math_private/mod.rs b/nitro/src/math_private/mod.rs new file mode 100644 index 00000000..bd82c31c --- /dev/null +++ b/nitro/src/math_private/mod.rs @@ -0,0 +1,5 @@ +pub mod vector; +pub mod transform; +pub mod polar_coords; +mod check_and_use; +pub use self::check_and_use::check_and_use; diff --git a/nitro/src/polar_coords.rs b/nitro/src/math_private/polar_coords.rs similarity index 96% rename from nitro/src/polar_coords.rs rename to nitro/src/math_private/polar_coords.rs index 27e702d0..a7ea2f53 100644 --- a/nitro/src/polar_coords.rs +++ b/nitro/src/math_private/polar_coords.rs @@ -1,4 +1,4 @@ -use Vector; +use math::Vector; use std::f32; pub struct PolarCoords { diff --git a/nitro/src/transform.rs b/nitro/src/math_private/transform.rs similarity index 98% rename from nitro/src/transform.rs rename to nitro/src/math_private/transform.rs index 2ab38382..678297c7 100644 --- a/nitro/src/transform.rs +++ b/nitro/src/math_private/transform.rs @@ -1,5 +1,5 @@ use std::f32; -use Vector; +use math::Vector; #[derive(Copy, Clone, Debug)] pub struct Transform { diff --git a/nitro/src/math_private/vector.rs b/nitro/src/math_private/vector.rs new file mode 100644 index 00000000..f3c63c05 --- /dev/null +++ b/nitro/src/math_private/vector.rs @@ -0,0 +1,3 @@ +use nphysics2d; + +pub type Vector = nphysics2d::math::Vector; diff --git a/nitro/src/option_loaned.rs b/nitro/src/option_loaned.rs new file mode 100644 index 00000000..82eeb88e --- /dev/null +++ b/nitro/src/option_loaned.rs @@ -0,0 +1,51 @@ +use std::borrow::{Borrow, BorrowMut}; + +/// Special variant of Option that indicates a value may exist but is currently loaned out in a +/// &mut. +pub enum OptionLoaned { + Some(T), + Loaned, + None, +} + +impl From>> for OptionLoaned { + fn from(result: Option>) -> OptionLoaned { + match result { + Some(Some(result)) => OptionLoaned::Some(result), + Some(None) => OptionLoaned::Loaned, + None => OptionLoaned::None, + } + } +} + +impl<'a, T, M> From>> for OptionLoaned<&'a T> + where M: Borrow +{ + fn from(result: Option<&'a Option>) -> OptionLoaned<&'a T> { + match result { + Some(inner) => { + match inner.as_ref() { + Some(inner) => OptionLoaned::Some(inner.borrow()), + None => OptionLoaned::Loaned, + } + } + None => OptionLoaned::None, + } + } +} + +impl<'a, T, M> From>> for OptionLoaned<&'a mut T> + where M: BorrowMut +{ + fn from(result: Option<&'a mut Option>) -> OptionLoaned<&'a mut T> { + match result { + Some(inner) => { + match inner.as_mut() { + Some(inner) => OptionLoaned::Some(inner.borrow_mut()), + None => OptionLoaned::Loaned, + } + } + None => OptionLoaned::None, + } + } +} diff --git a/nitro/src/rect.rs b/nitro/src/rect.rs index 609dba12..99af8057 100644 --- a/nitro/src/rect.rs +++ b/nitro/src/rect.rs @@ -1,4 +1,4 @@ -use Vector; +use math::Vector; use sdl2::rect::Rect as SdlRect; pub struct Rect { diff --git a/src/main.rs b/src/main.rs index 635a2ae1..d43e6e6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ mod actions; use nitro::App; use nitro::input::{Axis, Button}; use nitro::input::keyboard::Key; -use nitro::Vector; +use nitro::math::Vector; use nitro::graphics::Sprite; use spinny::Spinny; use axes::AxisId;