diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:24 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:24 +0100 |
commit | 6367a9ba5a549b62f01da61fb50323877b9f52ff (patch) | |
tree | eb83ed6487f2ec35f9ea1e97124c9a173b8ceef0 /stockton-skeleton/src | |
parent | f84ec1a3e077fa0f2e9cd310b6e32e81f1bc1129 (diff) |
refactor(all): move types to -skeleton
Diffstat (limited to 'stockton-skeleton/src')
-rw-r--r-- | stockton-skeleton/src/components/mod.rs | 61 | ||||
-rw-r--r-- | stockton-skeleton/src/context.rs | 3 | ||||
-rw-r--r-- | stockton-skeleton/src/draw_passes/cons.rs | 3 | ||||
-rw-r--r-- | stockton-skeleton/src/draw_passes/mod.rs | 3 | ||||
-rw-r--r-- | stockton-skeleton/src/lib.rs | 16 | ||||
-rw-r--r-- | stockton-skeleton/src/session.rs | 33 | ||||
-rw-r--r-- | stockton-skeleton/src/target.rs | 2 | ||||
-rw-r--r-- | stockton-skeleton/src/texture/image.rs | 8 | ||||
-rw-r--r-- | stockton-skeleton/src/texture/load.rs | 2 | ||||
-rw-r--r-- | stockton-skeleton/src/texture/loader.rs | 2 | ||||
-rw-r--r-- | stockton-skeleton/src/texture/mod.rs | 3 | ||||
-rw-r--r-- | stockton-skeleton/src/texture/repo.rs | 2 | ||||
-rw-r--r-- | stockton-skeleton/src/texture/resolver.rs | 55 | ||||
-rw-r--r-- | stockton-skeleton/src/types.rs | 8 |
14 files changed, 127 insertions, 74 deletions
diff --git a/stockton-skeleton/src/components/mod.rs b/stockton-skeleton/src/components/mod.rs new file mode 100644 index 0000000..74e5a69 --- /dev/null +++ b/stockton-skeleton/src/components/mod.rs @@ -0,0 +1,61 @@ +use na::{Mat4, Vec4}; +use std::f32::consts::PI; + +use crate::types::Vector3; + +/// 90 degrees in radians +const R89: f32 = (PI / 180.0) * 89.0; + +/// 180 degrees in radians +const R180: f32 = PI; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Transform { + /// Position of the object + pub position: Vector3, + + /// Rotation of the object (euler angles in radians) + pub rotation: Vector3, +} + +impl Transform { + pub fn rotate(&mut self, vec: Vector3) { + self.rotation += vec; + + // Clamp -pi/2 < pitch < pi/2 + if self.rotation.x > R89 { + self.rotation.x = R89; + } else if self.rotation.x <= -R89 { + self.rotation.x = -R89; + } + + // -pi < yaw <= pi + if self.rotation.y <= -R180 { + self.rotation.y = R180 - self.rotation.y % -R180; + } else if self.rotation.y > 180.0 { + self.rotation.y = -R180 + self.rotation.y % R180; + } + } + + pub fn translate(&mut self, delta: Vector3) { + let rot_matrix = + Mat4::from_euler_angles(-self.rotation.x, self.rotation.y, self.rotation.z); + + let new = rot_matrix * Vec4::new(delta.x, delta.y, delta.z, 1.0); + self.position.x += new.x; + self.position.y += new.y; + self.position.z += new.z; + } +} + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct CameraSettings { + /// FOV (radians) + pub fov: f32, + + /// Near clipping plane (world units) + pub near: f32, + + /// Far clipping plane (world units) + pub far: f32, +} diff --git a/stockton-skeleton/src/context.rs b/stockton-skeleton/src/context.rs index 72b15c7..943e0dc 100644 --- a/stockton-skeleton/src/context.rs +++ b/stockton-skeleton/src/context.rs @@ -35,10 +35,9 @@ use crate::{ mem::MemoryPool, queue_negotiator::{QueueFamilyNegotiator, QueueFamilySelector, SharedQueue}, types::*, + session::Session }; -use stockton_types::Session; - /// The actual data behind [`StatefulRenderingContext`] struct InnerRenderingContext { /// Vulkan Instance diff --git a/stockton-skeleton/src/draw_passes/cons.rs b/stockton-skeleton/src/draw_passes/cons.rs index dea42af..f6c3d1b 100644 --- a/stockton-skeleton/src/draw_passes/cons.rs +++ b/stockton-skeleton/src/draw_passes/cons.rs @@ -2,8 +2,7 @@ //! Note that this can be extended to an arbitrary amount of draw passes. use super::{Beginning, DrawPass, End, IntoDrawPass, Middle, Singular}; -use crate::{context::RenderingContext, queue_negotiator::QueueFamilyNegotiator, types::*}; -use stockton_types::Session; +use crate::{session::Session, context::RenderingContext, queue_negotiator::QueueFamilyNegotiator, types::*}; use anyhow::Result; diff --git a/stockton-skeleton/src/draw_passes/mod.rs b/stockton-skeleton/src/draw_passes/mod.rs index 5b138c2..9a13c73 100644 --- a/stockton-skeleton/src/draw_passes/mod.rs +++ b/stockton-skeleton/src/draw_passes/mod.rs @@ -1,12 +1,11 @@ //! Traits and common draw passes. use std::ops::Range; -use crate::{context::RenderingContext, queue_negotiator::QueueFamilyNegotiator, types::*}; +use crate::{context::RenderingContext, queue_negotiator::QueueFamilyNegotiator, types::*, session::Session}; use hal::{ image::Layout, pass::{AttachmentLoadOp, AttachmentOps, AttachmentStoreOp}, }; -use stockton_types::Session; use anyhow::Result; diff --git a/stockton-skeleton/src/lib.rs b/stockton-skeleton/src/lib.rs index 2b6fe70..d57c5f6 100644 --- a/stockton-skeleton/src/lib.rs +++ b/stockton-skeleton/src/lib.rs @@ -17,15 +17,17 @@ mod target; pub mod texture; pub mod types; pub mod utils; +pub mod components; +pub mod session; -use std::mem::ManuallyDrop; - -use context::RenderingContext; -use draw_passes::{DrawPass, IntoDrawPass, Singular}; +pub use context::RenderingContext; +pub use session::Session; +pub use draw_passes::{DrawPass, IntoDrawPass, PassPosition}; +pub use anyhow::Result; -use anyhow::{Context, Result}; - -use stockton_types::Session; +use draw_passes::Singular; +use std::mem::ManuallyDrop; +use anyhow::{Context}; use winit::window::Window; /// Renders a world to a window when you tell it to. diff --git a/stockton-skeleton/src/session.rs b/stockton-skeleton/src/session.rs new file mode 100644 index 0000000..5b91739 --- /dev/null +++ b/stockton-skeleton/src/session.rs @@ -0,0 +1,33 @@ +//! The thing you play on and all the associated state. + +use legion::systems::Builder; +use legion::*; + +/// A loaded world. +pub struct Session { + pub world: World, + pub resources: Resources, + schedule: Schedule, +} + +impl Session { + /// The level can be any format, as long as it has the required features of a bsp. + pub fn new<S: FnOnce(&mut Builder)>(add_systems: S) -> Session { + let world = World::default(); + + let resources = Resources::default(); + let mut schedule = Schedule::builder(); + add_systems(&mut schedule); + let schedule = schedule.build(); + + Session { + world, + resources, + schedule, + } + } + + pub fn do_update(&mut self) { + self.schedule.execute(&mut self.world, &mut self.resources); + } +} diff --git a/stockton-skeleton/src/target.rs b/stockton-skeleton/src/target.rs index 647aa96..23f56b9 100644 --- a/stockton-skeleton/src/target.rs +++ b/stockton-skeleton/src/target.rs @@ -5,8 +5,8 @@ use crate::{ context::ContextProperties, draw_passes::{DrawPass, Singular}, types::*, + session::Session }; -use stockton_types::Session; use std::{ borrow::Borrow, diff --git a/stockton-skeleton/src/texture/image.rs b/stockton-skeleton/src/texture/image.rs index f984b72..6ccd22a 100644 --- a/stockton-skeleton/src/texture/image.rs +++ b/stockton-skeleton/src/texture/image.rs @@ -41,3 +41,11 @@ impl LoadableImage for RgbaImage { copy_nonoverlapping(row.as_ptr(), ptr, row.len()); } } + +/// An object that can be used to resolve a texture from a BSP File +pub trait TextureResolver { + type Image: LoadableImage; + + /// Get the given texture, or None if it's corrupt/not there. + fn resolve(&mut self, texture_id: u32) -> Option<Self::Image>; +} diff --git a/stockton-skeleton/src/texture/load.rs b/stockton-skeleton/src/texture/load.rs index c4d3b72..80c332e 100644 --- a/stockton-skeleton/src/texture/load.rs +++ b/stockton-skeleton/src/texture/load.rs @@ -1,6 +1,6 @@ use std::sync::{Arc, RwLock}; -use super::{block::TexturesBlock, repo::BLOCK_SIZE, resolver::TextureResolver, LoadableImage}; +use super::{block::TexturesBlock, repo::BLOCK_SIZE, TextureResolver, LoadableImage}; use crate::{ buffers::{ image::{ImageSpec, SampledImage, COLOR_RESOURCES}, diff --git a/stockton-skeleton/src/texture/loader.rs b/stockton-skeleton/src/texture/loader.rs index 6de4a4d..6212492 100644 --- a/stockton-skeleton/src/texture/loader.rs +++ b/stockton-skeleton/src/texture/loader.rs @@ -6,7 +6,7 @@ use super::{ load_image, QueuedLoad, TextureLoadConfig, TextureLoadError, FORMAT, LAYERS, RESOURCES, }, repo::BLOCK_SIZE, - resolver::TextureResolver, + TextureResolver, PIXEL_SIZE, }; use crate::{ diff --git a/stockton-skeleton/src/texture/mod.rs b/stockton-skeleton/src/texture/mod.rs index 10fbbad..c5306ce 100644 --- a/stockton-skeleton/src/texture/mod.rs +++ b/stockton-skeleton/src/texture/mod.rs @@ -5,10 +5,9 @@ mod image; mod load; mod loader; mod repo; -pub mod resolver; pub use self::block::TexturesBlock; -pub use self::image::LoadableImage; +pub use self::image::{LoadableImage, TextureResolver}; pub use self::load::TextureLoadConfig; pub use self::loader::BlockRef; pub use self::repo::{TexLoadQueue, TextureRepo}; diff --git a/stockton-skeleton/src/texture/repo.rs b/stockton-skeleton/src/texture/repo.rs index 22fc099..bd36a1e 100644 --- a/stockton-skeleton/src/texture/repo.rs +++ b/stockton-skeleton/src/texture/repo.rs @@ -2,7 +2,7 @@ use super::{ block::TexturesBlock, load::TextureLoadConfig, loader::{BlockRef, LoaderRequest, TextureLoader, TextureLoaderRemains, NUM_SIMULTANEOUS_CMDS}, - resolver::TextureResolver, + TextureResolver, }; use crate::types::*; use crate::{context::RenderingContext, error::LockPoisoned, mem::MappableBlock}; diff --git a/stockton-skeleton/src/texture/resolver.rs b/stockton-skeleton/src/texture/resolver.rs deleted file mode 100644 index f66b724..0000000 --- a/stockton-skeleton/src/texture/resolver.rs +++ /dev/null @@ -1,55 +0,0 @@ -//! Resolves a texture in a BSP File to an image - -use crate::texture::image::LoadableImage; -use stockton_levels::{parts::IsTexture, prelude::HasTextures}; - -use std::{ - path::Path, - sync::{Arc, RwLock}, -}; - -use image::{io::Reader, RgbaImage}; - -/// An object that can be used to resolve a texture from a BSP File -pub trait TextureResolver { - type Image: LoadableImage; - - /// Get the given texture, or None if it's corrupt/not there. - fn resolve(&mut self, texture_id: u32) -> Option<Self::Image>; -} - -/// A basic filesystem resolver which gets the texture name from any HasTextures Object. -pub struct FsResolver<'a, T: HasTextures> { - path: &'a Path, - map_lock: Arc<RwLock<T>>, -} - -impl<'a, T: HasTextures> FsResolver<'a, T> { - pub fn new(path: &'a Path, map_lock: Arc<RwLock<T>>) -> Self { - FsResolver { path, map_lock } - } -} - -impl<'a, T: HasTextures> TextureResolver for FsResolver<'a, T> { - type Image = RgbaImage; - - fn resolve(&mut self, tex: u32) -> Option<Self::Image> { - let map = self.map_lock.read().unwrap(); - let tex = map.get_texture(tex)?; - let path = self.path.join(&tex.name()); - - // drop(tex); - // drop(map); - - if let Ok(file) = Reader::open(path) { - if let Ok(guessed) = file.with_guessed_format() { - if let Ok(decoded) = guessed.decode() { - return Some(decoded.into_rgba8()); - } - } - } - - log::warn!("Couldn't resolve texture {:?}", tex.name()); - None - } -} diff --git a/stockton-skeleton/src/types.rs b/stockton-skeleton/src/types.rs index 03c6e37..d3cf316 100644 --- a/stockton-skeleton/src/types.rs +++ b/stockton-skeleton/src/types.rs @@ -33,3 +33,11 @@ pub type DynamicAllocator = rendy_memory::DynamicAllocator<back::Backend>; pub type DynamicBlock = rendy_memory::DynamicBlock<back::Backend>; pub type RDescriptorSet = rendy_descriptor::DescriptorSet<back::Backend>; + +pub type Vector2 = na::Vec2; +pub type Vector3 = na::Vec3; + +pub type Vector2i = na::IVec2; +pub type Vector3i = na::IVec3; + +pub type Matrix4 = na::Mat4x4; |