From f6d399c08f0878459bcd1c6b3f4958f300243a71 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 31 Aug 2012 21:52:33 +0200 Subject: [PATCH] made Gauge default constructible --- src/graphics/Gauge.cpp | 36 ++++++++++++++++++++++++------------ src/graphics/Gauge.h | 19 +++++++++++++------ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/graphics/Gauge.cpp b/src/graphics/Gauge.cpp index eda9942..0d72ac0 100644 --- a/src/graphics/Gauge.cpp +++ b/src/graphics/Gauge.cpp @@ -17,6 +17,18 @@ void Gauge::Draw(SDL_Surface *dest, const Vector &position, int width, Uint int filledWidth = fill * (width - startWidth - endWidth) / 255; int emptyWidth = (255 - fill) * (width - startWidth - endWidth) / 255; + if (!surface) { + destRect.x = position.X(); + destRect.y = position.Y(); + destRect.w = filledWidth + startWidth; + destRect.h = height; + SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0x00)); + destRect.x += destRect.w; + destRect.w = emptyWidth + endWidth; + SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00)); + return; + } + // start srcRect.w = startWidth; srcRect.h = height; @@ -24,28 +36,28 @@ void Gauge::Draw(SDL_Surface *dest, const Vector &position, int width, Uint destRect.y = position.Y(); // full part if (fill == 0) { - srcRect.x = emptyX; - srcRect.y = emptyY; + srcRect.x = emptyOffset.X(); + srcRect.y = emptyOffset.Y(); SDL_BlitSurface(surface, &srcRect, dest, &destRect); } else { - srcRect.x = fullX; - srcRect.y = fullY; + srcRect.x = fullOffset.X(); + srcRect.y = fullOffset.Y(); SDL_BlitSurface(surface, &srcRect, dest, &destRect); } destRect.x = position.X() + startWidth; // fill - srcRect.x = fullX + startWidth; - srcRect.y = fullY; + srcRect.x = fullOffset.X() + startWidth; + srcRect.y = fullOffset.Y(); srcRect.w = repeatWidth; while (filledWidth > repeatWidth) { SDL_BlitSurface(surface, &srcRect, dest, &destRect); destRect.x += repeatWidth; filledWidth -= repeatWidth; } - srcRect.x = emptyX + startWidth; - srcRect.y = emptyY; + srcRect.x = emptyOffset.X() + startWidth; + srcRect.y = emptyOffset.Y(); while (emptyWidth > repeatWidth) { SDL_BlitSurface(surface, &srcRect, dest, &destRect); destRect.x += repeatWidth; @@ -55,12 +67,12 @@ void Gauge::Draw(SDL_Surface *dest, const Vector &position, int width, Uint // end srcRect.w = endWidth; if (fill == 255) { - srcRect.x = fullX + startWidth + repeatWidth; - srcRect.y = fullY; + srcRect.x = fullOffset.X() + startWidth + repeatWidth; + srcRect.y = fullOffset.Y(); SDL_BlitSurface(surface, &srcRect, dest, &destRect); } else { - srcRect.x = emptyX + startWidth + repeatWidth; - srcRect.y = emptyY; + srcRect.x = emptyOffset.X() + startWidth + repeatWidth; + srcRect.y = emptyOffset.Y(); SDL_BlitSurface(surface, &srcRect, dest, &destRect); } diff --git a/src/graphics/Gauge.h b/src/graphics/Gauge.h index fd81877..3251ec2 100644 --- a/src/graphics/Gauge.h +++ b/src/graphics/Gauge.h @@ -17,20 +17,27 @@ namespace graphics { class Gauge { public: - Gauge(SDL_Surface *s, int fullX, int fullY, int emptyX, int emptyY, int height, int startWidth, int repeatWidth, int endWidth) - : surface(s), fullX(fullX), fullY(fullY), emptyX(emptyX), emptyY(emptyY), height(height), startWidth(startWidth), repeatWidth(repeatWidth), endWidth(endWidth) { } + explicit Gauge(SDL_Surface *s = 0, int fullX = 0, int fullY = 0, int emptyX = 0, int emptyY = 0, int height = 1, int startWidth = 0, int repeatWidth = 1, int endWidth = 0) + : surface(s), fullOffset(fullX, fullY), emptyOffset(emptyX, emptyY), height(height), startWidth(startWidth), repeatWidth(repeatWidth), endWidth(endWidth) { } public: int MinWidth() const { return startWidth + endWidth; } int Height() const { return height; } void Draw(SDL_Surface *dest, const geometry::Vector &position, int width, Uint8 fill) const; +public: + void SetSurface(SDL_Surface *s) { surface = s; } + void SetFullOffset(const geometry::Vector &o) { fullOffset = o; } + void SetEmptyOffset(const geometry::Vector &o) { emptyOffset = o; } + void SetHeight(int h) { height = h; } + void SetStartWidth(int w) { startWidth = w; } + void SetRepeatWidth(int w) { repeatWidth = w; } + void SetEndWidth(int w) { endWidth = w; } + private: SDL_Surface *surface; - int fullX; - int fullY; - int emptyX; - int emptyY; + geometry::Vector fullOffset; + geometry::Vector emptyOffset; int height; int startWidth; int repeatWidth; -- 2.39.2