Skip to content

Commit

Permalink
C++11 code modernization
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Mar 26, 2020
1 parent 1d678ff commit 9096f70
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 66 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -13,6 +13,7 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
endif()

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g2 -Wall")

Expand Down
13 changes: 5 additions & 8 deletions PixelAttributes.cpp
Expand Up @@ -7,8 +7,6 @@
* =====================================================================
*/

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

using namespace std;
Expand All @@ -17,7 +15,7 @@ PixelAttributes::PixelAttributes():
m_width(0)
{
for (size_t i = 0; i < LineCount; ++i) {
m_pixelAttributes[i] = 0;
m_pixelAttributes[i] = nullptr;
}
}

Expand All @@ -37,19 +35,18 @@ void PixelAttributes::setWidth(int width)

void PixelAttributes::scroll()
{
size_t lineLength = m_width * sizeof(PixelAttribute);
memcpy(m_pixelAttributes[FirstLine], m_pixelAttributes[LastLine], lineLength);
*m_pixelAttributes[FirstLine] = *m_pixelAttributes[LastLine];
for (size_t i = 1; i < LineCount - 1; ++i) {
memcpy(m_pixelAttributes[i], m_pixelAttributes[EmptyLine], lineLength);
*m_pixelAttributes[i] = *m_pixelAttributes[EmptyLine];
}
}

void PixelAttributes::freeAttributes()
{
for (size_t i = 0; i < LineCount; ++i) {
if (m_pixelAttributes[i] != 0) {
if (m_pixelAttributes[i] != nullptr) {
delete[] m_pixelAttributes[i];
m_pixelAttributes[i] = 0;
m_pixelAttributes[i] = nullptr;
}
}
}
Expand Down
54 changes: 27 additions & 27 deletions TileGenerator.cpp
Expand Up @@ -270,7 +270,7 @@ void TileGenerator::parseColorsStream(std::istream &in)
{
char line[128];
while (in.good()) {
in.getline(line, 128);
in.getline(line, sizeof(line));

for(char *p = line; *p; p++) {
if(*p != '#')
Expand All @@ -281,7 +281,7 @@ void TileGenerator::parseColorsStream(std::istream &in)
if(strlen(line) == 0)
continue;

char name[64 + 1];
char name[64 + 1] = {0};
unsigned int r, g, b, a, t;
a = 255;
t = 0;
Expand All @@ -291,7 +291,7 @@ void TileGenerator::parseColorsStream(std::istream &in)
continue;
}

ColorEntry color = ColorEntry(r, g, b, a, t);
ColorEntry color(r, g, b, a, t);
m_colorMap[name] = color;
}
}
Expand Down Expand Up @@ -352,7 +352,7 @@ void TileGenerator::loadBlocks()
m_zMin = pos.z;
if (pos.z > m_zMax)
m_zMax = pos.z;
m_positions.push_back(std::pair<int, int>(pos.x, pos.z));
m_positions.push_back(std::make_pair(pos.x, pos.z));
}
m_positions.sort();
m_positions.unique();
Expand Down Expand Up @@ -393,24 +393,24 @@ void TileGenerator::createImage()
image_height = (m_mapHeight * m_zoom) + m_yBorder;
image_height += (m_scales & SCALE_BOTTOM) ? scale_d : 0;

if(image_width > 4096 || image_height > 4096)
if(image_width > 4096 || image_height > 4096) {
std::cerr << "Warning: The width or height of the image to be created exceeds 4096 pixels!"
<< " (Dimensions: " << image_width << "x" << image_height << ")"
<< std::endl;
}
m_image = new Image(image_width, image_height);
m_image->drawFilledRect(0, 0, image_width, image_height, m_bgColor); // Background
}

void TileGenerator::renderMap()
{
BlockDecoder blk;
std::list<int> zlist = getZValueList();
for (std::list<int>::iterator zPosition = zlist.begin(); zPosition != zlist.end(); ++zPosition) {
int zPos = *zPosition;
std::list<int16_t> zlist = getZValueList();
for (int16_t zPos : zlist) {
std::map<int16_t, BlockList> blocks;
m_db->getBlocksOnZ(blocks, zPos);
for (std::list<std::pair<int, int> >::const_iterator position = m_positions.begin(); position != m_positions.end(); ++position) {
if (position->second != zPos)
for (const auto position : m_positions) {
if (position.second != zPos)
continue;

m_readPixels.reset();
Expand All @@ -423,14 +423,14 @@ void TileGenerator::renderMap()
}
}

int xPos = position->first;
int16_t xPos = position.first;
blocks[xPos].sort();
const BlockList &blockStack = blocks[xPos];
for (BlockList::const_iterator it = blockStack.begin(); it != blockStack.end(); ++it) {
const BlockPos &pos = it->first;
for (const auto &it : blockStack) {
const BlockPos &pos = it.first;

blk.reset();
blk.decode(it->second);
blk.decode(it.second);
if (blk.isEmpty())
continue;
renderMapBlock(blk, pos);
Expand Down Expand Up @@ -636,26 +636,26 @@ void TileGenerator::renderOrigin()
void TileGenerator::renderPlayers(const std::string &inputPath)
{
PlayerAttributes players(inputPath);
for (PlayerAttributes::Players::iterator player = players.begin(); player != players.end(); ++player) {
if (player->x < m_xMin * 16 || player->x > m_xMax * 16 ||
player->z < m_zMin * 16 || player->z > m_zMax * 16)
for (auto &player : players) {
if (player.x < m_xMin * 16 || player.x > m_xMax * 16 ||
player.z < m_zMin * 16 || player.z > m_zMax * 16)
continue;
if (player->y < m_yMin || player->y > m_yMax)
if (player.y < m_yMin || player.y > m_yMax)
continue;
int imageX = getImageX(player->x, true),
imageY = getImageY(player->z, true);
int imageX = getImageX(player.x, true),
imageY = getImageY(player.z, true);

m_image->drawFilledRect(imageX - 1, imageY, 3, 1, m_playerColor);
m_image->drawFilledRect(imageX, imageY - 1, 1, 3, m_playerColor);
m_image->drawText(imageX + 2, imageY, player->name, m_playerColor);
m_image->drawText(imageX + 2, imageY, player.name, m_playerColor);
}
}

inline std::list<int> TileGenerator::getZValueList() const
inline std::list<int16_t> TileGenerator::getZValueList() const
{
std::list<int> zlist;
for (std::list<std::pair<int, int> >::const_iterator position = m_positions.begin(); position != m_positions.end(); ++position)
zlist.push_back(position->second);
std::list<int16_t> zlist;
for (const auto position : m_positions)
zlist.push_back(position.second);
zlist.sort();
zlist.unique();
zlist.reverse();
Expand All @@ -674,8 +674,8 @@ void TileGenerator::printUnknown()
if (m_unknownNodes.size() == 0)
return;
std::cerr << "Unknown nodes:" << std::endl;
for (NameSet::iterator node = m_unknownNodes.begin(); node != m_unknownNodes.end(); ++node)
std::cerr << "\t" << *node << std::endl;
for (const auto &node : m_unknownNodes)
std::cerr << "\t" << node << std::endl;
}

inline int TileGenerator::getImageX(int val, bool absolute) const
Expand Down
8 changes: 4 additions & 4 deletions db-leveldb.cpp
Expand Up @@ -60,13 +60,13 @@ void DBLevelDB::getBlocksOnZ(std::map<int16_t, BlockList> &blocks, int16_t zPos)
std::string datastr;
leveldb::Status status;

for (std::vector<BlockPos>::iterator it = posCache.begin(); it != posCache.end(); ++it) {
if (it->z != zPos) {
for (const auto &it : posCache) {
if (it.z != zPos) {
continue;
}
status = db->Get(leveldb::ReadOptions(), i64tos(encodeBlockPos(*it)), &datastr);
status = db->Get(leveldb::ReadOptions(), i64tos(encodeBlockPos(it)), &datastr);
if (status.ok()) {
Block b(*it, ustring((const unsigned char *) datastr.data(), datastr.size()));
Block b(it, ustring((const unsigned char *) datastr.data(), datastr.size()));
blocks[b.first.x].push_back(b);
}
}
Expand Down
8 changes: 0 additions & 8 deletions include/BlockDecoder.h
@@ -1,11 +1,7 @@
#ifndef BLOCKDECODER_H
#define BLOCKDECODER_H

#if __cplusplus >= 201103L
#include <unordered_map>
#else
#include <map>
#endif

#include "types.h"

Expand All @@ -19,11 +15,7 @@ class BlockDecoder {
std::string getNode(u8 x, u8 y, u8 z) const; // returns "" for air, ignore and invalid nodes

private:
#if __cplusplus >= 201103L
typedef std::unordered_map<int, std::string> NameMap;
#else
typedef std::map<int, std::string> NameMap;
#endif
NameMap m_nameMap;
int m_blockAirId;
int m_blockIgnoreId;
Expand Down
14 changes: 2 additions & 12 deletions include/TileGenerator.h
Expand Up @@ -4,13 +4,8 @@
#include <iosfwd>
#include <list>
#include <config.h>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#else
#include <map>
#include <set>
#endif
#include <stdint.h>
#include <string>

Expand Down Expand Up @@ -60,13 +55,8 @@ struct BitmapThing { // 16x16 bitmap
class TileGenerator
{
private:
#if __cplusplus >= 201103L
typedef std::unordered_map<std::string, ColorEntry> ColorMap;
typedef std::unordered_set<std::string> NameSet;
#else
typedef std::map<std::string, ColorEntry> ColorMap;
typedef std::set<std::string> NameSet;
#endif

public:
TileGenerator();
Expand Down Expand Up @@ -98,7 +88,7 @@ class TileGenerator
void loadBlocks();
void createImage();
void renderMap();
std::list<int> getZValueList() const;
std::list<int16_t> getZValueList() const;
void renderMapBlock(const BlockDecoder &blk, const BlockPos &pos);
void renderMapBlockBottom(const BlockPos &pos);
void renderShading(int zPos);
Expand Down Expand Up @@ -140,7 +130,7 @@ class TileGenerator
int m_geomY2;
int m_mapWidth;
int m_mapHeight;
std::list<std::pair<int, int> > m_positions;
std::list<std::pair<int16_t, int16_t>> m_positions;
ColorMap m_colorMap;
BitmapThing m_readPixels;
BitmapThing m_readInfo;
Expand Down
3 changes: 1 addition & 2 deletions include/db.h
Expand Up @@ -10,8 +10,7 @@
#include "types.h"


class BlockPos {
public:
struct BlockPos {
int16_t x;
int16_t y;
int16_t z;
Expand Down
11 changes: 6 additions & 5 deletions mapper.cpp
Expand Up @@ -10,7 +10,7 @@
#include "cmake_config.h"
#include "TileGenerator.h"

void usage()
static void usage()
{
const char *usage_text = "minetestmapper [options]\n"
" -i/--input <world_path>\n"
Expand All @@ -37,13 +37,13 @@ void usage()
std::cout << usage_text;
}

bool file_exists(const std::string &path)
static bool file_exists(const std::string &path)
{
std::ifstream ifs(path.c_str());
return ifs.is_open();
}

std::string search_colors(const std::string &worldpath)
static std::string search_colors(const std::string &worldpath)
{
if(file_exists(worldpath + "/colors.txt"))
return worldpath + "/colors.txt";
Expand All @@ -57,7 +57,8 @@ std::string search_colors(const std::string &worldpath)
}
#endif

if(!(SHAREDIR[0] == '.' || SHAREDIR[0] == '\0') && file_exists(SHAREDIR "/colors.txt"))
constexpr bool sharedir_valid = !(SHAREDIR[0] == '.' || SHAREDIR[0] == '\0');
if(sharedir_valid && file_exists(SHAREDIR "/colors.txt"))
return SHAREDIR "/colors.txt";

std::cerr << "Warning: Falling back to using colors.txt from current directory." << std::endl;
Expand All @@ -66,7 +67,7 @@ std::string search_colors(const std::string &worldpath)

int main(int argc, char *argv[])
{
static struct option long_options[] =
const static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"input", required_argument, 0, 'i'},
Expand Down

0 comments on commit 9096f70

Please sign in to comment.