kitgit

tirbofish/dropbear · diff

ac47895 · tk

changing to rc refs instead of boxes for shared scene mgmt and so on

Unverified

diff --git a/.gitignore b/.gitignore
index a0fbdf1..5ce925a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,3 +20,4 @@ target
 #  option (not recommended) you can uncomment the following to ignore the entire idea folder.
 .idea/
 Cargo.lock
+dropbear-engine/src/resources/textures/Autism.png
diff --git a/dropbear-engine/src/graphics.rs b/dropbear-engine/src/graphics.rs
index 305137c..a6294f4 100644
--- a/dropbear-engine/src/graphics.rs
+++ b/dropbear-engine/src/graphics.rs
@@ -148,7 +148,7 @@ impl Shader {
 }
 
 pub struct Texture {
-    pub diffuse: wgpu::BindGroup,
+    pub bind_group: wgpu::BindGroup,
     pub layout: wgpu::BindGroupLayout,
 }
 
@@ -239,12 +239,12 @@ impl Texture {
                         resource: wgpu::BindingResource::Sampler(&diffuse_sampler),
                     }
                 ],
-                label: Some("diffuse_bind_group"),
+                label: Some("texture_bind_group"),
             }
         );
         
         Self {
-            diffuse: diffuse_bind_group,
+            bind_group: diffuse_bind_group,
             layout: texture_bind_group_layout,
         }
     }
diff --git a/dropbear-engine/src/input.rs b/dropbear-engine/src/input.rs
index c9582a5..e1cdc0e 100644
--- a/dropbear-engine/src/input.rs
+++ b/dropbear-engine/src/input.rs
@@ -1,8 +1,11 @@
-use std::collections::{HashMap, HashSet};
+use std::{cell::RefCell, collections::{HashMap, HashSet}, rc::Rc};
 use winit::{
     dpi::PhysicalPosition, event::MouseButton, event_loop::ActiveEventLoop, keyboard::KeyCode,
 };
 
+pub type KeyboardImpl = Rc<RefCell<dyn Keyboard>>;
+pub type MouseImpl = Rc<RefCell<dyn Mouse>>;
+
 pub trait Keyboard {
     fn key_down(&mut self, key: KeyCode, event_loop: &ActiveEventLoop);
     fn key_up(&mut self, key: KeyCode, event_loop: &ActiveEventLoop);
@@ -24,8 +27,8 @@ pub struct Manager {
     just_released_mouse_buttons: HashSet<MouseButton>,
     mouse_position: PhysicalPosition<f64>,
 
-    input_handlers: HashMap<String, Box<dyn Keyboard>>,
-    mouse_handlers: HashMap<String, Box<dyn Mouse>>,
+    input_handlers: HashMap<String, KeyboardImpl>,
+    mouse_handlers: HashMap<String, MouseImpl>,
 }
 
 impl Manager {
@@ -43,11 +46,11 @@ impl Manager {
         }
     }
 
-    pub fn add_keyboard(&mut self, name: &str, handler: Box<dyn Keyboard>) {
+    pub fn add_keyboard(&mut self, name: &str, handler: KeyboardImpl) {
         self.input_handlers.insert(name.to_string(), handler);
     }
 
-    pub fn add_mouse(&mut self, name: &str, handler: Box<dyn Mouse>) {
+    pub fn add_mouse(&mut self, name: &str, handler: MouseImpl) {
         self.mouse_handlers.insert(name.to_string(), handler);
     }
 
@@ -57,7 +60,7 @@ impl Manager {
                 self.just_pressed_keys.insert(key);
                 // Notify all input handlers of key down
                 for handler in self.input_handlers.values_mut() {
-                    handler.key_down(key, event_loop);
+                    handler.borrow_mut().key_down(key, event_loop);
                 }
             }
             self.pressed_keys.insert(key);
@@ -66,7 +69,7 @@ impl Manager {
                 self.just_released_keys.insert(key);
                 // Notify all input handlers of key up
                 for handler in self.input_handlers.values_mut() {
-                    handler.key_up(key, event_loop);
+                    handler.borrow_mut().key_up(key, event_loop);
                 }
             }
             self.pressed_keys.remove(&key);
@@ -79,7 +82,7 @@ impl Manager {
                 self.just_pressed_mouse_buttons.insert(button);
                 // Notify all mouse handlers of button down
                 for handler in self.mouse_handlers.values_mut() {
-                    handler.mouse_down(button);
+                    handler.borrow_mut().mouse_down(button);
                 }
             }
             self.pressed_mouse_buttons.insert(button);
@@ -88,7 +91,7 @@ impl Manager {
                 self.just_released_mouse_buttons.insert(button);
                 // Notify all mouse handlers of button up
                 for handler in self.mouse_handlers.values_mut() {
-                    handler.mouse_up(button);
+                    handler.borrow_mut().mouse_up(button);
                 }
             }
             self.pressed_mouse_buttons.remove(&button);
@@ -99,7 +102,7 @@ impl Manager {
         self.mouse_position = position;
         // Notify all mouse handlers of movement
         for handler in self.mouse_handlers.values_mut() {
-            handler.mouse_move(position);
+            handler.borrow_mut().mouse_move(position);
         }
     }
 
diff --git a/dropbear-engine/src/scene.rs b/dropbear-engine/src/scene.rs
index ddff2aa..ae1d17b 100644
--- a/dropbear-engine/src/scene.rs
+++ b/dropbear-engine/src/scene.rs
@@ -1,5 +1,5 @@
 use crate::graphics::Graphics;
-use std::collections::HashMap;
+use std::{cell::RefCell, collections::HashMap, rc::Rc};
 
 pub trait Scene {
     fn load(&mut self, graphics: &mut Graphics);
@@ -8,12 +8,12 @@ pub trait Scene {
     fn exit(&mut self);
 }
 
-pub type SceneImpl = Box<dyn Scene>;
+pub type SceneImpl = Rc<RefCell<dyn Scene>>;
 
 pub struct Manager {
     current_scene: Option<String>,
     next_scene: Option<String>,
-    scenes: HashMap<String, Box<dyn Scene>>,
+    scenes: HashMap<String, SceneImpl>,
     scene_input_map: HashMap<String, String>,
 }
 
@@ -33,7 +33,7 @@ impl Manager {
         }
     }
 
-    pub fn add(&mut self, name: &str, scene: Box<dyn Scene>) {
+    pub fn add(&mut self, name: &str, scene: Rc<RefCell<dyn Scene>>) {
         self.scenes.insert(name.to_string(), scene);
     }
 
@@ -47,11 +47,11 @@ impl Manager {
         if let Some(next_scene_name) = self.next_scene.take() {
             if let Some(current_scene_name) = &self.current_scene {
                 if let Some(scene) = self.scenes.get_mut(current_scene_name) {
-                    scene.exit();
+                    scene.borrow_mut().exit();
                 }
             }
             if let Some(scene) = self.scenes.get_mut(&next_scene_name) {
-                scene.load(graphics);
+                scene.borrow_mut().load(graphics);
             }
             self.current_scene = Some(next_scene_name);
         }
@@ -59,7 +59,7 @@ impl Manager {
         // update scene
         if let Some(scene_name) = &self.current_scene {
             if let Some(scene) = self.scenes.get_mut(scene_name) {
-                scene.update(dt);
+                scene.borrow_mut().update(dt);
             }
         }
     }
@@ -67,7 +67,7 @@ impl Manager {
     pub fn render(&mut self, graphics: &mut Graphics) {
         if let Some(scene_name) = &self.current_scene {
             if let Some(scene) = self.scenes.get_mut(scene_name) {
-                scene.render(graphics);
+                scene.borrow_mut().render(graphics);
             }
         }
     }
diff --git a/eucalyptus/src/main.rs b/eucalyptus/src/main.rs
index 6a8e5dd..c29caaf 100644
--- a/eucalyptus/src/main.rs
+++ b/eucalyptus/src/main.rs
@@ -1,6 +1,10 @@
 mod scene1;
+use std::{cell::RefCell, rc::Rc};
+
 use dropbear_engine::WindowConfiguration;
 
+use crate::scene1::TestingScene1;
+
 fn main() {
     let config = WindowConfiguration {
         width: 1280u32,
@@ -9,13 +13,11 @@ fn main() {
     };
 
     let _app = dropbear_engine::run_app!(config, |scene_manager, input_manager| {
-        let testing_scene = scene1::TestingScene1::new();
-        let testing_keyboard = scene1::TestingScene1::new();
-        let testing_mouse = scene1::TestingScene1::new();
+        let testing_scene = Rc::new(RefCell::new(TestingScene1::new()));
 
-        scene_manager.add("testing_scene", Box::new(testing_scene));
-        input_manager.add_keyboard("testing_scene_keyboard", Box::new(testing_keyboard));
-        input_manager.add_mouse("testing_scene_mouse", Box::new(testing_mouse));
+        scene_manager.add("testing_scene", testing_scene.clone());
+        input_manager.add_keyboard("testing_scene_keyboard", testing_scene.clone());
+        input_manager.add_mouse("testing_scene_mouse", testing_scene.clone());
 
         scene_manager.attach_input("testing_scene", "testing_scene_keyboard");
         scene_manager.attach_input("testing_scene", "testing_scene_mouse");
diff --git a/eucalyptus/src/scene1.rs b/eucalyptus/src/scene1.rs
index 28254fd..3e006c1 100644
--- a/eucalyptus/src/scene1.rs
+++ b/eucalyptus/src/scene1.rs
@@ -1,3 +1,5 @@
+use std::collections::HashMap;
+
 use dropbear_engine::buffer::Vertex;
 use dropbear_engine::graphics::{Graphics, Texture, Shader};
 use dropbear_engine::wgpu::{Buffer, Color, IndexFormat, RenderPipeline};
@@ -12,16 +14,20 @@ pub struct TestingScene1 {
     render_pipeline: Option<RenderPipeline>,
     vertex_buffer: Option<Buffer>,
     index_buffer: Option<Buffer>,
-    texture: Option<Texture>,
+    texture: HashMap<String, Texture>,
+
+    texture_toggle: bool,
 }
 
 impl TestingScene1 {
     pub fn new() -> Self {
+        debug!("TestingScene1 instance created");
         Self {
             render_pipeline: None,
             vertex_buffer: None,
             index_buffer: None,
-            texture: None,
+            texture: HashMap::new(),
+            texture_toggle: false,
         }
     }
 }
@@ -48,16 +54,21 @@ impl Scene for TestingScene1 {
 
         self.vertex_buffer = Some(graphics.create_vertex(VERTICES));
         self.index_buffer = Some(graphics.create_index(INDICES));
-        let texture = Texture::new(graphics, include_bytes!("../../dropbear-engine/src/resources/textures/no-texture.png"));
+        let texture1 = Texture::new(graphics, include_bytes!("../../dropbear-engine/src/resources/textures/no-texture.png"));
+        let texture2 = Texture::new(graphics, include_bytes!("../../dropbear-engine/src/resources/textures/Autism.png"));
+        
+        // using one of them for now
+        let pipeline = graphics.create_render_pipline(&shader, vec![&texture1]);
         
-        let pipeline = graphics.create_render_pipline(&shader, vec![&texture]);
+
+        self.texture.insert("texture1".into(), texture1);
+        self.texture.insert("texture2".into(), texture2);
+
+        // ensure that this is the last line
         self.render_pipeline = Some(pipeline);
-        self.texture = Some(texture);
     }
 
-    fn update(&mut self, _dt: f32) {
-        // log::info!("FPS: {}", 1.0 / dt)
-    }
+    fn update(&mut self, _dt: f32) {}
 
     fn render(&mut self, graphics: &mut Graphics) {
         let color = Color {
@@ -70,7 +81,14 @@ impl Scene for TestingScene1 {
 
         if let Some(pipeline) = &self.render_pipeline {
             render_pass.set_pipeline(pipeline);
-            render_pass.set_bind_group(0, &self.texture.as_ref().unwrap().diffuse, &[]);
+            debug!("texture_toggle: {}", self.texture_toggle);
+            if self.texture_toggle {
+                debug!("Binding texture1");
+                render_pass.set_bind_group(0, &self.texture.get("texture1").as_ref().unwrap().bind_group, &[]);
+            } else {
+                debug!("Binding texture2");
+                render_pass.set_bind_group(0, &self.texture.get("texture2").as_ref().unwrap().bind_group, &[]);
+            }
             render_pass.set_vertex_buffer(0, self.vertex_buffer.as_ref().unwrap().slice(..));
             render_pass.set_index_buffer(
                 self.index_buffer.as_ref().unwrap().slice(..),
@@ -87,15 +105,19 @@ impl Scene for TestingScene1 {
 
 impl Keyboard for TestingScene1 {
     fn key_down(&mut self, key: KeyCode, event_loop: &ActiveEventLoop) {
-        debug!("Key pressed: {:?}", key);
+        // debug!("Key pressed: {:?}", key);
         match key {
             KeyCode::Escape => event_loop.exit(),
+            KeyCode::Space => {
+                self.texture_toggle = !self.texture_toggle;
+                debug!("New: {}", self.texture_toggle);
+            }
             _ => {}
         }
     }
 
-    fn key_up(&mut self, key: KeyCode, _event_loop: &ActiveEventLoop) {
-        debug!("Key released: {:?}", key);
+    fn key_up(&mut self, _key: KeyCode, _event_loop: &ActiveEventLoop) {
+        // debug!("Key released: {:?}", key);
     }
 }