summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2022-11-20 16:53:46 -0500
committerMicha White <botahamec@outlook.com>2022-11-20 16:53:46 -0500
commit2d0e03426f95cc402609d13042827de649fa253c (patch)
treec0835f6bd30ae39df68f8532b0dd9eaa3715e19f
parent6ef68bd98be011acf8c981cc91184801967360f1 (diff)
Brought back the texture atlas
-rw-r--r--alligator_resources/src/texture.rs59
1 files changed, 58 insertions, 1 deletions
diff --git a/alligator_resources/src/texture.rs b/alligator_resources/src/texture.rs
index 74451f3..569f2c6 100644
--- a/alligator_resources/src/texture.rs
+++ b/alligator_resources/src/texture.rs
@@ -363,6 +363,56 @@ impl TextureManager {
Ok(id)
}
+ fn resize_atlas(&mut self, width: u32, height: u32) {
+ self.packer = packer(width, height);
+
+ for (id, texture) in &mut self.textures {
+ if texture.priority == Priority::Urgent {
+ let texture = texture
+ .load_texture()
+ .expect("unable to load texture when putting it in the atlas");
+
+ self.packer
+ .pack_own(*id, texture.clone())
+ .expect("resized atlas is too small");
+ }
+ }
+ }
+
+ fn reset_atlas(&mut self, fallback_width: u32, fallback_height: u32) {
+ self.packer = packer(self.width, self.height);
+
+ let mut failed = false;
+ for (id, texture) in &mut self.textures {
+ if texture.priority == Priority::Urgent {
+ let texture = texture
+ .load_texture()
+ .expect("unable to load texture when putting it in the atlas");
+
+ if self.packer.pack_own(*id, texture.clone()).is_err() {
+ failed = true;
+ }
+ }
+ }
+
+ if failed {
+ self.resize_atlas(fallback_width, fallback_height);
+ }
+ }
+
+ fn load_to_atlas(&mut self, id: TextureId) {
+ let texture = self.textures.get_mut(&id).expect("invalid texture id");
+ let texture = texture
+ .load_texture()
+ .expect("unable to load texture when putting it in the atlas");
+
+ if self.packer.pack_own(id, texture.clone()).is_err() {
+ let fallback_width = self.width * 2 + texture.width();
+ let fallback_height = self.height * 2 + texture.height();
+ self.reset_atlas(fallback_width, fallback_height);
+ }
+ }
+
pub fn load_from_file(
&mut self,
path: impl AsRef<Path>,
@@ -388,6 +438,10 @@ impl TextureManager {
self.textures.insert(id, texture);
}
+ if priority == Priority::Urgent {
+ self.load_to_atlas(id);
+ }
+
Ok(id)
}
@@ -396,7 +450,6 @@ impl TextureManager {
let old_priority = texture.priority();
texture.set_priority(priority);
let size = texture.allocated_size();
- let priority = texture.priority();
if priority > old_priority
&& priority != Priority::Unnecessary
@@ -407,6 +460,10 @@ impl TextureManager {
texture.load_texture()?;
}
+ if priority == Priority::Urgent {
+ self.load_to_atlas(id);
+ }
+
Ok(())
}