using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Solitaire.Entities; using Solitaire.Logic; namespace Solitaire { public class SolitaireGame : Game { public const int gameWidth = 300; public const int gameHeight = 300; private GraphicsDeviceManager _graphics; private SpriteBatch _spriteBatch; private SpriteBatch _renderTargetBatch; private RenderTarget2D renderTarget; private Rectangle renderTargetRectangle; private float renderRatio; private MouseState mState; public Vector2 mPosition; public bool mClicked; public bool mReleased; public bool mHolding; private Texture2D _spriteSheet; public static GameLogic logic; public static List entities; private void setRenderRatio() { float wRatio = Window.ClientBounds.Width / (float)gameWidth; float hRatio = Window.ClientBounds.Height / (float)gameHeight; if (wRatio > hRatio) { renderRatio = hRatio; } else { renderRatio = wRatio; } } private Rectangle renderRectangle() { float drawnWidth = gameWidth * renderRatio; float drawnHeight = gameHeight * renderRatio; Rectangle rect = new Rectangle( (int)((Window.ClientBounds.Width - drawnWidth)/2), (int)((Window.ClientBounds.Height - drawnHeight)/2), (int)drawnWidth, (int)drawnHeight ); return rect; } public SolitaireGame() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; this.Window.AllowUserResizing = true; this.Window.ClientSizeChanged += new EventHandler(Window_ClientSizeChanged); void Window_ClientSizeChanged(object sender, EventArgs e) { setRenderRatio(); } } protected override void Initialize() { _graphics.PreferredBackBufferWidth = gameWidth * 2; _graphics.PreferredBackBufferHeight = gameHeight * 2; _graphics.ApplyChanges(); setRenderRatio(); base.Initialize(); } protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); _renderTargetBatch = new SpriteBatch(GraphicsDevice); renderTarget = new RenderTarget2D( GraphicsDevice, gameWidth, gameHeight ); _spriteSheet = Content.Load("playing cards"); entities = new List(); logic = new GameLogic(entities, _spriteSheet); } protected override void Update(GameTime gameTime) { renderTargetRectangle = renderRectangle(); mState = Mouse.GetState(); mPosition = mState.Position.ToVector2(); mPosition.X -= renderTargetRectangle.Left; mPosition.Y -= renderTargetRectangle.Top; mPosition /= renderRatio; if (mReleased) { mReleased = false; } if (mState.LeftButton == ButtonState.Pressed && !mClicked) { mClicked = true; } else if (mState.LeftButton == ButtonState.Released && mClicked) { mClicked = false; mReleased = true; } entities.Sort(delegate(IGameEntity a, IGameEntity b) { return a.DrawOrder.CompareTo(b.DrawOrder); }); mHolding = false; foreach (IGameEntity entity in entities) { if (entity is Card && entity.Following) { mHolding = true; } } foreach (IGameEntity entity in entities) { entity.Update(gameTime, mPosition, mReleased, mHolding, entities); } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.Clear(Color.Green); _spriteBatch.Begin(); foreach (IGameEntity entity in entities) { entity.Draw(_spriteBatch, gameTime); } _spriteBatch.End(); GraphicsDevice.SetRenderTarget(null); _renderTargetBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone); _renderTargetBatch.Draw( renderTarget, renderTargetRectangle, Color.White ); _renderTargetBatch.End(); base.Draw(gameTime); } } }