1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/// [`guillotiere::AtlasAllocator`] handler for [`crate::AtlasSet`].
///
pub struct Allocator {
    /// [`guillotiere::AtlasAllocator`] holding Allocations for Textures.
    allocator: guillotiere::AtlasAllocator,
    /// Amount of Allocations currently Held.
    allocations: usize,
    /// Amount of Current Deallocations since last clear or creation.
    deallocations: usize,
}

impl Allocator {
    /// Returns a new Allocation if Room exists within the Texture layer.
    ///
    pub fn allocate(
        &mut self,
        width: u32,
        height: u32,
    ) -> Option<guillotiere::Allocation> {
        let allocation = self
            .allocator
            .allocate(guillotiere::Size::new(width as i32, height as i32))?;

        self.allocations += 1;

        Some(allocation)
    }

    /// Clears the Allocator and its counters.
    ///
    pub fn clear(&mut self) {
        self.allocator.clear();
        self.allocations = 0;
        self.deallocations = 0;
    }

    /// Removes a Allocation to make it usable again.
    ///
    pub fn deallocate(&mut self, allocation: guillotiere::Allocation) {
        self.allocator.deallocate(allocation.id);

        self.allocations = self.allocations.saturating_sub(1);
        self.deallocations = self.deallocations.saturating_add(1);
    }

    /// If there are no Allocations.
    ///
    pub fn is_empty(&self) -> bool {
        self.allocations == 0
    }

    /// How many deallocations have been made. Used for defragmentation.
    ///
    pub fn deallocations(&self) -> usize {
        self.deallocations
    }

    /// Creates a new [`Allocator`] layer.
    ///
    pub fn new(size: u32) -> Self {
        let allocator = guillotiere::AtlasAllocator::new(
            guillotiere::Size::new(size as i32, size as i32),
        );

        Self {
            allocator,
            allocations: 0,
            deallocations: 0,
        }
    }
}