fix atlas.insert_set
authoralfadur
Thu, 28 Mar 2019 00:33:36 +0300
changeset 14728 069291842d52
parent 14727 2cc36cb1c258
child 14729 6aa8080b3b39
fix atlas.insert_set
rust/lib-hedgewars-engine/src/render/atlas.rs
--- a/rust/lib-hedgewars-engine/src/render/atlas.rs	Thu Mar 28 00:13:13 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/render/atlas.rs	Thu Mar 28 00:33:36 2019 +0300
@@ -127,8 +127,7 @@
             .collect();
     }
 
-    pub fn insert(&mut self, size: Size) -> Option<Rect> {
-        let (rect, _) = self.find_position(size)?;
+    fn split_insert(&mut self, rect: Rect) {
         let mut splits = vec![];
 
         for i in (0..self.free_rects.len()).rev() {
@@ -136,10 +135,15 @@
                 self.free_rects.swap_remove(i as usize);
             }
         }
+
         self.free_rects.extend(splits);
         self.prune();
+        self.used_rects.push(rect);
+    }
 
-        self.used_rects.push(rect);
+    pub fn insert(&mut self, size: Size) -> Option<Rect> {
+        let (rect, _) = self.find_position(size)?;
+        self.split_insert(rect);
         Some(rect)
     }
 
@@ -156,15 +160,12 @@
             .filter_map(|(i, s)| self.find_position(*s).map(|res| (i, res)))
             .min_by_key(|(_, (_, fit))| fit.clone())
         {
+            self.split_insert(rect);
+
             result.push(rect);
             sizes.swap_remove(index);
         }
-        if sizes.is_empty() {
-            self.used_rects.extend_from_slice(&result);
-            result
-        } else {
-            vec![]
-        }
+        result
     }
 
     pub fn reset(&mut self) {