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.

82 lines
2.9 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 backgroundColor = Color.Black;
  9. private readonly GraphicsDevice graphics;
  10. private readonly RenderTarget2D sceneTarget;
  11. private readonly SpriteBatch spriteBatch;
  12. public ShmupScene(GraphicsDevice graphics, Point worldSize) {
  13. this.graphics = graphics;
  14. sceneTarget = new RenderTarget2D(
  15. graphics, worldSize.X, worldSize.Y, false /* mipmap */,
  16. graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  17. spriteBatch = new SpriteBatch(graphics);
  18. }
  19. ~ShmupScene() {
  20. Dispose();
  21. }
  22. public void Dispose() {
  23. sceneTarget.Dispose();
  24. spriteBatch.Dispose();
  25. GC.SuppressFinalize(this);
  26. }
  27. public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
  28. ShmupWorld world = (ShmupWorld) iworld;
  29. graphics.SetRenderTarget(null);
  30. graphics.Clear(backgroundColor);
  31. // Draw scene to sceneTarget.
  32. graphics.SetRenderTarget(sceneTarget);
  33. graphics.Clear(backgroundColor);
  34. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
  35. SamplerState.PointClamp, DepthStencilState.Default,
  36. RasterizerState.CullNone);
  37. // Draw player.
  38. Texture2D playerTexture = world.Player.Texture.Get;
  39. Vector2 spriteCenter = new Vector2(playerTexture.Width / 2, playerTexture.Height / 2);
  40. Vector2 drawPos = Vector2.Floor(Vector2.Subtract(world.Player.Position, spriteCenter));
  41. spriteBatch.Draw(playerTexture, drawPos, Color.White);
  42. spriteBatch.End();
  43. // Get ready to draw sceneTarget to screen.
  44. graphics.SetRenderTarget(null);
  45. // Letterbox the scene if needed.
  46. float aspectRatio = 1.0f * graphics.Viewport.Width / graphics.Viewport.Height;
  47. Rectangle drawRect;
  48. if (aspectRatio > DESIRED_ASPECT_RATIO) {
  49. // Need to letterbox the sides.
  50. int desiredWidth = (int) (graphics.Viewport.Height * DESIRED_ASPECT_RATIO);
  51. int padding = (graphics.Viewport.Width - desiredWidth) / 2;
  52. drawRect = new Rectangle(padding, 0, desiredWidth, graphics.Viewport.Height);
  53. } else {
  54. // Need to letterbox the top / bottom.
  55. int desiredHeight = (int) (graphics.Viewport.Width / DESIRED_ASPECT_RATIO);
  56. int padding = (graphics.Viewport.Height - desiredHeight) / 2;
  57. drawRect = new Rectangle(0, padding, graphics.Viewport.Width, desiredHeight);
  58. }
  59. // Actually draw to screen.
  60. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
  61. SamplerState.PointClamp, DepthStencilState.Default,
  62. RasterizerState.CullNone);
  63. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  64. spriteBatch.End();
  65. }
  66. }
  67. }