Game1.cs 6.1 KB

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