Dynamic Lighting

by Forum_account
Dynamic Lighting
An easy way to add dynamic lighting to your game.
ID:542024
 
I just posted an update that contains mostly performance tweaks. It's hard to say exactly how much it has improved since the performance depends greatly on how many light sources there are, how often they change, and how large their radius is, but it should be a significant improvement.

Here is the list of changes:
  • Changed how lighting is initialized. The lighting.init() proc is no longer used to set the icon, instead you just set lighting.icon directly. The init() proc initializes lighting. Any argument passed to it is a z level to be initialized, if no z levels are specified, lighting is initialized for all z levels.
  • Changed how light sources work. Previously you could set their radius and intensity vars directly. It took extra CPU usage to detect these changes so now you have to use the radius() and intensity() procs to set these values. This way lighting is only recalculated when it's absolutely necessary.
  • Reorganized the code for the /light object. It now has a proc called loop() that is called by the lighting object's central loop. This proc checks for changes and calls apply(). The apply() proc removes the light's current effect and applies the updated effect.
  • Added the intensity argument to the /light object's constructor and re-named the radius arg to be called "radius". Both are optional (the default is radius = 3, intensity = 1)
  • Added a check so that shading.update() is only called when the lum value has changed in a way that will cause the icon_state to change. For example, a change from 0 to 0.1 will still use the same state and won't cause an update to occur.
  • Removed the shading.update() proc and made it inline. It was being called so many times that the overhead was significant. Profiling shows this change decreased the CPU time used by the light.apply() proc by 15%.
  • Changed the default light.lum proc to avoid unnecessary calls to sqrt(). Profiling shows this reduced the average CPU time of the light.effect() proc by about 20% and the average CPU time of light.apply() by 17%.
  • Added the light.ambient() proc which can be used to change a light's ambient level.