Porytiles
Loading...
Searching...
No Matches
palette_pool.cpp
Go to the documentation of this file.
2
4
5namespace porytiles {
6
7PalettePool::PalettePool(std::bitset<pal::num_pals> available_indexes)
8 : available_indexes_{available_indexes}, checked_out_{}, checkout_stack_{}
9{
10}
11
12bool PalettePool::is_available(std::size_t hardware_index) const
13{
14 if (hardware_index >= pal::num_pals) {
15 panic("index out of bounds");
16 }
17 return available_indexes_.test(hardware_index) && !checked_out_.test(hardware_index);
18}
19
21{
22 if (!has_available_pal()) {
23 panic("called with no available indexes");
24 }
25
26 // Find the lowest available index that is not checked out
27 for (std::size_t i = 0; i < pal::num_pals; ++i) {
28 if (available_indexes_.test(i) && !checked_out_.test(i)) {
29 checked_out_.set(i);
30 checkout_stack_.push_back(i);
31 return i;
32 }
33 }
34
35 // Should be unreachable due to has_available_index() check above
36 panic("unreachable state");
37}
38
39void PalettePool::checkout(std::size_t index)
40{
41 if (!is_available(index)) {
42 panic("index is not available for checkout");
43 }
44
45 checked_out_.set(index);
46 checkout_stack_.push_back(index);
47}
48
50{
51 if (checkout_stack_.empty()) {
52 panic("called with empty checkout stack");
53 }
54
55 std::size_t index = checkout_stack_.back();
56 checkout_stack_.pop_back();
57 checked_out_.reset(index);
58}
59
60} // namespace porytiles
std::size_t checkout()
Checks out the next available hardware palette index.
void checkin()
Returns the most recently checked-out hardware palette index index to the pool.
PalettePool(std::bitset< pal::num_pals > available_indexes)
Constructs a PalettePool with the specified available indexes.
bool is_available(std::size_t hardware_index) const
Checks if a specific index is available for checkout.
bool has_available_pal() const
Checks if there is at least one available pal that can be checked out.
constexpr std::size_t num_pals
Definition palette.hpp:21
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43