diff --git a/client/render/Canvas.cpp b/client/render/Canvas.cpp index 87e27df2c..971b6ebc9 100644 --- a/client/render/Canvas.cpp +++ b/client/render/Canvas.cpp @@ -54,8 +54,8 @@ Canvas::Canvas(const Canvas & other, const Rect & newClipRect): Canvas::Canvas(const Point & size, CanvasScalingPolicy scalingPolicy): scalingPolicy(scalingPolicy), - renderArea(Point(0,0), size * getScalingFactor()), - surface(CSDL_Ext::newSurface(size * getScalingFactor())) + surface(CSDL_Ext::newSurface(size * getScalingFactor())), + renderArea(Point(0,0), size * getScalingFactor()) { CSDL_Ext::fillSurface(surface, CSDL_Ext::toSDL(Colors::TRANSPARENCY) ); SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE); diff --git a/client/render/IFont.cpp b/client/render/IFont.cpp index efe7aaa75..3567a4da9 100644 --- a/client/render/IFont.cpp +++ b/client/render/IFont.cpp @@ -41,13 +41,13 @@ void IFont::renderTextLeft(SDL_Surface * surface, const std::string & data, cons void IFont::renderTextRight(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const { - Point size = Point((int)getStringWidth(data), (int)getLineHeight()) * getScalingFactor(); + Point size = Point(getStringWidth(data), getLineHeight()) * getScalingFactor(); renderText(surface, data, color, pos - size); } void IFont::renderTextCenter(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const { - Point size = Point((int)getStringWidth(data), (int)getLineHeight()) * getScalingFactor(); + Point size = Point(getStringWidth(data), getLineHeight()) * getScalingFactor(); renderText(surface, data, color, pos - size / 2); } @@ -65,7 +65,7 @@ void IFont::renderTextLinesLeft(SDL_Surface * surface, const std::vector & data, const ColorRGBA & color, const Point & pos) const { Point currPos = pos; - currPos.y -= (int)data.size() * (int)getLineHeight() * getScalingFactor(); + currPos.y -= data.size() * getLineHeight() * getScalingFactor(); for(const std::string & line : data) { @@ -77,7 +77,7 @@ void IFont::renderTextLinesRight(SDL_Surface * surface, const std::vector & data, const ColorRGBA & color, const Point & pos) const { Point currPos = pos; - currPos.y -= (int)data.size() * (int)getLineHeight() / 2 * getScalingFactor(); + currPos.y -= data.size() * getLineHeight() / 2 * getScalingFactor(); for(const std::string & line : data) { diff --git a/client/render/IFont.h b/client/render/IFont.h index 38e67afb8..8810c4931 100644 --- a/client/render/IFont.h +++ b/client/render/IFont.h @@ -16,7 +16,7 @@ VCMI_LIB_NAMESPACE_END struct SDL_Surface; -class IFont +class IFont : boost::noncopyable { protected: /// Internal function to render font, see renderTextLeft diff --git a/client/renderSDL/CBitmapFont.cpp b/client/renderSDL/CBitmapFont.cpp index 852982cbe..8a3f7eb05 100644 --- a/client/renderSDL/CBitmapFont.cpp +++ b/client/renderSDL/CBitmapFont.cpp @@ -34,7 +34,7 @@ struct AtlasLayout /// Attempts to pack provided list of images into 2d box of specified size /// Returns resulting layout on success and empty optional on failure -static std::optional tryAtlasPacking(Point dimensions, std::map images) +static std::optional tryAtlasPacking(Point dimensions, const std::map & images) { // Simple atlas packing algorithm. Can be extended if needed, however optimal solution is NP-complete problem, so 'perfect' solution is too costly @@ -71,9 +71,9 @@ static std::optional tryAtlasPacking(Point dimensions, std::map images) +static AtlasLayout doAtlasPacking(const std::map & images) { // initial size of an atlas. Smaller size won't even fit tiniest H3 font Point dimensions(128, 128); @@ -181,20 +181,19 @@ CBitmapFont::CBitmapFont(const std::string & filename): storedEntry.rightOffset = symbol.second.rightOffset; storedEntry.positionInAtlas = atlas.images.at(symbol.first); + // Copy pixel data to atlas + uint8_t *dstPixels = static_cast(atlasImage->pixels); + uint8_t *dstLine = dstPixels + storedEntry.positionInAtlas.y * atlasImage->pitch; + uint8_t *dst = dstLine + storedEntry.positionInAtlas.x; + + for (size_t i = 0; i < storedEntry.positionInAtlas.h; ++i) { - // Copy pixel data to atlas - uint8_t *dstPixels = (uint8_t*)atlasImage->pixels; - uint8_t *dstLine = dstPixels + storedEntry.positionInAtlas.y * atlasImage->pitch; - uint8_t *dst = dstLine + storedEntry.positionInAtlas.x; + const uint8_t *srcPtr = symbol.second.pixels.data() + i * storedEntry.positionInAtlas.w; + uint8_t * dstPtr = dst + i * atlasImage->pitch; - for (size_t i = 0; i < storedEntry.positionInAtlas.h; ++i) - { - const uint8_t *srcPtr = symbol.second.pixels.data() + i * storedEntry.positionInAtlas.w; - uint8_t * dstPtr = dst + i * atlasImage->pitch; - - std::copy_n(srcPtr, storedEntry.positionInAtlas.w, dstPtr); - } + std::copy_n(srcPtr, storedEntry.positionInAtlas.w, dstPtr); } + chars[symbol.first] = storedEntry; } @@ -256,7 +255,6 @@ void CBitmapFont::renderCharacter(SDL_Surface * surface, const BitmapChar & char if (atlasImage->format->palette) SDL_SetPaletteColors(atlasImage->format->palette, &sdlColor, 255, 1); -// atlasImage->format->palette->colors[255] = CSDL_Ext::toSDL(color); else SDL_SetSurfaceColorMod(atlasImage, color.r, color.g, color.b); diff --git a/client/renderSDL/CBitmapFont.h b/client/renderSDL/CBitmapFont.h index 8ef49d71d..fc5a8253a 100644 --- a/client/renderSDL/CBitmapFont.h +++ b/client/renderSDL/CBitmapFont.h @@ -17,7 +17,7 @@ VCMI_LIB_NAMESPACE_BEGIN class ResourcePath; VCMI_LIB_NAMESPACE_END -class CBitmapFont : public IFont +class CBitmapFont final : public IFont { SDL_Surface * atlasImage; diff --git a/client/renderSDL/CBitmapHanFont.h b/client/renderSDL/CBitmapHanFont.h index 707c360b2..5181787b7 100644 --- a/client/renderSDL/CBitmapHanFont.h +++ b/client/renderSDL/CBitmapHanFont.h @@ -18,7 +18,7 @@ VCMI_LIB_NAMESPACE_END class CBitmapFont; /// supports multi-byte characters for such languages like Chinese -class CBitmapHanFont : public IFont +class CBitmapHanFont final : public IFont { std::unique_ptr fallback; // data, directly copied from file diff --git a/client/renderSDL/CTrueTypeFont.h b/client/renderSDL/CTrueTypeFont.h index 804217a0b..75d19f0e6 100644 --- a/client/renderSDL/CTrueTypeFont.h +++ b/client/renderSDL/CTrueTypeFont.h @@ -19,7 +19,7 @@ class CBitmapFont; using TTF_Font = struct _TTF_Font; -class CTrueTypeFont : public IFont +class CTrueTypeFont final : public IFont { std::unique_ptr fallbackFont; const std::pair, ui64> data; diff --git a/client/renderSDL/SDLImage.cpp b/client/renderSDL/SDLImage.cpp index a4b68601e..be5e0228f 100644 --- a/client/renderSDL/SDLImage.cpp +++ b/client/renderSDL/SDLImage.cpp @@ -26,7 +26,7 @@ class SDLImageLoader; //First 8 colors in def palette used for transparency -static const SDL_Color sourcePalette[8] = { +static constexpr std::array sourcePalette = {{ {0, 255, 255, SDL_ALPHA_OPAQUE}, {255, 150, 255, SDL_ALPHA_OPAQUE}, {255, 100, 255, SDL_ALPHA_OPAQUE}, @@ -35,9 +35,9 @@ static const SDL_Color sourcePalette[8] = { {255, 255, 0, SDL_ALPHA_OPAQUE}, {180, 0, 255, SDL_ALPHA_OPAQUE}, {0, 255, 0, SDL_ALPHA_OPAQUE} -}; +}}; -static const ColorRGBA targetPalette[8] = { +static constexpr std::array targetPalette = {{ {0, 0, 0, 0 }, // 0 - transparency ( used in most images ) {0, 0, 0, 64 }, // 1 - shadow border ( used in battle, adventure map def's ) {0, 0, 0, 64 }, // 2 - shadow border ( used in fog-of-war def's ) @@ -46,7 +46,7 @@ static const ColorRGBA targetPalette[8] = { {0, 0, 0, 0 }, // 5 - selection / owner flag ( used in battle, adventure map def's ) {0, 0, 0, 128}, // 6 - shadow body below selection ( used in battle def's ) {0, 0, 0, 64 } // 7 - shadow border below selection ( used in battle def's ) -}; +}}; static ui8 mixChannels(ui8 c1, ui8 c2, ui8 a1, ui8 a2) { @@ -59,7 +59,7 @@ static ColorRGBA addColors(const ColorRGBA & base, const ColorRGBA & over) mixChannels(over.r, base.r, over.a, base.a), mixChannels(over.g, base.g, over.a, base.a), mixChannels(over.b, base.b, over.a, base.a), - ui8(over.a + base.a * (255 - over.a) / 256) + static_cast(over.a + base.a * (255 - over.a) / 256) ); } @@ -88,7 +88,7 @@ int IImage::height() const return dimensions().y; } -SDLImageShared::SDLImageShared(CDefFile * data, size_t frame, size_t group) +SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group) : surf(nullptr), margins(0, 0), fullSize(0, 0), @@ -205,7 +205,7 @@ void SDLImageShared::optimizeSurface() { for (int y = 0; y < surf->h; ++y) { - const uint8_t * row = (uint8_t *)surf->pixels + y * surf->pitch; + const uint8_t * row = static_cast(surf->pixels) + y * surf->pitch; for (int x = 0; x < surf->w; ++x) { if (row[x] != 0) @@ -288,7 +288,7 @@ std::shared_ptr SDLImageShared::scaleInteger(int factor, SDL_Palet // TODO: compare performance and size of images, recheck values for potentially better parameters const int granulation = std::clamp(surf->h / 64 * 8, 8, 64); - tbb::parallel_for(tbb::blocked_range(0, intermediate->h, granulation), [&](const tbb::blocked_range & r) + tbb::parallel_for(tbb::blocked_range(0, intermediate->h, granulation), [factor, srcPixels, dstPixels, intermediate](const tbb::blocked_range & r) { xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, xbrz::ColorFormat::ARGB, {}, r.begin(), r.end()); }); diff --git a/client/renderSDL/SDLImage.h b/client/renderSDL/SDLImage.h index 0355357a1..81d93c8e6 100644 --- a/client/renderSDL/SDLImage.h +++ b/client/renderSDL/SDLImage.h @@ -42,7 +42,7 @@ class SDLImageShared final : public ISharedImage, public std::enable_shared_from public: //Load image from def file - SDLImageShared(CDefFile *data, size_t frame, size_t group=0); + SDLImageShared(const CDefFile *data, size_t frame, size_t group=0); //Load from bitmap file SDLImageShared(const ImagePath & filename); //Create using existing surface, extraRef will increase refcount on SDL_Surface diff --git a/client/renderSDL/SDL_Extensions.cpp b/client/renderSDL/SDL_Extensions.cpp index 97fc4509c..37b519eb1 100644 --- a/client/renderSDL/SDL_Extensions.cpp +++ b/client/renderSDL/SDL_Extensions.cpp @@ -675,7 +675,7 @@ SDL_Surface * CSDL_Ext::scaleSurfaceIntegerFactor(SDL_Surface * surf, int factor // TODO: compare performance and size of images, recheck values for potentially better parameters const int granulation = std::clamp(surf->h / 64 * 8, 8, 64); - tbb::parallel_for(tbb::blocked_range(0, intermediate->h, granulation), [&](const tbb::blocked_range & r) + tbb::parallel_for(tbb::blocked_range(0, intermediate->h, granulation), [factor, srcPixels, dstPixels, intermediate](const tbb::blocked_range & r) { xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, xbrz::ColorFormat::ARGB, {}, r.begin(), r.end()); });