|
@@ -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 *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 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
|
|
|
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);
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
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
|
|
|
double det = a * d - b * c;
|
|
|
- if (det == 0)
|
|
|
+ if (static_cast<int>(det) == 0)
|
|
|
throw std::runtime_error("Singular transform matrix!");
|
|
|
double invDet = 1.0 / det;
|
|
|
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);
|
|
|
|
|
|
// Nearest neighbor sampling (can be improved to bilinear)
|
|
|
- int srcXi = static_cast<int>(round(srcX));
|
|
|
- int srcYi = static_cast<int>(round(srcY));
|
|
|
+ auto srcXi = static_cast<int>(round(srcX));
|
|
|
+ auto srcYi = static_cast<int>(round(srcY));
|
|
|
|
|
|
// Check bounds
|
|
|
if (srcXi >= 0 && srcXi < src->w && srcYi >= 0 && srcYi < src->h)
|
|
|
{
|
|
|
- Uint32* srcPixels = (Uint32*)src->pixels;
|
|
|
- Uint32* dstPixels = (Uint32*)dst->pixels;
|
|
|
+ auto srcPixels = (Uint32*)src->pixels;
|
|
|
+ auto dstPixels = (Uint32*)dst->pixels;
|
|
|
|
|
|
Uint32 pixel = srcPixels[srcYi * src->w + srcXi];
|
|
|
dstPixels[y * dst->w + x] = pixel;
|
|
@@ -787,7 +797,7 @@ void applyAffineTransform(SDL_Surface* src, SDL_Surface* dst, double a, double b
|
|
|
else
|
|
|
{
|
|
|
// 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
|
|
|
}
|
|
|
}
|
|
@@ -801,13 +811,12 @@ int getLowestNonTransparentY(SDL_Surface* surface)
|
|
|
{
|
|
|
assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
|
|
|
|
|
|
- if(SDL_MUSTLOCK(surface))
|
|
|
- SDL_LockSurface(surface);
|
|
|
+ if(SDL_MUSTLOCK(surface)) SDL_LockSurface(surface);
|
|
|
|
|
|
int w = surface->w;
|
|
|
int h = surface->h;
|
|
|
int bpp = surface->format->BytesPerPixel;
|
|
|
- Uint8* pixels = (Uint8*)surface->pixels;
|
|
|
+ auto pixels = (Uint8*)surface->pixels;
|
|
|
|
|
|
for(int y = h - 1; y >= 0; --y)
|
|
|
{
|
|
@@ -817,7 +826,10 @@ int getLowestNonTransparentY(SDL_Surface* surface)
|
|
|
{
|
|
|
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);
|
|
|
|
|
|
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);
|
|
|
|
|
|
- Uint32* pixels = (Uint32*)surface->pixels;
|
|
|
+ auto pixels = (Uint32*)surface->pixels;
|
|
|
int pixelCount = surface->w * surface->h;
|
|
|
|
|
|
for (int i = 0; i < pixelCount; i++)
|
|
|
{
|
|
|
Uint32 pixel = pixels[i];
|
|
|
-
|
|
|
- Uint8 pr, pg, pb, pa;
|
|
|
+ Uint8 pr;
|
|
|
+ Uint8 pg;
|
|
|
+ Uint8 pb;
|
|
|
+ Uint8 pa;
|
|
|
// Extract existing RGBA components using SDL_GetRGBA
|
|
|
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 (SDL_MUSTLOCK(surface))
|
|
|
- {
|
|
|
- if (SDL_LockSurface(surface) != 0)
|
|
|
- throw std::runtime_error("Failed to lock surface!");
|
|
|
- }
|
|
|
+ if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface);
|
|
|
|
|
|
int width = surface->w;
|
|
|
int height = surface->h;
|
|
|
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> srcG(pixelCount);
|
|
@@ -894,7 +904,10 @@ void gaussianBlur(SDL_Surface* surface, int amount)
|
|
|
for (int x = 0; x < 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);
|
|
|
|
|
|
int idx = y * width + x;
|
|
@@ -906,20 +919,28 @@ void gaussianBlur(SDL_Surface* surface, int amount)
|
|
|
}
|
|
|
|
|
|
// 3x3 Gaussian kernel
|
|
|
- float kernel[3][3] = {
|
|
|
- {1.f/16, 2.f/16, 1.f/16},
|
|
|
- {2.f/16, 4.f/16, 2.f/16},
|
|
|
- {1.f/16, 2.f/16, 1.f/16}
|
|
|
- };
|
|
|
+ std::array<std::array<float, 3>, 3> kernel = {{
|
|
|
+ {{1.f/16, 2.f/16, 1.f/16}},
|
|
|
+ {{2.f/16, 4.f/16, 2.f/16}},
|
|
|
+ {{1.f/16, 2.f/16, 1.f/16}}
|
|
|
+ }};
|
|
|
|
|
|
// Apply the blur 'amount' times for stronger blur
|
|
|
- for (int iteration = 0; iteration < amount; ++iteration) {
|
|
|
- for (int y = 0; y < height; ++y) {
|
|
|
- for (int x = 0; x < width; ++x) {
|
|
|
- float sumR = 0.f, sumG = 0.f, sumB = 0.f, sumA = 0.f;
|
|
|
+ for (int iteration = 0; iteration < amount; ++iteration)
|
|
|
+ {
|
|
|
+ for (int y = 0; y < height; ++y)
|
|
|
+ {
|
|
|
+ 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 kx = -1; kx <= 1; ++kx) {
|
|
|
+ for (int ky = -1; ky <= 1; ++ky)
|
|
|
+ {
|
|
|
+ for (int kx = -1; kx <= 1; ++kx)
|
|
|
+ {
|
|
|
int nx = x + kx;
|
|
|
int ny = y + ky;
|
|
|
|
|
@@ -954,16 +975,16 @@ void gaussianBlur(SDL_Surface* surface, int amount)
|
|
|
}
|
|
|
|
|
|
// After final iteration, write back to surface pixels
|
|
|
- for (int y = 0; y < height; ++y) {
|
|
|
- for (int x = 0; x < width; ++x) {
|
|
|
+ for (int y = 0; y < height; ++y)
|
|
|
+ {
|
|
|
+ for (int x = 0; x < width; ++x)
|
|
|
+ {
|
|
|
int idx = y * width + x;
|
|
|
pixels[idx] = SDL_MapRGBA(surface->format, srcR[idx], srcG[idx], srcB[idx], srcA[idx]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (SDL_MUSTLOCK(surface)) {
|
|
|
- SDL_UnlockSurface(surface);
|
|
|
- }
|
|
|
+ if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);
|
|
|
}
|
|
|
|
|
|
SDL_Surface * CSDL_Ext::drawShadow(SDL_Surface * source, bool doSheer)
|