X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgraphics%2FGauge.cpp;h=6925c2ff2482ae1a302d60a8d9f3bc77add64a7c;hb=147732d7eaf3c082b9120a7f2b815a4a7886aa97;hp=042dc2704b4705e7c152926582ce85924f3ce0ee;hpb=2e0bbc9fcc1cce3e1faeff85cc63de7f676e8077;p=l2e.git diff --git a/src/graphics/Gauge.cpp b/src/graphics/Gauge.cpp index 042dc27..6925c2f 100644 --- a/src/graphics/Gauge.cpp +++ b/src/graphics/Gauge.cpp @@ -7,117 +7,99 @@ #include "Gauge.h" -using geometry::Point; +#include "../loader/TypeDescription.h" + +using geometry::Vector; +using loader::FieldDescription; +using loader::TypeDescription; namespace graphics { -// TODO: add start and end "ignore" offsets -void Gauge::Draw(SDL_Surface *dest, Point position, int width, Uint8 level) const { - int fullWidth(width * level / 255); +void Gauge::Draw(SDL_Surface *dest, const Vector &position, int width, Uint8 fill) const { SDL_Rect srcRect, destRect; + 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; destRect.x = position.X(); destRect.y = position.Y(); // full part - if (fullWidth >= startWidth) { // completely filled - srcRect.x = fullX; - srcRect.y = fullY; - srcRect.w = startWidth; - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - } else if (fullWidth > 0) { // partially filled - srcRect.x = fullX; - srcRect.y = fullY; - srcRect.w = fullWidth; + if (fill == 0) { + srcRect.x = emptyOffset.X(); + srcRect.y = emptyOffset.Y(); SDL_BlitSurface(surface, &srcRect, dest, &destRect); - } - // empty part - if (fullWidth == 0) { // completely empty - srcRect.x = emptyX; - srcRect.y = emptyY; - srcRect.w = startWidth; - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - } else if (fullWidth < startWidth) { // partially empty - srcRect.x = emptyX + fullWidth; - srcRect.y = emptyY; - srcRect.w = startWidth - fullWidth; - destRect.x = position.X() + fullWidth; + } else { + srcRect.x = fullOffset.X(); + srcRect.y = fullOffset.Y(); SDL_BlitSurface(surface, &srcRect, dest, &destRect); } destRect.x = position.X() + startWidth; // fill - if (fullWidth >= width - endWidth) { // completely filled - srcRect.x = fullX + startWidth; - srcRect.y = fullY; - srcRect.w = repeatWidth; - int fillWidth(width - startWidth - endWidth); - int fillCursor(0); - while (fillCursor < fillWidth) { - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - destRect.x += repeatWidth; - fillCursor += repeatWidth; - } - } else if (fullWidth <= startWidth) { // completely empty - srcRect.x = emptyX + startWidth; - srcRect.y = emptyY; - srcRect.w = repeatWidth; - int fillWidth(width - startWidth - endWidth); - int fillCursor(0); - while (fillCursor < fillWidth) { - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - destRect.x += repeatWidth; - fillCursor += repeatWidth; - } - } else { // partially filled/empty - srcRect.x = fullX + startWidth; - srcRect.y = fullY; - srcRect.w = repeatWidth; - int fillCursor(0); - while (fillCursor < fullWidth) { - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - destRect.x += repeatWidth; - fillCursor += repeatWidth; - } - srcRect.x = emptyX + startWidth; - srcRect.y = emptyY; - int remaining(width - startWidth - endWidth); - while (fillCursor < remaining) { - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - destRect.x += repeatWidth; - fillCursor += repeatWidth; - } - } - - // end - // full part - if (fullWidth >= width) { // completely filled - srcRect.x = fullX + startWidth + repeatWidth; - srcRect.y = fullY; - srcRect.w = endWidth; + srcRect.x = fullOffset.X() + startWidth; + srcRect.y = fullOffset.Y(); + srcRect.w = repeatWidth; + while (filledWidth > repeatWidth) { SDL_BlitSurface(surface, &srcRect, dest, &destRect); - } else if (fullWidth > (width - endWidth)) { // partially filled - srcRect.x = fullX + startWidth + repeatWidth; - srcRect.y = fullY; - srcRect.w = endWidth - width + fullWidth; + destRect.x += repeatWidth; + filledWidth -= repeatWidth; + } + srcRect.x = emptyOffset.X() + startWidth; + srcRect.y = emptyOffset.Y(); + while (emptyWidth > repeatWidth) { SDL_BlitSurface(surface, &srcRect, dest, &destRect); + destRect.x += repeatWidth; + emptyWidth -= repeatWidth; } - // empty part - if (fullWidth <= width - endWidth) { // completely empty - srcRect.x = emptyX + startWidth + repeatWidth; - srcRect.y = emptyY; - srcRect.w = startWidth; + + // end + srcRect.w = endWidth; + if (fill == 255) { + srcRect.x = fullOffset.X() + startWidth + repeatWidth; + srcRect.y = fullOffset.Y(); SDL_BlitSurface(surface, &srcRect, dest, &destRect); - } else if (fullWidth > (width - endWidth) && fullWidth < width) { // partially empty - srcRect.x = emptyX + startWidth + repeatWidth + fullWidth - width + endWidth; - srcRect.y = emptyY; - srcRect.w = width - fullWidth; - destRect.x = position.X() + fullWidth; + } else { + srcRect.x = emptyOffset.X() + startWidth + repeatWidth; + srcRect.y = emptyOffset.Y(); SDL_BlitSurface(surface, &srcRect, dest, &destRect); } } + +void Gauge::CreateTypeDescription() { + Gauge g; + + int imageId(TypeDescription::GetTypeId("Image")); + int numberId(TypeDescription::GetTypeId("Number")); + int vectorId(TypeDescription::GetTypeId("Vector")); + + TypeDescription &td(TypeDescription::CreateOrGet("Gauge")); + td.SetSize(sizeof(Gauge)); + + td.AddField("image", FieldDescription(((char *)&g.surface) - ((char *)&g), imageId, true)); + td.AddField("full", FieldDescription(((char *)&g.fullOffset) - ((char *)&g), vectorId, false)); + td.AddField("empty", FieldDescription(((char *)&g.emptyOffset) - ((char *)&g), vectorId, false)); + td.AddField("height", FieldDescription(((char *)&g.height) - ((char *)&g), numberId, false)); + td.AddField("start", FieldDescription(((char *)&g.startWidth) - ((char *)&g), numberId, false)); + td.AddField("repeat", FieldDescription(((char *)&g.repeatWidth) - ((char *)&g), numberId, false)); + td.AddField("end", FieldDescription(((char *)&g.endWidth) - ((char *)&g), numberId, false)); +} + }