With the SDL library, I'm creating an SDL_Surface*
at w
and h
dimensions.
SDL_Color
is a struct
with 4 unsigned char
fields: a
, r
, g
and b
for alpha, red, green and blue. c1
is the origin color, c2
the destination color, and c
is the color of the pixel at position (i,j).
That gradient works fine, but it is very basic and I'm just wondering if there is any way to optimize it, whether with a better SDL use or a faster algorithm.
SDL_Surface* DrawDescendingDiagonalGradient(SDL_Color& c1, SDL_Color& c2, int w, int h) {
SDL_Color c;
SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0);
SDL_LockSurface(s);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
c.a = c1.a * (w + h - i - j) / (w + h) + c2.a * (i + j) / (w + h);
c.r = c1.r * (w + h - i - j) / (w + h) + c2.r * (i + j) / (w + h);
c.g = c1.g * (w + h - i - j) / (w + h) + c2.g * (i + j) / (w + h);
c.b = c1.b * (w + h - i - j) / (w + h) + c2.b * (i + j) / (w + h);
PutPixel32(s, i, j, SDL_MapRGBA(s->format, c.r, c.g, c.b, c.a));
}
}
SDL_UnlockSurface(s);
return s;
}