2
0

Game1.cs 6.1 KB

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