Gradients and SGFont update

SIEGE has just got a new feature – gradients.

SGGradient is a new structure which holds the data for a gradient – a smooth (or, depending on interpolation, not-so-smooth) transition of colors on a scale from 0.0 to 1.0 – of course, stops can be added in between, so that you can go from, say, 0.75 past 0.25 to 1.0. The gradients are (by design) not limited to color information, but can hold any form of info – for example, a heightmap for a side-scroller could be stored in this way. SGColorGradient will be added later on, and will be a “specialization” of SGGradient for RGBA color info.

For those wondering how to use this functionality, here is an example:

// creates a gradient with two stops: (0.0 -> 0.0) and (1.0 -> 1.0)
SGGradient* grad = sgGradientCreate();

/*
    Add some stops; note that the stops at
    0.00 and 1.00 are automatically replaced
*/
sgGradientSetStopKey(grad, 0.00, 1.00); // 0.00 -> 1.00
sgGradientSetStopKey(grad, 0.25, 0.75); // 0.25 -> 0.75
sgGradientSetStopKey(grad, 0.75, 0.00); // 0.75 -> 0.00
sgGradientSetStopKey(grad, 1.00, 0.50); // 1.00 -> 0.50

// ...
// draw the gradient...
size_t i;
for(i = 0; i < 640; i += 16)
{
    // grayscale gradient color: rgba(g, g, g, 0.0)
    sgDrawColor1f(sgGradientGetValue(grad, i / 640.0));
    sgDrawRectangleWH(i, 0, 16, 480, SG_TRUE);
}
// ...

sgGradientDestroy(grad);

…and that’s about it regarding the use! Here is a screenshot of the gradients in action (note: this is not the same gradient as the above code):

Interpolations from top to bottom: Nearest-neighbour, linear, cosine, cubic - custom interpolation functions are supported. Dim orange lines are stops.

Now that gradients are settled, onto the 2nd update: SGFont API change.

Until now, SGFont had a LOT of different functions for printing:

sgFontPrintCentered*
sgFontPrintXCentered*
sgFontPrintYCentered*

All of these had 9 variants, for a total of 27 functions. They have all been replaced with a set of new functions, sgFontPrintAligned*:

void sgFontPrintAligned(SGFont* font, float x, float y, SGenum align, const char* text);

// example:
// print X-cented, but on the Y baseline
sgFontPrintAligned(myfont, 320, 240, SG_ALIGN_CENTER | SG_ALIGN_BASELINE, "Hello, font world!");

// the following constants are available:
// SG_ALIGN_CENTER   - x or y-centered (used for both because its value is 0)
// SG_ALIGN_LEFT     - left-aligned on x
// SG_ALIGN_RIGHT    - right-aligned on x
// SG_ALIGN_TOP      - top-aligned on y
// SG_ALIGN_BASELINE - baseline-aligned on y
// SG_ALIGN_BOTTOM   - bottom-aligned on y

…plus variants. The “old” functions still exist, but they have been deprecated and are scheduled to be removed soon (since most are almost unused, very soon) without notice whatsoever.

Yes, there are more arguments to pass, but it is much more flexible – for example, there existed no way to print right-, top- or bottom-aligned text – there was only left/center on x and center/baseline on y. Furthermore, a switch was previous required in case certain alignments were required under certain conditions (to make sure the right function is called) – now, it is only a matter of a different argument.

Well, that pretty much concludes today’s post – I hope the new gradients and font drawing functionality will come in handy!

Posted in SIEGE.