浏览代码

Fixes for Sonar issues

Ivan Savenko 1 年之前
父节点
当前提交
b3158c52ba

+ 2 - 2
client/render/Canvas.cpp

@@ -54,8 +54,8 @@ Canvas::Canvas(const Canvas & other, const Rect & newClipRect):
 
 
 Canvas::Canvas(const Point & size, CanvasScalingPolicy scalingPolicy):
 Canvas::Canvas(const Point & size, CanvasScalingPolicy scalingPolicy):
 	scalingPolicy(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) );
 	CSDL_Ext::fillSurface(surface, CSDL_Ext::toSDL(Colors::TRANSPARENCY) );
 	SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
 	SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);

+ 4 - 4
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
 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);
 	renderText(surface, data, color, pos - size);
 }
 }
 
 
 void IFont::renderTextCenter(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
 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);
 	renderText(surface, data, color, pos - size / 2);
 }
 }
 
 
@@ -65,7 +65,7 @@ void IFont::renderTextLinesLeft(SDL_Surface * surface, const std::vector<std::st
 void IFont::renderTextLinesRight(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
 void IFont::renderTextLinesRight(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
 {
 {
 	Point currPos = pos;
 	Point currPos = pos;
-	currPos.y -= (int)data.size() * (int)getLineHeight() * getScalingFactor();
+	currPos.y -= data.size() * getLineHeight() * getScalingFactor();
 
 
 	for(const std::string & line : data)
 	for(const std::string & line : data)
 	{
 	{
@@ -77,7 +77,7 @@ void IFont::renderTextLinesRight(SDL_Surface * surface, const std::vector<std::s
 void IFont::renderTextLinesCenter(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
 void IFont::renderTextLinesCenter(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
 {
 {
 	Point currPos = pos;
 	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)
 	for(const std::string & line : data)
 	{
 	{

+ 1 - 1
client/render/IFont.h

@@ -16,7 +16,7 @@ VCMI_LIB_NAMESPACE_END
 
 
 struct SDL_Surface;
 struct SDL_Surface;
 
 
-class IFont
+class IFont : boost::noncopyable
 {
 {
 protected:
 protected:
 	/// Internal function to render font, see renderTextLeft
 	/// Internal function to render font, see renderTextLeft

+ 14 - 16
client/renderSDL/CBitmapFont.cpp

@@ -34,7 +34,7 @@ struct AtlasLayout
 
 
 /// Attempts to pack provided list of images into 2d box of specified size
 /// Attempts to pack provided list of images into 2d box of specified size
 /// Returns resulting layout on success and empty optional on failure
 /// Returns resulting layout on success and empty optional on failure
-static std::optional<AtlasLayout> tryAtlasPacking(Point dimensions, std::map<int, Point> images)
+static std::optional<AtlasLayout> tryAtlasPacking(Point dimensions, const std::map<int, Point> & images)
 {
 {
 	// Simple atlas packing algorithm. Can be extended if needed, however optimal solution is NP-complete problem, so 'perfect' solution is too costly
 	// 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<AtlasLayout> tryAtlasPacking(Point dimensions, std::map<int
 	return result;
 	return result;
 }
 }
 
 
-/// Arranges images to fit into texture atlas with automatic selection of iamge size
+/// Arranges images to fit into texture atlas with automatic selection of image size
 /// Returns images arranged into 2d box
 /// Returns images arranged into 2d box
-static AtlasLayout doAtlasPacking(std::map<int, Point> images)
+static AtlasLayout doAtlasPacking(const std::map<int, Point> & images)
 {
 {
 	// initial size of an atlas. Smaller size won't even fit tiniest H3 font
 	// initial size of an atlas. Smaller size won't even fit tiniest H3 font
 	Point dimensions(128, 128);
 	Point dimensions(128, 128);
@@ -181,20 +181,19 @@ CBitmapFont::CBitmapFont(const std::string & filename):
 		storedEntry.rightOffset = symbol.second.rightOffset;
 		storedEntry.rightOffset = symbol.second.rightOffset;
 		storedEntry.positionInAtlas = atlas.images.at(symbol.first);
 		storedEntry.positionInAtlas = atlas.images.at(symbol.first);
 
 
+		// Copy pixel data to atlas
+		uint8_t *dstPixels = static_cast<uint8_t*>(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;
-
-			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);
-			}
+			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);
 		}
 		}
+
 		chars[symbol.first] = storedEntry;
 		chars[symbol.first] = storedEntry;
 	}
 	}
 
 
@@ -256,7 +255,6 @@ void CBitmapFont::renderCharacter(SDL_Surface * surface, const BitmapChar & char
 
 
 	if (atlasImage->format->palette)
 	if (atlasImage->format->palette)
 		SDL_SetPaletteColors(atlasImage->format->palette, &sdlColor, 255, 1);
 		SDL_SetPaletteColors(atlasImage->format->palette, &sdlColor, 255, 1);
-//		atlasImage->format->palette->colors[255] = CSDL_Ext::toSDL(color);
 	else
 	else
 		SDL_SetSurfaceColorMod(atlasImage, color.r, color.g, color.b);
 		SDL_SetSurfaceColorMod(atlasImage, color.r, color.g, color.b);
 
 

+ 1 - 1
client/renderSDL/CBitmapFont.h

@@ -17,7 +17,7 @@ VCMI_LIB_NAMESPACE_BEGIN
 class ResourcePath;
 class ResourcePath;
 VCMI_LIB_NAMESPACE_END
 VCMI_LIB_NAMESPACE_END
 
 
-class CBitmapFont : public IFont
+class CBitmapFont final : public IFont
 {
 {
 	SDL_Surface * atlasImage;
 	SDL_Surface * atlasImage;
 
 

+ 1 - 1
client/renderSDL/CBitmapHanFont.h

@@ -18,7 +18,7 @@ VCMI_LIB_NAMESPACE_END
 class CBitmapFont;
 class CBitmapFont;
 
 
 /// supports multi-byte characters for such languages like Chinese
 /// supports multi-byte characters for such languages like Chinese
-class CBitmapHanFont : public IFont
+class CBitmapHanFont final : public IFont
 {
 {
 	std::unique_ptr<CBitmapFont> fallback;
 	std::unique_ptr<CBitmapFont> fallback;
 	// data, directly copied from file
 	// data, directly copied from file

+ 1 - 1
client/renderSDL/CTrueTypeFont.h

@@ -19,7 +19,7 @@ class CBitmapFont;
 
 
 using TTF_Font = struct _TTF_Font;
 using TTF_Font = struct _TTF_Font;
 
 
-class CTrueTypeFont : public IFont
+class CTrueTypeFont final : public IFont
 {
 {
 	std::unique_ptr<CBitmapFont> fallbackFont;
 	std::unique_ptr<CBitmapFont> fallbackFont;
 	const std::pair<std::unique_ptr<ui8[]>, ui64> data;
 	const std::pair<std::unique_ptr<ui8[]>, ui64> data;

+ 8 - 8
client/renderSDL/SDLImage.cpp

@@ -26,7 +26,7 @@
 class SDLImageLoader;
 class SDLImageLoader;
 
 
 //First 8 colors in def palette used for transparency
 //First 8 colors in def palette used for transparency
-static const SDL_Color sourcePalette[8] = {
+static constexpr std::array<SDL_Color, 8> sourcePalette = {{
 	{0,   255, 255, SDL_ALPHA_OPAQUE},
 	{0,   255, 255, SDL_ALPHA_OPAQUE},
 	{255, 150, 255, SDL_ALPHA_OPAQUE},
 	{255, 150, 255, SDL_ALPHA_OPAQUE},
 	{255, 100, 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},
 	{255, 255, 0,   SDL_ALPHA_OPAQUE},
 	{180, 0,   255, SDL_ALPHA_OPAQUE},
 	{180, 0,   255, SDL_ALPHA_OPAQUE},
 	{0,   255, 0,   SDL_ALPHA_OPAQUE}
 	{0,   255, 0,   SDL_ALPHA_OPAQUE}
-};
+}};
 
 
-static const ColorRGBA targetPalette[8] = {
+static constexpr std::array<ColorRGBA, 8> targetPalette = {{
 	{0, 0, 0, 0  }, // 0 - transparency                  ( used in most images )
 	{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 }, // 1 - shadow border                 ( used in battle, adventure map def's )
 	{0, 0, 0, 64 }, // 2 - shadow border                 ( used in fog-of-war 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, 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, 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 )
 	{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)
 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.r, base.r, over.a, base.a),
 		mixChannels(over.g, base.g, over.a, base.a),
 		mixChannels(over.g, base.g, over.a, base.a),
 		mixChannels(over.b, base.b, over.a, base.a),
 		mixChannels(over.b, base.b, over.a, base.a),
-		ui8(over.a + base.a * (255 - over.a) / 256)
+		static_cast<ui8>(over.a + base.a * (255 - over.a) / 256)
 		);
 		);
 }
 }
 
 
@@ -88,7 +88,7 @@ int IImage::height() const
 	return dimensions().y;
 	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),
 	: surf(nullptr),
 	margins(0, 0),
 	margins(0, 0),
 	fullSize(0, 0),
 	fullSize(0, 0),
@@ -205,7 +205,7 @@ void SDLImageShared::optimizeSurface()
 	{
 	{
 		for (int y = 0; y < surf->h; ++y)
 		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<uint8_t *>(surf->pixels) + y * surf->pitch;
 			for (int x = 0; x < surf->w; ++x)
 			for (int x = 0; x < surf->w; ++x)
 			{
 			{
 				if (row[x] != 0)
 				if (row[x] != 0)
@@ -288,7 +288,7 @@ std::shared_ptr<ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palet
 	// TODO: compare performance and size of images, recheck values for potentially better parameters
 	// TODO: compare performance and size of images, recheck values for potentially better parameters
 	const int granulation = std::clamp(surf->h / 64 * 8, 8, 64);
 	const int granulation = std::clamp(surf->h / 64 * 8, 8, 64);
 
 
-	tbb::parallel_for(tbb::blocked_range<size_t>(0, intermediate->h, granulation), [&](const tbb::blocked_range<size_t> & r)
+	tbb::parallel_for(tbb::blocked_range<size_t>(0, intermediate->h, granulation), [factor, srcPixels, dstPixels, intermediate](const tbb::blocked_range<size_t> & r)
 	{
 	{
 		xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, xbrz::ColorFormat::ARGB, {}, r.begin(), r.end());
 		xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, xbrz::ColorFormat::ARGB, {}, r.begin(), r.end());
 	});
 	});

+ 1 - 1
client/renderSDL/SDLImage.h

@@ -42,7 +42,7 @@ class SDLImageShared final : public ISharedImage, public std::enable_shared_from
 
 
 public:
 public:
 	//Load image from def file
 	//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
 	//Load from bitmap file
 	SDLImageShared(const ImagePath & filename);
 	SDLImageShared(const ImagePath & filename);
 	//Create using existing surface, extraRef will increase refcount on SDL_Surface
 	//Create using existing surface, extraRef will increase refcount on SDL_Surface

+ 1 - 1
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
 	// TODO: compare performance and size of images, recheck values for potentially better parameters
 	const int granulation = std::clamp(surf->h / 64 * 8, 8, 64);
 	const int granulation = std::clamp(surf->h / 64 * 8, 8, 64);
 
 
-	tbb::parallel_for(tbb::blocked_range<size_t>(0, intermediate->h, granulation), [&](const tbb::blocked_range<size_t> & r)
+	tbb::parallel_for(tbb::blocked_range<size_t>(0, intermediate->h, granulation), [factor, srcPixels, dstPixels, intermediate](const tbb::blocked_range<size_t> & r)
 	{
 	{
 		xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, xbrz::ColorFormat::ARGB, {}, r.begin(), r.end());
 		xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, xbrz::ColorFormat::ARGB, {}, r.begin(), r.end());
 	});
 	});