﻿/*************************************************************** 
 * Licensed to the Apache Software Foundation(ASF) under one
 * or more contributor license agreements.See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 ***************************************************************/

using ExampleDrawableComponents.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ExampleDrawableComponents
{
    /// <summary>
    /// The game.
    /// </summary>
    public class MyGame : Game
    {
        #region Fields
        /// <summary>
        /// A logo.
        /// </summary>
        private Texture2D _logo;

        /// <summary>
        /// A font.
        /// </summary>
        private SpriteFont _font;

        /// <summary>
        /// Key pressed.
        /// </summary>
        private bool keyPressed = false;
        #endregion
        #region Properties
        /// <summary>
        /// Return the sprite batch.
        /// </summary>
        public SpriteBatch SpriteBatch { get; private set; }

        /// <summary>
        /// The graphics device manager.
        /// </summary>
        public GraphicsDeviceManager Graphics { get; private set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public MyGame()
        {
            Graphics = new GraphicsDeviceManager(this);
            Graphics.PreferredBackBufferWidth = 1024;
            Graphics.PreferredBackBufferHeight = 768;

            Content.RootDirectory = "Content";
        }
        #endregion

        /// <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()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            SpriteBatch = new SpriteBatch(GraphicsDevice);

            // Declare the components
            Components.Add(new FPSComponent(this));
            Components.Add(new FullScreenComponent(this));

            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()
        {
            _logo = Content.Load<Texture2D>("logo");
            _font = Content.Load<SpriteFont>("font");

            base.LoadContent();
        }

        /// <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 ||
                Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            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)
        {
            GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);

            GraphicsDevice.SetRenderTarget(null);
            SpriteBatch.Begin();
            SpriteBatch.Draw(_logo, new Vector2(0, 0), Microsoft.Xna.Framework.Color.White);

            // Display the help informations
            DisplayHelp();

            SpriteBatch.End();

            base.Draw(gameTime);
        }

        /// <summary>
        /// Display the help.
        /// </summary>
        private void DisplayHelp()
        {
            SpriteBatch.DrawString(_font, "Help", new Vector2(10, 10), Color.Yellow);
            SpriteBatch.DrawString(_font, "Press ESCAPE to quit", new Vector2(10, 30), Color.Yellow);
            SpriteBatch.DrawString(_font, "Press CTRL+ENTER to switch fullscreen/windowed", new Vector2(10, 50), Color.Yellow);
        }
    }
}
