Skip to content

Commit

Permalink
Allow rendering (or omitting) scales at every image edge
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Nov 18, 2016
1 parent 89ed549 commit 77fdcd1
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 32 deletions.
3 changes: 3 additions & 0 deletions README.rst
Expand Up @@ -86,3 +86,6 @@ zoom:

colors:
Forcefully set path to colors.txt file (it's autodetected otherwise), e.g. ``--colors ../minetest/mycolors.txt``

scales:
Draw scales on specified image edges (letters *t b l r* meaning top, bottom, left and right), e.g. ``--scales tbr``
101 changes: 71 additions & 30 deletions TileGenerator.cpp
Expand Up @@ -109,7 +109,8 @@ TileGenerator::TileGenerator():
m_drawAlpha(false),
m_shading(true),
m_backend(""),
m_border(0),
m_xBorder(0),
m_yBorder(0),
m_db(NULL),
m_image(NULL),
m_xMin(INT_MAX),
Expand All @@ -122,7 +123,8 @@ TileGenerator::TileGenerator():
m_geomY(-2048),
m_geomX2(2048),
m_geomY2(2048),
m_zoom(1)
m_zoom(1),
m_scales(SCALE_LEFT | SCALE_TOP)
{
}

Expand Down Expand Up @@ -159,6 +161,11 @@ void TileGenerator::setZoom(int zoom)
m_zoom = zoom;
}

void TileGenerator::setScales(uint flags)
{
m_scales = flags;
}

Color TileGenerator::parseColor(const std::string &color)
{
Color parsed;
Expand Down Expand Up @@ -190,9 +197,6 @@ void TileGenerator::setDrawPlayers(bool drawPlayers)
void TileGenerator::setDrawScale(bool drawScale)
{
m_drawScale = drawScale;
if (m_drawScale) {
m_border = 40;
}
}

void TileGenerator::setDrawAlpha(bool drawAlpha)
Expand Down Expand Up @@ -359,9 +363,16 @@ void TileGenerator::createImage()
{
m_mapWidth = (m_xMax - m_xMin + 1) * 16;
m_mapHeight = (m_zMax - m_zMin + 1) * 16;
if(m_drawScale) {
m_xBorder = (m_scales & SCALE_LEFT) ? 40 : 0;
m_yBorder = (m_scales & SCALE_TOP) ? 40 : 0;
}

int image_width, image_height;
image_width = (m_mapWidth * m_zoom) + m_border;
image_height = (m_mapHeight * m_zoom) + m_border;
image_width = (m_mapWidth * m_zoom) + m_xBorder;
image_width += m_drawScale && (m_scales & SCALE_RIGHT) ? 40 : 0;
image_height = (m_mapHeight * m_zoom) + m_yBorder;
image_height += m_drawScale && (m_scales & SCALE_BOTTOM) ? 40 : 0;
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 << ")"
Expand Down Expand Up @@ -633,36 +644,66 @@ inline void TileGenerator::renderShading(int zPos)
void TileGenerator::renderScale()
{
int color = color2int(m_scaleColor);
gdImageString(m_image, gdFontGetMediumBold(), 24, 0, reinterpret_cast<unsigned char *>(const_cast<char *>("X")), color);
gdImageString(m_image, gdFontGetMediumBold(), 2, 24, reinterpret_cast<unsigned char *>(const_cast<char *>("Z")), color);

string scaleText;

for (int i = (m_xMin / 4) * 4; i <= m_xMax; i += 4) {
stringstream buf;
buf << i * 16;
scaleText = buf.str();
if (m_scales & SCALE_TOP) {
gdImageString(m_image, gdFontGetMediumBold(), 24, 0, reinterpret_cast<unsigned char *>(const_cast<char *>("X")), color);
for (int i = (m_xMin / 4) * 4; i <= m_xMax; i += 4) {
stringstream buf;
buf << i * 16;
scaleText = buf.str();

int xPos = (m_xMin * -16 + i * 16)*m_zoom + m_border;
gdImageString(m_image, gdFontGetMediumBold(), xPos + 2, 0, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
gdImageLine(m_image, xPos, 0, xPos, m_border - 1, color);
int xPos = (m_xMin * -16 + i * 16)*m_zoom + m_xBorder;
gdImageString(m_image, gdFontGetMediumBold(), xPos + 2, 0, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
gdImageLine(m_image, xPos, 0, xPos, m_yBorder - 1, color);
}
}

for (int i = (m_zMax / 4) * 4; i >= m_zMin; i -= 4) {
stringstream buf;
buf << i * 16;
scaleText = buf.str();
if (m_scales & SCALE_LEFT) {
gdImageString(m_image, gdFontGetMediumBold(), 2, 24, reinterpret_cast<unsigned char *>(const_cast<char *>("Z")), color);
for (int i = (m_zMax / 4) * 4; i >= m_zMin; i -= 4) {
stringstream buf;
buf << i * 16;
scaleText = buf.str();

int yPos = (m_mapHeight - 1 - (i * 16 - m_zMin * 16))*m_zoom + m_border;
gdImageString(m_image, gdFontGetMediumBold(), 2, yPos, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
gdImageLine(m_image, 0, yPos, m_border - 1, yPos, color);
int yPos = (m_mapHeight - 1 - (i * 16 - m_zMin * 16))*m_zoom + m_yBorder;
gdImageString(m_image, gdFontGetMediumBold(), 2, yPos, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
gdImageLine(m_image, 0, yPos, m_xBorder - 1, yPos, color);
}
}

if (m_scales & SCALE_BOTTOM) {
for (int i = (m_xMin / 4) * 4; i <= m_xMax; i += 4) {
stringstream buf;
buf << i * 16;
scaleText = buf.str();

int xPos = (m_xMin * -16 + i * 16)*m_zoom + m_xBorder;
int yPos = m_yBorder + m_mapHeight;
gdImageString(m_image, gdFontGetMediumBold(), xPos + 2, yPos, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
gdImageLine(m_image, xPos, yPos, xPos, yPos + 39, color);
}
}

if (m_scales & SCALE_RIGHT) {
for (int i = (m_zMax / 4) * 4; i >= m_zMin; i -= 4) {
stringstream buf;
buf << i * 16;
scaleText = buf.str();

int xPos = m_xBorder + m_mapWidth;
int yPos = (m_mapHeight - 1 - (i * 16 - m_zMin * 16))*m_zoom + m_yBorder;
gdImageString(m_image, gdFontGetMediumBold(), xPos + 2, yPos, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
gdImageLine(m_image, xPos, yPos, xPos + 39, yPos, color);
}
}
}

void TileGenerator::renderOrigin()
{
int imageX = (-m_xMin * 16)*m_zoom + m_border;
int imageY = (m_mapHeight - m_zMin * -16)*m_zoom + m_border;
int imageX = (-m_xMin * 16)*m_zoom + m_xBorder;
int imageY = (m_mapHeight - m_zMin * -16)*m_zoom + m_yBorder;
gdImageArc(m_image, imageX, imageY, 12, 12, 0, 360, color2int(m_originColor));
}

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

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

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

inline int TileGenerator::getImageX(int val) const
{
return (m_zoom*val) + m_border;
return (m_zoom*val) + m_xBorder;
}

inline int TileGenerator::getImageY(int val) const
{
return (m_zoom*val) + m_border;
return (m_zoom*val) + m_yBorder;
}

inline void TileGenerator::setZoomed(gdImagePtr image, int y, int x, int color) {
int xx,yy;
for (xx = 0; xx < m_zoom; xx++) {
for (yy = 0; yy < m_zoom; yy++) {
image->tpixels[m_border + (y*m_zoom) + xx][m_border + (x*m_zoom) + yy] = color;
image->tpixels[m_yBorder + (y*m_zoom) + xx][m_xBorder + (x*m_zoom) + yy] = color;
}
}
}
11 changes: 10 additions & 1 deletion TileGenerator.h
Expand Up @@ -18,6 +18,13 @@
#include "db.h"
#include "types.h"

enum {
SCALE_TOP = (1 << 0),
SCALE_BOTTOM = (1 << 1),
SCALE_LEFT = (1 << 2),
SCALE_RIGHT = (1 << 3),
};

struct Color {
Color(): r(0xFF), g(0xFF), b(0xFF), a(0) {};
Color(uint8_t r, uint8_t g, uint8_t b): r(r), g(g), b(b), a(0) {};
Expand Down Expand Up @@ -72,6 +79,7 @@ class TileGenerator
void setBackend(std::string backend);
void generate(const std::string &input, const std::string &output);
void setZoom(int zoom);
void setScales(uint flags);

private:
void parseColorsStream(std::istream &in);
Expand Down Expand Up @@ -104,7 +112,7 @@ class TileGenerator
bool m_drawAlpha;
bool m_shading;
std::string m_backend;
int m_border;
int m_xBorder, m_yBorder;

DB *m_db;
gdImagePtr m_image;
Expand Down Expand Up @@ -133,6 +141,7 @@ class TileGenerator
int m_blockAirId;
int m_blockIgnoreId;
int m_zoom;
uint m_scales;
}; // class TileGenerator

#endif // TILEGENERATOR_HEADER
16 changes: 16 additions & 0 deletions mapper.cpp
@@ -1,4 +1,5 @@
#include <cstdlib>
#include <cstring>
#include <getopt.h>
#include <fstream>
#include <iostream>
Expand Down Expand Up @@ -29,6 +30,7 @@ void usage()
" --geometry x:y+w+h\n"
" --zoom <zoomlevel>\n"
" --colors <colors.txt>\n"
" --scales [t][b][l][r]\n"
"Color format: '#000000'\n";
std::cout << usage_text;
}
Expand Down Expand Up @@ -82,6 +84,7 @@ int main(int argc, char *argv[])
{"max-y", required_argument, 0, 'c'},
{"zoom", required_argument, 0, 'z'},
{"colors", required_argument, 0, 'C'},
{"scales", required_argument, 0, 'f'},
{0, 0, 0, 0}
};

Expand Down Expand Up @@ -171,6 +174,19 @@ int main(int argc, char *argv[])
generator.setGeometry(x, y, w, h);
}
break;
case 'f': {
uint flags = 0;
if(strchr(optarg, 't') != NULL)
flags |= SCALE_TOP;
if(strchr(optarg, 'b') != NULL)
flags |= SCALE_BOTTOM;
if(strchr(optarg, 'l') != NULL)
flags |= SCALE_LEFT;
if(strchr(optarg, 'r') != NULL)
flags |= SCALE_RIGHT;
generator.setScales(flags);
}
break;
case 'z': {
std::istringstream iss;
iss.str(optarg);
Expand Down
2 changes: 1 addition & 1 deletion types.h
Expand Up @@ -2,4 +2,4 @@
#include <string>

typedef std::basic_string<unsigned char> ustring;

typedef unsigned int uint;

0 comments on commit 77fdcd1

Please sign in to comment.