Game1.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. #if ANDROID
  4. using Android.App;
  5. #endif
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Storage;
  11. namespace Microsoft.Xna.Samples.Draw2D
  12. {
  13. /// <summary>
  14. /// This is the main type for your game
  15. /// </summary>
  16. public class Game1 : Microsoft.Xna.Framework.Game
  17. {
  18. GraphicsDeviceManager graphics;
  19. SpriteBatch spriteBatch;
  20. Texture2D texture, ball;
  21. SpriteFont font;
  22. float size, rotation;
  23. float clippingSize = 0.0f;
  24. Color alphaColor = Color.White;
  25. FPSCounterComponent fps;
  26. #if ANDROID
  27. public Game1 (AndroidGameActivity activity) : base (activity)
  28. #else
  29. public Game1 ()
  30. #endif
  31. {
  32. graphics = new GraphicsDeviceManager (this);
  33. Content.RootDirectory = "Content";
  34. graphics.PreferMultiSampling = true;
  35. graphics.IsFullScreen = true;
  36. graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight | DisplayOrientation.PortraitUpsideDown;
  37. }
  38. /// <summary>
  39. /// Allows the game to perform any initialization it needs to before starting to run.
  40. /// This is where it can query for any required services and load any non-graphic
  41. /// related content. Calling base.Initialize will enumerate through any components
  42. /// and initialize them as well.
  43. /// </summary>
  44. protected override void Initialize ()
  45. {
  46. // TODO: Add your initialization logic here
  47. base.Initialize ();
  48. }
  49. /// <summary>
  50. /// LoadContent will be called once per game and is the place to load
  51. /// all of your content.
  52. /// </summary>
  53. protected override void LoadContent ()
  54. {
  55. // Create a new SpriteBatch, which can be used to draw textures.
  56. spriteBatch = new SpriteBatch (GraphicsDevice);
  57. // TODO: use this.Content to load your game content here
  58. texture = Content.Load<Texture2D> ("monogameicon");
  59. // TODO ball = Content.Load<Texture2D> ("purpleBall.xnb");
  60. font = Content.Load<SpriteFont> ("spriteFont1");
  61. fps = new FPSCounterComponent (this,spriteBatch,font);
  62. Components.Add(fps);
  63. }
  64. /// <summary>
  65. /// Allows the game to run logic such as updating the world,
  66. /// checking for collisions, gathering input, and playing audio.
  67. /// </summary>
  68. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  69. protected override void Update (GameTime gameTime)
  70. {
  71. // TODO: Add your update logic here
  72. size += 0.5f;
  73. if (size > 150) {
  74. size = 0;
  75. }
  76. rotation += 0.1f;
  77. if (rotation > MathHelper.TwoPi) {
  78. rotation = 0.0f;
  79. }
  80. clippingSize += 0.5f;
  81. if (clippingSize > graphics.GraphicsDevice.Viewport.Width) {
  82. clippingSize = 0.0f;
  83. }
  84. alphaColor.A ++;
  85. base.Update (gameTime);
  86. }
  87. /// <summary>
  88. /// This is called when the game should draw itself.
  89. /// </summary>
  90. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  91. protected override void Draw (GameTime gameTime)
  92. {
  93. graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
  94. // Draw without blend
  95. spriteBatch.Begin (SpriteSortMode.Deferred, BlendState.Opaque);
  96. spriteBatch.Draw (texture, new Vector2 (250,20), Color.White);
  97. spriteBatch.End ();
  98. // Draw with additive blend
  99. spriteBatch.Begin (SpriteSortMode.Deferred, BlendState.Additive);
  100. spriteBatch.Draw (texture, new Vector2 (250,110), Color.White);
  101. spriteBatch.Draw (texture, new Vector2 (260,120), Color.White);
  102. spriteBatch.End ();
  103. spriteBatch.Begin ();
  104. // Normal draw
  105. // TODO spriteBatch.Draw (ball, new Vector2 (200,300), Color.White);
  106. // TODO spriteBatch.Draw (ball, new Vector2 (200,300), null, Color.Yellow, 0.0f, new Vector2 (5,5), 1.0f, SpriteEffects.None, 0);
  107. // Normal draw
  108. spriteBatch.Draw (texture, new Vector2 (10,390), Color.White);
  109. // Draw stretching
  110. spriteBatch.Draw (texture, new Rectangle (0,0,(int)size,(int)size), Color.White);
  111. // Draw with Filter Color
  112. spriteBatch.Draw (texture, new Vector2 (120,120), Color.Red);
  113. // Draw rotated
  114. spriteBatch.Draw (texture, new Rectangle (100,300,texture.Width,texture.Height), null, Color.White, rotation, new Vector2 (texture.Width / 2,texture.Height / 2), SpriteEffects.None, 0);
  115. // Draw texture section
  116. spriteBatch.Draw (texture, new Vector2 (200,200), new Rectangle (20,20,40,40), Color.White);
  117. // Draw texture section and scale
  118. spriteBatch.Draw (texture, new Rectangle (10,120,(int)size,(int)size), new Rectangle (20,20,40,40), Color.White);
  119. // Alpha blending
  120. spriteBatch.Draw (texture, new Vector2 (140,0), alphaColor);
  121. // Flip horizontaly
  122. spriteBatch.Draw (texture, new Rectangle (80,390,texture.Width,texture.Height), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.FlipHorizontally, 0);
  123. // Flip verticaly
  124. spriteBatch.Draw (texture, new Rectangle (150,390,texture.Width,texture.Height), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.FlipVertically, 0);
  125. // Flip horizontaly and verticaly
  126. spriteBatch.Draw (texture, new Rectangle (220,390,texture.Width,texture.Height), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.FlipHorizontally | SpriteEffects.FlipVertically, 0);
  127. base.Draw (gameTime);
  128. spriteBatch.End ();
  129. // Now let's try some scissoring
  130. spriteBatch.Begin ();
  131. spriteBatch.GraphicsDevice.ScissorRectangle = new Rectangle (50, 40, (int)clippingSize, (int)clippingSize);
  132. spriteBatch.GraphicsDevice.RasterizerState.ScissorTestEnable = true;
  133. spriteBatch.Draw (texture, new Rectangle (50, 40, 320, 40), Color.White);
  134. spriteBatch.DrawString (font, "Scissor Clipping Test", new Vector2 (50, 40), Color.Red);
  135. spriteBatch.End ();
  136. spriteBatch.GraphicsDevice.RasterizerState.ScissorTestEnable = false;
  137. }
  138. }
  139. }