Skip to content
This repository has been archived by the owner on Jan 23, 2018. It is now read-only.

Commit

Permalink
Rearrange files and fix play_sound
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaeroxe committed Mar 18, 2017
1 parent b8bd8c3 commit e260189
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 103 deletions.
19 changes: 9 additions & 10 deletions nitro/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion nitro/src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use transform::Transform;
use math::Transform;

pub struct Camera {
pub transform: Transform,
Expand Down
2 changes: 1 addition & 1 deletion nitro/src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
16 changes: 8 additions & 8 deletions nitro/src/game_object.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -74,20 +74,20 @@ impl GameObject {
}))
}

pub fn remove_component(&mut self, app: &mut App, index: i32) -> OptionAway<Box<ComponentAny>> {
pub fn remove_component(&mut self, app: &mut App, index: i32) -> OptionLoaned<Box<ComponentAny>> {
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<ComponentAny>> {
OptionAway::from(self.components.get(&index))
pub fn component(&self, index: i32) -> OptionLoaned<&Box<ComponentAny>> {
OptionLoaned::from(self.components.get(&index))
}

pub fn component_mut(&mut self, index: i32) -> OptionAway<&mut Box<ComponentAny>> {
OptionAway::from(self.components.get_mut(&index))
pub fn component_mut(&mut self, index: i32) -> OptionLoaned<&mut Box<ComponentAny>> {
OptionLoaned::from(self.components.get_mut(&index))
}

pub fn component_with_type<T>(&self, index: i32) -> Option<&T>
Expand Down
87 changes: 8 additions & 79 deletions nitro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -52,80 +53,8 @@ pub mod input {
pub use input_private::Button;
}


mod camera;
pub use camera::Camera;

pub type Vector = nphysics2d::math::Vector<f32>;

/// 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<T> {
Some(T),
Away,
None,
}

use std::borrow::{Borrow, BorrowMut};

impl<T> From<Option<Option<T>>> for OptionAway<T> {
fn from(result: Option<Option<T>>) -> OptionAway<T> {
match result {
Some(Some(result)) => OptionAway::Some(result),
Some(None) => OptionAway::Away,
None => OptionAway::None,
}
}
}

impl<'a, T, M> From<Option<&'a Option<M>>> for OptionAway<&'a T>
where M: Borrow<T>
{
fn from(result: Option<&'a Option<M>>) -> 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<Option<&'a mut Option<M>>> for OptionAway<&'a mut T>
where M: BorrowMut<T>
{
fn from(result: Option<&'a mut Option<M>>) -> 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<T, K>(resource: &mut T, cost: K) -> bool
where T: SubAssign<K> + PartialOrd<K>
{
if *resource >= cost {
*resource -= cost;
true
} else {
false
}
}
mod option_loaned;
pub use option_loaned::OptionLoaned;
11 changes: 11 additions & 0 deletions nitro/src/math_private/check_and_use.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::ops::SubAssign;
pub fn check_and_use<T, K>(resource: &mut T, cost: K) -> bool
where T: SubAssign<K> + PartialOrd<K>
{
if *resource >= cost {
*resource -= cost;
true
} else {
false
}
}
5 changes: 5 additions & 0 deletions nitro/src/math_private/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Vector;
use math::Vector;
use std::f32;

pub struct PolarCoords {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::f32;
use Vector;
use math::Vector;

#[derive(Copy, Clone, Debug)]
pub struct Transform {
Expand Down
3 changes: 3 additions & 0 deletions nitro/src/math_private/vector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use nphysics2d;

pub type Vector = nphysics2d::math::Vector<f32>;
51 changes: 51 additions & 0 deletions nitro/src/option_loaned.rs
Original file line number Diff line number Diff line change
@@ -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<T> {
Some(T),
Loaned,
None,
}

impl<T> From<Option<Option<T>>> for OptionLoaned<T> {
fn from(result: Option<Option<T>>) -> OptionLoaned<T> {
match result {
Some(Some(result)) => OptionLoaned::Some(result),
Some(None) => OptionLoaned::Loaned,
None => OptionLoaned::None,
}
}
}

impl<'a, T, M> From<Option<&'a Option<M>>> for OptionLoaned<&'a T>
where M: Borrow<T>
{
fn from(result: Option<&'a Option<M>>) -> 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<Option<&'a mut Option<M>>> for OptionLoaned<&'a mut T>
where M: BorrowMut<T>
{
fn from(result: Option<&'a mut Option<M>>) -> 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,
}
}
}
2 changes: 1 addition & 1 deletion nitro/src/rect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Vector;
use math::Vector;
use sdl2::rect::Rect as SdlRect;

pub struct Rect {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e260189

Please sign in to comment.