A fairytale about knights and kings, witches and princesses, and a girl who hears a disembodied voice in her head.
A devlog post about the lighting engine in K.I.S.A, with a couple of reasons explaining why it's so fast.
Posted by Oracizan on Jun 27th, 2013
I had it relatively easy when it came time to create the lighting engine for K.I.S.A. Because the game world is purely 2D, I didn't have to worry about normal mapping, ray tracing, or anything that ends in -ometry. Still, there are a few things about K.I.S.A's lighting engine that I find interesting enough to share.
I'm not going to get too deep into the base workings of the lighting engine, because what the engine does is very boring and common. In a nutshell: I draw gradients onto a surface (canvas for you non GameMaker folks), then render that surface over the top of the game area with blend modes.
Below are 3 lighting practices used in K.I.S.A that are not, to my knowledge, common and boring:
The lighting surface is not the size of the level. In K.I.S.A, levels sizes measure in the tens of thousands of pixels - surfaces of this size would not be feasible. Surfaces take time to draw on and take up memory just by existing, so they should be as small as possible. In this case, I made the surface equal to the size of the game window and always draw the surface at the (0,0) position on screen. When I render the lights, I offset their coordinates so that they are correctly positioned relative to the position of the game window.
Coders reading this are probably thinking "This is obvious! There's nothing special here!", but I see too many games and tutorials that make use of massive surfaces. I desperately wish that this was common and boring, because it should be.
The lighting surface is scaled. I lied earlier. The lighting surface is not the size of the game window: it's smaller.
I think that very few games require pixel perfect lighting, and K.I.S.A is not one of them. It's just not necessary. The beautiful thing about scaling is that it's not a tradeoff between quality and speed, because there is next to no sacrifice of quality. The nature of lighting usually means thatscaling does not significantly affect the quality of lighting. Lighting in K.I.S.A is all about gradients, and gradients are one of the few things that interpolated scaling handles very, very well.
In K.I.S.A, the highest quality lighting sets the scaling at 50% and the lowest at 25%. This means that the lighting surface is drawn at 25-50% size, and then stretched to fill the game window. 50% scaling is nearly indistinguishable from 100% quality, and if I did not have to worry about straight edges, which interpolated scaling does not handle well, I could afford to go much lower.
Scaling is a really simple way to speed up a 2D lighting engine, with almost no loss in graphics quality.
The lighting is not static. Light floods into doorways, torches flicker, etc. This is pretty common in 3D games, but I do not see it very much when I play 2D games. I'll let this final image speak for itself.