this is a week or so worth of brainstorming. Looking at pulling everything sprite related into it's own little struct. And also doing a message everything arch

This commit is contained in:
2020-09-14 21:27:00 -07:00
parent a42d23e5f9
commit 369a305817
13 changed files with 242 additions and 188 deletions

View File

@@ -25,7 +25,7 @@ use winit::event_loop::{ControlFlow, EventLoop, EventLoopProxy};
use winit::platform::unix::WindowBuilderExtUnix;
use winit::window::{WindowBuilder, Window};
use crate::canvas::canvas_frame::{CanvasFrame, Drawable, Eventable, Updatable};
use crate::canvas::canvas_frame::{CanvasFrame, Drawable};
use crate::canvas::canvas_state::CanvasState;
use crate::canvas::managed::handles::{CanvasFontHandle, CanvasTextureHandle, Handle};
use canvas::compu_frame::CompuFrame;
@@ -46,41 +46,16 @@ pub mod vkprocessor;
pub mod drawables;
pub mod canvas;
extern crate specs;
use specs::prelude::*;
use vulkano::swapchain::Surface;
//struct Draws(Box<dyn Drawable + Sync + Send>, Box<dyn Eventable<TrEventExtension> + Sync + Send>);
struct SSprite(Box<dyn Drawable + Sync + Send>);
impl Component for SSprite {
type Storage = VecStorage<Self>;
pub struct Render {
render_actor: Box<dyn Drawable + Sync + Send>,
}
pub struct Move {
vel_x: f32,
vel_y: f32,
}
impl Component for Move {
type Storage = VecStorage<Self>;
}
pub struct Geom {
pos_x: f32,
pos_y: f32,
size_x: f32,
size_y: f32,
rotation: f32,
depth: f32,
}
impl Component for Geom {
impl Component for Render {
type Storage = VecStorage<Self>;
}
@@ -93,65 +68,23 @@ struct PersistentState {
compu_frame: CompuFrame,
}
// If I were to have multiple systems
/*
One for rendering
One for updating
One for eventing
Rendering is easy enough. It needs all the components necessary in order
to generate the vertices. This includes the position, size, and vertex generator
Updating can probably be multiple types, I imagine something that implemented an Updatable
trait could then be used.
So the big problem here is that I have two traits that I want to expose, BUT
I have to put the concrete value in both containers... I don't think this is something
that specs will like since it wants to be the only owner. No use in RefCell'ing it
because that's just stupid
What if I turn this on it's head and really embrace the systems. So for example I could have
the User system. Ooof this is a big question actually...
// Components that want to be updated
Move
// want to be drawn
Drawable
Geom
Notifyable
*/
struct RenderSystem;
impl<'a> System<'a> for RenderSystem {
type SystemData = (
WriteStorage<'a, Move>, // its velocity
WriteStorage<'a, Geom>, // its size, position & rotation
WriteStorage<'a, SSprite>, // generates the vertices
Write<'a, PersistentState>, // delta_time, window size, etc.
Write<'a, VkProcessor>, // Renderer
WriteStorage<'a, Render>, // generates the vertices
Write<'a, PersistentState>, // delta_time, window size, etc.
Write<'a, VkProcessor>, // Renderer
);
fn run(&mut self, (mut mv, mut geom, mut draw, mut state, mut vk_processor): Self::SystemData) {
fn run(&mut self, (mut mv, mut draw, mut state, mut vk_processor): Self::SystemData) {
state.canvas_frame = CanvasFrame::new(state.window_size);
state.compu_frame = CompuFrame::new(state.window_size);
// compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);
// compu_frame.add(compute_buffer.clone(), compute_kernel.clone());
for (mv, geom, draw) in (&mut mv, &mut geom, &mut draw).join() {
for (mv, geom, draw) in (&mut mv, &mut draw).join() {
geom.pos_x += mv.vel_x * state.delta_time;
geom.pos_y += mv.vel_y * state.delta_time;
@@ -180,18 +113,18 @@ struct EventSystem;
impl<'a> System<'a> for EventSystem {
type SystemData = (
WriteStorage<'a, SSprite>,
WriteStorage<'a, Render>,
Write<'a, PersistentState>,
Write<'a, VkProcessor>,
Write<'a, Vec<TrEvent<TrEventExtension>>>
);
fn run(&mut self, (mut draw, mut state, mut vk_processor, event_stack): Self::SystemData) {
for draw_data in (&mut draw).join() {
for event in event_stack.iter() {
draw_data.1.notify(event)
}
}
// for draw_data in (&mut draw).join() {
// for event in event_stack.iter() {
// draw_data.0.notify(event)
// }
// }
}
}
@@ -263,9 +196,7 @@ pub fn main() {
processor.get_texture_handle(String::from("sfml.png")).unwrap();
let mut world = World::new();
world.register::<Move>();
world.register::<Geom>();
world.register::<SSprite>();
world.register::<Render>();
world.insert::<VkProcessor>(processor);
world.insert::<Vec<TrEvent<TrEventExtension>>>(Vec::new());
world.insert::<PersistentState>(PersistentState {
@@ -281,23 +212,13 @@ pub fn main() {
// An entity may or may not contain some component.
let t = world.create_entity()
.with(Move { vel_x: 0.0, vel_y: 0.0})
.with(Geom { size_x: 100.0, size_y: 100.0, rotation: 0.0, depth: 1.0, pos_x: 100.0, pos_y: 400.0 })
.with(SSprite(thing.clone(), thing.clone()))
.build();
world.create_entity()
.with(Move { vel_x: 0.0, vel_y: 0.0 })
.with(Geom { size_x: 100.0, size_y: 100.0, rotation: 0.0, depth: 1.0, pos_x: 100.0, pos_y: 400.0 })
.with(SSprite(thing.clone(), thing))
.with(Render{ render_actor: thing })// just a drawable
.build();
let thing2 = Box::new(Slider::new((300.0, 50.0), (550.0, 100.0), 30000));
world.create_entity()
.with(Move { vel_x: 0.0, vel_y: 0.0 })
.with(Geom { size_x: 100.0, size_y: 100.0, rotation: 0.0, depth: 1.0, pos_x: 100.0, pos_y: 400.0 })
.with(SSprite(thing2.clone(), thing2))
.with(Render{ render_actor: thing2 })// just a drawable
.build();
@@ -352,7 +273,7 @@ pub fn main() {
world.write_resource::<PersistentState>()
.window_size = surface.window().inner_size().into();
} else {
// println!("{}", world.write_resource::<Vec<TrEvent<TrEventExtension>>>().len());
// println!("{}", world.write_resource::<Vec<TrEvent<TrEventExtension>>>().len());
world.write_resource::<Vec<TrEvent<TrEventExtension>>>().clear();
}
}
@@ -362,8 +283,7 @@ pub fn main() {
Event::WindowEvent { event: WindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => {
match button {
MouseButton::Left => {
if state == ElementState::Pressed {
}
if state == ElementState::Pressed {}
}
_ => {}
}