From 6e653648b53c13f657eaf6825e1492519ebd354c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bend=C3=ADk?= Date: Fri, 24 Aug 2012 09:46:14 +0200 Subject: [PATCH] Added reading of data. --- TileGenerator.cpp | 81 +++++++++++++++++++++++++++++++++-------------- TileGenerator.h | 10 ++++-- 2 files changed, 65 insertions(+), 26 deletions(-) diff --git a/TileGenerator.cpp b/TileGenerator.cpp index e8de2d3..4f6495f 100644 --- a/TileGenerator.cpp +++ b/TileGenerator.cpp @@ -11,11 +11,16 @@ #include #include #include +#include #include "TileGenerator.h" using namespace std; TileGenerator::TileGenerator(): + m_bgColor(255, 255, 255), + m_scaleColor(0, 0, 0), + m_originColor(255, 0, 0), + m_playerColor(255, 0, 0), m_drawOrigin(false), m_drawPlayers(false), m_drawScale(false), @@ -27,10 +32,6 @@ TileGenerator::TileGenerator(): m_zMin(0), m_zMax(0) { - m_bgColor = {255, 255, 255}; - m_scaleColor = {0, 0, 0}; - m_originColor = {255, 0, 0}; - m_playerColor = {255, 0, 0}; } TileGenerator::~TileGenerator() @@ -189,7 +190,7 @@ void TileGenerator::loadBlocks() } } -inline BlockPos TileGenerator::decodeBlockPos(sqlite3_int64 blockId) +inline BlockPos TileGenerator::decodeBlockPos(sqlite3_int64 blockId) const { BlockPos pos; pos.x = unsignedToSigned(blockId % 4096, 2048); @@ -200,12 +201,12 @@ inline BlockPos TileGenerator::decodeBlockPos(sqlite3_int64 blockId) return pos; } -inline sqlite3_int64 TileGenerator::encodeBlockPos(int x, int y, int z) +inline sqlite3_int64 TileGenerator::encodeBlockPos(int x, int y, int z) const { return sqlite3_int64(z) * 16777216l + sqlite3_int64(y) * 4096l + sqlite3_int64(x); } -inline int TileGenerator::unsignedToSigned(long i, long max_positive) +inline int TileGenerator::unsignedToSigned(long i, long max_positive) const { if (i < max_positive) { return i; @@ -227,35 +228,69 @@ void TileGenerator::createImage() void TileGenerator::renderMap() { sqlite3_stmt *statement; - //string sql = "SELECT pos, data FROM blocks WHERE pos >= ? AND pos <= ? AND (pos - ?) % 4096 = 0"; string sql = "SELECT pos, data FROM blocks WHERE pos >= ? AND pos <= ?"; - std::list zlist; - for (std::list >::iterator position = m_positions.begin(); position != m_positions.end(); ++position) { - zlist.push_back(position->second); - } - zlist.sort(); - zlist.unique(); if (sqlite3_prepare_v2(m_db, sql.c_str(), sql.length(), &statement, 0) != SQLITE_OK) { throw DbError(); } + std::list zlist = getZValueList(); for (std::list::iterator position = zlist.begin(); position != zlist.end(); ++position) { int zPos = *position; + getBlocksOnZ(zPos, statement); + } +} - sqlite3_int64 psMin = encodeBlockPos(-2048, -2048, zPos); - sqlite3_int64 psMax = encodeBlockPos( 2047, 2047, zPos); - sqlite3_bind_int64(statement, 1, psMin); - sqlite3_bind_int64(statement, 2, psMax); - int result = 0; - while (true) { - result = sqlite3_step(statement); - if(result == SQLITE_ROW) { +inline std::list TileGenerator::getZValueList() const +{ + std::list zlist; + for (std::list >::const_iterator position = m_positions.begin(); position != m_positions.end(); ++position) { + zlist.push_back(position->second); + } + zlist.sort(); + zlist.unique(); + return zlist; +} + +void TileGenerator::getBlocksOnZ(int zPos, sqlite3_stmt *statement) const +{ + map > > blocks; + + sqlite3_int64 psMin = encodeBlockPos(-2048, -2048, zPos); + sqlite3_int64 psMax = encodeBlockPos( 2047, 2047, zPos); + std::stringstream minStream; + std::stringstream maxStream; + minStream << psMin; + maxStream << psMax; + string minStr = minStream.str(); + string maxStr = maxStream.str(); + sqlite3_bind_text(statement, 1, minStr.c_str(), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(statement, 2, maxStr.c_str(), -1, SQLITE_TRANSIENT); + int result = 0; + while (true) { + result = sqlite3_step(statement); + if(result == SQLITE_ROW) { + sqlite3_int64 blocknum = sqlite3_column_int64(statement, 0); + const void *data = sqlite3_column_blob(statement, 1); + + uint8_t version = static_cast(data)[0]; + uint8_t flags = static_cast(data)[1]; + if (version >= 22) { + data += 4; } else { - break; + data += 2; } + + + BlockPos pos = decodeBlockPos(blocknum); + blocks[pos.x].push_back(pair (pos, "")); + + } + else { + break; } } + sqlite3_reset(statement); } void TileGenerator::writeImage(const std::string &output) diff --git a/TileGenerator.h b/TileGenerator.h index f85fd8d..33a46eb 100644 --- a/TileGenerator.h +++ b/TileGenerator.h @@ -18,6 +18,8 @@ #include struct Color { + Color() {}; + Color(uint8_t r, uint8_t g, uint8_t b): r(r), g(g), b(b) {}; uint8_t r; uint8_t g; uint8_t b; @@ -58,11 +60,13 @@ public: private: void openDb(const std::string &input); void loadBlocks(); - BlockPos decodeBlockPos(sqlite3_int64 blockId); - sqlite3_int64 encodeBlockPos(int x, int y, int z); - int unsignedToSigned(long i, long max_positive); + BlockPos decodeBlockPos(sqlite3_int64 blockId) const; + sqlite3_int64 encodeBlockPos(int x, int y, int z) const; + int unsignedToSigned(long i, long max_positive) const; void createImage(); void renderMap(); + std::list getZValueList() const; + void getBlocksOnZ(int zPos, sqlite3_stmt *statement) const; void writeImage(const std::string &output); private: