Lighting engine
Root / Programming Questions / [.]
MZ952Created:
(SB3)
Trying to break my programming hiatus by making a Terraria-like sandbox game (again, again, again). Difficulty as always is the lighting engine.
I think I'll lay the game out really simply. World tiles, like grass and stone and whatnot, exist on the text screen. Background tiles, like walls, may exist on the BG screen and will probably be larger than world tiles, 16x16 or larger. And the lighting engine, the "light field", may be on the sprite page. Light "tiles" may be one-pixel-square, individually-declared sprites blown up to maybe 3 or more world tiles wide.
My thought is this. Lighting can be broken into two camps: static and dynamic. Sources like torches placed on walls ain't going nowhere, and don't have to be recomputed at every frame. These static sources can be identified off-screen and their light effect drawn onto the light field pixels on the sprite page (to be off-screen, the light field will have to be larger than the screen itself, maybe by 3x). Then, instead of computing static sources, we simply scroll the pixels as the player moves through the world, adding new sources as they appear off-screen.
Dynamic sources, like torches the player holds as they move, is probably the lesser issue. The pixels representing the light field may be duplicated on the sprite page. There will be a hardcopy light field, containing the static light sources, then the duplicate, where dynamic light sources are drawn on top of the static.
I don't think it will be efficient to have the rendering routine to loop through and check every off-screen tile for an entire screen width for static light sources, so I think they will have to be stored separate from the world tiles somehow. I thought of large light source chunks, which define square regions of the world. Each chunk would be a list of light source types and locations, and four of these chunks could be loaded at any given time. The rendering routine would I guess look through the current light source chunks and do a calculation on each light source to check if it's within range to be loaded onto the light field.
It could be made faster by dividing the chunks up into light sources that have already been loaded and those that haven't, I guess.
Because I want color blending and alpha filtering in the lighting engine, every time a static light source is broken or placed, all the light chunks would have to be reloaded onto the hardcopy static light source field.
How's this implementation sounding? Do you think there are better ways to go about it?