From 9df565b73cf3947f37fd25fe5a4e043ed7bc53bc Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 17 Jul 2014 22:15:50 +0200 Subject: [PATCH] Made rect+color the preferred argument style for (filled) rectangle drawing This is the opposite of the old situation, when the structs were unraveled. The definitions for the alternative styles were moved to the header, so the compiler has more opportunities for optimizing the conversions. --- src/surface.cpp | 24 ++++++------------------ src/surface.h | 24 +++++++++++++++++------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/surface.cpp b/src/surface.cpp index 4d98736..fac7a93 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -150,28 +150,16 @@ void Surface::blitRight(Surface *destination, int x, int y, int w, int h, int a) blitRight(destination->raw,x,y,w,h,a); } -void Surface::box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { - boxRGBA(raw, x, y, x + w - 1, y + h - 1, r, g, b, a); -} -void Surface::box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b) { - SDL_Rect re = { x, y, w, h }; - SDL_FillRect(raw, &re, SDL_MapRGBA(raw->format, r, g, b, 255)); -} -void Surface::box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, RGBAColor c) { - box(x, y, w, h, c.r, c.g, c.b, c.a); -} void Surface::box(SDL_Rect re, RGBAColor c) { - boxRGBA(raw, re.x, re.y, re.x + re.w - 1, re.y + re.h - 1, c.r, c.g, c.b, c.a); + if (c.a == 255) { + SDL_FillRect(raw, &re, c.pixelValue(raw->format)); + } else if (c.a != 0) { + boxRGBA(raw, re.x, re.y, re.x + re.w - 1, re.y + re.h - 1, c.r, c.g, c.b, c.a); + } } -void Surface::rectangle(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { - rectangleRGBA(raw, x, y, x + w - 1, y + h - 1, r, g, b, a); -} -void Surface::rectangle(Sint16 x, Sint16 y, Uint16 w, Uint16 h, RGBAColor c) { - rectangle(x, y, w, h, c.r, c.g, c.b, c.a); -} void Surface::rectangle(SDL_Rect re, RGBAColor c) { - rectangle(re.x, re.y, re.w, re.h, c.r, c.g, c.b, c.a); + rectangleRGBA(raw, re.x, re.y, re.x + re.w - 1, re.y + re.h - 1, c.r, c.g, c.b, c.a); } void Surface::clearClipRect() { diff --git a/src/surface.h b/src/surface.h index f81a073..6cb66c1 100644 --- a/src/surface.h +++ b/src/surface.h @@ -33,6 +33,9 @@ struct RGBAColor { RGBAColor() : r(0), g(0), b(0), a(0) {} RGBAColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) : r(r), g(g), b(b), a(a) {} + Uint32 pixelValue(SDL_PixelFormat *fmt) { + return SDL_MapRGBA(fmt, r, g, b, a); + } }; /** @@ -75,13 +78,20 @@ public: font->write(this, text, x, y, halign, valign); } - void box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a); - void box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b); - void box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, RGBAColor); - void box(SDL_Rect, RGBAColor); - void rectangle(Sint16, Sint16, Uint16, Uint16, Uint8 r, Uint8 g, Uint8 b, Uint8 a); - void rectangle(Sint16, Sint16, Uint16, Uint16, RGBAColor); - void rectangle(SDL_Rect, RGBAColor); + void box(SDL_Rect re, RGBAColor c); + void box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, RGBAColor c) { + box((SDL_Rect){ x, y, w, h }, c); + } + void box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { + box((SDL_Rect){ x, y, w, h }, RGBAColor(r, g, b, a)); + } + void rectangle(SDL_Rect re, RGBAColor c); + void rectangle(Sint16 x, Sint16 y, Uint16 w, Uint16 h, RGBAColor c) { + rectangle((SDL_Rect){ x, y, w, h }, c); + } + void rectangle(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { + rectangle((SDL_Rect){ x, y, w, h }, RGBAColor(r, g, b, a)); + } private: Surface(SDL_Surface *raw, bool freeWhenDone);