using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; namespace SemiColinGames { class Scene { Color backgroundColor = Color.CornflowerBlue; readonly GraphicsDevice graphics; readonly Camera camera; readonly RenderTarget2D sceneTarget; readonly RenderTarget2D lightingTarget; readonly BasicEffect lightingEffect; readonly SpriteBatch spriteBatch; readonly SpriteFont font; readonly Texture2D grasslandBg1; readonly Texture2D grasslandBg2; public Scene(ContentManager content, GraphicsDevice graphics, Camera camera) { Enabled = false; this.graphics = graphics; this.camera = camera; sceneTarget = new RenderTarget2D( graphics, camera.Width, camera.Height, false /* mipmap */, graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24); lightingTarget = new RenderTarget2D( graphics, camera.Width, camera.Height, false /* mipmap */, graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24); lightingEffect = new BasicEffect(graphics); lightingEffect.World = Matrix.CreateTranslation(0, 0, 0); lightingEffect.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up); lightingEffect.VertexColorEnabled = true; // TODO: handle unloading of resources when the level is done. spriteBatch = new SpriteBatch(graphics); font = content.Load("font"); grasslandBg1 = content.Load("backgrounds/anokolisa/grassland_bg1"); grasslandBg2 = content.Load("backgrounds/anokolisa/grassland_bg2"); } public bool Enabled { get; set; } public void Draw(World world, Player player, LinesOfSight linesOfSight) { graphics.SetRenderTarget(null); graphics.Clear(backgroundColor); if (!Enabled) { return; } graphics.SetRenderTarget(sceneTarget); graphics.Clear(backgroundColor); // Draw scene to sceneTarget. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null); // Draw background. Color bgBlend = Color.FromNonPremultiplied(new Vector4(1, 1, 1, 0.5f)); Rectangle bgSource = new Rectangle( (int) (camera.Left * 0.25), 0, camera.Width, camera.Height); Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height); spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, bgBlend); bgSource = new Rectangle( (int) (camera.Left * 0.5), 0, camera.Width, camera.Height); spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, bgBlend); spriteBatch.End(); // Set up transformation matrix for drawing world objects. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0); spriteBatch.Begin( SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform); // Draw player. player.Draw(spriteBatch); // Draw foreground tiles. world.Draw(spriteBatch); // Aaaaand we're done. spriteBatch.End(); // Draw lighting to lightingTarget. graphics.SetRenderTarget(lightingTarget); graphics.Clear(new Color(0, 0, 0, 0f)); lightingEffect.Projection = camera.Projection; linesOfSight.Draw(player, world.CollisionTargets, graphics, lightingEffect); // Draw debug rects & lines on top. Debug.Draw(graphics, lightingEffect); // Draw sceneTarget to screen. graphics.SetRenderTarget(null); spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone); Rectangle drawRect = new Rectangle( 0, 0, graphics.Viewport.Width, graphics.Viewport.Height); spriteBatch.Draw(sceneTarget, drawRect, Color.White); spriteBatch.Draw(lightingTarget, drawRect, Color.White); // Draw debug toasts. Debug.DrawToasts(spriteBatch, font); spriteBatch.End(); } } }