From a2fe2786682e626dae25db2d375280c83b615796 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 18 Aug 2017 20:13:01 +0500 Subject: 2017-08-18 --- src/Section.cpp | 250 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 134 insertions(+), 116 deletions(-) (limited to 'src/Section.cpp') diff --git a/src/Section.cpp b/src/Section.cpp index b2d9ac9..3fb100f 100644 --- a/src/Section.cpp +++ b/src/Section.cpp @@ -1,97 +1,120 @@ #include "Section.hpp" - -Section::Section(Vector position, byte *dataBlocks, size_t dataBlocksLength, byte *dataLight, byte *dataSky, - byte bitsPerBlock, - std::vector palette) { - worldPosition = position; - - m_dataBlocksLen = dataBlocksLength; - m_dataBlocks = new byte[m_dataBlocksLen]; - std::copy(dataBlocks, dataBlocks + m_dataBlocksLen, m_dataBlocks); - - m_dataLight = new byte[2048]; - std::copy(dataLight, dataLight + 2048, m_dataLight); - - if (dataSky) { - m_dataSkyLight = new byte[2048]; - std::copy(dataSky, dataSky + 2048, m_dataSkyLight); - } - - m_palette = palette; - m_bitsPerBlock = bitsPerBlock; -} - Section::~Section() { - delete[] m_dataBlocks; - m_dataBlocksLen = 0; - m_dataBlocks = nullptr; - delete[] m_dataLight; - m_dataLight = nullptr; - delete[] m_dataSkyLight; - m_dataSkyLight = nullptr; } Block &Section::GetBlock(Vector pos) { - return m_blocks[pos.y * 256 + pos.z * 16 + pos.x]; + return blocks[pos.y * 256 + pos.z * 16 + pos.x]; +} + +Block Section::GetBlock(Vector pos) const +{ + return blocks[pos.y * 256 + pos.z * 16 + pos.x]; } double totalParsingTime = 0; -void Section::Parse() { - if (!m_blocks.empty()) - return; - - long long *longArray = reinterpret_cast(m_dataBlocks); - for (size_t i = 0; i < m_dataBlocksLen / 8; i++) - endswap(&longArray[i]); - std::vector blocks; - blocks.reserve(4096); - { - auto begin = std::chrono::steady_clock::now(); - int bitPos = 0; - unsigned short t = 0; - for (size_t i = 0; i < m_dataBlocksLen; i++) { - for (int j = 0; j < 8; j++) { - t |= (m_dataBlocks[i] & 0x01) ? 0x80 : 0x00; - t >>= 1; - m_dataBlocks[i] >>= 1; - bitPos++; - if (bitPos >= m_bitsPerBlock) { - bitPos = 0; - t >>= m_bitsPerBlock - 1; - blocks.push_back(t); - t = 0; - } - } - } - auto end = std::chrono::steady_clock::now(); - std::chrono::duration time = end - begin; - totalParsingTime += time.count(); - } - std::vector light; - light.reserve(4096); - for (int i = 0; i < 2048; i++) { - byte t = m_dataLight[i]; - byte first = t & 0b11110000; - byte second = t >> 4; - light.push_back(first); - light.push_back(second); - } - for (int i = 0; i < 4096; i++) { - unsigned short blockId = m_palette.size() > 0 ? m_palette[blocks[i]] : blocks[i]; - Block block(blockId >> 4, blockId & 0xF); - m_blocks.push_back(block); - } - delete[] m_dataBlocks; - m_dataBlocksLen = 0; - m_dataBlocks = nullptr; - delete[] m_dataLight; - m_dataLight = nullptr; - delete[] m_dataSkyLight; - m_dataSkyLight = nullptr; - - parseWaiter.notify_all(); +//void Section::Parse() { +// if (!m_blocks.empty()) +// return; +// +// long long *longArray = reinterpret_cast(m_dataBlocks); +// for (size_t i = 0; i < m_dataBlocksLen / 8; i++) +// endswap(&longArray[i]); +// std::vector blocks; +// blocks.reserve(4096); +// { +// auto begin = std::chrono::steady_clock::now(); +// int bitPos = 0; +// unsigned short t = 0; +// for (size_t i = 0; i < m_dataBlocksLen; i++) { +// for (int j = 0; j < 8; j++) { +// t |= (m_dataBlocks[i] & 0x01) ? 0x80 : 0x00; +// t >>= 1; +// m_dataBlocks[i] >>= 1; +// bitPos++; +// if (bitPos >= m_bitsPerBlock) { +// bitPos = 0; +// t >>= m_bitsPerBlock - 1; +// blocks.push_back(t); +// t = 0; +// } +// } +// } +// auto end = std::chrono::steady_clock::now(); +// std::chrono::duration time = end - begin; +// totalParsingTime += time.count(); +// } +// std::vector light; +// light.reserve(4096); +// for (int i = 0; i < 2048; i++) { +// byte t = m_dataLight[i]; +// byte first = t & 0b11110000; +// byte second = t >> 4; +// light.push_back(first); +// light.push_back(second); +// } +// for (int i = 0; i < 4096; i++) { +// unsigned short blockId = m_palette.size() > 0 ? m_palette[blocks[i]] : blocks[i]; +// Block block(blockId >> 4, blockId & 0xF); +// m_blocks.push_back(block); +// } +// delete[] m_dataBlocks; +// m_dataBlocksLen = 0; +// m_dataBlocks = nullptr; +// delete[] m_dataLight; +// m_dataLight = nullptr; +// delete[] m_dataSkyLight; +// m_dataSkyLight = nullptr; +// +// parseWaiter.notify_all(); +//} + +Section::Section(PackedSection data) +{ + worldPosition = data.position; + + long long *longArray = reinterpret_cast(data.blocks.data()); + for (size_t i = 0; i < data.blocks.size() / 8; i++) + endswap(&longArray[i]); + std::vector blocks; + blocks.reserve(4096); + { + auto begin = std::chrono::steady_clock::now(); + int bitPos = 0; + unsigned short t = 0; + for (size_t i = 0; i < data.blocks.size(); i++) { + for (int j = 0; j < 8; j++) { + t |= (data.blocks.data()[i] & 0x01) ? 0x80 : 0x00; + t >>= 1; + data.blocks.data()[i] >>= 1; + bitPos++; + if (bitPos >= data.bitsPerBlock) { + bitPos = 0; + t >>= data.bitsPerBlock - 1; + blocks.push_back(t); + t = 0; + } + } + } + auto end = std::chrono::steady_clock::now(); + std::chrono::duration time = end - begin; + totalParsingTime += time.count(); + } + std::vector light; + light.reserve(4096); + for (int i = 0; i < 2048; i++) { + byte t = data.light[i]; + byte first = t & 0b11110000; + byte second = t >> 4; + light.push_back(first); + light.push_back(second); + } + for (int i = 0; i < 4096; i++) { + unsigned short blockId = data.palette.size() > 0 ? data.palette[blocks[i]] : blocks[i]; + Block block(blockId >> 4, blockId & 0xF); + this->blocks.push_back(block); + } } Section &Section::operator=(Section other) { @@ -101,47 +124,42 @@ Section &Section::operator=(Section other) { void swap(Section &a, Section &b) { using std::swap; - swap(a.m_dataBlocksLen, b.m_dataBlocksLen); - swap(a.m_dataBlocks, b.m_dataBlocks); - swap(a.m_dataLight, b.m_dataLight); - swap(a.m_dataSkyLight, b.m_dataSkyLight); - swap(a.m_blocks, b.m_blocks); - swap(a.m_palette, b.m_palette); - swap(a.m_bitsPerBlock, b.m_bitsPerBlock); + swap(a.blocks, b.blocks); } Section::Section(const Section &other) { - worldPosition = other.worldPosition; - m_dataBlocksLen = other.m_dataBlocksLen; - if (other.m_blocks.empty()) { - m_dataBlocks = new byte[m_dataBlocksLen]; - std::copy(other.m_dataBlocks, other.m_dataBlocks + m_dataBlocksLen, m_dataBlocks); - - m_dataLight = new byte[2048]; - std::copy(other.m_dataLight, other.m_dataLight + 2048, m_dataLight); - - if (other.m_dataSkyLight) { - m_dataSkyLight = new byte[2048]; - std::copy(other.m_dataSkyLight, other.m_dataSkyLight + 2048, m_dataSkyLight); - } - } else { - std::copy(other.m_blocks.begin(), other.m_blocks.end(), std::back_inserter(m_blocks)); - } - - m_palette = other.m_palette; - m_bitsPerBlock = other.m_bitsPerBlock; + worldPosition = other.worldPosition; + this->blocks = other.blocks; + //std::copy(other.blocks.begin(), other.blocks.end(), std::back_inserter(blocks)); } -Vector Section::GetPosition() { +Vector Section::GetPosition() const { return worldPosition; } -size_t Section::GetHash() { - if (m_blocks.empty()) return 0; +size_t Section::GetHash() const { + if (blocks.empty()) return 0; - unsigned char *from = reinterpret_cast(m_blocks.data()); - size_t length = m_blocks.size() * sizeof(Block); + const unsigned char *from = reinterpret_cast(blocks.data()); + size_t length = blocks.size() * sizeof(Block); std::string str(from, from + length); return std::hash{}(str); -} \ No newline at end of file +} + +PackedSection::PackedSection(Vector position, byte * dataBlocks, size_t dataBlocksLength, byte * dataLight, byte * dataSky, byte bitsPerBlock, std::vector palette) +{ + this->position = position; + + this->palette = palette; + + this->bitsPerBlock = bitsPerBlock; + + blocks.assign(dataBlocks, dataBlocks + dataBlocksLength); + + light.assign(dataLight, dataLight + 2048); + + if (dataSky != nullptr) { + sky.assign(dataSky, dataSky + 2048); + } +} -- cgit v1.2.3