added hello-world example + modified vkproccessor to live inside specs

This commit is contained in:
2020-08-08 23:38:44 -07:00
parent b070a7dd32
commit c10115e7b9
15 changed files with 150 additions and 102 deletions

View File

@@ -37,7 +37,7 @@ use crate::drawables::text::Text;
use crate::util::load_raw;
use crate::util::timer::Timer;
use crate::util::tr_event::TrEvent;
use crate::util::vertex::{TextureVertex3D, VertexType};
use crate::util::vertex::{TextureVertex3D, VertexTypeContainer};
use crate::vkprocessor::VkProcessor;
use crate::drawables::slider::Slider;
@@ -52,8 +52,14 @@ extern crate specs;
use specs::prelude::*;
// A component contains data which is
// associated with an entity.
struct Draws(VertexTypeContainer);
impl Component for Draws {
type Storage = VecStorage<Self>;
}
struct Renderer(u32);
struct Vel(f32);
@@ -67,6 +73,32 @@ impl Component for Pos {
type Storage = VecStorage<Self>;
}
struct RenderSystem;
impl<'a> System<'a> for RenderSystem {
type SystemData = (
WriteStorage<'a, Pos>,
WriteStorage<'a, Vel>,
WriteStorage<'a, Draws>,
);
fn run(&mut self, (mut pos, vel, data): Self::SystemData) {
// The `.join()` combines multiple components,
// so we only access those entities which have
// both of them.
// This joins the component storages for Position
// and Velocity together; it's also possible to do this
// in parallel using rayon's `ParallelIterator`s.
// See `ParJoin` for more.
for (pos, vel) in (&mut pos, &vel).join() {
pos.0 += vel.0;
}
}
}
struct SysA;
impl<'a> System<'a> for SysA {
@@ -93,34 +125,6 @@ impl<'a> System<'a> for SysA {
pub fn main() {
// The `World` is our
// container for components
// and other resources.
let mut world = World::new();
world.register::<Pos>();
world.register::<Vel>();
world.reg
// An entity may or may not contain some component.
world.create_entity().with(Vel(2.0)).with(Pos(0.0)).build();
world.create_entity().with(Vel(4.0)).with(Pos(1.6)).build();
world.create_entity().with(Vel(1.5)).with(Pos(5.4)).build();
world.create_entity().with(Pos(2.0)).build();
let mut dispatcher = DispatcherBuilder::new()
.with(SysA, "sys_a", &[]).build();
// This dispatches all the systems in parallel (but blocking).
dispatcher.dispatch(&mut world);
hprof::start_frame();
let q1 = hprof::enter("setup");
@@ -159,7 +163,7 @@ pub fn main() {
let step_size: f32 = 0.005;
let mut elapsed_time: f32 = timer.elap_time();
;
let mut delta_time: f32 = 0.0;
let mut accumulator_time: f32 = 0.0;
let mut current_time: f32 = timer.elap_time();
@@ -191,6 +195,41 @@ pub fn main() {
//let font_handle : Arc<CanvasFontHandle> =
// processor.get_font_handle(String::from("sansation.ttf")).unwrap();
// The `World` is our
// container for components
// and other resources.
let mut world = World::new();
world.register::<Pos>();
world.register::<Vel>();
world.register::<Draws>();
world.insert::<Renderer>(Renderer(10));
world.insert::<VkProcessor>(processor);
// An entity may or may not contain some component.
world.create_entity().with(Vel(2.0)).with(Pos(0.0)).build();
let mut dispatcher = DispatcherBuilder::new()
.with(SysA, "sys_a", &[])
.with(RenderSystem, "render_s", &[]).build();
// This dispatches all the systems in parallel (but blocking).
dispatcher.dispatch(&mut world);
let mut funky_sprite = Sprite::new(
(200.0, 200.0),
(100.0, 150.0), 10, funky_handle.clone());
@@ -254,10 +293,10 @@ pub fn main() {
// maybe some sort of input system
//
let mut big_container = vec![
Box::new(Slider::new((0.1, 0.1), (0.9, 0.9), 5000)),
Box::new(Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone())),
];
// let mut big_container = vec![
// Box::new(Slider::new((0.1, 0.1), (0.9, 0.9), 5000)),
// Box::new(Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone())),
// ];
//container.push(Sprite::new((0.1)));
@@ -265,13 +304,13 @@ pub fn main() {
events_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
for eventable in &mut big_container {
eventable.notify(&event);
}
for drawable in &mut big_container {
canvas_frame.draw(&drawable);
}
// for eventable in &mut big_container {
// eventable.notify(&event);
// }
//
// for drawable in &mut big_container {
// canvas_frame.draw(&drawable);
// }
match event {
Event::NewEvents(cause) => {
@@ -294,7 +333,7 @@ pub fn main() {
*control_flow = ControlFlow::Exit
}
Event::WindowEvent { event: WindowEvent::Resized(new_size), .. } => {
processor.swapchain_recreate_needed = true;
world.write_resource::<VkProcessor>().swapchain_recreate_needed = true;
let size = (new_size.width, new_size.height);
}
Event::WindowEvent {
@@ -317,7 +356,7 @@ pub fn main() {
}
VirtualKeyCode::P => {
if keyboard_input.state == ElementState::Pressed {
let data = processor.read_compute_buffer(compute_buffer.clone());
let data = world.write_resource::<VkProcessor>().read_compute_buffer(compute_buffer.clone());
image::save_buffer(&Path::new("image.png"), data.as_slice(), (image_data.1).0, (image_data.1).1, image::RGBA(8));
}
}
@@ -343,7 +382,8 @@ pub fn main() {
{
let g = hprof::enter("Run");
processor.run(&surface.clone(),
world.write_resource::<VkProcessor>()
.run(&surface.clone(),
&canvas_frame,
&compu_frame);
}