How to create a gradient using SDL
Shoot! Looks like I missed a day in updating this thing. I was looking through my NOGDUS archives, and was going to just toss up another old piece of code for your viewing pleasure, but then I decided it would be better to write something new.
This code is inspired by a small demo written for NOGDUS, however the original was an Allegro demonstration. I decided to build an SDL 1.2 example from the ground up. I don't have time to explain everything in detail, however, if you have questions, feel free to contact me and ask me.
The code below is an excerpt from the code that I wrote for this post, which you can check out the example using the link below the code listing. I wanted to create a linear gradient between two colors. The following class is what performs this magic.
class VGradient { public: VGradient(const SDL_Surface* surface, const SDL_Rect* rect, const Uint32 colorStop0, const Uint32 colorStop1) { target = surface; int x1 = rect->x; int x2 = x1 + rect->w; int y = rect->y; int height = rect->h; for (int i = 0; i < height; i++) { float distance = (float)i / (float)height; Uint8 r = LinearInterpolation(GetRValue(colorStop0), GetRValue(colorStop1), distance); Uint8 g = LinearInterpolation(GetGValue(colorStop0), GetGValue(colorStop1), distance); Uint8 b = LinearInterpolation(GetBValue(colorStop0), GetBValue(colorStop1), distance); Uint32 color = GetRGBValue(r, g, b); DrawHLine(x1, x2, y+i, color); } } private: inline Uint8 LinearInterpolation(const Uint8 from, const Uint8 to, const float distance) const { return (from + (to - from) * distance); } private: inline void DrawPixel(const int x, const int y, const Uint32 color) const { *((Uint32*)((Uint8*)target->pixels + (y * target->pitch) + x * target->format->BytesPerPixel)) = color; } private: void DrawHLine(const int x1, const int x2, const int y, const Uint32 color) const { for (int x = x1; x < x2; x++) { DrawPixel(x, y, color); } } private: inline Uint8 GetRValue(const Uint32 color) { return (color >> 16) & 0xFF; } private: inline Uint8 GetGValue(const Uint32 color) { return (color >> 8) & 0xFF; } private: inline Uint8 GetBValue(const Uint32 color) { return (color & 0xFF); } private: inline Uint32 GetRGBValue(const Uint8 r, const Uint8 g, const Uint8 b) { return ((r << 16) | (g << 8) | b); } private: const SDL_Surface* target; };
View the complete example
The code in this post is licensed as follows.
/* The MIT License (MIT) Copyright (c) 2014 Richard Marks, Bang Bang Attack Studios Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
Thank you for taking the time to read my blog.









