/// <summary> /// Draws the background first. /// Calls draw on the screen objects in the order: Lights, Background, GameObjects, InGameUIObjects, ScreenUIObjects. /// Draws the mouse at the after everything else. /// </summary> /// <param name="spriteBatch">The SpriteBatch we should use for drawing sprites</param> public override void Draw(SpriteBatch spriteBatch) { base.Draw(spriteBatch); GraphicsDevice graphicsDevice = ScreenManager.Instance.GraphicsDeviceManager.GraphicsDevice; // Draw our lighting { graphicsDevice.SetRenderTarget(LightRenderTarget); if (Lights.ShouldDraw) { graphicsDevice.Clear(Color.Black); spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive, null, null, null, null, Camera.TransformationMatrix); Lights.Draw(spriteBatch); spriteBatch.End(); } else { graphicsDevice.Clear(Color.White); } } // Draw our game world { graphicsDevice.SetRenderTarget(GameWorldRenderTarget); graphicsDevice.Clear(Color.White); if (Background != null) { spriteBatch.Begin(); { if (Background.ShouldDraw) { Background.Draw(spriteBatch); } } spriteBatch.End(); } // Draw the camera dependent objects using the camera transformation matrix spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, Camera.TransformationMatrix); { if (EnvironmentObjects.ShouldDraw) { EnvironmentObjects.Draw(spriteBatch); } if (GameObjects.ShouldDraw) { GameObjects.Draw(spriteBatch); } } spriteBatch.End(); } // Draw our InGameUI { graphicsDevice.SetRenderTarget(InGameUIRenderTarget); graphicsDevice.Clear(Color.Transparent); spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, Camera.TransformationMatrix); { if (InGameUIObjects.ShouldDraw) { InGameUIObjects.Draw(spriteBatch); } } spriteBatch.End(); } // Draw our Screen UI { graphicsDevice.SetRenderTarget(ScreenUIRenderTarget); graphicsDevice.Clear(Color.Transparent); // Draw the camera independent objects and the mouse last spriteBatch.Begin(); { if (ScreenUIObjects.ShouldDraw) { ScreenUIObjects.Draw(spriteBatch); } if (GameMouse.Instance.ShouldDraw) { GameMouse.Instance.Draw(spriteBatch); } } spriteBatch.End(); } // Combine the separate render targets together using the appropriate effects { graphicsDevice.SetRenderTarget(null); graphicsDevice.Clear(Color.Black); // Combine the Light and Game World render targets spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend); Lights.LightEffect.Parameters["lightMask"].SetValue(LightRenderTarget); Lights.LightEffect.Parameters["ambientLight"].SetValue(Lights.AmbientLight.ToVector4()); Lights.LightEffect.CurrentTechnique.Passes[0].Apply(); spriteBatch.Draw(GameWorldRenderTarget, Vector2.Zero, Color.White); spriteBatch.End(); // Draw our UI render targets spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend); spriteBatch.Draw(InGameUIRenderTarget, Vector2.Zero, Color.White); spriteBatch.Draw(ScreenUIRenderTarget, Vector2.Zero, Color.White); spriteBatch.End(); } }