Game1.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Content;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7. namespace Draw2D
  8. {
  9. /// <summary>
  10. /// This is the main type for your game
  11. /// </summary>
  12. public class Game1 : Game
  13. {
  14. GraphicsDeviceManager graphics;
  15. SpriteBatch spriteBatch;
  16. Texture2D texture;
  17. SpriteFont font;
  18. float size, rotation;
  19. float clippingSize = 0.0f;
  20. Color alphaColor = Color.White;
  21. FPSCounterComponent fps;
  22. public Game1 ()
  23. {
  24. graphics = new GraphicsDeviceManager (this);
  25. Content.RootDirectory = "Content";
  26. graphics.PreferMultiSampling = true;
  27. #if ___MOBILE___
  28. graphics.IsFullScreen = true;
  29. #endif
  30. graphics.PreferredBackBufferHeight = 480;
  31. graphics.PreferredBackBufferWidth = 620;
  32. graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight | DisplayOrientation.PortraitDown;
  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> ("logo");
  55. font = Content.Load<SpriteFont> ("Font");
  56. fps = new FPSCounterComponent (this,spriteBatch,font);
  57. Components.Add(fps);
  58. }
  59. /// <summary>
  60. /// Allows the game to run logic such as updating the world,
  61. /// checking for collisions, gathering input, and playing audio.
  62. /// </summary>
  63. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  64. protected override void Update (GameTime gameTime)
  65. {
  66. KeyboardState currentKeyboardState = Keyboard.GetState();
  67. GamePadState currentGamePadState = GamePad.GetState (PlayerIndex.One);
  68. // Check for exit.
  69. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  70. currentGamePadState.Buttons.Back == ButtonState.Pressed)
  71. {
  72. #if !___IOS___
  73. Exit();
  74. #endif
  75. }
  76. // TODO: Add your update logic here
  77. size += 0.5f;
  78. if (size > 150) {
  79. size = 0;
  80. }
  81. rotation += 0.1f;
  82. if (rotation > MathHelper.TwoPi) {
  83. rotation = 0.0f;
  84. }
  85. clippingSize += 0.5f;
  86. if (clippingSize > graphics.GraphicsDevice.Viewport.Width) {
  87. clippingSize = 0.0f;
  88. }
  89. alphaColor.A ++;
  90. base.Update (gameTime);
  91. }
  92. /// <summary>
  93. /// This is called when the game should draw itself.
  94. /// </summary>
  95. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  96. protected override void Draw (GameTime gameTime)
  97. {
  98. graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
  99. // Draw without blend
  100. spriteBatch.Begin (SpriteSortMode.Deferred, BlendState.Opaque);
  101. spriteBatch.Draw (texture, new Vector2 (250,20), Color.White);
  102. spriteBatch.End ();
  103. // Draw with additive blend
  104. spriteBatch.Begin (SpriteSortMode.Deferred, BlendState.Additive);
  105. spriteBatch.Draw (texture, new Vector2 (250,110), Color.White);
  106. spriteBatch.Draw (texture, new Vector2 (260,120), Color.White);
  107. spriteBatch.End ();
  108. spriteBatch.Begin ();
  109. // Normal draw
  110. // TODO spriteBatch.Draw (ball, new Vector2 (200,300), Color.White);
  111. // TODO spriteBatch.Draw (ball, new Vector2 (200,300), null, Color.Yellow, 0.0f, new Vector2 (5,5), 1.0f, SpriteEffects.None, 0);
  112. // Normal draw
  113. spriteBatch.Draw (texture, new Vector2 (10,390), Color.White);
  114. // Draw stretching
  115. spriteBatch.Draw (texture, new Rectangle (0,0,(int)size,(int)size), Color.White);
  116. // Draw with Filter Color
  117. spriteBatch.Draw (texture, new Vector2 (120,120), Color.Red);
  118. // Draw rotated
  119. 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);
  120. // Draw texture section
  121. spriteBatch.Draw (texture, new Vector2 (200,200), new Rectangle (20,20,40,40), Color.White);
  122. // Draw texture section and scale
  123. spriteBatch.Draw (texture, new Rectangle (10,120,(int)size,(int)size), new Rectangle (20,20,40,40), Color.White);
  124. // Alpha blending
  125. spriteBatch.Draw (texture, new Vector2 (140,0), alphaColor);
  126. // Flip horizontaly
  127. spriteBatch.Draw (texture, new Rectangle (80,390,texture.Width,texture.Height), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.FlipHorizontally, 0);
  128. // Flip verticaly
  129. spriteBatch.Draw (texture, new Rectangle (150,390,texture.Width,texture.Height), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.FlipVertically, 0);
  130. // Flip horizontaly and verticaly
  131. spriteBatch.Draw (texture, new Rectangle (220,390,texture.Width,texture.Height), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.FlipHorizontally | SpriteEffects.FlipVertically, 0);
  132. // Scale
  133. spriteBatch.Draw (texture, new Vector2 (290,390), null, Color.White, 0.0f, Vector2.Zero, new Vector2(0.5f, 1.6f), SpriteEffects.None,0.0f);
  134. base.Draw (gameTime);
  135. spriteBatch.End ();
  136. // Now let's try some scissoring
  137. //Disabled as this scissoring code is /wrong/
  138. /*spriteBatch.Begin ();
  139. spriteBatch.GraphicsDevice.ScissorRectangle = new Rectangle (50, 40, (int)clippingSize, (int)clippingSize);
  140. spriteBatch.GraphicsDevice.RasterizerState.ScissorTestEnable = true;
  141. spriteBatch.Draw (texture, new Rectangle (50, 40, 320, 40), Color.White);
  142. spriteBatch.DrawString (font, "Scissor Clipping Test", new Vector2 (50, 40), Color.Red);
  143. spriteBatch.End ();
  144. spriteBatch.GraphicsDevice.RasterizerState.ScissorTestEnable = false;*/
  145. }
  146. }
  147. }