C++11 code modernization

This commit is contained in:
sfan5 2020-03-26 23:07:27 +01:00
parent 1d678ffa82
commit 9096f70188
8 changed files with 46 additions and 66 deletions

View File

@ -13,6 +13,7 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
endif() endif()
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -DNDEBUG") set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g2 -Wall") set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g2 -Wall")

View File

@ -7,8 +7,6 @@
* ===================================================================== * =====================================================================
*/ */
#include <cstdlib>
#include <cstring>
#include "PixelAttributes.h" #include "PixelAttributes.h"
using namespace std; using namespace std;
@ -17,7 +15,7 @@ PixelAttributes::PixelAttributes():
m_width(0) m_width(0)
{ {
for (size_t i = 0; i < LineCount; ++i) { for (size_t i = 0; i < LineCount; ++i) {
m_pixelAttributes[i] = 0; m_pixelAttributes[i] = nullptr;
} }
} }
@ -37,19 +35,18 @@ void PixelAttributes::setWidth(int width)
void PixelAttributes::scroll() void PixelAttributes::scroll()
{ {
size_t lineLength = m_width * sizeof(PixelAttribute); *m_pixelAttributes[FirstLine] = *m_pixelAttributes[LastLine];
memcpy(m_pixelAttributes[FirstLine], m_pixelAttributes[LastLine], lineLength);
for (size_t i = 1; i < LineCount - 1; ++i) { 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() void PixelAttributes::freeAttributes()
{ {
for (size_t i = 0; i < LineCount; ++i) { for (size_t i = 0; i < LineCount; ++i) {
if (m_pixelAttributes[i] != 0) { if (m_pixelAttributes[i] != nullptr) {
delete[] m_pixelAttributes[i]; delete[] m_pixelAttributes[i];
m_pixelAttributes[i] = 0; m_pixelAttributes[i] = nullptr;
} }
} }
} }

View File

@ -270,7 +270,7 @@ void TileGenerator::parseColorsStream(std::istream &in)
{ {
char line[128]; char line[128];
while (in.good()) { while (in.good()) {
in.getline(line, 128); in.getline(line, sizeof(line));
for(char *p = line; *p; p++) { for(char *p = line; *p; p++) {
if(*p != '#') if(*p != '#')
@ -281,7 +281,7 @@ void TileGenerator::parseColorsStream(std::istream &in)
if(strlen(line) == 0) if(strlen(line) == 0)
continue; continue;
char name[64 + 1]; char name[64 + 1] = {0};
unsigned int r, g, b, a, t; unsigned int r, g, b, a, t;
a = 255; a = 255;
t = 0; t = 0;
@ -291,7 +291,7 @@ void TileGenerator::parseColorsStream(std::istream &in)
continue; continue;
} }
ColorEntry color = ColorEntry(r, g, b, a, t); ColorEntry color(r, g, b, a, t);
m_colorMap[name] = color; m_colorMap[name] = color;
} }
} }
@ -352,7 +352,7 @@ void TileGenerator::loadBlocks()
m_zMin = pos.z; m_zMin = pos.z;
if (pos.z > m_zMax) if (pos.z > m_zMax)
m_zMax = pos.z; 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.sort();
m_positions.unique(); m_positions.unique();
@ -393,10 +393,11 @@ void TileGenerator::createImage()
image_height = (m_mapHeight * m_zoom) + m_yBorder; image_height = (m_mapHeight * m_zoom) + m_yBorder;
image_height += (m_scales & SCALE_BOTTOM) ? scale_d : 0; 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!" std::cerr << "Warning: The width or height of the image to be created exceeds 4096 pixels!"
<< " (Dimensions: " << image_width << "x" << image_height << ")" << " (Dimensions: " << image_width << "x" << image_height << ")"
<< std::endl; << std::endl;
}
m_image = new Image(image_width, image_height); m_image = new Image(image_width, image_height);
m_image->drawFilledRect(0, 0, image_width, image_height, m_bgColor); // Background m_image->drawFilledRect(0, 0, image_width, image_height, m_bgColor); // Background
} }
@ -404,13 +405,12 @@ void TileGenerator::createImage()
void TileGenerator::renderMap() void TileGenerator::renderMap()
{ {
BlockDecoder blk; BlockDecoder blk;
std::list<int> zlist = getZValueList(); std::list<int16_t> zlist = getZValueList();
for (std::list<int>::iterator zPosition = zlist.begin(); zPosition != zlist.end(); ++zPosition) { for (int16_t zPos : zlist) {
int zPos = *zPosition;
std::map<int16_t, BlockList> blocks; std::map<int16_t, BlockList> blocks;
m_db->getBlocksOnZ(blocks, zPos); m_db->getBlocksOnZ(blocks, zPos);
for (std::list<std::pair<int, int> >::const_iterator position = m_positions.begin(); position != m_positions.end(); ++position) { for (const auto position : m_positions) {
if (position->second != zPos) if (position.second != zPos)
continue; continue;
m_readPixels.reset(); m_readPixels.reset();
@ -423,14 +423,14 @@ void TileGenerator::renderMap()
} }
} }
int xPos = position->first; int16_t xPos = position.first;
blocks[xPos].sort(); blocks[xPos].sort();
const BlockList &blockStack = blocks[xPos]; const BlockList &blockStack = blocks[xPos];
for (BlockList::const_iterator it = blockStack.begin(); it != blockStack.end(); ++it) { for (const auto &it : blockStack) {
const BlockPos &pos = it->first; const BlockPos &pos = it.first;
blk.reset(); blk.reset();
blk.decode(it->second); blk.decode(it.second);
if (blk.isEmpty()) if (blk.isEmpty())
continue; continue;
renderMapBlock(blk, pos); renderMapBlock(blk, pos);
@ -636,26 +636,26 @@ void TileGenerator::renderOrigin()
void TileGenerator::renderPlayers(const std::string &inputPath) void TileGenerator::renderPlayers(const std::string &inputPath)
{ {
PlayerAttributes players(inputPath); PlayerAttributes players(inputPath);
for (PlayerAttributes::Players::iterator player = players.begin(); player != players.end(); ++player) { for (auto &player : players) {
if (player->x < m_xMin * 16 || player->x > m_xMax * 16 || if (player.x < m_xMin * 16 || player.x > m_xMax * 16 ||
player->z < m_zMin * 16 || player->z > m_zMax * 16) player.z < m_zMin * 16 || player.z > m_zMax * 16)
continue; continue;
if (player->y < m_yMin || player->y > m_yMax) if (player.y < m_yMin || player.y > m_yMax)
continue; continue;
int imageX = getImageX(player->x, true), int imageX = getImageX(player.x, true),
imageY = getImageY(player->z, true); imageY = getImageY(player.z, true);
m_image->drawFilledRect(imageX - 1, imageY, 3, 1, m_playerColor); m_image->drawFilledRect(imageX - 1, imageY, 3, 1, m_playerColor);
m_image->drawFilledRect(imageX, imageY - 1, 1, 3, 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; std::list<int16_t> zlist;
for (std::list<std::pair<int, int> >::const_iterator position = m_positions.begin(); position != m_positions.end(); ++position) for (const auto position : m_positions)
zlist.push_back(position->second); zlist.push_back(position.second);
zlist.sort(); zlist.sort();
zlist.unique(); zlist.unique();
zlist.reverse(); zlist.reverse();
@ -674,8 +674,8 @@ void TileGenerator::printUnknown()
if (m_unknownNodes.size() == 0) if (m_unknownNodes.size() == 0)
return; return;
std::cerr << "Unknown nodes:" << std::endl; std::cerr << "Unknown nodes:" << std::endl;
for (NameSet::iterator node = m_unknownNodes.begin(); node != m_unknownNodes.end(); ++node) for (const auto &node : m_unknownNodes)
std::cerr << "\t" << *node << std::endl; std::cerr << "\t" << node << std::endl;
} }
inline int TileGenerator::getImageX(int val, bool absolute) const inline int TileGenerator::getImageX(int val, bool absolute) const

View File

@ -60,13 +60,13 @@ void DBLevelDB::getBlocksOnZ(std::map<int16_t, BlockList> &blocks, int16_t zPos)
std::string datastr; std::string datastr;
leveldb::Status status; leveldb::Status status;
for (std::vector<BlockPos>::iterator it = posCache.begin(); it != posCache.end(); ++it) { for (const auto &it : posCache) {
if (it->z != zPos) { if (it.z != zPos) {
continue; continue;
} }
status = db->Get(leveldb::ReadOptions(), i64tos(encodeBlockPos(*it)), &datastr); status = db->Get(leveldb::ReadOptions(), i64tos(encodeBlockPos(it)), &datastr);
if (status.ok()) { 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); blocks[b.first.x].push_back(b);
} }
} }

View File

@ -1,11 +1,7 @@
#ifndef BLOCKDECODER_H #ifndef BLOCKDECODER_H
#define BLOCKDECODER_H #define BLOCKDECODER_H
#if __cplusplus >= 201103L
#include <unordered_map> #include <unordered_map>
#else
#include <map>
#endif
#include "types.h" #include "types.h"
@ -19,11 +15,7 @@ public:
std::string getNode(u8 x, u8 y, u8 z) const; // returns "" for air, ignore and invalid nodes std::string getNode(u8 x, u8 y, u8 z) const; // returns "" for air, ignore and invalid nodes
private: private:
#if __cplusplus >= 201103L
typedef std::unordered_map<int, std::string> NameMap; typedef std::unordered_map<int, std::string> NameMap;
#else
typedef std::map<int, std::string> NameMap;
#endif
NameMap m_nameMap; NameMap m_nameMap;
int m_blockAirId; int m_blockAirId;
int m_blockIgnoreId; int m_blockIgnoreId;

View File

@ -4,13 +4,8 @@
#include <iosfwd> #include <iosfwd>
#include <list> #include <list>
#include <config.h> #include <config.h>
#if __cplusplus >= 201103L
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#else
#include <map>
#include <set>
#endif
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
@ -60,13 +55,8 @@ struct BitmapThing { // 16x16 bitmap
class TileGenerator class TileGenerator
{ {
private: private:
#if __cplusplus >= 201103L
typedef std::unordered_map<std::string, ColorEntry> ColorMap; typedef std::unordered_map<std::string, ColorEntry> ColorMap;
typedef std::unordered_set<std::string> NameSet; typedef std::unordered_set<std::string> NameSet;
#else
typedef std::map<std::string, ColorEntry> ColorMap;
typedef std::set<std::string> NameSet;
#endif
public: public:
TileGenerator(); TileGenerator();
@ -98,7 +88,7 @@ private:
void loadBlocks(); void loadBlocks();
void createImage(); void createImage();
void renderMap(); void renderMap();
std::list<int> getZValueList() const; std::list<int16_t> getZValueList() const;
void renderMapBlock(const BlockDecoder &blk, const BlockPos &pos); void renderMapBlock(const BlockDecoder &blk, const BlockPos &pos);
void renderMapBlockBottom(const BlockPos &pos); void renderMapBlockBottom(const BlockPos &pos);
void renderShading(int zPos); void renderShading(int zPos);
@ -140,7 +130,7 @@ private:
int m_geomY2; int m_geomY2;
int m_mapWidth; int m_mapWidth;
int m_mapHeight; int m_mapHeight;
std::list<std::pair<int, int> > m_positions; std::list<std::pair<int16_t, int16_t>> m_positions;
ColorMap m_colorMap; ColorMap m_colorMap;
BitmapThing m_readPixels; BitmapThing m_readPixels;
BitmapThing m_readInfo; BitmapThing m_readInfo;

View File

@ -10,8 +10,7 @@
#include "types.h" #include "types.h"
class BlockPos { struct BlockPos {
public:
int16_t x; int16_t x;
int16_t y; int16_t y;
int16_t z; int16_t z;

View File

@ -10,7 +10,7 @@
#include "cmake_config.h" #include "cmake_config.h"
#include "TileGenerator.h" #include "TileGenerator.h"
void usage() static void usage()
{ {
const char *usage_text = "minetestmapper [options]\n" const char *usage_text = "minetestmapper [options]\n"
" -i/--input <world_path>\n" " -i/--input <world_path>\n"
@ -37,13 +37,13 @@ void usage()
std::cout << usage_text; 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()); std::ifstream ifs(path.c_str());
return ifs.is_open(); 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")) if(file_exists(worldpath + "/colors.txt"))
return worldpath + "/colors.txt"; return worldpath + "/colors.txt";
@ -57,7 +57,8 @@ std::string search_colors(const std::string &worldpath)
} }
#endif #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"; return SHAREDIR "/colors.txt";
std::cerr << "Warning: Falling back to using colors.txt from current directory." << std::endl; std::cerr << "Warning: Falling back to using colors.txt from current directory." << std::endl;
@ -66,7 +67,7 @@ std::string search_colors(const std::string &worldpath)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
static struct option long_options[] = const static struct option long_options[] =
{ {
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{"input", required_argument, 0, 'i'}, {"input", required_argument, 0, 'i'},