From 7025cfe3bfb136c2e949cb9dcd8c7603376844a0 Mon Sep 17 00:00:00 2001 From: Martijn Versteegh Date: Wed, 14 Nov 2018 12:17:16 +0100 Subject: [PATCH] Allow the playername labels/markers to cross image edges. Also the orgin circle. --- Image.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/Image.cpp b/Image.cpp index dc6eacc..5f1936d 100644 --- a/Image.cpp +++ b/Image.cpp @@ -10,11 +10,16 @@ #include "Image.h" #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 -#define SIZECHECK(x, y) do {} while(0) +#define SIZECHECK_FUZZY(x, y) do {} while(0) #endif + // ARGB but with inverted alpha static inline int color2int(Color c) @@ -35,18 +40,18 @@ static inline Color int2color(int c) 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; oss << "Access outside image bounds (x), 0 < " << x << " < " << width << " is false."; throw std::out_of_range(oss.str()); } - if(y < 0 || y >= height) { + if(y < -border || y >= height + border) { std::ostringstream oss; - oss << "Access outside image bounds (y), 0 < " - << y << " < " << height << " is false."; + oss << "Access outside image bounds (y)," << -border << " 0 < " + << y << " < " << height+border << " is false."; throw std::out_of_range(oss.str()); } } @@ -65,39 +70,39 @@ Image::~Image() void Image::setPixel(int x, int y, const Color &c) { - SIZECHECK(x, y); + SIZECHECK_FUZZY(x, y); m_image->tpixels[y][x] = color2int(c); } Color Image::getPixel(int x, int y) { - SIZECHECK(x, y); + SIZECHECK_FUZZY(x, y); return int2color(m_image->tpixels[y][x]); } void Image::drawLine(int x1, int y1, int x2, int y2, const Color &c) { - SIZECHECK(x1, y1); - SIZECHECK(x2, y2); + SIZECHECK_FUZZY(x1, y1); + SIZECHECK_FUZZY(x2, y2); gdImageLine(m_image, x1, y1, x2, y2, color2int(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)); } void Image::drawFilledRect(int x, int y, int w, int h, const Color &c) { - SIZECHECK(x, y); - SIZECHECK(x + w - 1, y + h - 1); + SIZECHECK_FUZZY(x, y); + SIZECHECK_FUZZY(x + w - 1, y + h - 1); 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) { - SIZECHECK(x, y); + SIZECHECK_FUZZY(x, y); gdImageArc(m_image, x, y, diameter, diameter, 0, 360, color2int(c)); }