feat: changed the preset system to a "with_" option. Removed auto of a component to entity (will be reintroduced differently)

This commit is contained in:
lisk77 2025-02-19 02:22:38 +01:00
parent 4513b97c99
commit f07b829b7d
3 changed files with 42 additions and 71 deletions

View file

@ -38,13 +38,7 @@ comet = { path = "crates/comet" }
// This will be cleaned up in the future // This will be cleaned up in the future
// but for now I don't have a prelude. // but for now I don't have a prelude.
use comet::{ use comet::prelude::*;
app::{
App,
ApplicationType::*
},
renderer::renderer2d::Renderer2D,
};
struct GameState {} 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 update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {}
fn main() { 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_title("Comet App") // Sets the window title
.with_icon(r"resources/textures/comet_icon.png") // Sets the window icon .with_icon(r"resources/textures/comet_icon.png") // Sets the window icon
.with_size(1920, 1080) // Sets the window size .with_size(1920, 1080) // Sets the window size
@ -78,7 +73,6 @@ use fs_extra::dir::CopyOptions;
use std::env; use std::env;
fn main() -> Result<()> { 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/textures/*");
println!("cargo:rerun-if-changed=resources/shaders/*"); println!("cargo:rerun-if-changed=resources/shaders/*");

View file

@ -1,7 +1,8 @@
use std::any::{type_name, Any}; 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 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_resources::{ResourceManager, Vertex};
use comet_renderer::renderer2d::Renderer2D; use comet_renderer::renderer2d::Renderer2D;
@ -42,12 +43,7 @@ pub struct App<'a> {
} }
impl<'a> App<'a> { impl<'a> App<'a> {
pub fn new(application_type: ApplicationType) -> Self { pub fn new() -> Self {
let world = match application_type {
ApplicationType::App2D => World::new("2D"),
ApplicationType::App3D => World::new("3D"),
};
Self { Self {
title: "Untitled", title: "Untitled",
icon: None, icon: None,
@ -57,7 +53,7 @@ impl<'a> App<'a> {
delta_time: 0.0, delta_time: 0.0,
update_timer: 0.0166667, update_timer: 0.0166667,
game_state: None, game_state: None,
world, world: World::new(),
fullscreen: false, fullscreen: false,
should_quit: false should_quit: false
} }
@ -88,6 +84,20 @@ impl<'a> App<'a> {
self self
} }
pub fn with_preset(mut self, preset: ApplicationType) -> Self {
match preset {
ApplicationType::App2D => {
info!("Creating 2D app!");
self.world.register_component::<Transform2D>()
},
ApplicationType::App3D => {
info!("Creating 3D app!");
self.world.register_component::<Transform3D>()
}
};
self
}
fn load_icon(path: &std::path::Path) -> Option<Icon> { fn load_icon(path: &std::path::Path) -> Option<Icon> {
let image = image::open(path).expect("Failed to open icon image"); let image = image::open(path).expect("Failed to open icon image");
let rgba_image = image.to_rgba8(); let rgba_image = image.to_rgba8();
@ -163,13 +173,28 @@ impl<'a> App<'a> {
winit_window.build(event_loop).unwrap() winit_window.build(event_loop).unwrap()
} }
/*pub fn run<R: Renderer>(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::<R>());
setup(&mut self, &mut renderer);
});
info!("Shutting down {}!", self.title);
}*/
pub fn run<R: Renderer>(mut self, setup: fn(&mut App, &mut R), update: fn(&mut App, &mut R, f32)) { pub fn run<R: Renderer>(mut self, setup: fn(&mut App, &mut R), update: fn(&mut App, &mut R, f32)) {
info!("Starting up {}!", self.title); info!("Starting up {}!", self.title);
pollster::block_on(async { pollster::block_on(async {
let event_loop = EventLoop::new().unwrap(); let event_loop = EventLoop::new().unwrap();
let window = Arc::new(Self::create_window(self.title, &self.icon, &self.size ,&event_loop)); 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<Mutex<Window>> to renderer let mut renderer = R::new(window.clone(), self.clear_color.clone()).await;
info!("Renderer created! ({})", type_name::<R>()); info!("Renderer created! ({})", type_name::<R>());
window.set_maximized(true); 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() }).unwrap()
} });
);
info!("Shutting down {}!", self.title);
} }
} }

View file

@ -14,7 +14,6 @@ use crate::{
use comet_log::*; use comet_log::*;
pub struct World { pub struct World {
dimension: String,
id_queue: IdQueue, id_queue: IdQueue,
next_id: u32, next_id: u32,
entities: Vec<Option<Entity>>, entities: Vec<Option<Entity>>,
@ -23,16 +22,10 @@ pub struct World {
} }
impl World { impl World {
pub fn new(application: &str) -> Self { pub fn new() -> Self {
let mut component_storage = ComponentStorage::new(); let mut component_storage = ComponentStorage::new();
match application {
"2D" => component_storage.register_component::<Transform2D>(0),
"3D" => component_storage.register_component::<Transform3D>(0),
_ => {}
}
Self { Self {
dimension: application.to_string(),
id_queue: IdQueue::new(), id_queue: IdQueue::new(),
next_id: 0, next_id: 0,
entities: Vec::new(), entities: Vec::new(),
@ -55,10 +48,6 @@ impl World {
} }
} }
pub fn dimension(&self) -> &String {
&self.dimension
}
pub fn id_queue(&self) -> &IdQueue { pub fn id_queue(&self) -> &IdQueue {
&self.id_queue &self.id_queue
} }
@ -79,21 +68,10 @@ impl World {
let id = self.next_id; let id = self.next_id;
if (self.next_id as usize) >= self.entities.len() { if (self.next_id as usize) >= self.entities.len() {
self.entities.push(Some(Entity::new(self.next_id))); self.entities.push(Some(Entity::new(self.next_id)));
match self.dimension.as_str() {
"2D" => self.add_component::<Transform2D>(self.next_id as usize, Transform2D::new()),
"3D" => self.add_component::<Transform3D>(self.next_id as usize, Transform3D::new()),
_ => {}
}
self.get_next_id(); self.get_next_id();
return id; return id;
} }
self.entities[self.next_id as usize] = Some(Entity::new(self.next_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::<Transform2D>(self.next_id as usize, Transform2D::new()),
"3D" => self.add_component::<Transform3D>(self.next_id as usize, Transform3D::new()),
_ => {}
}
self.get_next_id(); self.get_next_id();
id id
} }