Box shit up to spare the stack

This commit is contained in:
Dane Johnson 2022-04-15 19:44:15 -05:00
parent 05bc8cb7d6
commit 0676fc075b
2 changed files with 8 additions and 20 deletions

View File

@ -19,10 +19,10 @@ pub trait Childbearing<T> { // TODO remove this
impl Node { impl Node {
pub fn find_node(&mut self, name: &str) -> Option<NodeRef> { pub fn find_node(&mut self, name: &str) -> Option<NodeRef> {
match self.children.iter().find(|node| node.borrow().name == name) { self.children
Some(node) => Some(Rc::clone(node)), .iter()
None => None .find(|node| node.borrow().name == name)
} .map(|node| Rc::clone(node))
} }
} }
@ -30,7 +30,7 @@ macro_rules! impl_childbearing {
($t:ty, $e:path) => { ($t:ty, $e:path) => {
impl Childbearing<$t> for Node { impl Childbearing<$t> for Node {
fn add_child(&mut self, name: &str, child: $t) { fn add_child(&mut self, name: &str, child: $t) {
self.children.push(Rc::new(RefCell::new(Node::new(name.to_string(), $e(child))))); self.children.push(Rc::new(RefCell::new(Node::new(name.to_string(), $e(Box::new(child))))));
} }
} }
} }
@ -50,8 +50,8 @@ impl Node {
pub enum GameObject { pub enum GameObject {
Null, Null,
Camera(Camera), Camera(Box<Camera>),
Mesh(Mesh), Mesh(Box<Mesh>),
} }
impl GameObject { impl GameObject {

View File

@ -93,19 +93,7 @@ fn main() {
GameObject::Mesh(mesh) => mesh, GameObject::Mesh(mesh) => mesh,
_ => panic!("cube is not a mesh"), _ => panic!("cube is not a mesh"),
}; };
let mut transform = cube.get_transform(); // TODO, do this over the whole scene tree
if device_manager.is_pressed(&device::Key::W) {
transform.position.z += 1.0 * delta;
} else if device_manager.is_pressed(&device::Key::S) {
transform.position.z -= 1.0 * delta;
}
if device_manager.is_pressed(&device::Key::A) {
transform.rotation.x += 1.0 * delta;
} else if device_manager.is_pressed(&device::Key::D) {
transform.rotation.x -= 1.0 * delta;
}
cube.set_transform(transform);
let mvp = glm::perspective::<f32>(4.0/3.0, 45.0_f32.to_radians(), 0.2, 100.0); let mvp = glm::perspective::<f32>(4.0/3.0, 45.0_f32.to_radians(), 0.2, 100.0);
let mvp = glm::translate::<f32>(&mvp, &cube.get_transform().position); let mvp = glm::translate::<f32>(&mvp, &cube.get_transform().position);
let mvp = glm::rotate_x(&mvp, cube.get_transform().rotation.x); let mvp = glm::rotate_x(&mvp, cube.get_transform().rotation.x);