using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; namespace SemiColinGames { class Scene : IDisposable { const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f; public bool Enabled = false; Color backgroundColor = Color.CornflowerBlue; readonly GraphicsDevice graphics; readonly Camera camera; readonly RenderTarget2D sceneTarget; readonly BasicEffect basicEffect; readonly SpriteBatch spriteBatch; public Scene(GraphicsDevice graphics, Camera camera) { this.graphics = graphics; this.camera = camera; sceneTarget = new RenderTarget2D( graphics, camera.Width, camera.Height, false /* mipmap */, graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24); basicEffect = new BasicEffect(graphics) { World = Matrix.CreateTranslation(0, 0, 0), View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up), VertexColorEnabled = true }; spriteBatch = new SpriteBatch(graphics); } ~Scene() { Dispose(); } public void Dispose() { sceneTarget.Dispose(); basicEffect.Dispose(); spriteBatch.Dispose(); GC.SuppressFinalize(this); } public void Draw(World world, Player player, LinesOfSight linesOfSight, bool paused) { graphics.SetRenderTarget(null); graphics.Clear(backgroundColor); if (!Enabled) { return; } // Draw scene to sceneTarget. graphics.SetRenderTarget(sceneTarget); graphics.Clear(backgroundColor); // Draw parallax backgrounds. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null); Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height); float xScale = 1f / 16; for (int i = 0; i < Textures.Backgrounds.Length; i++) { int yOffset = Textures.Backgrounds[i].Get.Height - camera.Height - 24; Rectangle bgSource = new Rectangle( (int) (camera.Left * xScale), yOffset, camera.Width, camera.Height); spriteBatch.Draw(Textures.Backgrounds[i].Get, bgTarget, bgSource, Color.White); xScale *= 2; } spriteBatch.End(); // Draw lines of sight. basicEffect.Projection = camera.Projection; if (Debug.Enabled) { linesOfSight.Draw(graphics, basicEffect); } // 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 background tiles. world.DrawBackground(spriteBatch); // Draw player. player.Draw(spriteBatch); // Draw foreground tiles. world.DrawForeground(spriteBatch); spriteBatch.End(); // Draw debug rects & lines on top. Debug.Draw(graphics, basicEffect); // Draw in-world UI on top of everything. // TODO: make a Text.cs file and move things like "black outlined text" there. if (paused) { string text = "Paused"; Vector2 size = Textures.BannerFont.MeasureString(text); Vector2 position = new Vector2((camera.Width - size.X) / 2, (camera.Height - size.Y) / 2); spriteBatch.Begin( SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, null); for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } Vector2 stencilPos = new Vector2(position.X + i, position.Y + j); spriteBatch.DrawString(Textures.BannerFont, text, stencilPos, Color.Black); } } spriteBatch.DrawString(Textures.BannerFont, text, position, Color.White); spriteBatch.End(); } // Get ready to draw sceneTarget to screen. graphics.SetRenderTarget(null); // Letterbox the scene if needed. float aspectRatio = 1.0f * graphics.Viewport.Width / graphics.Viewport.Height; Rectangle drawRect; if (aspectRatio > DESIRED_ASPECT_RATIO) { // Need to letterbox the sides. int desiredWidth = (int) (graphics.Viewport.Height * DESIRED_ASPECT_RATIO); int padding = (graphics.Viewport.Width - desiredWidth) / 2; drawRect = new Rectangle(padding, 0, desiredWidth, graphics.Viewport.Height); } else { // Need to letterbox the top / bottom. int desiredHeight = (int) (graphics.Viewport.Width / DESIRED_ASPECT_RATIO); int padding = (graphics.Viewport.Height - desiredHeight) / 2; drawRect = new Rectangle(0, padding, graphics.Viewport.Width, desiredHeight); } // Actually draw to screen. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone); spriteBatch.Draw(sceneTarget, drawRect, Color.White); // Draw debug toasts. Debug.DrawToasts(spriteBatch); spriteBatch.End(); } } }