summaryrefslogtreecommitdiffstats
path: root/src/world/Section.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/world/Section.cpp80
1 files changed, 43 insertions, 37 deletions
diff --git a/src/world/Section.cpp b/src/world/Section.cpp
index a338e49..279d2b2 100644
--- a/src/world/Section.cpp
+++ b/src/world/Section.cpp
@@ -33,20 +33,13 @@ Section::~Section() {
}
Block &Section::GetBlock(Vector pos) {
- if (m_dataBlocks != nullptr) {
- std::mutex parseMutex;
- std::unique_lock<std::mutex> parseLocker(parseMutex);
- parseWaiter.wait(parseLocker);
- while (m_dataBlocks != nullptr) {
- parseWaiter.wait(parseLocker);
- }
- LOG(WARNING) << "Successfully waited for block render!";
- }
return m_blocks[pos.GetY() * 256 + pos.GetZ() * 16 + pos.GetX()];
}
+double totalParsingTime = 0;
+
void Section::Parse() {
- if (m_dataBlocks == nullptr)
+ if (!m_blocks.empty())
return;
long long *longArray = reinterpret_cast<long long *>(m_dataBlocks);
@@ -54,23 +47,28 @@ void Section::Parse() {
endswap(&longArray[i]);
std::vector<unsigned short> blocks;
blocks.reserve(4096);
- 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 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<double, std::milli> time = end - begin;
+ totalParsingTime += time.count();
+ }
std::vector<byte> light;
light.reserve(4096);
for (int i = 0; i < 2048; i++) {
@@ -85,9 +83,6 @@ void Section::Parse() {
Block block(blockId >> 4, blockId & 0xF);
m_blocks.push_back(block);
}
- if ((light.size() + blocks.size()) / 2 != 4096) {
- throw 118;
- }
delete[] m_dataBlocks;
m_dataBlocksLen = 0;
m_dataBlocks = nullptr;
@@ -118,15 +113,19 @@ void swap(Section &a, Section &b) {
Section::Section(const Section &other) {
worldPosition = other.worldPosition;
m_dataBlocksLen = other.m_dataBlocksLen;
- m_dataBlocks = new byte[m_dataBlocksLen];
- std::copy(other.m_dataBlocks, other.m_dataBlocks + m_dataBlocksLen, m_dataBlocks);
+ 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);
+ 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);
+ 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;
@@ -136,3 +135,10 @@ Section::Section(const Section &other) {
Vector Section::GetPosition() {
return worldPosition;
}
+
+size_t Section::GetHash() {
+ if (m_blocks.empty())
+ return 0;
+ std::string str((unsigned char*)m_blocks.data(), (unsigned char*)m_blocks.data() + m_blocks.size() * sizeof(Block));
+ return std::hash<std::string>{}(str);
+} \ No newline at end of file