# 2-d lighting day/night cycle

Off the back of this post in which I asked two questions and received one answer, which I accepted as a valid answer. I have decided to re-ask the outstanding question.

I have implemented light points with shadow casting as shown here but I would like an overall map light with no point/light source.

The map setup is a top-down 2-d 50X50 pixel grid.

How would I go about implementing a day/night cycle lighting across a map?

• possible duplicate of How Do Day Night Cycles Work? – MichaelHouse Oct 24 '12 at 13:21
• I did search i think that post relates more to 3-d. but i'm sorry if its duplicated :( – LoveofSnow Oct 24 '12 at 13:29
• The concepts are the same. Changing the visuals of your game to represent more or less light. – MichaelHouse Oct 24 '12 at 13:32

You should take a look into ambient lightning. This means you set a light color which is applied to all elements. This could be done in several ways. If you are using a shader for postprocessing you can add it there as a parameter and just change it to simulate day and night cycles.

A nice example was shown here (its in german, but maybe the source will help you a bit). It basically uses blend states to mix up different render targets containing the light sources.

Ambient lighting - lighting which is from no particular source but which is background lighting across all entities.

I've found this blog post with some very simple code which I'll be trying later.

this is the key for me:

new Color(ambientColor.R / 255f * ambient, ambientColor.G / 255f * ambient, ambientColor.B / 255f * ambient);

From the blog post:

(the approach is to modify the color of every sprite when drawing it)

float ambient = 1f;
Color ambientColor = Color.White;


In the Update method:

KeyboardState c = Keyboard.GetState();
if (c.IsKeyDown(Keys.Space))
{
ambient -= 0.01f;
if (ambient <= 0)
ambient = 1f;
}


And finally, to apply ambient lighting to sprites:

Color drawColor = new Color(ambientColor.R / 255f * ambient, ambientColor.G / 255f * ambient, ambientColor.B / 255f * ambient);
spriteBatch.Begin();
spriteBatch.Draw(mySprite, new Vector2(0,0), drawColor);
spriteBatch.End();


As you see, it's very easy :) With this, you could simulate for example day cycle.