2
0

Game1.cs 5.9 KB

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