diff --git a/include/guisan/sdl/sdlpixel.hpp b/include/guisan/sdl/sdlpixel.hpp index 2e3da8f..3ff9709 100644 --- a/include/guisan/sdl/sdlpixel.hpp +++ b/include/guisan/sdl/sdlpixel.hpp @@ -73,47 +73,38 @@ namespace gcn */ inline const Color SDLgetPixel(SDL_Surface* surface, int x, int y) { - int bpp = surface->format->BytesPerPixel; + const int bpp = surface->format->BytesPerPixel; SDL_LockSurface(surface); - Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; + const Uint8* p = + reinterpret_cast(surface->pixels) + y * surface->pitch + x * bpp; unsigned int color = 0; - switch(bpp) + switch (bpp) { - case 1: - color = *p; - break; - - case 2: - color = *(Uint16 *)p; - break; - - case 3: - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) - { - color = p[0] << 16 | p[1] << 8 | p[2]; - } - else - { - color = p[0] | p[1] << 8 | p[2] << 16; - } - break; - - case 4: - color = *(Uint32 *)p; - break; + case 1: color = *p; break; + case 2: color = *reinterpret_cast(p); break; + + case 3: +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + color = p[0] << 16 | p[1] << 8 | p[2]; +#else + color = p[0] | p[1] << 8 | p[2] << 16; +#endif + break; + + case 4: color = *reinterpret_cast(p); break; } - unsigned char r,g,b,a; + unsigned char r, g, b, a; SDL_GetRGBA(color, surface->format, &r, &g, &b, &a); SDL_UnlockSurface(surface); - return Color(r,g,b,a); + return Color(r, g, b, a); } /** @@ -125,42 +116,33 @@ namespace gcn */ inline void SDLputPixel(SDL_Surface* surface, int x, int y, const Color& color) { - int bpp = surface->format->BytesPerPixel; + const int bpp = surface->format->BytesPerPixel; SDL_LockSurface(surface); - Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; + Uint8* p = reinterpret_cast(surface->pixels) + y * surface->pitch + x * bpp; Uint32 pixel = SDL_MapRGB(surface->format, color.r, color.g, color.b); - switch(bpp) + switch (bpp) { - case 1: - *p = pixel; - break; - - case 2: - *(Uint16 *)p = pixel; - break; - - case 3: - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) - { - p[0] = (pixel >> 16) & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = pixel & 0xff; - } - else - { - p[0] = pixel & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = (pixel >> 16) & 0xff; - } - break; - - case 4: - *(Uint32 *)p = pixel; - break; + case 1: *p = pixel; break; + + case 2: *reinterpret_cast(p) = pixel; break; + + case 3: +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + p[0] = static_cast((pixel >> 16) & 0xff); + p[1] = static_cast((pixel >> 8) & 0xff); + p[2] = static_cast(pixel & 0xff); +#else + p[0] = static_cast(pixel & 0xff); + p[1] = static_cast((pixel >> 8) & 0xff); + p[2] = static_cast((pixel >> 16) & 0xff); +#endif + break; + + case 4: *reinterpret_cast(p) = pixel; break; } SDL_UnlockSurface(surface); @@ -189,13 +171,14 @@ namespace gcn * @param dst the destination color. * @param a alpha. */ - inline unsigned short SDLAlpha16(unsigned short src, unsigned short dst, unsigned char a, const SDL_PixelFormat *f) + inline unsigned short + SDLAlpha16(unsigned short src, unsigned short dst, unsigned char a, const SDL_PixelFormat* f) { unsigned int b = ((src & f->Rmask) * a + (dst & f->Rmask) * (255 - a)) >> 8; unsigned int g = ((src & f->Gmask) * a + (dst & f->Gmask) * (255 - a)) >> 8; unsigned int r = ((src & f->Bmask) * a + (dst & f->Bmask) * (255 - a)) >> 8; - return (unsigned short)((b & f->Rmask) | (g & f->Gmask) | (r & f->Bmask)); + return (unsigned short) ((b & f->Rmask) | (g & f->Gmask) | (r & f->Bmask)); } /* @@ -220,54 +203,52 @@ namespace gcn */ inline void SDLputPixelAlpha(SDL_Surface* surface, int x, int y, const Color& color) { - int bpp = surface->format->BytesPerPixel; + const int bpp = surface->format->BytesPerPixel; SDL_LockSurface(surface); - Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; + Uint8* p = reinterpret_cast(surface->pixels) + y * surface->pitch + x * bpp; Uint32 pixel = SDL_MapRGB(surface->format, color.r, color.g, color.b); - switch(bpp) + switch (bpp) { - case 1: - *p = pixel; - break; - - case 2: - *(Uint16 *)p = SDLAlpha16(pixel, *(Uint32 *)p, color.a, surface->format); - break; - - case 3: - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) - { - unsigned int r = (p[0] * (255 - color.a) + color.r * color.a) >> 8; - unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8; - unsigned int b = (p[2] * (255 - color.a) + color.b * color.a) >> 8; - - p[2] = b; - p[1] = g; - p[0] = r; - } - else - { - unsigned int r = (p[2] * (255 - color.a) + color.r * color.a) >> 8; - unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8; - unsigned int b = (p[0] * (255 - color.a) + color.b * color.a) >> 8; - - p[0] = b; - p[1] = g; - p[2] = r; - } - break; - - case 4: - *(Uint32 *)p = SDLAlpha32(pixel, *(Uint32 *)p, color.a); - break; + case 1: *p = pixel; break; + + case 2: + *reinterpret_cast(p) = + SDLAlpha16(pixel, *reinterpret_cast(p), color.a, surface->format); + break; + + case 3: + { +#if (SDL_BYTEORDER == SDL_BIG_ENDIAN) + const unsigned int r = (p[0] * (255 - color.a) + color.r * color.a) >> 8; + const unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8; + const unsigned int b = (p[2] * (255 - color.a) + color.b * color.a) >> 8; + + p[2] = b; + p[1] = g; + p[0] = r; +#else + const unsigned int r = (p[2] * (255 - color.a) + color.r * color.a) >> 8; + const unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8; + const unsigned int b = (p[0] * (255 - color.a) + color.b * color.a) >> 8; + + p[0] = b; + p[1] = g; + p[2] = r; +#endif + break; + } + case 4: + *reinterpret_cast(p) = + SDLAlpha32(pixel, *reinterpret_cast(p), color.a); + break; } SDL_UnlockSurface(surface); } -} +} // namespace gcn #endif // end GCN_SDLPIXEL_HPP diff --git a/include/guisan/widgets/container.hpp b/include/guisan/widgets/container.hpp index a366849..dd84dfd 100644 --- a/include/guisan/widgets/container.hpp +++ b/include/guisan/widgets/container.hpp @@ -194,6 +194,7 @@ namespace gcn void draw(Graphics* graphics) override; Rectangle getChildrenArea() override; + void drawChildren(Graphics* graphics); protected: /** diff --git a/premake5.lua b/premake5.lua index 3725594..ff147f2 100644 --- a/premake5.lua +++ b/premake5.lua @@ -54,6 +54,8 @@ workspace "Guisan" warnings "Extra" --flags { "FatalWarnings"} + vpaths {['*'] = '*'} + filter { "action:vs*" } nuget(nugetPackages) defines { "_CRT_SECURE_NO_WARNINGS" } -- 4996: '$func': This function or variable may be unsafe. Consider using $func2 instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. @@ -67,8 +69,6 @@ workspace "Guisan" disablewarnings { "4018", -- '<': signed/unsigned mismatch "4100", -- '%var': unreferenced formal parameter - "4127", -- conditional expression is constant - "4189", -- '%var': local variable is initialized but not referenced "4244", -- '=': conversion from '$type1' to '$type2', possible loss of data "4458", -- declaration of '$var' hides class member } diff --git a/src/imagefont.cpp b/src/imagefont.cpp index 6080dac..8570339 100644 --- a/src/imagefont.cpp +++ b/src/imagefont.cpp @@ -140,7 +140,7 @@ namespace gcn int y = 0; for (int i = glyphsFrom; i < glyphsTo + 1; i++) { - mGlyph[i] = scanForGlyph(i, x, y, separator); + mGlyph[i] = scanForGlyph(static_cast(i), x, y, separator); // Update x and y with new coordinates. x = mGlyph[i].x + mGlyph[i].width; y = mGlyph[i].y; diff --git a/src/sdl/sdlgraphics.cpp b/src/sdl/sdlgraphics.cpp index cfae933..57b5022 100644 --- a/src/sdl/sdlgraphics.cpp +++ b/src/sdl/sdlgraphics.cpp @@ -69,16 +69,14 @@ // For some reason an old version of MSVC did not like std::abs, // so we added this macro. #ifndef ABS -#define ABS(x) ((x)<0?-(x):(x)) +# define ABS(x) ((x) < 0 ? -(x) : (x)) #endif namespace gcn { SDLGraphics::~SDLGraphics() - { - - } + {} void SDLGraphics::_beginDraw() { @@ -140,16 +138,16 @@ namespace gcn return mTarget; } - void SDLGraphics::drawImage(const Image* image, int srcX, - int srcY, int dstX, int dstY, - int width, int height) + void SDLGraphics::drawImage( + const Image* image, int srcX, int srcY, int dstX, int dstY, int width, int height) { - if (mClipStack.empty()) { - throw GCN_EXCEPTION("Clip stack is empty, perhaps you" - "called a draw function outside of _beginDraw() and _endDraw()?"); - } + if (mClipStack.empty()) + { + throw GCN_EXCEPTION("Clip stack is empty, perhaps you" + "called a draw function outside of _beginDraw() and _endDraw()?"); + } - const ClipRectangle& top = mClipStack.top(); + const ClipRectangle& top = mClipStack.top(); SDL_Rect src; SDL_Rect dst; src.x = srcX; @@ -171,10 +169,11 @@ namespace gcn void SDLGraphics::fillRectangle(const Rectangle& rectangle) { - if (mClipStack.empty()) { - throw GCN_EXCEPTION("Clip stack is empty, perhaps you" - "called a draw function outside of _beginDraw() and _endDraw()?"); - } + if (mClipStack.empty()) + { + throw GCN_EXCEPTION("Clip stack is empty, perhaps you" + "called a draw function outside of _beginDraw() and _endDraw()?"); + } const ClipRectangle& top = mClipStack.top(); @@ -182,7 +181,7 @@ namespace gcn area.x += top.xOffset; area.y += top.yOffset; - if(!area.isIntersecting(top)) + if (!area.isIntersecting(top)) { return; } @@ -191,8 +190,10 @@ namespace gcn { const int x1 = area.x > top.x ? area.x : top.x; const int y1 = area.y > top.y ? area.y : top.y; - const int x2 = area.x + area.width < top.x + top.width ? area.x + area.width : top.x + top.width; - const int y2 = area.y + area.height < top.y + top.height ? area.y + area.height : top.y + top.height; + const int x2 = + area.x + area.width < top.x + top.width ? area.x + area.width : top.x + top.width; + const int y2 = area.y + area.height < top.y + top.height ? area.y + area.height + : top.y + top.height; SDL_LockSurface(mTarget); for (int y = y1; y < y2; y++) @@ -212,25 +213,29 @@ namespace gcn rect.w = area.width; rect.h = area.height; - const Uint32 color = SDL_MapRGBA(mTarget->format, mColor.r, mColor.g, mColor.b, mColor.a); + const Uint32 color = + SDL_MapRGBA(mTarget->format, mColor.r, mColor.g, mColor.b, mColor.a); SDL_FillRect(mTarget, &rect, color); } } void SDLGraphics::drawPoint(int x, int y) { - if (mClipStack.empty()) { - throw GCN_EXCEPTION("Clip stack is empty, perhaps you" - "called a draw function outside of _beginDraw() and _endDraw()?"); - } + if (mClipStack.empty()) + { + throw GCN_EXCEPTION("Clip stack is empty, perhaps you" + "called a draw function outside of _beginDraw() and _endDraw()?"); + } const ClipRectangle& top = mClipStack.top(); x += top.xOffset; y += top.yOffset; - if(!top.isContaining(x,y)) + if (!top.isContaining(x, y)) + { return; + } if (mAlpha) { @@ -244,10 +249,11 @@ namespace gcn void SDLGraphics::drawHLine(int x1, int y, int x2) { - if (mClipStack.empty()) { - throw GCN_EXCEPTION("Clip stack is empty, perhaps you" - "called a draw function outside of _beginDraw() and _endDraw()?"); - } + if (mClipStack.empty()) + { + throw GCN_EXCEPTION("Clip stack is empty, perhaps you" + "called a draw function outside of _beginDraw() and _endDraw()?"); + } const ClipRectangle& top = mClipStack.top(); x1 += top.xOffset; @@ -255,7 +261,9 @@ namespace gcn x2 += top.xOffset; if (y < top.y || y >= top.y + top.height) + { return; + } if (x1 > x2) { @@ -279,74 +287,77 @@ namespace gcn { return; } - x2 = top.x + top.width -1; + x2 = top.x + top.width - 1; } const int bpp = mTarget->format->BytesPerPixel; SDL_LockSurface(mTarget); - Uint8 *p = (Uint8 *)mTarget->pixels + y * mTarget->pitch + x1 * bpp; + Uint8* p = reinterpret_cast(mTarget->pixels) + y * mTarget->pitch + x1 * bpp; Uint32 pixel = SDL_MapRGB(mTarget->format, mColor.r, mColor.g, mColor.b); - switch(bpp) { - case 1: - { - for (;x1 <= x2; ++x1) - { - *(p++) = pixel; - } - } break; - - case 2: - { - Uint16* q = (Uint16*)p; - for (;x1 <= x2; ++x1) - { - *(q++) = pixel; - } - } break; - - case 3: - { - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { - for (;x1 <= x2; ++x1) - { - p[0] = (pixel >> 16) & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = pixel & 0xff; - p += 3; - } - } - else - { - for (;x1 <= x2; ++x1) - { - p[0] = pixel & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = (pixel >> 16) & 0xff; - p += 3; - } - } - } break; - - case 4: - { - Uint32* q = (Uint32*)p; - for (;x1 <= x2; ++x1) - { - if (mAlpha) - { - *q = SDLAlpha32(pixel,*q,mColor.a); - q++; - } - else - { - *(q++) = pixel; - } - } - } break; + switch (bpp) + { + case 1: + { + for (; x1 <= x2; ++x1) + { + *(p++) = pixel; + } + } + break; + + case 2: + { + Uint16* q = reinterpret_cast(p); + for (; x1 <= x2; ++x1) + { + *(q++) = pixel; + } + } + break; + + case 3: + { +#if (SDL_BYTEORDER == SDL_BIG_ENDIAN) + for (; x1 <= x2; ++x1) + { + p[0] = (pixel >> 16) & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = pixel & 0xff; + p += 3; + } +#else + for (; x1 <= x2; ++x1) + { + p[0] = pixel & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = (pixel >> 16) & 0xff; + p += 3; + } +#endif + } + break; + + case 4: + { + Uint32* q = reinterpret_cast(p); + for (; x1 <= x2; ++x1) + { + if (mAlpha) + { + *q = SDLAlpha32(pixel, *q, mColor.a); + q++; + } + else + { + *(q++) = pixel; + } + } + } + break; } // end switch @@ -355,10 +366,11 @@ namespace gcn void SDLGraphics::drawVLine(int x, int y1, int y2) { - if (mClipStack.empty()) { - throw GCN_EXCEPTION("Clip stack is empty, perhaps you" - "called a draw function outside of _beginDraw() and _endDraw()?"); - } + if (mClipStack.empty()) + { + throw GCN_EXCEPTION("Clip stack is empty, perhaps you" + "called a draw function outside of _beginDraw() and _endDraw()?"); + } const ClipRectangle& top = mClipStack.top(); x += top.xOffset; @@ -366,7 +378,9 @@ namespace gcn y2 += top.yOffset; if (x < top.x || x >= top.x + top.width) + { return; + } if (y1 > y2) { @@ -397,67 +411,71 @@ namespace gcn SDL_LockSurface(mTarget); - Uint8 *p = (Uint8 *)mTarget->pixels + y1 * mTarget->pitch + x * bpp; + Uint8* p = reinterpret_cast(mTarget->pixels) + y1 * mTarget->pitch + x * bpp; Uint32 pixel = SDL_MapRGB(mTarget->format, mColor.r, mColor.g, mColor.b); - switch(bpp) { - case 1: - { - for (;y1 <= y2; ++y1) - { - *p = pixel; - p += mTarget->pitch; - } - } break; - - case 2: - { - for (;y1 <= y2; ++y1) - { - *(Uint16*)p = pixel; - p += mTarget->pitch; - } - } break; - - case 3: - { - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { - for (;y1 <= y2; ++y1) - { - p[0] = (pixel >> 16) & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = pixel & 0xff; - p += mTarget->pitch; - } - } - else - { - for (;y1 <= y2; ++y1) - { - p[0] = pixel & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = (pixel >> 16) & 0xff; - p += mTarget->pitch; - } - } - } break; - - case 4: - { - for (;y1 <= y2; ++y1) - { - if (mAlpha) - { - *(Uint32*)p = SDLAlpha32(pixel,*(Uint32*)p,mColor.a); - } - else - { - *(Uint32*)p = pixel; - } - p += mTarget->pitch; - } - } break; + switch (bpp) + { + case 1: + { + for (; y1 <= y2; ++y1) + { + *p = pixel; + p += mTarget->pitch; + } + } + break; + + case 2: + { + for (; y1 <= y2; ++y1) + { + *reinterpret_cast(p) = pixel; + p += mTarget->pitch; + } + } + break; + + case 3: + { +#if (SDL_BYTEORDER == SDL_BIG_ENDIAN) + for (; y1 <= y2; ++y1) + { + p[0] = (pixel >> 16) & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = pixel & 0xff; + p += mTarget->pitch; + } +#else + for (; y1 <= y2; ++y1) + { + p[0] = pixel & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = (pixel >> 16) & 0xff; + p += mTarget->pitch; + } +#endif + } + break; + + case 4: + { + for (; y1 <= y2; ++y1) + { + if (mAlpha) + { + *reinterpret_cast(p) = + SDLAlpha32(pixel, *reinterpret_cast(p), mColor.a); + } + else + { + *reinterpret_cast(p) = pixel; + } + p += mTarget->pitch; + } + } + break; } // end switch SDL_UnlockSurface(mTarget); @@ -490,10 +508,11 @@ namespace gcn return; } - if (mClipStack.empty()) { - throw GCN_EXCEPTION("Clip stack is empty, perhaps you" - "called a draw function outside of _beginDraw() and _endDraw()?"); - } + if (mClipStack.empty()) + { + throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw function outside " + "of _beginDraw() and _endDraw()?"); + } const ClipRectangle& top = mClipStack.top(); x1 += top.xOffset; @@ -664,12 +683,12 @@ namespace gcn return mColor; } - void SDLGraphics::drawSDLSurface(SDL_Surface* surface, SDL_Rect source, - SDL_Rect destination) + void SDLGraphics::drawSDLSurface(SDL_Surface* surface, SDL_Rect source, SDL_Rect destination) { - if (mClipStack.empty()) { - throw GCN_EXCEPTION("Clip stack is empty, perhaps you" - "called a draw function outside of _beginDraw() and _endDraw()?"); + if (mClipStack.empty()) + { + throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw function outside " + "of _beginDraw() and _endDraw()?"); } const ClipRectangle& top = mClipStack.top(); @@ -678,4 +697,4 @@ namespace gcn SDL_BlitSurface(surface, &source, mTarget, &destination); } -} +} // namespace gcn diff --git a/src/sdl/sdltruetypefont.cpp b/src/sdl/sdltruetypefont.cpp index ac5e1f5..71788e5 100644 --- a/src/sdl/sdltruetypefont.cpp +++ b/src/sdl/sdltruetypefont.cpp @@ -107,10 +107,10 @@ namespace gcn SDL_Color sdlCol; if (enabled) { - sdlCol.r = mColor.r; - sdlCol.g = mColor.g; - sdlCol.b = mColor.b; - sdlCol.a = mColor.a; + sdlCol.r = static_cast(mColor.r); + sdlCol.g = static_cast(mColor.g); + sdlCol.b = static_cast(mColor.b); + sdlCol.a = static_cast(mColor.a); } else { diff --git a/src/widgets/container.cpp b/src/widgets/container.cpp index 0a3ed3a..9b5e9b5 100644 --- a/src/widgets/container.cpp +++ b/src/widgets/container.cpp @@ -67,9 +67,7 @@ namespace gcn { Container::~Container() - { - - } + = default; void Container::draw(Graphics* graphics) { @@ -78,6 +76,7 @@ namespace gcn graphics->setColor(getBaseColor()); graphics->fillRectangle(Rectangle(0, 0, getWidth(), getHeight())); } + drawChildren(graphics); } void Container::setOpaque(bool opaque) @@ -162,4 +161,34 @@ namespace gcn { return Rectangle(0, 0, getWidth(), getHeight()); } + + void Container::drawChildren(Graphics* graphics) + { + graphics->pushClipArea(getChildrenArea()); + + std::list children = getChildren(); + for (const auto& iter : children) + { + if (iter->isVisible()) + { + // If the widget has a frame, + // draw it before drawing the widget + if (iter->getFrameSize() > 0) + { + Rectangle rec = iter->getDimension(); + rec.x -= static_cast(iter->getFrameSize()); + rec.y -= static_cast(iter->getFrameSize()); + rec.width += static_cast(2 * iter->getFrameSize()); + rec.height += static_cast(2 * iter->getFrameSize()); + graphics->pushClipArea(rec); + iter->drawFrame(graphics); + graphics->popClipArea(); + } + graphics->pushClipArea(iter->getDimension()); + iter->draw(graphics); + graphics->popClipArea(); + } + } + graphics->popClipArea(); + } } // namespace gcn diff --git a/src/widgets/textfield.cpp b/src/widgets/textfield.cpp index eb20d1e..ffb9fe9 100644 --- a/src/widgets/textfield.cpp +++ b/src/widgets/textfield.cpp @@ -139,7 +139,6 @@ namespace gcn graphics->setFont(getFont()); - const Rectangle& dim = mText->getCaretDimension(getFont()); if (mText->getNumberOfRows() != 0) graphics->drawText(mText->getRow(0), 1 - mXScroll, 2, Graphics::Left, isEnabled());