Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix scene loading #988

Merged
merged 1 commit into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
};
use bevy_app::{prelude::Events, AppBuilder};
use bevy_ecs::{FromResources, ResMut};
use bevy_reflect::RegisterTypeBuilder;
use bevy_utils::HashMap;
use crossbeam_channel::Sender;
use std::fmt::Debug;
Expand Down Expand Up @@ -219,6 +220,7 @@ impl AddAsset for AppBuilder {
self.add_resource(assets)
.add_system_to_stage(super::stage::ASSET_EVENTS, Assets::<T>::asset_event_system)
.add_system_to_stage(crate::stage::LOAD_ASSETS, update_asset_storage_system::<T>)
.register_type::<Handle<T>>()
.add_event::<AssetEvent<T>>()
}

Expand Down
53 changes: 28 additions & 25 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
path::{AssetPath, AssetPathId},
Asset, Assets,
};
use bevy_reflect::{Reflect, ReflectDeserialize};
use bevy_reflect::{Reflect, ReflectComponent, ReflectDeserialize};
use bevy_utils::Uuid;
use crossbeam_channel::{Receiver, Sender};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -57,9 +57,10 @@ impl HandleId {
///
/// Handles contain a unique id that corresponds to a specific asset in the [Assets](crate::Assets) collection.
#[derive(Reflect)]
#[reflect(Component)]
pub struct Handle<T>
where
T: 'static,
T: Asset,
{
pub id: HandleId,
#[reflect(ignore)]
Expand All @@ -82,17 +83,6 @@ impl Debug for HandleType {
}
}

impl<T> Handle<T> {
// TODO: remove "uuid" parameter whenever rust support type constraints in const fns
pub const fn weak_from_u64(uuid: Uuid, id: u64) -> Self {
Self {
id: HandleId::new(uuid, id),
handle_type: HandleType::Weak,
marker: PhantomData,
}
}
}

impl<T: Asset> Handle<T> {
pub(crate) fn strong(id: HandleId, ref_change_sender: Sender<RefChange>) -> Self {
ref_change_sender.send(RefChange::Increment(id)).unwrap();
Expand All @@ -111,7 +101,7 @@ impl<T: Asset> Handle<T> {
}
}

pub fn as_weak<U>(&self) -> Handle<U> {
pub fn as_weak<U: Asset>(&self) -> Handle<U> {
Handle {
id: self.id,
handle_type: HandleType::Weak,
Expand Down Expand Up @@ -152,7 +142,7 @@ impl<T: Asset> Handle<T> {
}
}

impl<T> Drop for Handle<T> {
impl<T: Asset> Drop for Handle<T> {
fn drop(&mut self) {
match self.handle_type {
HandleType::Strong(ref sender) => {
Expand All @@ -164,45 +154,51 @@ impl<T> Drop for Handle<T> {
}
}

impl<T> From<Handle<T>> for HandleId {
impl<T: Asset> From<Handle<T>> for HandleId {
fn from(value: Handle<T>) -> Self {
value.id
}
}

impl From<HandleUntyped> for HandleId {
fn from(value: HandleUntyped) -> Self {
value.id
}
}

impl From<&str> for HandleId {
fn from(value: &str) -> Self {
AssetPathId::from(value).into()
}
}

impl<T> From<&Handle<T>> for HandleId {
impl<T: Asset> From<&Handle<T>> for HandleId {
fn from(value: &Handle<T>) -> Self {
value.id
}
}

impl<T> Hash for Handle<T> {
impl<T: Asset> Hash for Handle<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
Hash::hash(&self.id, state);
}
}

impl<T> PartialEq for Handle<T> {
impl<T: Asset> PartialEq for Handle<T> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

impl<T> Eq for Handle<T> {}
impl<T: Asset> Eq for Handle<T> {}

impl<T> PartialOrd for Handle<T> {
impl<T: Asset> PartialOrd for Handle<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.id.cmp(&other.id))
}
}

impl<T> Ord for Handle<T> {
impl<T: Asset> Ord for Handle<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
Expand All @@ -214,7 +210,7 @@ impl<T: Asset> Default for Handle<T> {
}
}

impl<T> Debug for Handle<T> {
impl<T: Asset> Debug for Handle<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
let name = std::any::type_name::<T>().split("::").last().unwrap();
write!(f, "{:?}Handle<{}>({:?})", self.handle_type, name, self.id)
Expand All @@ -231,8 +227,8 @@ impl<T: Asset> Clone for Handle<T> {
}

// SAFE: T is phantom data and Handle::id is an integer
unsafe impl<T> Send for Handle<T> {}
unsafe impl<T> Sync for Handle<T> {}
unsafe impl<T: Asset> Send for Handle<T> {}
unsafe impl<T: Asset> Sync for Handle<T> {}

/// A non-generic version of [Handle]
///
Expand All @@ -244,6 +240,13 @@ pub struct HandleUntyped {
}

impl HandleUntyped {
pub const fn weak_from_u64(uuid: Uuid, id: u64) -> Self {
Self {
id: HandleId::new(uuid, id),
handle_type: HandleType::Weak,
}
}

pub(crate) fn strong(id: HandleId, ref_change_sender: Sender<RefChange>) -> Self {
ref_change_sender.send(RefChange::Increment(id)).unwrap();
Self {
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_audio/src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::{AudioSource, Decodable};
use bevy_asset::Handle;
use bevy_asset::{Asset, Handle};
use parking_lot::RwLock;
use std::{collections::VecDeque, fmt};

/// The external struct used to play audio
pub struct Audio<P = AudioSource>
where
P: Decodable,
P: Asset + Decodable,
{
pub queue: RwLock<VecDeque<Handle<P>>>,
}

impl<P> fmt::Debug for Audio<P>
impl<P: Asset> fmt::Debug for Audio<P>
where
P: Decodable,
{
Expand All @@ -22,7 +22,7 @@ where

impl<P> Default for Audio<P>
where
P: Decodable,
P: Asset + Decodable,
{
fn default() -> Self {
Self {
Expand All @@ -33,7 +33,7 @@ where

impl<P> Audio<P>
where
P: Decodable,
P: Asset + Decodable,
<P as Decodable>::Decoder: rodio::Source + Send + Sync,
<<P as Decodable>::Decoder as Iterator>::Item: rodio::Sample + Send + Sync,
{
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core/src/label.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy_ecs::prelude::*;
use bevy_reflect::Reflect;
use bevy_reflect::{Reflect, ReflectComponent};
use bevy_utils::{HashMap, HashSet};
use std::{
borrow::Cow,
Expand All @@ -9,6 +9,7 @@ use std::{

/// A collection of labels
#[derive(Default, Reflect)]
#[reflect(Component)]
pub struct Labels {
labels: HashSet<Cow<'static, str>>,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Default for PbrBundle {
fn default() -> Self {
Self {
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new(
FORWARD_PIPELINE_HANDLE,
FORWARD_PIPELINE_HANDLE.typed(),
)]),
mesh: Default::default(),
material: Default::default(),
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_pbr/src/render_graph/forward_pipeline/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_asset::{Assets, Handle};
use bevy_asset::{Assets, HandleUntyped};
use bevy_reflect::TypeUuid;
use bevy_render::{
pipeline::{
Expand All @@ -10,8 +10,8 @@ use bevy_render::{
texture::TextureFormat,
};

pub const FORWARD_PIPELINE_HANDLE: Handle<PipelineDescriptor> =
Handle::weak_from_u64(PipelineDescriptor::TYPE_UUID, 13148362314012771389);
pub const FORWARD_PIPELINE_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(PipelineDescriptor::TYPE_UUID, 13148362314012771389);

pub(crate) fn build_forward_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
PipelineDescriptor {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use super::CameraProjection;
use bevy_app::prelude::{EventReader, Events};
use bevy_ecs::{Added, Component, Entity, Local, Query, QuerySet, Res};
use bevy_math::Mat4;
use bevy_reflect::Reflect;
use bevy_reflect::{Reflect, ReflectComponent};
use bevy_window::{WindowCreated, WindowId, WindowResized, Windows};

#[derive(Default, Debug, Reflect)]
#[reflect(Component)]
pub struct Camera {
pub projection_matrix: Mat4,
pub name: Option<String>,
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_render/src/camera/projection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::DepthCalculation;
use bevy_math::Mat4;
use bevy_reflect::{Reflect, ReflectDeserialize};
use bevy_reflect::{Reflect, ReflectComponent, ReflectDeserialize};
use serde::{Deserialize, Serialize};

pub trait CameraProjection {
Expand All @@ -10,6 +10,7 @@ pub trait CameraProjection {
}

#[derive(Debug, Clone, Reflect)]
#[reflect(Component)]
pub struct PerspectiveProjection {
pub fov: f32,
pub aspect_ratio: f32,
Expand Down Expand Up @@ -51,6 +52,7 @@ pub enum WindowOrigin {
}

#[derive(Debug, Clone, Reflect)]
#[reflect(Component)]
pub struct OrthographicProjection {
pub left: f32,
pub right: f32,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/camera/visible_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{Camera, DepthCalculation};
use crate::Draw;
use bevy_core::FloatOrd;
use bevy_ecs::{Entity, Query, With};
use bevy_reflect::Reflect;
use bevy_reflect::{Reflect, ReflectComponent};
use bevy_transform::prelude::GlobalTransform;

#[derive(Debug)]
Expand All @@ -12,6 +12,7 @@ pub struct VisibleEntity {
}

#[derive(Default, Debug, Reflect)]
#[reflect(Component)]
pub struct VisibleEntities {
#[reflect(ignore)]
pub value: Vec<VisibleEntity>,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
};
use bevy_asset::{Assets, Handle};
use bevy_ecs::{Query, Res, ResMut, SystemParam};
use bevy_reflect::Reflect;
use bevy_reflect::{Reflect, ReflectComponent};
use std::{ops::Range, sync::Arc};
use thiserror::Error;

Expand Down Expand Up @@ -45,6 +45,7 @@ pub enum RenderCommand {

/// A component that indicates how to draw an entity.
#[derive(Debug, Clone, Reflect)]
#[reflect(Component)]
pub struct Draw {
pub is_visible: bool,
pub is_transparent: bool,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/pipeline/render_pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};
use bevy_asset::{Assets, Handle};
use bevy_ecs::{Query, Res, ResMut};
use bevy_reflect::Reflect;
use bevy_reflect::{Reflect, ReflectComponent};
use bevy_utils::HashSet;

#[derive(Debug, Default, Clone, Reflect)]
Expand Down Expand Up @@ -40,6 +40,7 @@ impl RenderPipeline {
}

#[derive(Debug, Clone, Reflect)]
#[reflect(Component)]
pub struct RenderPipelines {
pub pipelines: Vec<RenderPipeline>,
#[reflect(ignore)]
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/render_graph/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use crate::{
texture::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage},
Color,
};
use bevy_reflect::Reflect;
use bevy_reflect::{Reflect, ReflectComponent};
use bevy_window::WindowId;

/// A component that indicates that an entity should be drawn in the "main pass"
#[derive(Default, Reflect)]
#[reflect(Component)]
pub struct MainPass;

#[derive(Debug)]
Expand Down
Loading