Some more code modernization

also a few small performance improvements
This commit is contained in:
sfan5
2020-05-08 22:10:49 +02:00
parent 2979dc5b6b
commit 8e83ce6464
21 changed files with 149 additions and 199 deletions

View File

@ -1,5 +1,4 @@
#ifndef BLOCKDECODER_H
#define BLOCKDECODER_H
#pragma once
#include <unordered_map>
@ -12,10 +11,11 @@ public:
void reset();
void decode(const ustring &data);
bool isEmpty() const;
std::string getNode(u8 x, u8 y, u8 z) const; // returns "" for air, ignore and invalid nodes
// returns "" for air, ignore and invalid nodes
const std::string &getNode(u8 x, u8 y, u8 z) const;
private:
typedef std::unordered_map<int, std::string> NameMap;
typedef std::unordered_map<uint16_t, std::string> NameMap;
NameMap m_nameMap;
int m_blockAirId;
int m_blockIgnoreId;
@ -23,5 +23,3 @@ private:
u8 m_version, m_contentWidth;
ustring m_mapData;
};
#endif // BLOCKDECODER_H

View File

@ -1,5 +1,4 @@
#ifndef IMAGE_HEADER
#define IMAGE_HEADER
#pragma once
#include "types.h"
#include <string>
@ -9,7 +8,6 @@ struct Color {
Color() : r(0), g(0), b(0), a(0) {};
Color(u8 r, u8 g, u8 b) : r(r), g(g), b(b), a(255) {};
Color(u8 r, u8 g, u8 b, u8 a) : r(r), g(g), b(b), a(a) {};
inline Color noAlpha() const { return Color(r, g, b); }
u8 r, g, b, a;
};
@ -19,6 +17,9 @@ public:
Image(int width, int height);
~Image();
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;
void setPixel(int x, int y, const Color &c);
Color getPixel(int x, int y);
void drawLine(int x1, int y1, int x2, int y2, const Color &c);
@ -28,10 +29,6 @@ public:
void save(const std::string &filename);
private:
Image(const Image&);
int m_width, m_height;
gdImagePtr m_image;
};
#endif // IMAGE_HEADER

View File

@ -1,25 +1,15 @@
/*
* =====================================================================
* Version: 1.0
* Created: 25.08.2012 10:55:29
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
#pragma once
#ifndef PIXELATTRIBUTES_H_ADZ35GYF
#define PIXELATTRIBUTES_H_ADZ35GYF
#include <limits>
#include <stdint.h>
#include <climits>
#include <cstdint>
#include "config.h"
struct PixelAttribute {
PixelAttribute(): height(std::numeric_limits<int>::min()), thickness(0) {};
int height;
PixelAttribute(): height(INT16_MIN), thickness(0) {};
int16_t height;
uint8_t thickness;
inline bool valid_height() {
return height != std::numeric_limits<int>::min();
return height != INT16_MIN;
}
};
@ -30,7 +20,9 @@ public:
virtual ~PixelAttributes();
void setWidth(int width);
void scroll();
inline PixelAttribute &attribute(int z, int x) { return m_pixelAttributes[z + 1][x + 1]; };
inline PixelAttribute &attribute(int z, int x) {
return m_pixelAttributes[z + 1][x + 1];
};
private:
void freeAttributes();
@ -45,6 +37,3 @@ private:
PixelAttribute *m_pixelAttributes[BLOCK_SIZE + 2]; // 1px gradient + empty
int m_width;
};
#endif /* end of include guard: PIXELATTRIBUTES_H_ADZ35GYF */

View File

@ -1,5 +1,4 @@
#ifndef PLAYERATTRIBUTES_H_D7THWFVV
#define PLAYERATTRIBUTES_H_D7THWFVV
#pragma once
#include <list>
#include <string>
@ -16,8 +15,8 @@ public:
typedef std::list<Player> Players;
PlayerAttributes(const std::string &worldDir);
Players::iterator begin();
Players::iterator end();
Players::const_iterator begin() const;
Players::const_iterator end() const;
private:
void readFiles(const std::string &playersPath);
@ -25,6 +24,3 @@ private:
Players m_players;
};
#endif /* end of include guard: PLAYERATTRIBUTES_H_D7THWFVV */

View File

@ -1,21 +1,22 @@
#ifndef TILEGENERATOR_HEADER
#define TILEGENERATOR_HEADER
#include <iosfwd>
#include <iostream>
#include <map>
#include <set>
#include <config.h>
#include <unordered_map>
#include <unordered_set>
#include <stdint.h>
#include <cstdint>
#include <string>
#include "PixelAttributes.h"
#include "BlockDecoder.h"
#include "Image.h"
#include "db.h"
#include "types.h"
class BlockDecoder;
class Image;
enum {
SCALE_TOP = (1 << 0),
SCALE_BOTTOM = (1 << 1),
@ -31,10 +32,12 @@ enum {
};
struct ColorEntry {
ColorEntry(): r(0), g(0), b(0), a(0), t(0) {};
ColorEntry(uint8_t r, uint8_t g, uint8_t b, uint8_t a, uint8_t t): r(r), g(g), b(b), a(a), t(t) {};
inline Color to_color() const { return Color(r, g, b, a); }
uint8_t r, g, b, a, t;
ColorEntry() : r(0), g(0), b(0), a(0), t(0) {};
ColorEntry(uint8_t r, uint8_t g, uint8_t b, uint8_t a, uint8_t t) :
r(r), g(g), b(b), a(a), t(t) {};
inline Color toColor() const { return Color(r, g, b, a); }
uint8_t r, g, b, a; // Red, Green, Blue, Alpha
uint8_t t; // "thickness" value
};
struct BitmapThing { // 16x16 bitmap
@ -73,7 +76,6 @@ public:
void setScaleColor(const std::string &scaleColor);
void setOriginColor(const std::string &originColor);
void setPlayerColor(const std::string &playerColor);
Color parseColor(const std::string &color);
void setDrawOrigin(bool drawOrigin);
void setDrawPlayers(bool drawPlayers);
void setDrawScale(bool drawScale);

View File

@ -1,14 +1,4 @@
/*
* =====================================================================
* Version: 1.0
* Created: 18.09.2012 10:20:51
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
#ifndef ZLIBDECOMPRESSOR_H_ZQL1PN8Q
#define ZLIBDECOMPRESSOR_H_ZQL1PN8Q
#pragma once
#include <cstdlib>
#include <string>
@ -31,7 +21,4 @@ private:
const unsigned char *m_data;
std::size_t m_seekPos;
std::size_t m_size;
}; /* ----- end of class ZlibDecompressor ----- */
#endif /* end of include guard: ZLIBDECOMPRESSOR_H_ZQL1PN8Q */
};

View File

@ -1,5 +1,4 @@
#ifndef DB_LEVELDB_HEADER
#define DB_LEVELDB_HEADER
#pragma once
#include "db.h"
#include <unordered_map>
@ -27,5 +26,3 @@ private:
std::unordered_map<int16_t, std::vector<pos2d>> posCache;
leveldb::DB *db;
};
#endif // DB_LEVELDB_HEADER

View File

@ -1,5 +1,4 @@
#ifndef _DB_POSTGRESQL_H
#define _DB_POSTGRESQL_H
#pragma once
#include "db.h"
#include <libpq-fe.h>
@ -31,5 +30,3 @@ protected:
private:
PGconn *db;
};
#endif // _DB_POSTGRESQL_H

View File

@ -1,5 +1,4 @@
#ifndef DB_REDIS_HEADER
#define DB_REDIS_HEADER
#pragma once
#include "db.h"
#include <unordered_map>
@ -33,5 +32,3 @@ private:
redisContext *ctx;
std::string hash;
};
#endif // DB_REDIS_HEADER

View File

@ -1,5 +1,4 @@
#ifndef _DB_SQLITE3_H
#define _DB_SQLITE3_H
#pragma once
#include "db.h"
#include <unordered_map>
@ -32,5 +31,3 @@ private:
int16_t blockCachedZ = -10000;
std::unordered_map<int16_t, BlockList> blockCache; // indexed by X
};
#endif // _DB_SQLITE3_H

View File

@ -1,8 +1,6 @@
#ifndef DB_HEADER
#define DB_HEADER
#pragma once
#include <stdint.h>
#include <map>
#include <cstdint>
#include <list>
#include <vector>
#include <utility>
@ -122,4 +120,3 @@ inline BlockPos DB::decodeBlockPos(int64_t hash) const
* End black magic *
*******************/
#endif // DB_HEADER

View File

@ -1,18 +1,9 @@
#ifndef UTIL_H
#define UTIL_H
#pragma once
#include <string>
#include <fstream>
std::string read_setting(const std::string &name, std::istream &is);
inline std::string read_setting_default(const std::string &name, std::istream &is, const std::string &def)
{
try {
return read_setting(name, is);
} catch(const std::runtime_error &e) {
return def;
}
}
#endif // UTIL_H
std::string read_setting_default(const std::string &name, std::istream &is,
const std::string &def);