SDL2 - Color Gradient


#1

Hello,

I’ve been attempting to create a function that takes in a specification for a square, where to place it on screen, width, height etc etc, then starting with a starting color transitions over to another set one. So a color gradient.

Now I could do this in Phtoshop sure, but I’d prefer to be able to do it in a function. I’ve done some Googling on the topic, and did find some examples though they were all done in SDL1.2, and were a fixed dimension and position on screen, so they don’t suit what I’m after.

So as an example for the start of the function:

void drawGradientBox(int x, int y, int width, int height, int startR, int startG, int startB, int endR, int endG, int endB)
{
SDL_Rect box = {x,y,width,height};
SDL_SetRenderDrawColor(sdlRenderer, r, g, b, alpha);
SDL_RenderFillRect(sdlRenderer, &box);
}

Then I’d imagine you’d have to go through a for loop going from the starting color and going upward etc etc. Though in my test attempts, nothings worked.

Looking for some assistance on the issue. Anyone able to provide a code example for SDL2?

Thanks