123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System;
- using System.Collections.Generic;
- #if ANDROID
- using Android.App;
- #endif
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Storage;
- using Microsoft.Xna.Framework.GamerServices;
- namespace Microsoft.Xna.Samples.Draw2D
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D texture, ball;
- SpriteFont font;
- float size, rotation;
- float clippingSize = 0.0f;
- Color alphaColor = Color.White;
- FPSCounterComponent fps;
-
- public Game1 ()
- {
- graphics = new GraphicsDeviceManager (this);
-
- Content.RootDirectory = "Content";
-
- graphics.PreferMultiSampling = true;
- #if ANDROID || IPHONE
- graphics.IsFullScreen = true;
- #else
- graphics.IsFullScreen = false;
- #endif
-
- graphics.PreferredBackBufferHeight = 480;
- graphics.PreferredBackBufferWidth = 320;
- graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight | DisplayOrientation.PortraitUpsideDown;
- }
-
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to run.
- /// This is where it can query for any required services and load any non-graphic
- /// related content. Calling base.Initialize will enumerate through any components
- /// and initialize them as well.
- /// </summary>
- protected override void Initialize ()
- {
- // TODO: Add your initialization logic here
- base.Initialize ();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent ()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch (GraphicsDevice);
- // TODO: use this.Content to load your game content here
- texture = Content.Load<Texture2D> ("monogameicon");
- // TODO ball = Content.Load<Texture2D> ("purpleBall.xnb");
- font = Content.Load<SpriteFont> ("spriteFont1");
- fps = new FPSCounterComponent (this,spriteBatch,font);
- Components.Add(fps);
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update (GameTime gameTime)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- {
- Exit();
- }
- // TODO: Add your update logic here
- size += 0.5f;
- if (size > 150) {
- size = 0;
- }
- rotation += 0.1f;
- if (rotation > MathHelper.TwoPi) {
- rotation = 0.0f;
- }
- clippingSize += 0.5f;
- if (clippingSize > graphics.GraphicsDevice.Viewport.Width) {
- clippingSize = 0.0f;
- }
- alphaColor.A ++;
- base.Update (gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw (GameTime gameTime)
- {
- graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
-
- // Draw without blend
- spriteBatch.Begin (SpriteSortMode.Deferred, BlendState.Opaque);
- spriteBatch.Draw (texture, new Vector2 (250,20), Color.White);
- spriteBatch.End ();
- // Draw with additive blend
- spriteBatch.Begin (SpriteSortMode.Deferred, BlendState.Additive);
- spriteBatch.Draw (texture, new Vector2 (250,110), Color.White);
- spriteBatch.Draw (texture, new Vector2 (260,120), Color.White);
- spriteBatch.End ();
-
- spriteBatch.Begin ();
- // Normal draw
- // TODO spriteBatch.Draw (ball, new Vector2 (200,300), Color.White);
- // TODO spriteBatch.Draw (ball, new Vector2 (200,300), null, Color.Yellow, 0.0f, new Vector2 (5,5), 1.0f, SpriteEffects.None, 0);
-
- // Normal draw
- spriteBatch.Draw (texture, new Vector2 (10,390), Color.White);
- // Draw stretching
- spriteBatch.Draw (texture, new Rectangle (0,0,(int)size,(int)size), Color.White);
- // Draw with Filter Color
- spriteBatch.Draw (texture, new Vector2 (120,120), Color.Red);
- // Draw rotated
- 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);
- // Draw texture section
- spriteBatch.Draw (texture, new Vector2 (200,200), new Rectangle (20,20,40,40), Color.White);
- // Draw texture section and scale
- spriteBatch.Draw (texture, new Rectangle (10,120,(int)size,(int)size), new Rectangle (20,20,40,40), Color.White);
- // Alpha blending
- spriteBatch.Draw (texture, new Vector2 (140,0), alphaColor);
- // Flip horizontaly
- spriteBatch.Draw (texture, new Rectangle (80,390,texture.Width,texture.Height), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.FlipHorizontally, 0);
- // Flip verticaly
- spriteBatch.Draw (texture, new Rectangle (150,390,texture.Width,texture.Height), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.FlipVertically, 0);
- // Flip horizontaly and verticaly
- spriteBatch.Draw (texture, new Rectangle (220,390,texture.Width,texture.Height), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.FlipHorizontally | SpriteEffects.FlipVertically, 0);
-
- // Scale
- spriteBatch.Draw (texture, new Vector2 (290,390), null, Color.White, 0.0f, Vector2.Zero, new Vector2(0.5f, 1.6f), SpriteEffects.None,0.0f);
-
-
- base.Draw (gameTime);
- spriteBatch.End ();
-
- // Now let's try some scissoring
- //Disabled as this scissoring code is /wrong/
- /*spriteBatch.Begin ();
- spriteBatch.GraphicsDevice.ScissorRectangle = new Rectangle (50, 40, (int)clippingSize, (int)clippingSize);
- spriteBatch.GraphicsDevice.RasterizerState.ScissorTestEnable = true;
- spriteBatch.Draw (texture, new Rectangle (50, 40, 320, 40), Color.White);
- spriteBatch.DrawString (font, "Scissor Clipping Test", new Vector2 (50, 40), Color.Red);
-
- spriteBatch.End ();
- spriteBatch.GraphicsDevice.RasterizerState.ScissorTestEnable = false;*/
-
- }
- }
- }
|