From 9096f701885f0dc88599359f9f5224c78127d6a2 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Thu, 26 Mar 2020 23:07:27 +0100 Subject: [PATCH] C++11 code modernization --- CMakeLists.txt | 1 + PixelAttributes.cpp | 13 ++++------ TileGenerator.cpp | 54 ++++++++++++++++++++--------------------- db-leveldb.cpp | 8 +++--- include/BlockDecoder.h | 8 ------ include/TileGenerator.h | 14 ++--------- include/db.h | 3 +-- mapper.cpp | 11 +++++---- 8 files changed, 46 insertions(+), 66 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8732b85..ffd5869 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") diff --git a/PixelAttributes.cpp b/PixelAttributes.cpp index 6eda50e..b67f3fd 100644 --- a/PixelAttributes.cpp +++ b/PixelAttributes.cpp @@ -7,8 +7,6 @@ * ===================================================================== */ -#include -#include #include "PixelAttributes.h" using namespace std; @@ -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; } } @@ -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; } } } diff --git a/TileGenerator.cpp b/TileGenerator.cpp index 90e311b..ed4b58e 100644 --- a/TileGenerator.cpp +++ b/TileGenerator.cpp @@ -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 != '#') @@ -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; @@ -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; } } @@ -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(pos.x, pos.z)); + m_positions.push_back(std::make_pair(pos.x, pos.z)); } m_positions.sort(); m_positions.unique(); @@ -393,10 +393,11 @@ 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 } @@ -404,13 +405,12 @@ void TileGenerator::createImage() void TileGenerator::renderMap() { BlockDecoder blk; - std::list zlist = getZValueList(); - for (std::list::iterator zPosition = zlist.begin(); zPosition != zlist.end(); ++zPosition) { - int zPos = *zPosition; + std::list zlist = getZValueList(); + for (int16_t zPos : zlist) { std::map blocks; m_db->getBlocksOnZ(blocks, zPos); - for (std::list >::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(); @@ -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); @@ -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 TileGenerator::getZValueList() const +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); + std::list zlist; + for (const auto position : m_positions) + zlist.push_back(position.second); zlist.sort(); zlist.unique(); zlist.reverse(); @@ -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 diff --git a/db-leveldb.cpp b/db-leveldb.cpp index 849384a..cbee57d 100644 --- a/db-leveldb.cpp +++ b/db-leveldb.cpp @@ -60,13 +60,13 @@ void DBLevelDB::getBlocksOnZ(std::map &blocks, int16_t zPos) std::string datastr; leveldb::Status status; - for (std::vector::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); } } diff --git a/include/BlockDecoder.h b/include/BlockDecoder.h index ce69449..38e3d5f 100644 --- a/include/BlockDecoder.h +++ b/include/BlockDecoder.h @@ -1,11 +1,7 @@ #ifndef BLOCKDECODER_H #define BLOCKDECODER_H -#if __cplusplus >= 201103L #include -#else -#include -#endif #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 private: -#if __cplusplus >= 201103L typedef std::unordered_map NameMap; -#else - typedef std::map NameMap; -#endif NameMap m_nameMap; int m_blockAirId; int m_blockIgnoreId; diff --git a/include/TileGenerator.h b/include/TileGenerator.h index 6c97a7a..dce1b89 100644 --- a/include/TileGenerator.h +++ b/include/TileGenerator.h @@ -4,13 +4,8 @@ #include #include #include -#if __cplusplus >= 201103L #include #include -#else -#include -#include -#endif #include #include @@ -60,13 +55,8 @@ struct BitmapThing { // 16x16 bitmap class TileGenerator { private: -#if __cplusplus >= 201103L typedef std::unordered_map ColorMap; typedef std::unordered_set NameSet; -#else - typedef std::map ColorMap; - typedef std::set NameSet; -#endif public: TileGenerator(); @@ -98,7 +88,7 @@ private: void loadBlocks(); void createImage(); void renderMap(); - std::list getZValueList() const; + std::list getZValueList() const; void renderMapBlock(const BlockDecoder &blk, const BlockPos &pos); void renderMapBlockBottom(const BlockPos &pos); void renderShading(int zPos); @@ -140,7 +130,7 @@ private: int m_geomY2; int m_mapWidth; int m_mapHeight; - std::list > m_positions; + std::list> m_positions; ColorMap m_colorMap; BitmapThing m_readPixels; BitmapThing m_readInfo; diff --git a/include/db.h b/include/db.h index 894e4c8..6ed5aae 100644 --- a/include/db.h +++ b/include/db.h @@ -10,8 +10,7 @@ #include "types.h" -class BlockPos { -public: +struct BlockPos { int16_t x; int16_t y; int16_t z; diff --git a/mapper.cpp b/mapper.cpp index 7acb7a0..115e79d 100644 --- a/mapper.cpp +++ b/mapper.cpp @@ -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 \n" @@ -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"; @@ -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; @@ -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'},