From f07b829b7d0d52b5969783892b5803fe4ba6f1a9 Mon Sep 17 00:00:00 2001 From: lisk77 Date: Wed, 19 Feb 2025 02:22:38 +0100 Subject: [PATCH] feat: changed the preset system to a "with_" option. Removed auto of a component to entity (will be reintroduced differently) --- README.md | 12 ++---- crates/comet_app/src/app.rs | 77 +++++++++++++++++------------------ crates/comet_ecs/src/world.rs | 24 +---------- 3 files changed, 42 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index b7efd53..3023fa0 100644 --- a/README.md +++ b/README.md @@ -38,13 +38,7 @@ comet = { path = "crates/comet" } // This will be cleaned up in the future // but for now I don't have a prelude. -use comet::{ - app::{ - App, - ApplicationType::* - }, - renderer::renderer2d::Renderer2D, -}; +use comet::prelude::*; struct GameState {} @@ -60,7 +54,8 @@ fn setup(app: &mut App, renderer: &mut Renderer2D) {} fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {} fn main() { - App::new(App2D) // Generate a basic 2D app + App::new() // Generate a basic 2D app + .with_preset(App2D) // Pre-registers the `Transform2D` component in the world .with_title("Comet App") // Sets the window title .with_icon(r"resources/textures/comet_icon.png") // Sets the window icon .with_size(1920, 1080) // Sets the window size @@ -78,7 +73,6 @@ use fs_extra::dir::CopyOptions; use std::env; fn main() -> Result<()> { - // This tells cargo to rerun this script if something in /resources/ changes. println!("cargo:rerun-if-changed=resources/textures/*"); println!("cargo:rerun-if-changed=resources/shaders/*"); diff --git a/crates/comet_app/src/app.rs b/crates/comet_app/src/app.rs index d57ab3c..d0dfd07 100644 --- a/crates/comet_app/src/app.rs +++ b/crates/comet_app/src/app.rs @@ -1,7 +1,8 @@ use std::any::{type_name, Any}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, RwLock}; +use std::thread; use std::time::{Duration, Instant}; -use comet_ecs::{Component, ComponentSet, Render, Transform2D, World}; +use comet_ecs::{Component, ComponentSet, Render, Transform2D, Transform3D, World}; use comet_resources::{ResourceManager, Vertex}; use comet_renderer::renderer2d::Renderer2D; @@ -42,12 +43,7 @@ pub struct App<'a> { } impl<'a> App<'a> { - pub fn new(application_type: ApplicationType) -> Self { - let world = match application_type { - ApplicationType::App2D => World::new("2D"), - ApplicationType::App3D => World::new("3D"), - }; - + pub fn new() -> Self { Self { title: "Untitled", icon: None, @@ -57,7 +53,7 @@ impl<'a> App<'a> { delta_time: 0.0, update_timer: 0.0166667, game_state: None, - world, + world: World::new(), fullscreen: false, should_quit: false } @@ -88,6 +84,20 @@ impl<'a> App<'a> { self } + pub fn with_preset(mut self, preset: ApplicationType) -> Self { + match preset { + ApplicationType::App2D => { + info!("Creating 2D app!"); + self.world.register_component::() + }, + ApplicationType::App3D => { + info!("Creating 3D app!"); + self.world.register_component::() + } + }; + self + } + fn load_icon(path: &std::path::Path) -> Option { let image = image::open(path).expect("Failed to open icon image"); let rgba_image = image.to_rgba8(); @@ -163,13 +173,28 @@ impl<'a> App<'a> { winit_window.build(event_loop).unwrap() } + /*pub fn run(mut self, setup: fn(&mut App, &mut R), update: fn(&mut App, &mut R, f32)) { + info!("Starting up {}!", self.title); + + pollster::block_on(async { + let event_loop = EventLoop::new().unwrap(); + let window = Arc::new(Self::create_window(self.title, &self.icon, &self.size ,&event_loop)); + let mut renderer = R::new(window.clone(), self.clear_color.clone()).await; + info!("Renderer created! ({})", type_name::()); + + setup(&mut self, &mut renderer); + }); + + info!("Shutting down {}!", self.title); + }*/ + pub fn run(mut self, setup: fn(&mut App, &mut R), update: fn(&mut App, &mut R, f32)) { info!("Starting up {}!", self.title); pollster::block_on(async { let event_loop = EventLoop::new().unwrap(); let window = Arc::new(Self::create_window(self.title, &self.icon, &self.size ,&event_loop)); - let mut renderer = R::new(window.clone(), self.clear_color.clone()).await; // Pass Arc> to renderer + let mut renderer = R::new(window.clone(), self.clear_color.clone()).await; info!("Renderer created! ({})", type_name::()); window.set_maximized(true); @@ -216,35 +241,9 @@ impl<'a> App<'a> { } _ => {} } - - /*match event { - Event::WindowEvent { ref event, window_id, } => - match event { - WindowEvent::CloseRequested {} => elwt.exit(), - WindowEvent::Resized(physical_size) => { - renderer.resize(*physical_size); - } - WindowEvent::RedrawRequested => { - window.request_redraw(); - - /*match renderer.render() { - Ok(_) => {} - Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => renderer.resize(renderer.size()), - Err(wgpu::SurfaceError::OutOfMemory) => { - error!("OutOfMemory"); - elwt.exit(); - } - Err(wgpu::SurfaceError::Timeout) => { - warn!("Surface timeout") - } - }*/ - } - _ => {} - } - _ => {} - */ }).unwrap() - } - ); + }); + + info!("Shutting down {}!", self.title); } } \ No newline at end of file diff --git a/crates/comet_ecs/src/world.rs b/crates/comet_ecs/src/world.rs index 3f62e50..744ae87 100644 --- a/crates/comet_ecs/src/world.rs +++ b/crates/comet_ecs/src/world.rs @@ -14,7 +14,6 @@ use crate::{ use comet_log::*; pub struct World { - dimension: String, id_queue: IdQueue, next_id: u32, entities: Vec>, @@ -23,16 +22,10 @@ pub struct World { } impl World { - pub fn new(application: &str) -> Self { + pub fn new() -> Self { let mut component_storage = ComponentStorage::new(); - match application { - "2D" => component_storage.register_component::(0), - "3D" => component_storage.register_component::(0), - _ => {} - } Self { - dimension: application.to_string(), id_queue: IdQueue::new(), next_id: 0, entities: Vec::new(), @@ -55,10 +48,6 @@ impl World { } } - pub fn dimension(&self) -> &String { - &self.dimension - } - pub fn id_queue(&self) -> &IdQueue { &self.id_queue } @@ -79,21 +68,10 @@ impl World { let id = self.next_id; if (self.next_id as usize) >= self.entities.len() { self.entities.push(Some(Entity::new(self.next_id))); - match self.dimension.as_str() { - "2D" => self.add_component::(self.next_id as usize, Transform2D::new()), - "3D" => self.add_component::(self.next_id as usize, Transform3D::new()), - _ => {} - } self.get_next_id(); return id; } self.entities[self.next_id as usize] = Some(Entity::new(self.next_id)); - println!("{:?}", self.dimension); - match self.dimension.as_str() { - "2D" => self.add_component::(self.next_id as usize, Transform2D::new()), - "3D" => self.add_component::(self.next_id as usize, Transform3D::new()), - _ => {} - } self.get_next_id(); id }