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 {
pub fn find_node(&mut self, name: &str) -> Option<NodeRef> {
match self.children.iter().find(|node| node.borrow().name == name) {
Some(node) => Some(Rc::clone(node)),
None => None
}
self.children
.iter()
.find(|node| node.borrow().name == name)
.map(|node| Rc::clone(node))
}
}
@ -30,7 +30,7 @@ macro_rules! impl_childbearing {
($t:ty, $e:path) => {
impl Childbearing<$t> for Node {
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 {
Null,
Camera(Camera),
Mesh(Mesh),
Camera(Box<Camera>),
Mesh(Box<Mesh>),
}
impl GameObject {

View File

@ -93,19 +93,7 @@ fn main() {
GameObject::Mesh(mesh) => mesh,
_ => panic!("cube is not a mesh"),
};
let mut transform = cube.get_transform();
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);
// TODO, do this over the whole scene tree
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::rotate_x(&mvp, cube.get_transform().rotation.x);