Game1.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. namespace SamplesContentBuilder
  5. {
  6. /// <summary>
  7. /// This is a sample MonoGame project demonstrating content building and loading.
  8. /// It showcases how to build and load various types of content assets.
  9. /// </summary>
  10. public class Game1 : Game
  11. {
  12. private GraphicsDeviceManager _graphics;
  13. private SpriteBatch _spriteBatch;
  14. private Texture2D _backgroundTexture;
  15. private SpriteFont _font;
  16. public Game1()
  17. {
  18. _graphics = new GraphicsDeviceManager(this);
  19. Content.RootDirectory = "Content";
  20. IsMouseVisible = true;
  21. }
  22. protected override void Initialize()
  23. {
  24. base.Initialize();
  25. }
  26. protected override void LoadContent()
  27. {
  28. _spriteBatch = new SpriteBatch(GraphicsDevice);
  29. // Try to load sample content if available
  30. try
  31. {
  32. _backgroundTexture = Content.Load<Texture2D>("Textures/background");
  33. }
  34. catch
  35. {
  36. // Create a simple colored texture if background is not available
  37. _backgroundTexture = new Texture2D(GraphicsDevice, 1, 1);
  38. _backgroundTexture.SetData(new[] { Color.CornflowerBlue });
  39. }
  40. try
  41. {
  42. _font = Content.Load<SpriteFont>("Fonts/Font");
  43. }
  44. catch
  45. {
  46. // Font will be null if not available
  47. }
  48. }
  49. protected override void Update(GameTime gameTime)
  50. {
  51. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
  52. Keyboard.GetState().IsKeyDown(Keys.Escape))
  53. Exit();
  54. base.Update(gameTime);
  55. }
  56. protected override void Draw(GameTime gameTime)
  57. {
  58. GraphicsDevice.Clear(Color.MonoGameOrange);
  59. _spriteBatch.Begin();
  60. // Draw background if available
  61. if (_backgroundTexture != null)
  62. {
  63. _spriteBatch.Draw(_backgroundTexture, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
  64. }
  65. // Draw text if font is available
  66. if (_font != null)
  67. {
  68. _spriteBatch.DrawString(_font, "MonoGame Samples Content Builder", new Vector2(10, 10), Color.White);
  69. _spriteBatch.DrawString(_font, "This project demonstrates content building with MonoGame 3.8.*", new Vector2(10, 40), Color.White);
  70. _spriteBatch.DrawString(_font, "Press Escape to exit", new Vector2(10, 70), Color.White);
  71. }
  72. _spriteBatch.End();
  73. base.Draw(gameTime);
  74. }
  75. }
  76. }