Skip to content

Commit 77fdcd1

Browse files
committed
Allow rendering (or omitting) scales at every image edge
1 parent 89ed549 commit 77fdcd1

File tree

5 files changed

+101
-32
lines changed

5 files changed

+101
-32
lines changed

README.rst

+3
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,6 @@ zoom:
8686

8787
colors:
8888
Forcefully set path to colors.txt file (it's autodetected otherwise), e.g. ``--colors ../minetest/mycolors.txt``
89+
90+
scales:
91+
Draw scales on specified image edges (letters *t b l r* meaning top, bottom, left and right), e.g. ``--scales tbr``

TileGenerator.cpp

+71-30
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ TileGenerator::TileGenerator():
109109
m_drawAlpha(false),
110110
m_shading(true),
111111
m_backend(""),
112-
m_border(0),
112+
m_xBorder(0),
113+
m_yBorder(0),
113114
m_db(NULL),
114115
m_image(NULL),
115116
m_xMin(INT_MAX),
@@ -122,7 +123,8 @@ TileGenerator::TileGenerator():
122123
m_geomY(-2048),
123124
m_geomX2(2048),
124125
m_geomY2(2048),
125-
m_zoom(1)
126+
m_zoom(1),
127+
m_scales(SCALE_LEFT | SCALE_TOP)
126128
{
127129
}
128130

@@ -159,6 +161,11 @@ void TileGenerator::setZoom(int zoom)
159161
m_zoom = zoom;
160162
}
161163

164+
void TileGenerator::setScales(uint flags)
165+
{
166+
m_scales = flags;
167+
}
168+
162169
Color TileGenerator::parseColor(const std::string &color)
163170
{
164171
Color parsed;
@@ -190,9 +197,6 @@ void TileGenerator::setDrawPlayers(bool drawPlayers)
190197
void TileGenerator::setDrawScale(bool drawScale)
191198
{
192199
m_drawScale = drawScale;
193-
if (m_drawScale) {
194-
m_border = 40;
195-
}
196200
}
197201

198202
void TileGenerator::setDrawAlpha(bool drawAlpha)
@@ -359,9 +363,16 @@ void TileGenerator::createImage()
359363
{
360364
m_mapWidth = (m_xMax - m_xMin + 1) * 16;
361365
m_mapHeight = (m_zMax - m_zMin + 1) * 16;
366+
if(m_drawScale) {
367+
m_xBorder = (m_scales & SCALE_LEFT) ? 40 : 0;
368+
m_yBorder = (m_scales & SCALE_TOP) ? 40 : 0;
369+
}
370+
362371
int image_width, image_height;
363-
image_width = (m_mapWidth * m_zoom) + m_border;
364-
image_height = (m_mapHeight * m_zoom) + m_border;
372+
image_width = (m_mapWidth * m_zoom) + m_xBorder;
373+
image_width += m_drawScale && (m_scales & SCALE_RIGHT) ? 40 : 0;
374+
image_height = (m_mapHeight * m_zoom) + m_yBorder;
375+
image_height += m_drawScale && (m_scales & SCALE_BOTTOM) ? 40 : 0;
365376
if(image_width > 4096 || image_height > 4096)
366377
std::cerr << "Warning: The width or height of the image to be created exceeds 4096 pixels!"
367378
<< " (Dimensions: " << image_width << "x" << image_height << ")"
@@ -633,36 +644,66 @@ inline void TileGenerator::renderShading(int zPos)
633644
void TileGenerator::renderScale()
634645
{
635646
int color = color2int(m_scaleColor);
636-
gdImageString(m_image, gdFontGetMediumBold(), 24, 0, reinterpret_cast<unsigned char *>(const_cast<char *>("X")), color);
637-
gdImageString(m_image, gdFontGetMediumBold(), 2, 24, reinterpret_cast<unsigned char *>(const_cast<char *>("Z")), color);
638647

639648
string scaleText;
640649

641-
for (int i = (m_xMin / 4) * 4; i <= m_xMax; i += 4) {
642-
stringstream buf;
643-
buf << i * 16;
644-
scaleText = buf.str();
650+
if (m_scales & SCALE_TOP) {
651+
gdImageString(m_image, gdFontGetMediumBold(), 24, 0, reinterpret_cast<unsigned char *>(const_cast<char *>("X")), color);
652+
for (int i = (m_xMin / 4) * 4; i <= m_xMax; i += 4) {
653+
stringstream buf;
654+
buf << i * 16;
655+
scaleText = buf.str();
645656

646-
int xPos = (m_xMin * -16 + i * 16)*m_zoom + m_border;
647-
gdImageString(m_image, gdFontGetMediumBold(), xPos + 2, 0, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
648-
gdImageLine(m_image, xPos, 0, xPos, m_border - 1, color);
657+
int xPos = (m_xMin * -16 + i * 16)*m_zoom + m_xBorder;
658+
gdImageString(m_image, gdFontGetMediumBold(), xPos + 2, 0, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
659+
gdImageLine(m_image, xPos, 0, xPos, m_yBorder - 1, color);
660+
}
649661
}
650662

651-
for (int i = (m_zMax / 4) * 4; i >= m_zMin; i -= 4) {
652-
stringstream buf;
653-
buf << i * 16;
654-
scaleText = buf.str();
663+
if (m_scales & SCALE_LEFT) {
664+
gdImageString(m_image, gdFontGetMediumBold(), 2, 24, reinterpret_cast<unsigned char *>(const_cast<char *>("Z")), color);
665+
for (int i = (m_zMax / 4) * 4; i >= m_zMin; i -= 4) {
666+
stringstream buf;
667+
buf << i * 16;
668+
scaleText = buf.str();
655669

656-
int yPos = (m_mapHeight - 1 - (i * 16 - m_zMin * 16))*m_zoom + m_border;
657-
gdImageString(m_image, gdFontGetMediumBold(), 2, yPos, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
658-
gdImageLine(m_image, 0, yPos, m_border - 1, yPos, color);
670+
int yPos = (m_mapHeight - 1 - (i * 16 - m_zMin * 16))*m_zoom + m_yBorder;
671+
gdImageString(m_image, gdFontGetMediumBold(), 2, yPos, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
672+
gdImageLine(m_image, 0, yPos, m_xBorder - 1, yPos, color);
673+
}
674+
}
675+
676+
if (m_scales & SCALE_BOTTOM) {
677+
for (int i = (m_xMin / 4) * 4; i <= m_xMax; i += 4) {
678+
stringstream buf;
679+
buf << i * 16;
680+
scaleText = buf.str();
681+
682+
int xPos = (m_xMin * -16 + i * 16)*m_zoom + m_xBorder;
683+
int yPos = m_yBorder + m_mapHeight;
684+
gdImageString(m_image, gdFontGetMediumBold(), xPos + 2, yPos, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
685+
gdImageLine(m_image, xPos, yPos, xPos, yPos + 39, color);
686+
}
687+
}
688+
689+
if (m_scales & SCALE_RIGHT) {
690+
for (int i = (m_zMax / 4) * 4; i >= m_zMin; i -= 4) {
691+
stringstream buf;
692+
buf << i * 16;
693+
scaleText = buf.str();
694+
695+
int xPos = m_xBorder + m_mapWidth;
696+
int yPos = (m_mapHeight - 1 - (i * 16 - m_zMin * 16))*m_zoom + m_yBorder;
697+
gdImageString(m_image, gdFontGetMediumBold(), xPos + 2, yPos, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
698+
gdImageLine(m_image, xPos, yPos, xPos + 39, yPos, color);
699+
}
659700
}
660701
}
661702

662703
void TileGenerator::renderOrigin()
663704
{
664-
int imageX = (-m_xMin * 16)*m_zoom + m_border;
665-
int imageY = (m_mapHeight - m_zMin * -16)*m_zoom + m_border;
705+
int imageX = (-m_xMin * 16)*m_zoom + m_xBorder;
706+
int imageY = (m_mapHeight - m_zMin * -16)*m_zoom + m_yBorder;
666707
gdImageArc(m_image, imageX, imageY, 12, 12, 0, 360, color2int(m_originColor));
667708
}
668709

@@ -672,8 +713,8 @@ void TileGenerator::renderPlayers(const std::string &inputPath)
672713

673714
PlayerAttributes players(inputPath);
674715
for (PlayerAttributes::Players::iterator player = players.begin(); player != players.end(); ++player) {
675-
int imageX = (player->x / 10 - m_xMin * 16)*m_zoom + m_border;
676-
int imageY = (m_mapHeight - (player->z / 10 - m_zMin * 16))*m_zoom + m_border;
716+
int imageX = (player->x / 10 - m_xMin * 16)*m_zoom + m_xBorder;
717+
int imageY = (m_mapHeight - (player->z / 10 - m_zMin * 16))*m_zoom + m_yBorder;
677718

678719
gdImageArc(m_image, imageX, imageY, 5, 5, 0, 360, color);
679720
gdImageString(m_image, gdFontGetMediumBold(), imageX + 2, imageY + 2, reinterpret_cast<unsigned char *>(const_cast<char *>(player->name.c_str())), color);
@@ -718,19 +759,19 @@ void TileGenerator::printUnknown()
718759

719760
inline int TileGenerator::getImageX(int val) const
720761
{
721-
return (m_zoom*val) + m_border;
762+
return (m_zoom*val) + m_xBorder;
722763
}
723764

724765
inline int TileGenerator::getImageY(int val) const
725766
{
726-
return (m_zoom*val) + m_border;
767+
return (m_zoom*val) + m_yBorder;
727768
}
728769

729770
inline void TileGenerator::setZoomed(gdImagePtr image, int y, int x, int color) {
730771
int xx,yy;
731772
for (xx = 0; xx < m_zoom; xx++) {
732773
for (yy = 0; yy < m_zoom; yy++) {
733-
image->tpixels[m_border + (y*m_zoom) + xx][m_border + (x*m_zoom) + yy] = color;
774+
image->tpixels[m_yBorder + (y*m_zoom) + xx][m_xBorder + (x*m_zoom) + yy] = color;
734775
}
735776
}
736777
}

TileGenerator.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
#include "db.h"
1919
#include "types.h"
2020

21+
enum {
22+
SCALE_TOP = (1 << 0),
23+
SCALE_BOTTOM = (1 << 1),
24+
SCALE_LEFT = (1 << 2),
25+
SCALE_RIGHT = (1 << 3),
26+
};
27+
2128
struct Color {
2229
Color(): r(0xFF), g(0xFF), b(0xFF), a(0) {};
2330
Color(uint8_t r, uint8_t g, uint8_t b): r(r), g(g), b(b), a(0) {};
@@ -72,6 +79,7 @@ class TileGenerator
7279
void setBackend(std::string backend);
7380
void generate(const std::string &input, const std::string &output);
7481
void setZoom(int zoom);
82+
void setScales(uint flags);
7583

7684
private:
7785
void parseColorsStream(std::istream &in);
@@ -104,7 +112,7 @@ class TileGenerator
104112
bool m_drawAlpha;
105113
bool m_shading;
106114
std::string m_backend;
107-
int m_border;
115+
int m_xBorder, m_yBorder;
108116

109117
DB *m_db;
110118
gdImagePtr m_image;
@@ -133,6 +141,7 @@ class TileGenerator
133141
int m_blockAirId;
134142
int m_blockIgnoreId;
135143
int m_zoom;
144+
uint m_scales;
136145
}; // class TileGenerator
137146

138147
#endif // TILEGENERATOR_HEADER

mapper.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <cstdlib>
2+
#include <cstring>
23
#include <getopt.h>
34
#include <fstream>
45
#include <iostream>
@@ -29,6 +30,7 @@ void usage()
2930
" --geometry x:y+w+h\n"
3031
" --zoom <zoomlevel>\n"
3132
" --colors <colors.txt>\n"
33+
" --scales [t][b][l][r]\n"
3234
"Color format: '#000000'\n";
3335
std::cout << usage_text;
3436
}
@@ -82,6 +84,7 @@ int main(int argc, char *argv[])
8284
{"max-y", required_argument, 0, 'c'},
8385
{"zoom", required_argument, 0, 'z'},
8486
{"colors", required_argument, 0, 'C'},
87+
{"scales", required_argument, 0, 'f'},
8588
{0, 0, 0, 0}
8689
};
8790

@@ -171,6 +174,19 @@ int main(int argc, char *argv[])
171174
generator.setGeometry(x, y, w, h);
172175
}
173176
break;
177+
case 'f': {
178+
uint flags = 0;
179+
if(strchr(optarg, 't') != NULL)
180+
flags |= SCALE_TOP;
181+
if(strchr(optarg, 'b') != NULL)
182+
flags |= SCALE_BOTTOM;
183+
if(strchr(optarg, 'l') != NULL)
184+
flags |= SCALE_LEFT;
185+
if(strchr(optarg, 'r') != NULL)
186+
flags |= SCALE_RIGHT;
187+
generator.setScales(flags);
188+
}
189+
break;
174190
case 'z': {
175191
std::istringstream iss;
176192
iss.str(optarg);

types.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#include <string>
33

44
typedef std::basic_string<unsigned char> ustring;
5-
5+
typedef unsigned int uint;

0 commit comments

Comments
 (0)