sneak/Shared/ShmupScene.cs

92 lines
3.2 KiB
C#
Raw Normal View History

2020-11-19 19:44:12 +00:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
namespace SemiColinGames {
public sealed class ShmupScene : IScene {
const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f;
2020-11-19 22:26:50 +00:00
private readonly Color backgroundColor = Color.DarkBlue;
2020-11-19 19:44:12 +00:00
private readonly GraphicsDevice graphics;
private readonly RenderTarget2D sceneTarget;
private readonly SpriteBatch spriteBatch;
2020-11-19 21:36:21 +00:00
public ShmupScene(GraphicsDevice graphics, Point worldSize) {
2020-11-19 19:44:12 +00:00
this.graphics = graphics;
sceneTarget = new RenderTarget2D(
2020-11-19 21:36:21 +00:00
graphics, worldSize.X, worldSize.Y, false /* mipmap */,
2020-11-19 19:44:12 +00:00
graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
spriteBatch = new SpriteBatch(graphics);
}
~ShmupScene() {
Dispose();
}
public void Dispose() {
2020-11-19 21:36:21 +00:00
sceneTarget.Dispose();
spriteBatch.Dispose();
2020-11-19 19:44:12 +00:00
GC.SuppressFinalize(this);
}
public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
ShmupWorld world = (ShmupWorld) iworld;
graphics.SetRenderTarget(null);
2020-11-19 22:26:50 +00:00
graphics.Clear(Color.Black);
2020-11-19 19:44:12 +00:00
// Draw scene to sceneTarget.
graphics.SetRenderTarget(sceneTarget);
graphics.Clear(backgroundColor);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
SamplerState.PointClamp, DepthStencilState.Default,
RasterizerState.CullNone);
2020-11-19 21:36:21 +00:00
// Draw player.
Texture2D playerTexture = world.Player.Texture.Get;
Vector2 spriteCenter = new Vector2(playerTexture.Width / 2, playerTexture.Height / 2);
Vector2 drawPos = Vector2.Floor(Vector2.Subtract(world.Player.Position, spriteCenter));
spriteBatch.Draw(playerTexture, drawPos, Color.White);
2020-11-19 22:26:50 +00:00
// Draw shots.
foreach (ShmupWorld.Shot s in world.Shots) {
Texture2D texture = s.Texture.Get;
Vector2 center = new Vector2(texture.Width / 2, texture.Height / 2);
spriteBatch.Draw(texture, Vector2.Floor(Vector2.Subtract(s.Position, center)), Color.White);
}
// Finish drawing sprites.
2020-11-19 19:44:12 +00:00
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.Deferred, BlendState.AlphaBlend,
SamplerState.PointClamp, DepthStencilState.Default,
RasterizerState.CullNone);
spriteBatch.Draw(sceneTarget, drawRect, Color.White);
spriteBatch.End();
}
}
}