SpriteFontGame.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. namespace SpriteFontSample.Core
  4. {
  5. /// <summary>
  6. /// This is the main type for your game
  7. /// </summary>
  8. public class SpriteFontGame : Game
  9. {
  10. private const string MONOGAME_FOUNDATION = "MonoGame Foundation";
  11. private const float ROTATION_SPEED = 0.03f; // Smaller value for slower rotation
  12. GraphicsDeviceManager graphics;
  13. SpriteBatch spriteBatch;
  14. SpriteFont font;
  15. float rotation; // Rotation angle in radians
  16. float rotationDirection = 1f; // 1 for clockwise, -1 for counter-clockwise
  17. Vector2 textSize;
  18. public SpriteFontGame(GraphicsDeviceManager graphicsManager = null)
  19. {
  20. graphics = graphicsManager ?? new GraphicsDeviceManager(this);
  21. Content.RootDirectory = "Content";
  22. graphics.PreferredBackBufferWidth = 480;
  23. graphics.PreferredBackBufferHeight = 720;
  24. }
  25. protected override void Initialize()
  26. {
  27. base.Initialize();
  28. }
  29. protected override void LoadContent()
  30. {
  31. spriteBatch = new SpriteBatch(GraphicsDevice);
  32. font = Content.Load<SpriteFont>("SpriteFont1");
  33. textSize = font.MeasureString(MONOGAME_FOUNDATION);
  34. textSize = new Vector2(textSize.X / 2, textSize.Y / 2);
  35. }
  36. protected override void Update(GameTime gameTime)
  37. {
  38. // Allow ESC to exit on platforms that support it
  39. #if !__IOS__
  40. if (Microsoft.Xna.Framework.Input.Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape))
  41. Exit();
  42. #endif
  43. rotation += rotationDirection * ROTATION_SPEED;
  44. if (rotation >= MathHelper.TwoPi)
  45. {
  46. rotation = MathHelper.TwoPi;
  47. rotationDirection = -1f;
  48. }
  49. else if (rotation <= 0f)
  50. {
  51. rotation = 0f;
  52. rotationDirection = 1f;
  53. }
  54. base.Update(gameTime);
  55. }
  56. protected override void Draw(GameTime gameTime)
  57. {
  58. graphics.GraphicsDevice.Clear(Color.MonoGameOrange);
  59. spriteBatch.Begin();
  60. spriteBatch.DrawString(font, MONOGAME_FOUNDATION, new Vector2(101, 95), Color.Black);
  61. spriteBatch.DrawString(font, MONOGAME_FOUNDATION, new Vector2(101, 97), Color.Black);
  62. spriteBatch.DrawString(font, MONOGAME_FOUNDATION, new Vector2(99, 95), Color.Black);
  63. spriteBatch.DrawString(font, MONOGAME_FOUNDATION, new Vector2(99, 97), Color.Black);
  64. spriteBatch.DrawString(font, MONOGAME_FOUNDATION, new Vector2(100, 96), Color.White);
  65. spriteBatch.DrawString(font, MONOGAME_FOUNDATION, new Vector2(100, 100), Color.Yellow, MathHelper.PiOver2, Vector2.Zero, 1.0f, SpriteEffects.None, 1);
  66. spriteBatch.DrawString(font, MONOGAME_FOUNDATION, new Vector2(100, 100), Color.Yellow, MathHelper.PiOver4, Vector2.Zero, 1.0f, SpriteEffects.None, 1);
  67. spriteBatch.DrawString(font, MONOGAME_FOUNDATION, new Vector2(160, 360), Color.Black, rotation, textSize, 1.0f, SpriteEffects.None, 1);
  68. spriteBatch.End();
  69. base.Draw(gameTime);
  70. }
  71. }
  72. }