Skip to content

Commit 9096f70

Browse files
committedMar 26, 2020
C++11 code modernization
1 parent 1d678ff commit 9096f70

8 files changed

+46
-66
lines changed
 

‎CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ if(NOT CMAKE_BUILD_TYPE)
1313
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
1414
endif()
1515

16+
set(CMAKE_CXX_STANDARD 11)
1617
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -DNDEBUG")
1718
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g2 -Wall")
1819

‎PixelAttributes.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
* =====================================================================
88
*/
99

10-
#include <cstdlib>
11-
#include <cstring>
1210
#include "PixelAttributes.h"
1311

1412
using namespace std;
@@ -17,7 +15,7 @@ PixelAttributes::PixelAttributes():
1715
m_width(0)
1816
{
1917
for (size_t i = 0; i < LineCount; ++i) {
20-
m_pixelAttributes[i] = 0;
18+
m_pixelAttributes[i] = nullptr;
2119
}
2220
}
2321

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

3836
void PixelAttributes::scroll()
3937
{
40-
size_t lineLength = m_width * sizeof(PixelAttribute);
41-
memcpy(m_pixelAttributes[FirstLine], m_pixelAttributes[LastLine], lineLength);
38+
*m_pixelAttributes[FirstLine] = *m_pixelAttributes[LastLine];
4239
for (size_t i = 1; i < LineCount - 1; ++i) {
43-
memcpy(m_pixelAttributes[i], m_pixelAttributes[EmptyLine], lineLength);
40+
*m_pixelAttributes[i] = *m_pixelAttributes[EmptyLine];
4441
}
4542
}
4643

4744
void PixelAttributes::freeAttributes()
4845
{
4946
for (size_t i = 0; i < LineCount; ++i) {
50-
if (m_pixelAttributes[i] != 0) {
47+
if (m_pixelAttributes[i] != nullptr) {
5148
delete[] m_pixelAttributes[i];
52-
m_pixelAttributes[i] = 0;
49+
m_pixelAttributes[i] = nullptr;
5350
}
5451
}
5552
}

‎TileGenerator.cpp

+27-27
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ void TileGenerator::parseColorsStream(std::istream &in)
270270
{
271271
char line[128];
272272
while (in.good()) {
273-
in.getline(line, 128);
273+
in.getline(line, sizeof(line));
274274

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

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

294-
ColorEntry color = ColorEntry(r, g, b, a, t);
294+
ColorEntry color(r, g, b, a, t);
295295
m_colorMap[name] = color;
296296
}
297297
}
@@ -352,7 +352,7 @@ void TileGenerator::loadBlocks()
352352
m_zMin = pos.z;
353353
if (pos.z > m_zMax)
354354
m_zMax = pos.z;
355-
m_positions.push_back(std::pair<int, int>(pos.x, pos.z));
355+
m_positions.push_back(std::make_pair(pos.x, pos.z));
356356
}
357357
m_positions.sort();
358358
m_positions.unique();
@@ -393,24 +393,24 @@ void TileGenerator::createImage()
393393
image_height = (m_mapHeight * m_zoom) + m_yBorder;
394394
image_height += (m_scales & SCALE_BOTTOM) ? scale_d : 0;
395395

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

404405
void TileGenerator::renderMap()
405406
{
406407
BlockDecoder blk;
407-
std::list<int> zlist = getZValueList();
408-
for (std::list<int>::iterator zPosition = zlist.begin(); zPosition != zlist.end(); ++zPosition) {
409-
int zPos = *zPosition;
408+
std::list<int16_t> zlist = getZValueList();
409+
for (int16_t zPos : zlist) {
410410
std::map<int16_t, BlockList> blocks;
411411
m_db->getBlocksOnZ(blocks, zPos);
412-
for (std::list<std::pair<int, int> >::const_iterator position = m_positions.begin(); position != m_positions.end(); ++position) {
413-
if (position->second != zPos)
412+
for (const auto position : m_positions) {
413+
if (position.second != zPos)
414414
continue;
415415

416416
m_readPixels.reset();
@@ -423,14 +423,14 @@ void TileGenerator::renderMap()
423423
}
424424
}
425425

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

432432
blk.reset();
433-
blk.decode(it->second);
433+
blk.decode(it.second);
434434
if (blk.isEmpty())
435435
continue;
436436
renderMapBlock(blk, pos);
@@ -636,26 +636,26 @@ void TileGenerator::renderOrigin()
636636
void TileGenerator::renderPlayers(const std::string &inputPath)
637637
{
638638
PlayerAttributes players(inputPath);
639-
for (PlayerAttributes::Players::iterator player = players.begin(); player != players.end(); ++player) {
640-
if (player->x < m_xMin * 16 || player->x > m_xMax * 16 ||
641-
player->z < m_zMin * 16 || player->z > m_zMax * 16)
639+
for (auto &player : players) {
640+
if (player.x < m_xMin * 16 || player.x > m_xMax * 16 ||
641+
player.z < m_zMin * 16 || player.z > m_zMax * 16)
642642
continue;
643-
if (player->y < m_yMin || player->y > m_yMax)
643+
if (player.y < m_yMin || player.y > m_yMax)
644644
continue;
645-
int imageX = getImageX(player->x, true),
646-
imageY = getImageY(player->z, true);
645+
int imageX = getImageX(player.x, true),
646+
imageY = getImageY(player.z, true);
647647

648648
m_image->drawFilledRect(imageX - 1, imageY, 3, 1, m_playerColor);
649649
m_image->drawFilledRect(imageX, imageY - 1, 1, 3, m_playerColor);
650-
m_image->drawText(imageX + 2, imageY, player->name, m_playerColor);
650+
m_image->drawText(imageX + 2, imageY, player.name, m_playerColor);
651651
}
652652
}
653653

654-
inline std::list<int> TileGenerator::getZValueList() const
654+
inline std::list<int16_t> TileGenerator::getZValueList() const
655655
{
656-
std::list<int> zlist;
657-
for (std::list<std::pair<int, int> >::const_iterator position = m_positions.begin(); position != m_positions.end(); ++position)
658-
zlist.push_back(position->second);
656+
std::list<int16_t> zlist;
657+
for (const auto position : m_positions)
658+
zlist.push_back(position.second);
659659
zlist.sort();
660660
zlist.unique();
661661
zlist.reverse();
@@ -674,8 +674,8 @@ void TileGenerator::printUnknown()
674674
if (m_unknownNodes.size() == 0)
675675
return;
676676
std::cerr << "Unknown nodes:" << std::endl;
677-
for (NameSet::iterator node = m_unknownNodes.begin(); node != m_unknownNodes.end(); ++node)
678-
std::cerr << "\t" << *node << std::endl;
677+
for (const auto &node : m_unknownNodes)
678+
std::cerr << "\t" << node << std::endl;
679679
}
680680

681681
inline int TileGenerator::getImageX(int val, bool absolute) const

‎db-leveldb.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ void DBLevelDB::getBlocksOnZ(std::map<int16_t, BlockList> &blocks, int16_t zPos)
6060
std::string datastr;
6161
leveldb::Status status;
6262

63-
for (std::vector<BlockPos>::iterator it = posCache.begin(); it != posCache.end(); ++it) {
64-
if (it->z != zPos) {
63+
for (const auto &it : posCache) {
64+
if (it.z != zPos) {
6565
continue;
6666
}
67-
status = db->Get(leveldb::ReadOptions(), i64tos(encodeBlockPos(*it)), &datastr);
67+
status = db->Get(leveldb::ReadOptions(), i64tos(encodeBlockPos(it)), &datastr);
6868
if (status.ok()) {
69-
Block b(*it, ustring((const unsigned char *) datastr.data(), datastr.size()));
69+
Block b(it, ustring((const unsigned char *) datastr.data(), datastr.size()));
7070
blocks[b.first.x].push_back(b);
7171
}
7272
}

‎include/BlockDecoder.h

-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#ifndef BLOCKDECODER_H
22
#define BLOCKDECODER_H
33

4-
#if __cplusplus >= 201103L
54
#include <unordered_map>
6-
#else
7-
#include <map>
8-
#endif
95

106
#include "types.h"
117

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

2117
private:
22-
#if __cplusplus >= 201103L
2318
typedef std::unordered_map<int, std::string> NameMap;
24-
#else
25-
typedef std::map<int, std::string> NameMap;
26-
#endif
2719
NameMap m_nameMap;
2820
int m_blockAirId;
2921
int m_blockIgnoreId;

‎include/TileGenerator.h

+2-12
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44
#include <iosfwd>
55
#include <list>
66
#include <config.h>
7-
#if __cplusplus >= 201103L
87
#include <unordered_map>
98
#include <unordered_set>
10-
#else
11-
#include <map>
12-
#include <set>
13-
#endif
149
#include <stdint.h>
1510
#include <string>
1611

@@ -60,13 +55,8 @@ struct BitmapThing { // 16x16 bitmap
6055
class TileGenerator
6156
{
6257
private:
63-
#if __cplusplus >= 201103L
6458
typedef std::unordered_map<std::string, ColorEntry> ColorMap;
6559
typedef std::unordered_set<std::string> NameSet;
66-
#else
67-
typedef std::map<std::string, ColorEntry> ColorMap;
68-
typedef std::set<std::string> NameSet;
69-
#endif
7060

7161
public:
7262
TileGenerator();
@@ -98,7 +88,7 @@ class TileGenerator
9888
void loadBlocks();
9989
void createImage();
10090
void renderMap();
101-
std::list<int> getZValueList() const;
91+
std::list<int16_t> getZValueList() const;
10292
void renderMapBlock(const BlockDecoder &blk, const BlockPos &pos);
10393
void renderMapBlockBottom(const BlockPos &pos);
10494
void renderShading(int zPos);
@@ -140,7 +130,7 @@ class TileGenerator
140130
int m_geomY2;
141131
int m_mapWidth;
142132
int m_mapHeight;
143-
std::list<std::pair<int, int> > m_positions;
133+
std::list<std::pair<int16_t, int16_t>> m_positions;
144134
ColorMap m_colorMap;
145135
BitmapThing m_readPixels;
146136
BitmapThing m_readInfo;

‎include/db.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#include "types.h"
1111

1212

13-
class BlockPos {
14-
public:
13+
struct BlockPos {
1514
int16_t x;
1615
int16_t y;
1716
int16_t z;

‎mapper.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "cmake_config.h"
1111
#include "TileGenerator.h"
1212

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

40-
bool file_exists(const std::string &path)
40+
static bool file_exists(const std::string &path)
4141
{
4242
std::ifstream ifs(path.c_str());
4343
return ifs.is_open();
4444
}
4545

46-
std::string search_colors(const std::string &worldpath)
46+
static std::string search_colors(const std::string &worldpath)
4747
{
4848
if(file_exists(worldpath + "/colors.txt"))
4949
return worldpath + "/colors.txt";
@@ -57,7 +57,8 @@ std::string search_colors(const std::string &worldpath)
5757
}
5858
#endif
5959

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

6364
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)
6667

6768
int main(int argc, char *argv[])
6869
{
69-
static struct option long_options[] =
70+
const static struct option long_options[] =
7071
{
7172
{"help", no_argument, 0, 'h'},
7273
{"input", required_argument, 0, 'i'},

0 commit comments

Comments
 (0)
Please sign in to comment.