engine: move resource manager to ecs global

master
q3k 2021-04-07 15:26:18 +00:00
parent 6055a034f0
commit 8c045ff1f1
4 changed files with 18 additions and 12 deletions

View File

@ -59,6 +59,7 @@ struct Main {
impl Main {
pub fn new(world: &mut World, renderer: &mut render::Renderer) -> Self {
let mut rm = render::resource::Manager::new();
let mesh = {
let vertices = Arc::new(vec![
data::Vertex::new([-0.5, -0.5, 0.5], [ 0.0, 0.0, 1.0], [1.0, 0.0]),
@ -103,10 +104,10 @@ impl Main {
20, 22, 21, 22, 20, 23,
]);
renderer.add_resource(Mesh::new(vertices, indices), Some("cube"))
rm.add(Mesh::new(vertices, indices), Some("cube"))
};
let material = renderer.add_resource(PBRMaterialBuilder {
let material = rm.add(PBRMaterialBuilder {
diffuse: Texture::from_image(String::from("//assets/test-128px.png")),
roughness: Texture::from_image(String::from("//assets/test-128px-roughness.png")),
}.build(), Some("test-128px"));
@ -122,7 +123,7 @@ impl Main {
}
}
let light = renderer.add_resource(Light::omni_test(), Some("omni"));
let light = rm.add(Light::omni_test(), Some("omni"));
// The Sun (Sol) is 1AU from the Earth. We ignore the diameter of the Sun and the Earth, as
// these are negligible at this scale.
@ -135,7 +136,7 @@ impl Main {
let sun_lumen: f32 = sun_luminous_emittance * (4.0 * 3.14159 * sun_distance * sun_distance);
let sun_color = color::XYZ::new(sun_lumen/3.0, sun_lumen/3.0, sun_lumen/3.0);
let sun = renderer.add_resource(Light::omni_with_color(sun_color), Some("sun"));
let sun = rm.add(Light::omni_with_color(sun_color), Some("sun"));
// In our scene, the sun at a 30 degree zenith.
let sun_angle: f32 = (3.14159 * 2.0) / (360.0 / 30.0);
@ -153,6 +154,8 @@ impl Main {
.with(Renderable::Light(sun))
.build();
world.set_global(rm);
Self {
light1, light2,
cx: 0.,
@ -217,6 +220,7 @@ fn main() {
let mut world = World::new();
world.register_component_lua_bindings(Transform::bindings());
world.register_component_lua_bindings(Renderable::bindings());
let mut renderer = render::Renderer::initialize(&mut world);
let main = Main::new(&mut world, &mut renderer);

View File

@ -57,8 +57,6 @@ pub struct Renderer {
instance: vulkan::Instance<Window>,
events_loop: EventLoop<()>,
surface: Arc<vs::Surface<Window>>,
rm: resource::Manager,
cursor_locked: bool,
}
@ -86,6 +84,7 @@ impl<'a> ecs::System<'a> for Renderer {
ecs::ReadComponent<'a, Renderable>,
ecs::ReadWriteGlobal<'a, Status>,
ecs::ReadGlobal<'a, SceneInfo>,
ecs::ReadGlobal<'a, resource::Manager>,
ecs::ReadWriteGlobal<'a, input::Input>,
);
@ -94,6 +93,7 @@ impl<'a> ecs::System<'a> for Renderer {
, renderables
, status
, scene
, rm
, input): Self::SystemData,
) {
let transformedRenderables = (transforms, renderables);
@ -119,7 +119,7 @@ impl<'a> ecs::System<'a> for Renderer {
}
let camera = &scene.camera;
let view = &scene.view;
self.instance.flip(camera, view, &rd, &self.rm);
self.instance.flip(camera, view, &rd, &rm.get());
// Retrieve current resolution into status.
match self.instance.swapchain_dimensions() {
@ -227,7 +227,6 @@ impl Renderer {
instance,
events_loop,
surface,
rm: resource::Manager::new(),
cursor_locked: false,
}
@ -302,8 +301,4 @@ impl Renderer {
});
return (close, events);
}
pub fn add_resource<T: Resource, S: ToString>(&mut self, r: T, label: Option<S>) -> ResourceID<T> {
self.rm.add(r, label)
}
}

View File

@ -112,6 +112,12 @@ pub enum Renderable {
}
impl mlua::UserData for Renderable {}
impl Renderable {
pub fn bindings() -> Box<dyn ComponentLuaBindings> {
Box::new(RenderableBindings)
}
}
impl Component for Renderable {
fn id(&self) -> ecs::component::ID {
ecs::component::component_id::<Renderable>()

View File

@ -13,6 +13,7 @@ pub struct Manager {
label_to_numeric: BTreeMap<String, u64>,
counter: u64,
}
impl ecs::Global for Manager {}
impl Manager {
pub fn new() -> Self {