Better struct for scene

This commit is contained in:
Thomas Forgione 2018-04-18 10:26:40 +02:00
parent 2725d04a3d
commit ef667b5a02
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
2 changed files with 41 additions and 6 deletions

View File

@ -4,8 +4,6 @@ extern crate glium;
extern crate verbose_log; extern crate verbose_log;
extern crate model_converter; extern crate model_converter;
use std::rc::Rc;
use std::cell::RefCell;
use std::process::exit; use std::process::exit;
use std::time::{Instant, Duration}; use std::time::{Instant, Duration};
@ -22,7 +20,7 @@ use glium::glutin::Event;
use glium::glutin::WindowEvent; use glium::glutin::WindowEvent;
use glium::glutin::VirtualKeyCode; use glium::glutin::VirtualKeyCode;
use model_converter::scene::Scene;
use model_converter::math::bounding_box::BoundingBox3; use model_converter::math::bounding_box::BoundingBox3;
use model_converter::math::vector::Vector3; use model_converter::math::vector::Vector3;
use model_converter::parser::parse; use model_converter::parser::parse;
@ -89,7 +87,7 @@ fn main() {
let display = Display::new(window, context, &events_loop).unwrap(); let display = Display::new(window, context, &events_loop).unwrap();
let mut renderer = Renderer::new(display); let mut renderer = Renderer::new(display);
let mut scene = vec![]; let mut scene = Scene::new();
let mut before; let mut before;
let mut duration; let mut duration;
@ -110,7 +108,7 @@ fn main() {
model.build_vertex_buffers(&renderer); model.build_vertex_buffers(&renderer);
duration = Instant::now().duration_since(before); duration = Instant::now().duration_since(before);
scene.push(Rc::new(RefCell::new(model))); scene.emplace(model);
logln!(" done in {}ms.\nFinished", as_millis(duration)); logln!(" done in {}ms.\nFinished", as_millis(duration));
} }

View File

@ -1,8 +1,45 @@
//! This module contains everything related to scenes. //! This module contains everything related to scenes.
use std::slice::Iter;
use std::vec::IntoIter;
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use model::Model; use model::Model;
/// Represents a 3D scene with models. /// Represents a 3D scene with models.
pub type Scene = Vec<Rc<RefCell<Model>>>; pub struct Scene {
models: Vec<Rc<RefCell<Model>>>,
}
impl Scene {
/// Creates a new empty scene.
pub fn new() -> Scene {
Scene {
models: vec![],
}
}
/// Adds a model to the scene.
pub fn push(&mut self, model: Rc<RefCell<Model>>) {
self.models.push(model);
}
/// Converts the model to a Rc<RefCell<Model>> and adds it to the scene.
pub fn emplace(&mut self, model: Model) {
self.models.push(Rc::new(RefCell::new(model)));
}
/// Returns an iterator to the models of the scene.
pub fn iter(&self) -> Iter<Rc<RefCell<Model>>> {
self.models.iter()
}
}
impl IntoIterator for Scene {
type IntoIter = IntoIter<Rc<RefCell<Model>>>;
type Item = Rc<RefCell<Model>>;
fn into_iter(self) -> Self::IntoIter {
self.models.into_iter()
}
}