Allow the playername labels/markers to cross image edges.

Also the orgin circle.
This commit is contained in:
Martijn Versteegh 2018-11-14 12:17:16 +01:00
parent 97c5dc0a83
commit c382c2c3ba
1 changed files with 20 additions and 15 deletions

View File

@ -10,11 +10,16 @@
#include "Image.h" #include "Image.h"
#ifndef NDEBUG #ifndef NDEBUG
#define SIZECHECK(x, y) check_bounds((x), (y), m_width, m_height) // allow x,y to be 256 pixels outside of the image.
// this allows stuff like the playername labels to be drawn somewhat outside of the
// image (relying on gd's built in clipping) while still catching probable
// buggy draw operations
#define SIZECHECK_FUZZY(x, y) check_bounds((x), (y), m_width, m_height, 256)
#else #else
#define SIZECHECK(x, y) do {} while(0) #define SIZECHECK_FUZZY(x, y) do {} while(0)
#endif #endif
// ARGB but with inverted alpha // ARGB but with inverted alpha
static inline int color2int(Color c) static inline int color2int(Color c)
@ -35,18 +40,18 @@ static inline Color int2color(int c)
return c2; return c2;
} }
static inline void check_bounds(int x, int y, int width, int height) static inline void check_bounds(int x, int y, int width, int height, int border)
{ {
if(x < 0 || x >= width) { if(x < -border || x >= width + border) {
std::ostringstream oss; std::ostringstream oss;
oss << "Access outside image bounds (x), 0 < " oss << "Access outside image bounds (x), 0 < "
<< x << " < " << width << " is false."; << x << " < " << width << " is false.";
throw std::out_of_range(oss.str()); throw std::out_of_range(oss.str());
} }
if(y < 0 || y >= height) { if(y < -border || y >= height + border) {
std::ostringstream oss; std::ostringstream oss;
oss << "Access outside image bounds (y), 0 < " oss << "Access outside image bounds (y)," << -border << " 0 < "
<< y << " < " << height << " is false."; << y << " < " << height+border << " is false.";
throw std::out_of_range(oss.str()); throw std::out_of_range(oss.str());
} }
} }
@ -65,39 +70,39 @@ Image::~Image()
void Image::setPixel(int x, int y, const Color &c) void Image::setPixel(int x, int y, const Color &c)
{ {
SIZECHECK(x, y); SIZECHECK_FUZZY(x, y);
m_image->tpixels[y][x] = color2int(c); m_image->tpixels[y][x] = color2int(c);
} }
Color Image::getPixel(int x, int y) Color Image::getPixel(int x, int y)
{ {
SIZECHECK(x, y); SIZECHECK_FUZZY(x, y);
return int2color(m_image->tpixels[y][x]); return int2color(m_image->tpixels[y][x]);
} }
void Image::drawLine(int x1, int y1, int x2, int y2, const Color &c) void Image::drawLine(int x1, int y1, int x2, int y2, const Color &c)
{ {
SIZECHECK(x1, y1); SIZECHECK_FUZZY(x1, y1);
SIZECHECK(x2, y2); SIZECHECK_FUZZY(x2, y2);
gdImageLine(m_image, x1, y1, x2, y2, color2int(c)); gdImageLine(m_image, x1, y1, x2, y2, color2int(c));
} }
void Image::drawText(int x, int y, const std::string &s, const Color &c) void Image::drawText(int x, int y, const std::string &s, const Color &c)
{ {
SIZECHECK(x, y); SIZECHECK_FUZZY(x, y);
gdImageString(m_image, gdFontGetMediumBold(), x, y, (unsigned char*) s.c_str(), color2int(c)); gdImageString(m_image, gdFontGetMediumBold(), x, y, (unsigned char*) s.c_str(), color2int(c));
} }
void Image::drawFilledRect(int x, int y, int w, int h, const Color &c) void Image::drawFilledRect(int x, int y, int w, int h, const Color &c)
{ {
SIZECHECK(x, y); SIZECHECK_FUZZY(x, y);
SIZECHECK(x + w - 1, y + h - 1); SIZECHECK_FUZZY(x + w - 1, y + h - 1);
gdImageFilledRectangle(m_image, x, y, x + w - 1, y + h - 1, color2int(c)); gdImageFilledRectangle(m_image, x, y, x + w - 1, y + h - 1, color2int(c));
} }
void Image::drawCircle(int x, int y, int diameter, const Color &c) void Image::drawCircle(int x, int y, int diameter, const Color &c)
{ {
SIZECHECK(x, y); SIZECHECK_FUZZY(x, y);
gdImageArc(m_image, x, y, diameter, diameter, 0, 360, color2int(c)); gdImageArc(m_image, x, y, diameter, diameter, 0, 360, color2int(c));
} }