attempt to merge functionality

This commit is contained in:
Theis Pieter Hollebeek 2024-04-08 09:27:03 +02:00
parent 13c6b2e2e0
commit bc3c2fef76
7 changed files with 117 additions and 53 deletions

44
Cargo.lock generated
View File

@ -14,6 +14,14 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "component-macro"
version = "0.1.0"
dependencies = [
"quote",
"syn",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -26,13 +34,32 @@ version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "proc-macro2"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [
"unicode-ident",
]
[[package]] [[package]]
name = "pvp-game-dilapidation" name = "pvp-game-dilapidation"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"component-macro",
"sdl2", "sdl2",
] ]
[[package]]
name = "quote"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
[[package]] [[package]]
name = "sdl2" name = "sdl2"
version = "0.36.0" version = "0.36.0"
@ -56,6 +83,23 @@ dependencies = [
"version-compare", "version-compare",
] ]
[[package]]
name = "syn"
version = "2.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]] [[package]]
name = "version-compare" name = "version-compare"
version = "0.1.1" version = "0.1.1"

View File

@ -7,3 +7,4 @@ edition = "2021"
[dependencies] [dependencies]
sdl2 = { version = "0.36.0", features = ["ttf", "image", "mixer"] } sdl2 = { version = "0.36.0", features = ["ttf", "image", "mixer"] }
component-macro = { version = "0.1.0", path = "./component-macro" }

View File

@ -22,5 +22,3 @@ fn impl_derive_macro(ast: &syn::DeriveInput) -> TokenStream {
}; };
gen.into() gen.into()
} }

View File

@ -1,3 +1,5 @@
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::time::Duration; use std::time::Duration;
use sdl2::{ use sdl2::{
@ -42,40 +44,49 @@ impl_from_T_for_Error!(String, WindowBuildError, IntegerOrSdlError);
pub struct Entity(u64); pub struct Entity(u64);
pub trait Component {} pub trait Component {
fn inner_type_id(&self) -> TypeId;
pub struct Sprite<'a> { fn as_any(&mut self) -> &mut dyn Any;
texture: Texture<'a>,
} }
pub struct Context<'a> { pub struct Sprite {
id_counter: &'a mut u64, path: std::path::PathBuf,
canvas: &'a mut Canvas<Window>,
texture_creator: &'a TextureCreator<WindowContext>,
entities: &'a mut Vec<Entity>,
components: &'a mut Vec<(u64, Box<dyn Component>)>,
systems: &'a mut Vec<Box<dyn System>>,
} }
impl<'a> Context<'a> { pub struct Context<'context, 'game>
pub fn load_sprite<'b, P>(&self, path: P) -> Result<Sprite<'b>, Error> where
'game: 'context,
{
id_counter: &'context mut u64,
canvas: &'context mut Canvas<Window>,
texture_creator: &'context TextureCreator<WindowContext>,
entities: &'context mut Vec<Entity>,
components: &'context mut Vec<(u64, Box<dyn Component>)>,
systems: &'context mut Vec<Box<dyn System>>,
textures: &'context mut HashMap<std::path::PathBuf, Texture<'game>>,
}
impl<'context, 'game> Context<'context, 'game> {
pub fn load_sprite<P>(&mut self, path: P) -> Result<Sprite, Error>
where where
'a: 'b,
P: AsRef<std::path::Path>, P: AsRef<std::path::Path>,
{ {
let texture = self.texture_creator.load_texture(path)?; let path = path.as_ref().to_path_buf();
Ok(Sprite { texture }) let texture: Texture<'game> = self.texture_creator.load_texture(&path)?;
self.textures.insert(path.clone(), texture);
Ok(Sprite { path })
} }
pub fn draw_sprite<'b>(&mut self, sprite: &Sprite<'b>, x: i32, y: i32) -> Result<(), Error> { pub fn draw_sprite(&mut self, sprite: &Sprite, x: i32, y: i32) -> Result<(), Error> {
let texture = &self.textures[&sprite.path];
self.canvas.copy( self.canvas.copy(
&sprite.texture, texture,
None, None,
Rect::new( Rect::new(
x * 4, x * 4,
y * 4, y * 4,
sprite.texture.query().width * 4, texture.query().width * 4,
sprite.texture.query().height * 4, texture.query().height * 4,
), ),
)?; )?;
Ok(()) Ok(())
@ -100,7 +111,7 @@ pub trait System {
fn on_update(&self, _ctx: &mut Context) {} fn on_update(&self, _ctx: &mut Context) {}
} }
pub struct Game { pub struct Game<'a> {
id_counter: u64, id_counter: u64,
sdl_context: Sdl, sdl_context: Sdl,
video_subsystem: VideoSubsystem, video_subsystem: VideoSubsystem,
@ -111,9 +122,10 @@ pub struct Game {
entities: Vec<Entity>, entities: Vec<Entity>,
components: Vec<(u64, Box<dyn Component>)>, components: Vec<(u64, Box<dyn Component>)>,
systems: Vec<Box<dyn System>>, systems: Vec<Box<dyn System>>,
textures: HashMap<std::path::PathBuf, Texture<'a>>,
} }
impl Game { impl<'game> Game<'game> {
pub fn new() -> Result<Self, Error> { pub fn new() -> Result<Self, Error> {
let sdl_context = sdl2::init()?; let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?; let video_subsystem = sdl_context.video()?;
@ -141,31 +153,35 @@ impl Game {
entities: vec![], entities: vec![],
components: vec![], components: vec![],
systems: vec![], systems: vec![],
textures: HashMap::new(),
}) })
} }
pub fn run<F: Fn(&mut Context) -> ()>(mut self, f: F) { pub fn run<F: Fn(&mut Context) -> ()>(&mut self, _f: F) {
'running: loop { // 'running: loop {
for event in self.event_pump.poll_iter() { // for event in self.event_pump.poll_iter() {
match event { // match event {
Event::Quit { .. } // Event::Quit { .. }
| Event::KeyDown { // | Event::KeyDown {
keycode: Some(Keycode::Escape), // keycode: Some(Keycode::Escape),
.. // ..
} => break 'running, // } => break 'running,
_ => {} // _ => {}
} // }
} // }
self.canvas.set_draw_color(Color::RGB(60, 180, 180)); // self.canvas.set_draw_color(Color::RGB(60, 180, 180));
self.canvas.clear(); // self.canvas.clear();
f(&mut self.context()); // f(&mut self.context());
self.canvas.present(); // self.canvas.present();
} // }
self.canvas.present(); // self.canvas.present();
std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)) // std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60))
} }
pub fn context<'a>(&'a mut self) -> Context<'a> { pub fn context<'context>(&'context mut self) -> Context<'context, 'game>
where
'game: 'context,
{
Context { Context {
id_counter: &mut self.id_counter, id_counter: &mut self.id_counter,
canvas: &mut self.canvas, canvas: &mut self.canvas,
@ -173,6 +189,7 @@ impl Game {
entities: &mut self.entities, entities: &mut self.entities,
components: &mut self.components, components: &mut self.components,
systems: &mut self.systems, systems: &mut self.systems,
textures: &mut self.textures,
} }
} }
} }

View File

@ -1,14 +1,16 @@
#![allow(dead_code)] #![allow(dead_code)]
#![allow(unused_imports)]
use component_macro::Component;
use engine::{Component, Sprite, System}; use engine::{Component, Sprite, System};
use std::any::{Any, TypeId};
mod engine; mod engine;
struct Player<'a> { #[derive(Component)]
sprite: Option<Sprite<'a>>, struct Player {
sprite: Option<Sprite>,
} }
impl<'a> Component for Player<'a> {}
struct PlayerRenderer; struct PlayerRenderer;
impl System for PlayerRenderer { impl System for PlayerRenderer {
@ -17,13 +19,16 @@ impl System for PlayerRenderer {
fn on_update(&self, _ctx: &mut engine::Context) {} fn on_update(&self, _ctx: &mut engine::Context) {}
} }
fn main() { fn add_player<'borrow, 'game>(game: &'borrow mut engine::Game<'game>) {
let mut game = engine::Game::new().unwrap();
{
let mut context = game.context(); let mut context = game.context();
context.add_system(Box::new(PlayerRenderer)); context.add_system(Box::new(PlayerRenderer));
context.spawn(vec![Box::new(Player { sprite: None })]); context.spawn(vec![Box::new(Player { sprite: None })]);
}
fn main() {
let mut game = engine::Game::new().unwrap();
{
add_player(&mut game);
} }
game.run(|context| { game.run(|context| {

View File

@ -1,4 +1,3 @@
workspace = { members = ["component-macro"] }
[package] [package]
name = "type-id-test-rs" name = "type-id-test-rs"
version = "0.1.0" version = "0.1.0"