creating lightsources

BlitzMax Forums/BlitzMax Programming/creating lightsources

Vert lights would definitely be less expensive but won't look as good. The thing that would help here is mod2x which according to Tom's nice example above is definitely possible and will hopefully be integrated into Max2D. Disclaimer: I REALLY suck at explaining things like this but here goes:

- Create an array that will define what portions of your map are "solid". This will define the areas that block light to create shadows. This array should be higher resolution than your tile grid for your map but the larger it is the more data you have to move each frame. I’m using an array that is 4x the size of my map so since the tiles are 32x32 each “lighted pixel” will be 8x8.

- Create two tImage objects. One for the lightblend overlay and one for the shadeblend overlay. For my images sizes, I use the screen resolution plus one tile overlap for scrolling divided by 4.

- Create lights on your map with at least these attributes: Static, Red, Green, Blue, Range. I preprocess the map and add to a list in each map location (tile) which lights are visible from that location based on each light’s range. Lights that are set as "static" are pre-calculated at this point and don't slow down the game rendering.

- Create a 3 dimensional array [w,h,3] of the same size you created for the first step. This will hold the color data for the static lights. Loop through your entire map and for each “pixel” or location in the static light array add the R,G,B values for each light within range. For each light you will need to calculate falloff and line of site to the pixel based on the contents of the array in the first step.

Everything up to now is preprocessing. Now for what you do every screen refresh or so:

- When drawing a screen full of tiles from the map data, pop all of the non-static lights visible from each location drawn into a temp list. These are the dynamic lights you will need to render this frame.

- Loop through a screen sized portion of the static light data array and add the static light data for each pixel to a temp array. At the same time, calculate the light data for the dynamic lights and add it to each pixel.

- I run a 2 pass blur filter over the light data in the temp array to smooth things out and hide the low res nature of the overlays.

- At this point, you will have an array that is the size of each of the overlay images. Loop through the temp array and write the color data to each overlay image. Color values below 127 are multiplied by 2 and written to the shadeblend image and anything above is multiplied by 2 and written to the lightblend image.

- Set ScaleImage to ScreenWidth()+1 tile overlap/overlay width, ScreenHeight()+1 tile overlap/overlay height so the overlays will be scaled up to screen size. Draw the shadeblend overlay and then the lightblend overlay to the screen.

This may sound slow but if done in an optimized manner is plenty fast for a 2D game. On my 1ghz P3 laptop with Radeon Mobility I so far can maintian around 60FPS depending on the number and size of dynamic lights. Anyway, sorry for the long post...