A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace SemiColinGames {
  6. public sealed class ShmupScene : IScene {
  7. const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f;
  8. private readonly Color letterboxColor = Color.DarkSlateBlue;
  9. private readonly Color backgroundColor = Color.Black;
  10. private readonly GraphicsDevice graphics;
  11. private readonly RenderTarget2D sceneTarget;
  12. private readonly SpriteBatch spriteBatch;
  13. public ShmupScene(GraphicsDevice graphics, Point worldSize) {
  14. this.graphics = graphics;
  15. sceneTarget = new RenderTarget2D(
  16. graphics, worldSize.X, worldSize.Y, false /* mipmap */,
  17. graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  18. spriteBatch = new SpriteBatch(graphics);
  19. }
  20. ~ShmupScene() {
  21. Dispose();
  22. }
  23. public void Dispose() {
  24. sceneTarget.Dispose();
  25. spriteBatch.Dispose();
  26. GC.SuppressFinalize(this);
  27. }
  28. public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
  29. ShmupWorld world = (ShmupWorld) iworld;
  30. // Draw scene to sceneTarget.
  31. graphics.SetRenderTarget(sceneTarget);
  32. graphics.Clear(backgroundColor);
  33. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
  34. SamplerState.PointClamp, DepthStencilState.Default,
  35. RasterizerState.CullNone);
  36. // Draw enemies, then player, then shots.
  37. foreach (ShmupWorld.Enemy e in world.Enemies) {
  38. e.Draw(spriteBatch);
  39. }
  40. world.Player.Draw(spriteBatch);
  41. foreach (ShmupWorld.Shot s in world.Shots) {
  42. s.Draw(spriteBatch);
  43. }
  44. // Finish drawing sprites.
  45. spriteBatch.End();
  46. // Get ready to draw sceneTarget to screen.
  47. graphics.SetRenderTarget(null);
  48. graphics.Clear(letterboxColor);
  49. // Letterbox the scene if needed.
  50. float aspectRatio = 1.0f * graphics.Viewport.Width / graphics.Viewport.Height;
  51. Rectangle drawRect;
  52. if (aspectRatio > DESIRED_ASPECT_RATIO) {
  53. // Need to letterbox the sides.
  54. int desiredWidth = (int) (graphics.Viewport.Height * DESIRED_ASPECT_RATIO);
  55. int padding = (graphics.Viewport.Width - desiredWidth) / 2;
  56. drawRect = new Rectangle(padding, 0, desiredWidth, graphics.Viewport.Height);
  57. } else {
  58. // Need to letterbox the top / bottom.
  59. int desiredHeight = (int) (graphics.Viewport.Width / DESIRED_ASPECT_RATIO);
  60. int padding = (graphics.Viewport.Height - desiredHeight) / 2;
  61. drawRect = new Rectangle(0, padding, graphics.Viewport.Width, desiredHeight);
  62. }
  63. // Actually draw to screen.
  64. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
  65. SamplerState.PointClamp, DepthStencilState.Default,
  66. RasterizerState.CullNone);
  67. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  68. // Draw debug toasts.
  69. Debug.DrawToasts(spriteBatch);
  70. spriteBatch.End();
  71. }
  72. }
  73. }