mirror of
https://github.com/minetest/minetestmapper.git
synced 2024-11-27 18:03:54 +01:00
C++11 code modernization
This commit is contained in:
parent
1d678ffa82
commit
9096f70188
@ -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")
|
||||
|
||||
|
@ -7,8 +7,6 @@
|
||||
* =====================================================================
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<int, int>(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<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();
|
||||
@ -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<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();
|
||||
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,7 @@
|
||||
#ifndef BLOCKDECODER_H
|
||||
#define BLOCKDECODER_H
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
#include <unordered_map>
|
||||
#else
|
||||
#include <map>
|
||||
#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<int, std::string> NameMap;
|
||||
#else
|
||||
typedef std::map<int, std::string> NameMap;
|
||||
#endif
|
||||
NameMap m_nameMap;
|
||||
int m_blockAirId;
|
||||
int m_blockIgnoreId;
|
||||
|
@ -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>
|
||||
|
||||
@ -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();
|
||||
@ -98,7 +88,7 @@ private:
|
||||
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);
|
||||
@ -140,7 +130,7 @@ private:
|
||||
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;
|
||||
|
@ -10,8 +10,7 @@
|
||||
#include "types.h"
|
||||
|
||||
|
||||
class BlockPos {
|
||||
public:
|
||||
struct BlockPos {
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
int16_t z;
|
||||
|
11
mapper.cpp
11
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 <world_path>\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'},
|
||||
|
Loading…
Reference in New Issue
Block a user