1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

fixing some sonar cloud issues

This commit is contained in:
Laserlicht
2025-07-12 13:23:46 +02:00
parent 2f00524439
commit f0c24c0ffa
2 changed files with 60 additions and 39 deletions

View File

@@ -16,13 +16,13 @@
struct SharedImageLocator struct SharedImageLocator
{ {
enum ShadowMode enum class ShadowMode
{ {
SHADOW_NONE, SHADOW_NONE,
SHADOW_NORMAL, SHADOW_NORMAL,
SHADOW_SHEAR SHADOW_SHEAR
}; };
enum OverlayMode enum class OverlayMode
{ {
OVERLAY_NONE, OVERLAY_NONE,
OVERLAY_OUTLINE, OVERLAY_OUTLINE,

View File

@@ -692,6 +692,10 @@ SDL_Surface * CSDL_Ext::drawOutline(SDL_Surface * source, const SDL_Color & colo
SDL_Surface *sourceSurface = SDL_ConvertSurfaceFormat(source, SDL_PIXELFORMAT_ARGB8888, 0); SDL_Surface *sourceSurface = SDL_ConvertSurfaceFormat(source, SDL_PIXELFORMAT_ARGB8888, 0);
SDL_Surface *destSurface = newSurface(Point(source->w, source->h)); SDL_Surface *destSurface = newSurface(Point(source->w, source->h));
// Lock surfaces for direct pixel access
if (SDL_MUSTLOCK(sourceSurface)) SDL_LockSurface(sourceSurface);
if (SDL_MUSTLOCK(destSurface)) SDL_LockSurface(destSurface);
int width = sourceSurface->w; int width = sourceSurface->w;
int height = sourceSurface->h; int height = sourceSurface->h;
@@ -721,7 +725,10 @@ SDL_Surface * CSDL_Ext::drawOutline(SDL_Surface * source, const SDL_Color & colo
{ {
// Get the pixel at the neighbor position // Get the pixel at the neighbor position
Uint32 pixel = *((Uint32*)sourceSurface->pixels + neighborY * width + neighborX); Uint32 pixel = *((Uint32*)sourceSurface->pixels + neighborY * width + neighborX);
Uint8 r, g, b, a; Uint8 r;
Uint8 g;
Uint8 b;
Uint8 a;
SDL_GetRGBA(pixel, sourceSurface->format, &r, &g, &b, &a); SDL_GetRGBA(pixel, sourceSurface->format, &r, &g, &b, &a);
// Compare the pixel alpha value to find the maximum and maximum // Compare the pixel alpha value to find the maximum and maximum
@@ -738,6 +745,9 @@ SDL_Surface * CSDL_Ext::drawOutline(SDL_Surface * source, const SDL_Color & colo
} }
} }
if (SDL_MUSTLOCK(sourceSurface)) SDL_UnlockSurface(sourceSurface);
if (SDL_MUSTLOCK(destSurface)) SDL_UnlockSurface(destSurface);
SDL_FreeSurface(sourceSurface); SDL_FreeSurface(sourceSurface);
return destSurface; return destSurface;
@@ -754,7 +764,7 @@ void applyAffineTransform(SDL_Surface* src, SDL_Surface* dst, double a, double b
// Calculate inverse matrix M_inv for mapping dst -> src // Calculate inverse matrix M_inv for mapping dst -> src
double det = a * d - b * c; double det = a * d - b * c;
if (det == 0) if (static_cast<int>(det) == 0)
throw std::runtime_error("Singular transform matrix!"); throw std::runtime_error("Singular transform matrix!");
double invDet = 1.0 / det; double invDet = 1.0 / det;
double ia = d * invDet; double ia = d * invDet;
@@ -772,14 +782,14 @@ void applyAffineTransform(SDL_Surface* src, SDL_Surface* dst, double a, double b
double srcY = ic * (x - tx) + id * (y - ty); double srcY = ic * (x - tx) + id * (y - ty);
// Nearest neighbor sampling (can be improved to bilinear) // Nearest neighbor sampling (can be improved to bilinear)
int srcXi = static_cast<int>(round(srcX)); auto srcXi = static_cast<int>(round(srcX));
int srcYi = static_cast<int>(round(srcY)); auto srcYi = static_cast<int>(round(srcY));
// Check bounds // Check bounds
if (srcXi >= 0 && srcXi < src->w && srcYi >= 0 && srcYi < src->h) if (srcXi >= 0 && srcXi < src->w && srcYi >= 0 && srcYi < src->h)
{ {
Uint32* srcPixels = (Uint32*)src->pixels; auto srcPixels = (Uint32*)src->pixels;
Uint32* dstPixels = (Uint32*)dst->pixels; auto dstPixels = (Uint32*)dst->pixels;
Uint32 pixel = srcPixels[srcYi * src->w + srcXi]; Uint32 pixel = srcPixels[srcYi * src->w + srcXi];
dstPixels[y * dst->w + x] = pixel; dstPixels[y * dst->w + x] = pixel;
@@ -787,7 +797,7 @@ void applyAffineTransform(SDL_Surface* src, SDL_Surface* dst, double a, double b
else else
{ {
// Outside source bounds: set transparent or black // Outside source bounds: set transparent or black
Uint32* dstPixels = (Uint32*)dst->pixels; auto dstPixels = (Uint32*)dst->pixels;
dstPixels[y * dst->w + x] = 0x00000000; // transparent black dstPixels[y * dst->w + x] = 0x00000000; // transparent black
} }
} }
@@ -801,13 +811,12 @@ int getLowestNonTransparentY(SDL_Surface* surface)
{ {
assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888); assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
if(SDL_MUSTLOCK(surface)) if(SDL_MUSTLOCK(surface)) SDL_LockSurface(surface);
SDL_LockSurface(surface);
int w = surface->w; int w = surface->w;
int h = surface->h; int h = surface->h;
int bpp = surface->format->BytesPerPixel; int bpp = surface->format->BytesPerPixel;
Uint8* pixels = (Uint8*)surface->pixels; auto pixels = (Uint8*)surface->pixels;
for(int y = h - 1; y >= 0; --y) for(int y = h - 1; y >= 0; --y)
{ {
@@ -817,7 +826,10 @@ int getLowestNonTransparentY(SDL_Surface* surface)
{ {
Uint32 pixel = *(Uint32*)(row + x * bpp); Uint32 pixel = *(Uint32*)(row + x * bpp);
Uint8 r, g, b, a; Uint8 r;
Uint8 g;
Uint8 b;
Uint8 a;
SDL_GetRGBA(pixel, surface->format, &r, &g, &b, &a); SDL_GetRGBA(pixel, surface->format, &r, &g, &b, &a);
if (a > 0) if (a > 0)
@@ -839,14 +851,16 @@ void fillAlphaPixelWithRGBA(SDL_Surface* surface, Uint8 r, Uint8 g, Uint8 b, Uin
if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface); if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface);
Uint32* pixels = (Uint32*)surface->pixels; auto pixels = (Uint32*)surface->pixels;
int pixelCount = surface->w * surface->h; int pixelCount = surface->w * surface->h;
for (int i = 0; i < pixelCount; i++) for (int i = 0; i < pixelCount; i++)
{ {
Uint32 pixel = pixels[i]; Uint32 pixel = pixels[i];
Uint8 pr;
Uint8 pr, pg, pb, pa; Uint8 pg;
Uint8 pb;
Uint8 pa;
// Extract existing RGBA components using SDL_GetRGBA // Extract existing RGBA components using SDL_GetRGBA
SDL_GetRGBA(pixel, surface->format, &pr, &pg, &pb, &pa); SDL_GetRGBA(pixel, surface->format, &pr, &pg, &pb, &pa);
@@ -866,17 +880,13 @@ void gaussianBlur(SDL_Surface* surface, int amount)
if (!surface || amount <= 0) return; if (!surface || amount <= 0) return;
if (SDL_MUSTLOCK(surface)) if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface);
{
if (SDL_LockSurface(surface) != 0)
throw std::runtime_error("Failed to lock surface!");
}
int width = surface->w; int width = surface->w;
int height = surface->h; int height = surface->h;
int pixelCount = width * height; int pixelCount = width * height;
Uint32* pixels = static_cast<Uint32*>(surface->pixels); auto pixels = static_cast<Uint32*>(surface->pixels);
std::vector<Uint8> srcR(pixelCount); std::vector<Uint8> srcR(pixelCount);
std::vector<Uint8> srcG(pixelCount); std::vector<Uint8> srcG(pixelCount);
@@ -894,7 +904,10 @@ void gaussianBlur(SDL_Surface* surface, int amount)
for (int x = 0; x < width; ++x) for (int x = 0; x < width; ++x)
{ {
Uint32 pixel = pixels[y * width + x]; Uint32 pixel = pixels[y * width + x];
Uint8 r, g, b, a; Uint8 r;
Uint8 g;
Uint8 b;
Uint8 a;
SDL_GetRGBA(pixel, surface->format, &r, &g, &b, &a); SDL_GetRGBA(pixel, surface->format, &r, &g, &b, &a);
int idx = y * width + x; int idx = y * width + x;
@@ -906,20 +919,28 @@ void gaussianBlur(SDL_Surface* surface, int amount)
} }
// 3x3 Gaussian kernel // 3x3 Gaussian kernel
float kernel[3][3] = { std::array<std::array<float, 3>, 3> kernel = {{
{1.f/16, 2.f/16, 1.f/16}, {{1.f/16, 2.f/16, 1.f/16}},
{2.f/16, 4.f/16, 2.f/16}, {{2.f/16, 4.f/16, 2.f/16}},
{1.f/16, 2.f/16, 1.f/16} {{1.f/16, 2.f/16, 1.f/16}}
}; }};
// Apply the blur 'amount' times for stronger blur // Apply the blur 'amount' times for stronger blur
for (int iteration = 0; iteration < amount; ++iteration) { for (int iteration = 0; iteration < amount; ++iteration)
for (int y = 0; y < height; ++y) { {
for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y)
float sumR = 0.f, sumG = 0.f, sumB = 0.f, sumA = 0.f; {
for (int x = 0; x < width; ++x)
{
float sumR = 0.f;
float sumG = 0.f;
float sumB = 0.f;
float sumA = 0.f;
for (int ky = -1; ky <= 1; ++ky) { for (int ky = -1; ky <= 1; ++ky)
for (int kx = -1; kx <= 1; ++kx) { {
for (int kx = -1; kx <= 1; ++kx)
{
int nx = x + kx; int nx = x + kx;
int ny = y + ky; int ny = y + ky;
@@ -954,16 +975,16 @@ void gaussianBlur(SDL_Surface* surface, int amount)
} }
// After final iteration, write back to surface pixels // After final iteration, write back to surface pixels
for (int y = 0; y < height; ++y) { for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x) { {
for (int x = 0; x < width; ++x)
{
int idx = y * width + x; int idx = y * width + x;
pixels[idx] = SDL_MapRGBA(surface->format, srcR[idx], srcG[idx], srcB[idx], srcA[idx]); pixels[idx] = SDL_MapRGBA(surface->format, srcR[idx], srcG[idx], srcB[idx], srcA[idx]);
} }
} }
if (SDL_MUSTLOCK(surface)) { if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);
SDL_UnlockSurface(surface);
}
} }
SDL_Surface * CSDL_Ext::drawShadow(SDL_Surface * source, bool doSheer) SDL_Surface * CSDL_Ext::drawShadow(SDL_Surface * source, bool doSheer)