packed chunk data
This commit is contained in:
@@ -3,7 +3,35 @@
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
const int BITS_IN_BYTE = 8;
|
||||
const int BITS_IN_LONG = BITS_IN_BYTE * sizeof(std::uint64_t);
|
||||
|
||||
static unsigned int countBits(unsigned int number) {
|
||||
// log function in base 2
|
||||
// take only integer part
|
||||
return static_cast<unsigned int>(std::log2(number) + 1);
|
||||
}
|
||||
|
||||
TileIndex Chunk::GetTileIndex(std::uint16_t tileNumber) const {
|
||||
const std::uint8_t bitsPerTile = countBits(m_Palette.size());
|
||||
|
||||
const std::size_t startLong = (tileNumber * bitsPerTile) / BITS_IN_LONG;
|
||||
const std::size_t startOffset = (tileNumber * bitsPerTile) % BITS_IN_LONG;
|
||||
const std::size_t endLong = ((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG;
|
||||
|
||||
const Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
|
||||
|
||||
td::game::Chunk::ChunkData::value_type value;
|
||||
if (startLong == endLong) {
|
||||
value = (m_Data[startLong] >> startOffset);
|
||||
} else {
|
||||
int endOffset = BITS_IN_LONG - startOffset;
|
||||
value = (m_Data[startLong] >> startOffset | m_Data[endLong] << endOffset);
|
||||
}
|
||||
value &= individualValueMask;
|
||||
|
||||
return m_Palette.at(value);
|
||||
}
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
|
||||
Reference in New Issue
Block a user