From 79dcc8a146f3db3120453b62ad338a0b51c04eea Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Sat, 19 Jul 2014 12:06:09 +0200 Subject: [PATCH] Implemented stream output for RGBAColor In commit 950518f3 I changed the component type of RGBAColor from 16-bit to 8-bit integers. Unfortunately, in C++ 8-bit integers are identical to characters, so this broke the writing of colors to output streams. --- src/gmenu2x.cpp | 11 ++--------- src/surface.cpp | 15 ++++++++++++++- src/surface.h | 7 +++++-- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 7e69604..878508a 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -549,15 +549,8 @@ void GMenu2X::writeSkinConfig() { int i; for (i = 0; i < NUM_COLORS; ++i) { - inf << colorToString((enum color)i) << "=#"; - inf.width(2); inf.fill('0'); - inf << right << hex << skinConfColors[i].r; - inf.width(2); inf.fill('0'); - inf << right << hex << skinConfColors[i].g; - inf.width(2); inf.fill('0'); - inf << right << hex << skinConfColors[i].b; - inf.width(2); inf.fill('0'); - inf << right << hex << skinConfColors[i].a << endl; + inf << colorToString((enum color)i) << "=#" + << skinConfColors[i] << endl; } inf.close(); diff --git a/src/surface.cpp b/src/surface.cpp index 48bb976..3b3a646 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -27,7 +27,7 @@ #include #include -#include +#include using namespace std; @@ -44,6 +44,19 @@ RGBAColor RGBAColor::fromString(const string &strColor) { }; } +ostream& operator<<(ostream& os, RGBAColor const& color) { + auto oldfill = os.fill('0'); + auto oldflags = os.setf(ios::hex | ios::right, + ios::basefield | ios::adjustfield); + os << setw(2) << uint32_t(color.r) + << setw(2) << uint32_t(color.g) + << setw(2) << uint32_t(color.b) + << setw(2) << uint32_t(color.a); + os.fill(oldfill); + os.setf(oldflags); + return os; +} + Surface *Surface::openOutputSurface(int width, int height, int bitsperpixel) { SDL_ShowCursor(SDL_DISABLE); SDL_Surface *raw = SDL_SetVideoMode( diff --git a/src/surface.h b/src/surface.h index ee9db66..5b7931c 100644 --- a/src/surface.h +++ b/src/surface.h @@ -24,8 +24,10 @@ #include "font.h" #include -#include + #include +#include +#include struct RGBAColor { uint8_t r, g, b, a; @@ -33,10 +35,11 @@ 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) { + Uint32 pixelValue(SDL_PixelFormat *fmt) const { return SDL_MapRGBA(fmt, r, g, b, a); } }; +std::ostream& operator<<(std::ostream& os, RGBAColor const& color); /** Wrapper around SDL_Surface