Added selection of blocks.

This commit is contained in:
Miroslav Bendík 2012-08-23 15:35:00 +02:00
parent b7f69cc3df
commit ace6f3b5b5
2 changed files with 42 additions and 0 deletions

@ -137,6 +137,7 @@ void TileGenerator::generate(const std::string &input, const std::string &output
openDb(input);
loadBlocks();
createImage();
renderMap();
writeImage(output);
}
@ -174,11 +175,14 @@ void TileGenerator::loadBlocks()
if (pos.z > m_zMax) {
m_zMax = pos.z;
}
m_positions.push_back(std::pair<int, int>(pos.x, pos.z));
}
else {
break;
}
}
m_positions.sort();
m_positions.unique();
}
else {
throw DbError();
@ -196,6 +200,11 @@ inline BlockPos TileGenerator::decodeBlockPos(sqlite3_int64 blockId)
return pos;
}
inline sqlite3_int64 TileGenerator::encodeBlockPos(int x, int y, int z)
{
return sqlite3_int64(z) * 16777216l + sqlite3_int64(y) * 4096l + sqlite3_int64(x);
}
inline int TileGenerator::unsignedToSigned(long i, long max_positive)
{
if (i < max_positive) {
@ -215,6 +224,35 @@ void TileGenerator::createImage()
gdImageColorAllocate(m_image, m_bgColor.r, m_bgColor.g, m_bgColor.b);
}
void TileGenerator::renderMap()
{
sqlite3_stmt *statement;
string sql = "SELECT pos FROM blocks WHERE pos >= ? AND pos <= ? AND (pos - ?) % 4096 = 0";
if (sqlite3_prepare_v2(m_db, sql.c_str(), sql.length(), &statement, 0) != SQLITE_OK) {
throw DbError();
}
for (auto position = m_positions.begin(); position != m_positions.end(); ++position) {
int xPos = position->first;
int zPos = position->second;
sqlite3_int64 psMin = encodeBlockPos(xPos, -2048, zPos);
sqlite3_int64 psMax = encodeBlockPos(xPos, 2047, zPos);
sqlite3_bind_int64(statement, 1, psMin);
sqlite3_bind_int64(statement, 2, psMax);
sqlite3_bind_int64(statement, 3, psMin);
int result = 0;
while (true) {
result = sqlite3_step(statement);
if(result == SQLITE_ROW) {
}
else {
break;
}
}
}
}
void TileGenerator::writeImage(const std::string &output)
{
FILE *out;

@ -11,6 +11,7 @@
#define TILEGENERATOR_H_JJNUCARH
#include <gd.h>
#include <list>
#include <map>
#include <sqlite3.h>
#include <stdint.h>
@ -58,8 +59,10 @@ 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);
void createImage();
void renderMap();
void writeImage(const std::string &output);
private:
@ -80,6 +83,7 @@ private:
int m_zMax;
int m_imgWidth;
int m_imgHeight;
std::list<std::pair<int, int> > m_positions;
ColorMap m_colors;
static const int SectorXMin = -1500/16;