Skip to content

Commit

Permalink
Some more code modernization
Browse files Browse the repository at this point in the history
also a few small performance improvements
  • Loading branch information
sfan5 committed May 8, 2020
1 parent 2979dc5 commit 8e83ce6
Show file tree
Hide file tree
Showing 21 changed files with 149 additions and 199 deletions.
19 changes: 11 additions & 8 deletions BlockDecoder.cpp
Expand Up @@ -11,7 +11,8 @@ static inline uint16_t readU16(const unsigned char *data)
return data[0] << 8 | data[1];
}

static int readBlockContent(const unsigned char *mapData, u8 contentWidth, unsigned int datapos)
static inline uint16_t readBlockContent(const unsigned char *mapData,
u8 contentWidth, unsigned int datapos)
{
if (contentWidth == 2) {
size_t index = datapos << 1;
Expand All @@ -21,7 +22,7 @@ static int readBlockContent(const unsigned char *mapData, u8 contentWidth, unsig
if (param <= 0x7f)
return param;
else
return (int(param) << 4) | (int(mapData[datapos + 0x2000]) >> 4);
return (param << 4) | (mapData[datapos + 0x2000] >> 4);
}
}

Expand Down Expand Up @@ -125,10 +126,10 @@ void BlockDecoder::decode(const ustring &datastr)

// Node timers
if (version >= 25) {
dataOffset++;
uint8_t timerLength = data[dataOffset++];
uint16_t numTimers = readU16(data + dataOffset);
dataOffset += 2;
dataOffset += numTimers * 10;
dataOffset += numTimers * timerLength;
}
}

Expand All @@ -138,16 +139,18 @@ bool BlockDecoder::isEmpty() const
return m_nameMap.empty();
}

std::string BlockDecoder::getNode(u8 x, u8 y, u8 z) const
const static std::string empty;

const std::string &BlockDecoder::getNode(u8 x, u8 y, u8 z) const
{
unsigned int position = x + (y << 4) + (z << 8);
int content = readBlockContent(m_mapData.c_str(), m_contentWidth, position);
uint16_t content = readBlockContent(m_mapData.c_str(), m_contentWidth, position);
if (content == m_blockAirId || content == m_blockIgnoreId)
return "";
return empty;
NameMap::const_iterator it = m_nameMap.find(content);
if (it == m_nameMap.end()) {
std::cerr << "Skipping node with invalid ID." << std::endl;
return "";
return empty;
}
return it->second;
}
5 changes: 4 additions & 1 deletion Image.cpp
Expand Up @@ -17,7 +17,7 @@

// ARGB but with inverted alpha

static inline int color2int(Color c)
static inline int color2int(const Color &c)
{
u8 a = (255 - c.a) * gdAlphaMax / 255;
return (a << 24) | (c.r << 16) | (c.g << 8) | c.b;
Expand All @@ -35,6 +35,7 @@ static inline Color int2color(int c)
return c2;
}

#ifndef NDEBUG
static inline void check_bounds(int x, int y, int width, int height)
{
if(x < 0 || x >= width) {
Expand All @@ -50,11 +51,13 @@ static inline void check_bounds(int x, int y, int width, int height)
throw std::out_of_range(oss.str());
}
}
#endif


Image::Image(int width, int height) :
m_width(width), m_height(height), m_image(NULL)
{
SIZECHECK(0, 0);
m_image = gdImageCreateTrueColor(m_width, m_height);
}

Expand Down
10 changes: 1 addition & 9 deletions PixelAttributes.cpp
@@ -1,14 +1,6 @@
/*
* =====================================================================
* Version: 1.0
* Created: 25.08.2012 10:55:27
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
#include <cstring>

#include "PixelAttributes.h"
#include <cstring>

PixelAttributes::PixelAttributes():
m_width(0)
Expand Down
20 changes: 9 additions & 11 deletions PlayerAttributes.cpp
Expand Up @@ -9,11 +9,9 @@
#include "PlayerAttributes.h"
#include "util.h"

using namespace std;

PlayerAttributes::PlayerAttributes(const std::string &worldDir)
{
std::ifstream ifs((worldDir + "world.mt").c_str());
std::ifstream ifs(worldDir + "world.mt");
if (!ifs.good())
throw std::runtime_error("Failed to read world.mt");
std::string backend = read_setting_default("player_backend", ifs, "files");
Expand All @@ -39,18 +37,18 @@ void PlayerAttributes::readFiles(const std::string &playersPath)
if (ent->d_name[0] == '.')
continue;

string path = playersPath + PATH_SEPARATOR + ent->d_name;
ifstream in(path.c_str());
std::string path = playersPath + PATH_SEPARATOR + ent->d_name;
std::ifstream in(path);
if(!in.good())
continue;

string name, position;
std::string name, position;
name = read_setting("name", in);
in.seekg(0);
position = read_setting("position", in);

Player player;
istringstream iss(position);
std::istringstream iss(position);
char tmp;
iss >> tmp; // '('
iss >> player.x;
Expand Down Expand Up @@ -121,13 +119,13 @@ void PlayerAttributes::readSqlite(const std::string &db_name)

/**********/

PlayerAttributes::Players::iterator PlayerAttributes::begin()
PlayerAttributes::Players::const_iterator PlayerAttributes::begin() const
{
return m_players.begin();
return m_players.cbegin();
}

PlayerAttributes::Players::iterator PlayerAttributes::end()
PlayerAttributes::Players::const_iterator PlayerAttributes::end() const
{
return m_players.end();
return m_players.cend();
}

12 changes: 7 additions & 5 deletions README.rst
Expand Up @@ -4,6 +4,8 @@ Minetest Mapper C++
.. image:: https://travis-ci.org/minetest/minetestmapper.svg?branch=master
:target: https://travis-ci.org/minetest/minetestmapper

Minetestmapper generates an overview image from a Minetest map.

A port of minetestmapper.py to C++ from https://github.com/minetest/minetest/tree/master/util.
This version is both faster and provides more features than the now deprecated Python script.

Expand All @@ -12,14 +14,14 @@ Requirements

* libgd
* sqlite3
* LevelDB (optional, set ENABLE_LEVELDB=1 in CMake to enable)
* hiredis library (optional, set ENABLE_REDIS=1 in CMake to enable)
* Postgres libraries (optional, set ENABLE_POSTGRES=1 in CMake to enable)
* LevelDB (optional)
* hiredis (optional)
* Postgres libraries (optional)

e.g. on Debian:
^^^^^^^^^^^^^^^

sudo apt-get install libgd-dev libsqlite3-dev libleveldb-dev libhiredis-dev libpq-dev
sudo apt install libgd-dev libsqlite3-dev libleveldb-dev libhiredis-dev libpq-dev

Windows
^^^^^^^
Expand All @@ -37,7 +39,7 @@ Compilation
::

cmake . -DENABLE_LEVELDB=1
make -j2
make -j4

Usage
-----
Expand Down

0 comments on commit 8e83ce6

Please sign in to comment.