This is the next post in my Fondusi’s Development Blog series. Click here to check out week 4 or start at the beginning.

Week 5 – August 29th – September 4th

This week we managed to get a lot of things done. Andrew did a lot of work on the region editor and I added some cool new features and fixed bugs in the lighting system.

Lighting Changes

As I mentioned in the previous post, there was a bit of a problem with how I organized the light objects. As I said before, Light objects were linked into MapCells. The problem with doing it this way is that the camera only draws the MapCells that are visible on screen, but a light can be larger than a single cell. This means that if a light spans across cells, but the MapCell that it’s linked to is no longer within the camera’s view, the Light will not get added to the main
List&lt;Light&gt;. Which means that the Light no longer gets drawn, and since some lights were several tiles or larger, they would just disappear when they weren’t supposed to. I had to develop a system for the lights that was dependent both upon the size of the screen and the size of the largest light.

Spatial Hashing/Partitioning

I read a bit about spatial hashing and it’s essentially the same way that the tilesets are separated into tiles. The only real difference is that the spaces are “buckets” (
List&lt;Light&gt;) instead of tiles.

Taking the Lights into Account

So, the idea is that the map gets split up into a spatial grid where each square is 1280×1280 (in pixels). I chose this number because it is half the maximum size of my lights. The lights are radial as you probably noticed from my previous post, and so this number corresponds to the maximum distance away that the Light can be positioned before it should stop being drawn. Each 1280×1280 square in the grid will represent a bucket (
List&lt;Light&gt;) into which I will add the Light objects that are located within that area.

What about the screen?

That takes care of the size of the lights, now we need to take into account the size of the screen. Why? Well consider that the screen may be differently sized on different computers. It could be as small as 640×480 or as large as 1900×1600. In the first case, the screen would only ever overlap 4 buckets maximum (if it were at the corner of a bucket). However, with the larger resolution, you could see up to 9 buckets at a time! So, in order to include lights from all of the buckets that you can see, you get the IDs of the buckets that each corner of the screen overlaps. Then, with a bit of looping and some math, you can figure out all of the buckets that the screen overlaps. You can’t use only the buckets overlapped by the corners because, depending on the screen size and position, there may be buckets between them.

Ok, so once you have all of the overlapped buckets, instead of looping through tiles in the Draw loop, you can loop through the buckets to get the appropriate Lights to draw. You simply loop through the bucket list and then add all of the Lights from the bucket to the to-be-drawn Light list and Voila! Only visible lights are drawn.

On a side note, it’s possible to make it more efficient by only updating the “currently overlapped” list when one of the corners changes to a new bucket (as opposed to calculating it every frame).

The Rest

Here’s the rest of what we did this week:

Added a controller for handling the time of day.

Added an enum for time of day

Updated the shader to use the new time of day values

Updated infobox text to show different info

Updated the tile engine based on changes

Fixed pixel shader drawing black

Added stuff to region and to controls form

Added control for the time of day to the menu on controls form

Set default override TOD for the map engine to Day

Changed some things in the region editor

Region colours are drawn

Made the rain engine emitter an area emitter and tweaked some settings.

That’s all for now! I know this has been a bit long in coming. That’s because LBC has been working on another project and I haven’t had the time to finish the post (it’s been sitting in draft since Sept 24!). The next week’s post will be about networking and all the fun stuff we’ve come across with that. Drop a comment if you have any questions.