|
|
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic;
namespace SemiColinGames { public sealed class SpiderScene : IScene {
const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f;
private readonly Color letterboxColor = Color.Blue; private readonly Color backgroundColor = Color.Black;
private readonly GraphicsDevice graphics; private readonly RenderTarget2D sceneTarget; private readonly SpriteBatch spriteBatch; private readonly BasicEffect basicEffect;
public SpiderScene(GraphicsDevice graphics, Point worldSize) { this.graphics = graphics;
sceneTarget = new RenderTarget2D( graphics, worldSize.X, worldSize.Y, false /* mipmap */, graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24); spriteBatch = new SpriteBatch(graphics);
basicEffect = new BasicEffect(graphics) { World = Matrix.CreateTranslation(0, 0, 0), View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up), VertexColorEnabled = true }; }
~SpiderScene() { Dispose(); }
public void Dispose() { sceneTarget.Dispose(); spriteBatch.Dispose(); basicEffect.Dispose(); GC.SuppressFinalize(this); }
public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) { SpiderWorld world = (SpiderWorld) iworld;
// Draw scene to sceneTarget.
graphics.SetRenderTarget(sceneTarget); graphics.Clear(backgroundColor);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);
foreach (SpiderWorld.Anchor anchor in world.Anchors) { anchor.Draw(spriteBatch); }
world.Player.Draw(spriteBatch);
// Finish drawing sprites.
spriteBatch.End();
// Get ready to draw sceneTarget to screen.
graphics.SetRenderTarget(null); graphics.Clear(letterboxColor);
// 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.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone); spriteBatch.Draw(sceneTarget, drawRect, Color.White);
// Draw debug toasts.
Debug.DrawToasts(spriteBatch);
spriteBatch.End(); } } }
|