From f528fdc1b9e5e26b2bc012e6122f1b4edf763567 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 2 Aug 2022 14:35:44 +0200 Subject: [PATCH] More cleaning --- src/engine/mod.rs | 31 ++++++++++++------------------- src/lib.rs | 4 ++-- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 0c60a05..33538de 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -40,17 +40,12 @@ macro_rules! unwrap { /// Our game engine. #[derive(Clone)] -pub struct Engine { - /// The inner engine. - /// - /// We need Rc in order to deal with events. - pub inner: Rc>, -} +pub struct Game(Rc>); -impl Engine { - /// Creates a new engine. - pub fn new() -> Result { - let inner = Rc::new(RefCell::new(InnerEngine::new()?)); +impl Game { + /// Creates a new game. + pub fn new() -> Result { + Ok(Game(Rc::new(RefCell::new(Engine::new()?)))) // let clone = inner.clone(); // let cb = Closure::::new(move |event: web_sys::GamepadEvent| { @@ -62,11 +57,9 @@ impl Engine { // .add_event_listener_with_callback("gamepadconnected", cb.as_ref().unchecked_ref())?; // cb.forget(); - - Ok(Engine { inner }) } - /// Starts the engine. + /// Starts the game. pub fn start(&self) -> Result<()> { let clone = self.clone(); let cb = Closure::::new(move |_event: web_sys::Event| { @@ -75,7 +68,7 @@ impl Engine { } }); - let inner = self.inner.borrow_mut(); + let inner = self.0.borrow_mut(); inner .window .request_animation_frame(cb.as_ref().unchecked_ref())?; @@ -87,7 +80,7 @@ impl Engine { /// Launches a loop of the engine, and schedules the next one. pub fn run(&self) -> Result<()> { - let mut inner = self.inner.borrow_mut(); + let mut inner = self.0.borrow_mut(); // Perform update inner.update()?; @@ -114,7 +107,7 @@ impl Engine { } /// The data contained in our engine. -pub struct InnerEngine { +pub struct Engine { /// The scene of the engine. pub scene: Scene, @@ -142,9 +135,9 @@ pub struct InnerEngine { pub performance: web_sys::Performance, } -impl InnerEngine { +impl Engine { /// Initializes the engine. - pub fn new() -> Result { + pub fn new() -> Result { let window = unwrap!(web_sys::window()); let document = unwrap!(window.document()); let performance = unwrap!(window.performance()); @@ -163,7 +156,7 @@ impl InnerEngine { let character = Character::new(); scene.add(character); - Ok(InnerEngine { + Ok(Engine { scene, after_loop: now(&performance), textures: TextureManager::new()?, diff --git a/src/lib.rs b/src/lib.rs index 0e6ec5f..976ae9d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,8 +61,8 @@ pub(crate) use log; pub(crate) use log_js; pub fn run() -> Result<()> { - let engine = engine::Engine::new()?; - engine.start()?; + let game = engine::Game::new()?; + game.start()?; Ok(()) }