use init_resource everywhere

This commit is contained in:
Carter Anderson 2020-05-13 16:35:38 -07:00
parent 16b568e00e
commit b58db0749e
8 changed files with 23 additions and 32 deletions

View file

@ -16,15 +16,17 @@ pub struct AssetStorage<T> {
events: Events<AssetEvent<T>>,
}
impl<T> AssetStorage<T> {
pub fn new() -> AssetStorage<T> {
impl<T> Default for AssetStorage<T> {
fn default() -> Self {
AssetStorage {
assets: HashMap::new(),
names: HashMap::new(),
assets: HashMap::default(),
names: HashMap::default(),
events: Events::default(),
}
}
}
impl<T> AssetStorage<T> {
pub fn get_named(&mut self, name: &str) -> Option<Handle<T>> {
self.names.get(name).map(|handle| *handle)
}
@ -102,7 +104,7 @@ impl AddAsset for AppBuilder {
where
T: Send + Sync + 'static,
{
self.add_resource(AssetStorage::<T>::new())
self.init_resource::<AssetStorage<T>>()
.add_system_to_stage(
stage::EVENT_UPDATE,
AssetStorage::<T>::asset_event_system.system(),

View file

@ -5,7 +5,7 @@ pub mod transform;
use bevy_app::{stage, AppBuilder, AppPlugin};
use bevy_transform::transform_system_bundle;
use legion::prelude::IntoSystem;
use time::{start_timer_system, stop_timer_system};
use time::{start_timer_system, stop_timer_system, Time};
#[derive(Default)]
pub struct CorePlugin;
@ -16,7 +16,7 @@ impl AppPlugin for CorePlugin {
app.add_system(transform_system);
}
app.add_resource(time::Time::new())
app.init_resource::<Time>()
.add_system_to_stage(stage::FIRST, start_timer_system.system())
.add_system_to_stage(stage::LAST, stop_timer_system.system());
}

View file

@ -8,8 +8,8 @@ pub struct Time {
pub delta_seconds: f32,
}
impl Time {
pub fn new() -> Time {
impl Default for Time {
fn default() -> Time {
Time {
delta: Duration::from_secs(0),
instant: Instant::now(),
@ -17,7 +17,9 @@ impl Time {
delta_seconds: 0.0,
}
}
}
impl Time {
pub fn start(&mut self) {
self.instant = Instant::now();
}

View file

@ -76,11 +76,11 @@ impl AppPlugin for RenderPlugin {
.add_asset::<Shader>()
.add_asset::<PipelineDescriptor>()
.add_resource(render_graph)
.add_resource(PipelineAssignments::new())
.add_resource(PipelineCompiler::new())
.add_resource(RenderResourceAssignments::default())
.add_resource(VertexBufferDescriptors::default())
.add_resource(EntityRenderResourceAssignments::default())
.init_resource::<PipelineAssignments>()
.init_resource::<PipelineCompiler>()
.init_resource::<RenderResourceAssignments>()
.init_resource::<VertexBufferDescriptors>()
.init_resource::<EntityRenderResourceAssignments>()
// core systems
.add_system(entity_render_resource_assignments_system())
.init_system_to_stage(stage::POST_UPDATE, camera::camera_update_system)

View file

@ -21,6 +21,7 @@ pub struct ShaderSpecialization {
}
// TODO: consider using (Typeid, fieldinfo.index) in place of string for hashes
#[derive(Default)]
pub struct PipelineCompiler {
pub shader_source_to_compiled:
HashMap<Handle<Shader>, Vec<(ShaderSpecialization, Handle<Shader>)>>,
@ -31,13 +32,6 @@ pub struct PipelineCompiler {
}
impl PipelineCompiler {
pub fn new() -> Self {
PipelineCompiler {
shader_source_to_compiled: HashMap::new(),
pipeline_source_to_compiled: HashMap::new(),
}
}
fn compile_shader(
&mut self,
shader_storage: &mut AssetStorage<Shader>,
@ -205,18 +199,11 @@ impl PipelineCompiler {
}
}
#[derive(Default)]
pub struct PipelineAssignments {
pub assignments: HashMap<Handle<PipelineDescriptor>, Vec<RenderResourceAssignmentsId>>,
}
impl PipelineAssignments {
pub fn new() -> Self {
PipelineAssignments {
assignments: HashMap::new(),
}
}
}
// TODO: make this a system
pub fn update_shader_assignments(world: &mut World, resources: &Resources) {
// PERF: this seems like a lot of work for things that don't change that often.

View file

@ -31,7 +31,7 @@ impl AppPlugin for WindowPlugin {
.add_event::<WindowCreated>()
.add_event::<WindowCloseRequested>()
.add_event::<CloseWindow>()
.add_resource(Windows::default());
.init_resource::<Windows>();
if let Some(ref primary_window_descriptor) = self.primary_window {
let mut create_window_event =

View file

@ -27,7 +27,7 @@ impl AppPlugin for WinitPlugin {
// TODO: It would be great to provide a raw winit WindowEvent here, but the lifetime on it is
// stopping us. there are plans to remove the lifetime: https://github.com/rust-windowing/winit/pull/1456
// .add_event::<winit::event::WindowEvent>()
.add_resource(WinitWindows::default())
.init_resource::<WinitWindows>()
.set_runner(winit_runner);
}
}

View file

@ -4,7 +4,7 @@ fn main() {
App::build()
.add_default_plugins()
.add_event::<MyEvent>()
.add_resource(EventTriggerState::default())
.init_resource::<EventTriggerState>()
.init_resource::<EventListenerState>()
.add_system(event_trigger_system.system())
.add_system(event_listener_system.system())