Skip to content

Commit ccd5d14

Browse files
committedSep 3, 2021
Add progress bar during map generation
closes #82
1 parent f471554 commit ccd5d14

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed
 

Diff for: ‎TileGenerator.cpp

+41-9
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ TileGenerator::TileGenerator():
112112
m_geomY2(2048),
113113
m_exhaustiveSearch(EXH_AUTO),
114114
m_zoom(1),
115-
m_scales(SCALE_LEFT | SCALE_TOP)
115+
m_scales(SCALE_LEFT | SCALE_TOP),
116+
m_progressMax(0),
117+
m_progressLast(-1)
116118
{
117119
}
118120

@@ -354,11 +356,10 @@ void TileGenerator::openDb(const std::string &input)
354356

355357
// Determine how we're going to traverse the database (heuristic)
356358
if (m_exhaustiveSearch == EXH_AUTO) {
357-
using u64 = uint64_t;
358-
u64 y_range = (m_yMax / 16 + 1) - (m_yMin / 16);
359-
u64 blocks = (u64)(m_geomX2 - m_geomX) * y_range * (u64)(m_geomY2 - m_geomY);
359+
size_t y_range = (m_yMax / 16 + 1) - (m_yMin / 16);
360+
size_t blocks = (m_geomX2 - m_geomX) * y_range * (m_geomY2 - m_geomY);
360361
#ifndef NDEBUG
361-
std::cout << "Heuristic parameters:"
362+
std::cerr << "Heuristic parameters:"
362363
<< " preferRangeQueries()=" << m_db->preferRangeQueries()
363364
<< " y_range=" << y_range << " blocks=" << blocks << std::endl;
364365
#endif
@@ -415,11 +416,12 @@ void TileGenerator::loadBlocks()
415416
m_positions[pos.z].emplace(pos.x);
416417
}
417418

418-
#ifndef NDEBUG
419419
int count = 0;
420420
for (const auto &it : m_positions)
421421
count += it.second.size();
422-
std::cout << "Loaded " << count
422+
m_progressMax = count;
423+
#ifndef NDEBUG
424+
std::cerr << "Loaded " << count
423425
<< " positions (across Z: " << m_positions.size() << ") for rendering" << std::endl;
424426
#endif
425427
}
@@ -473,6 +475,7 @@ void TileGenerator::renderMap()
473475
{
474476
BlockDecoder blk;
475477
const int16_t yMax = m_yMax / 16 + 1;
478+
size_t count = 0;
476479

477480
auto renderSingle = [&] (int16_t xPos, int16_t zPos, BlockList &blockStack) {
478481
m_readPixels.reset();
@@ -519,6 +522,7 @@ void TileGenerator::renderMap()
519522
blockStack.sort();
520523

521524
renderSingle(xPos, zPos, blockStack);
525+
reportProgress(count++);
522526
}
523527
postRenderRow(zPos);
524528
}
@@ -543,17 +547,21 @@ void TileGenerator::renderMap()
543547
blockStack.sort();
544548

545549
renderSingle(xPos, zPos, blockStack);
550+
reportProgress(count++);
546551
}
547552
postRenderRow(zPos);
548553
}
549554
} else if (m_exhaustiveSearch == EXH_FULL) {
555+
const size_t span_y = yMax - (m_yMin / 16);
556+
m_progressMax = (m_geomX2 - m_geomX) * span_y * (m_geomY2 - m_geomY);
550557
#ifndef NDEBUG
551558
std::cerr << "Exhaustively searching "
552-
<< (m_geomX2 - m_geomX) << "x" << (yMax - (m_yMin / 16)) << "x"
559+
<< (m_geomX2 - m_geomX) << "x" << span_y << "x"
553560
<< (m_geomY2 - m_geomY) << " blocks" << std::endl;
554561
#endif
562+
555563
std::vector<BlockPos> positions;
556-
positions.reserve(yMax - (m_yMin / 16));
564+
positions.reserve(span_y);
557565
for (int16_t zPos = m_geomY2 - 1; zPos >= m_geomY; zPos--) {
558566
for (int16_t xPos = m_geomX2 - 1; xPos >= m_geomX; xPos--) {
559567
positions.clear();
@@ -565,10 +573,13 @@ void TileGenerator::renderMap()
565573
blockStack.sort();
566574

567575
renderSingle(xPos, zPos, blockStack);
576+
reportProgress(count++);
568577
}
569578
postRenderRow(zPos);
570579
}
571580
}
581+
582+
reportProgress(m_progressMax);
572583
}
573584

574585
void TileGenerator::renderMapBlock(const BlockDecoder &blk, const BlockPos &pos)
@@ -801,6 +812,27 @@ void TileGenerator::printUnknown()
801812
std::cerr << "\t" << node << std::endl;
802813
}
803814

815+
void TileGenerator::reportProgress(size_t count)
816+
{
817+
if (!m_progressMax)
818+
return;
819+
int percent = count / static_cast<float>(m_progressMax) * 100;
820+
if (percent == m_progressLast)
821+
return;
822+
m_progressLast = percent;
823+
824+
// Print a nice-looking ASCII progress bar
825+
char bar[51] = {0};
826+
memset(bar, ' ', 50);
827+
int i = 0, j = percent;
828+
for (; j >= 2; j -= 2)
829+
bar[i++] = '=';
830+
if (j)
831+
bar[i++] = '-';
832+
std::cout << "[" << bar << "] " << percent << "% " << (percent == 100 ? "\n" : "\r");
833+
std::cout.flush();
834+
}
835+
804836
inline int TileGenerator::getImageX(int val, bool absolute) const
805837
{
806838
if (absolute)

Diff for: ‎include/TileGenerator.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef TILEGENERATOR_HEADER
2-
#define TILEGENERATOR_HEADER
1+
#pragma once
32

43
#include <iostream>
54
#include <map>
@@ -110,6 +109,7 @@ class TileGenerator
110109
void renderPlayers(const std::string &inputPath);
111110
void writeImage(const std::string &output);
112111
void printUnknown();
112+
void reportProgress(size_t count);
113113
int getImageX(int val, bool absolute=false) const;
114114
int getImageY(int val, bool absolute=false) const;
115115
void setZoomed(int x, int y, Color color);
@@ -144,7 +144,7 @@ class TileGenerator
144144
int16_t m_geomY; /* Y in terms of rendered image, Z in the world */
145145
int16_t m_geomX2;
146146
int16_t m_geomY2;
147-
/* */
147+
148148
int m_mapWidth;
149149
int m_mapHeight;
150150
int m_exhaustiveSearch;
@@ -158,6 +158,7 @@ class TileGenerator
158158

159159
int m_zoom;
160160
uint m_scales;
161-
}; // class TileGenerator
162161

163-
#endif // TILEGENERATOR_HEADER
162+
size_t m_progressMax;
163+
int m_progressLast; // percentage
164+
}; // class TileGenerator

0 commit comments

Comments
 (0)
Please sign in to comment.