Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Aug 10, 2024
1 parent fa93466 commit c3a5e9a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
13 changes: 9 additions & 4 deletions core/common/src/io/virtualfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,24 @@ pub trait ToVirtualPath {
fn to_virtual_path(self) -> VirtualPath;
}

/// Allow virtual paths to be converted into themselves.
impl<R: AsRef<str>> ToVirtualPath for R {
#[inline]
fn to_virtual_path(self) -> VirtualPath {
VirtualPath::new(self.as_ref())
}
}

impl ToVirtualPath for VirtualPath {
#[inline]
fn to_virtual_path(self) -> VirtualPath {
self.clone()
}
}

/// Allow string references to be converted into [`VirtualPath`] instances.
impl<R: AsRef<str>> ToVirtualPath for R {
impl ToVirtualPath for &VirtualPath {
#[inline]
fn to_virtual_path(self) -> VirtualPath {
VirtualPath::new(self.as_ref())
self.clone()
}
}

Expand Down
29 changes: 29 additions & 0 deletions core/graphics/src/textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ impl Texture {
.expect("Failed to initialize texture");
}

/// Returns a [`TextureAtlas`] that represents the entire texture.
pub fn to_atlas(&self, size: UVec2) -> TextureAtlas {
TextureAtlas::new(self.to_region(), size)
}

/// Returns a [`TextureRegion`] that represents the entire texture.
pub fn to_region(&self) -> TextureRegion {
TextureRegion::new(self)
Expand Down Expand Up @@ -252,6 +257,30 @@ impl Drop for TextureState {
}
}

/// Represents a collection of textures that can be used for rendering.
#[derive(Clone)]
pub struct TextureAtlas {
pub texture: TextureRegion,
pub size: UVec2,
}

impl TextureAtlas {
/// Creates a new texture atlas from the given texture and size.
pub fn new(texture: TextureRegion, size: UVec2) -> Self {
Self { texture, size }
}

/// Slices the texture atlas into a smaller region.
pub fn slice(&self, x: u32, y: u32) -> TextureRegion {
let width = self.size.x;
let height = self.size.y;
let x = x * width;
let y = y * height;

self.texture.slice(x, y, width, height)
}
}

/// Represents a sub-region of a [`Texture`].
#[derive(Clone)]
pub struct TextureRegion {
Expand Down

0 comments on commit c3a5e9a

Please sign in to comment.