diff options
Diffstat (limited to 'alligator_resources/src')
| -rw-r--r-- | alligator_resources/src/texture.rs | 76 |
1 files changed, 26 insertions, 50 deletions
diff --git a/alligator_resources/src/texture.rs b/alligator_resources/src/texture.rs index 9e29d08..74451f3 100644 --- a/alligator_resources/src/texture.rs +++ b/alligator_resources/src/texture.rs @@ -219,63 +219,43 @@ impl Texture { TextureBuffer::Disk(file) => file.is_loaded(), } } - - fn unqueue(&mut self) { - if let Some(queued_priority) = *self.queued_priority.lock() { - self.priority = queued_priority; - } - } } -pub struct TextureRef<'a> { +pub struct TextureRef { id: TextureId, + // TODO we don't need this. Just the width and height will do texture: Arc<Rgba16Texture>, queued_priority: Arc<Mutex<Option<Priority>>>, - manager: &'a TextureManager, } -impl<'a> TextureRef<'a> { +impl TextureRef { #[must_use] pub const fn id(&self) -> TextureId { self.id } - fn texture_width(&self) -> u32 { - self.texture.width() - } - - fn texture_height(&self) -> u32 { - self.texture.height() - } - - #[allow(clippy::missing_const_for_fn)] - fn texture(&self) -> &Rgba16Texture { - &self.texture - } - // TODO: it's safer to replace this with a position thingy #[must_use] - pub fn atlas_x(&self) -> f32 { - self.manager - .subtexture_x(self.id) - .expect("not in texture atlas") + pub fn atlas_x(&self, manager: &TextureManager) -> f32 { + manager.subtexture_x(self.id).expect("not in texture atlas") } #[must_use] - pub fn atlas_y(&self) -> f32 { - self.manager - .subtexture_y(self.id) - .expect("not in texture atlas") + pub fn atlas_y(&self, manager: &TextureManager) -> f32 { + manager.subtexture_y(self.id).expect("not in texture atlas") } + // TODO: it's safer to replace this with a position thingy #[must_use] - pub fn width(&self) -> f32 { - self.manager.texture_width(self) + #[allow(clippy::cast_precision_loss)] // TODO remove this + pub fn width(&self, manager: &TextureManager) -> f32 { + self.texture.width() as f32 / manager.atlas_width() as f32 } #[must_use] - pub fn height(&self) -> f32 { - self.manager.texture_height(self) + #[allow(clippy::cast_precision_loss)] // TODO remove this + pub fn height(&self, manager: &TextureManager) -> f32 { + self.texture.height() as f32 / manager.atlas_height() as f32 } pub fn queue_priority(&self, priority: Priority) { @@ -413,11 +393,13 @@ impl TextureManager { pub fn set_priority(&mut self, id: TextureId, priority: Priority) -> Result<(), LoadError> { let texture = self.textures.get_mut(&id).expect("invalid texture id"); + let old_priority = texture.priority(); texture.set_priority(priority); let size = texture.allocated_size(); let priority = texture.priority(); - if priority != Priority::Unnecessary + if priority > old_priority + && priority != Priority::Unnecessary && !texture.is_loaded() && (priority == Priority::Urgent || self.can_load(size, priority)) { @@ -447,6 +429,14 @@ impl TextureManager { self.packer.get_frame(&id) } + const fn atlas_width(&self) -> u32 { + self.width + } + + const fn atlas_height(&self) -> u32 { + self.height + } + /// Get the x-position of a texture, if it is in the texture atlas #[must_use] #[allow(clippy::cast_precision_loss)] // TODO remove this @@ -457,23 +447,9 @@ impl TextureManager { /// Get the y-position of a texture, if it is in the texture atlas #[must_use] - #[allow(clippy::cast_precision_loss)] + #[allow(clippy::cast_precision_loss)] // TODO remove this pub fn subtexture_y(&self, id: TextureId) -> Option<f32> { let y = self.subtexture(id)?.frame.y; Some(y as f32 / self.height as f32) } - - /// Get the width of a texture - #[must_use] - #[allow(clippy::cast_precision_loss)] - pub fn texture_width(&self, texture: &TextureRef) -> f32 { - texture.texture_width() as f32 / self.width as f32 - } - - /// Get the height of a texture - #[must_use] - #[allow(clippy::cast_precision_loss)] - pub fn texture_height(&self, texture: &TextureRef) -> f32 { - texture.texture_height() as f32 / self.height as f32 - } } |
