summaryrefslogtreecommitdiffstats
path: root/wgpu/src/texture/atlas/allocator.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-02-26 12:34:34 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-02-26 12:34:34 +0100
commit59d45a5440aaa46c7dc8f3dc70c8518167c10418 (patch)
tree2b5ad3d2c577e9ebb7fcac7452a74680174f8108 /wgpu/src/texture/atlas/allocator.rs
parent82f0a49062d10f3cbf202a5379c061a2509ec97b (diff)
downloadiced-59d45a5440aaa46c7dc8f3dc70c8518167c10418.tar.gz
iced-59d45a5440aaa46c7dc8f3dc70c8518167c10418.tar.bz2
iced-59d45a5440aaa46c7dc8f3dc70c8518167c10418.zip
Refactor texture atlas
- Split into multiple modules - Rename some concepts - Change API details
Diffstat (limited to 'wgpu/src/texture/atlas/allocator.rs')
-rw-r--r--wgpu/src/texture/atlas/allocator.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/wgpu/src/texture/atlas/allocator.rs b/wgpu/src/texture/atlas/allocator.rs
new file mode 100644
index 00000000..cd710522
--- /dev/null
+++ b/wgpu/src/texture/atlas/allocator.rs
@@ -0,0 +1,57 @@
+use guillotiere::{AtlasAllocator, Size};
+
+pub struct Allocator {
+ raw: AtlasAllocator,
+}
+
+impl Allocator {
+ pub fn new(size: u32) -> Allocator {
+ let raw = AtlasAllocator::new(Size::new(size as i32, size as i32));
+
+ Allocator { raw }
+ }
+
+ pub fn allocate(&mut self, width: u32, height: u32) -> Option<Region> {
+ let allocation = self
+ .raw
+ .allocate(Size::new(width as i32 + 2, height as i32 + 2))?;
+
+ Some(Region(allocation))
+ }
+
+ pub fn deallocate(&mut self, region: Region) {
+ self.raw.deallocate(region.0.id);
+ }
+}
+
+pub struct Region(guillotiere::Allocation);
+
+impl Region {
+ pub fn position(&self) -> (u32, u32) {
+ let rectangle = &self.0.rectangle;
+
+ (rectangle.min.x as u32 + 1, rectangle.min.y as u32 + 1)
+ }
+
+ pub fn size(&self) -> (u32, u32) {
+ let size = self.0.rectangle.size();
+
+ (size.width as u32 - 2, size.height as u32 - 2)
+ }
+}
+
+impl std::fmt::Debug for Allocator {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "Allocator")
+ }
+}
+
+impl std::fmt::Debug for Region {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(
+ f,
+ "Region {{ id: {:?}, rectangle: {:?} }}",
+ self.0.id, self.0.rectangle
+ )
+ }
+}