source: 2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Game.cs @ 4512

Revision 4512, 4.7 KB checked in by dezhidki, 7 years ago (diff)

Talletus.

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using Microsoft.Xna.Framework;
5using Microsoft.Xna.Framework.Audio;
6using Microsoft.Xna.Framework.Content;
7using Microsoft.Xna.Framework.GamerServices;
8using Microsoft.Xna.Framework.Graphics;
9using Microsoft.Xna.Framework.Input;
10using Microsoft.Xna.Framework.Media;
11using CastleMaster.Input;
12
13using Keys = System.Windows.Forms.Keys;
14using System.Text;
15using CastleMaster.Graphics;
16using CastleMaster.World;
17
18namespace CastleMaster
19{
20    /// <summary>
21    /// This is the main type for your game
22    /// </summary>
23    public class Game : Microsoft.Xna.Framework.Game
24    {
25        public const int WIDTH = 800;
26        public const int HEIGHT = 600;
27        public const string TITLE = "Castle Master Alpha 0.1";
28
29        private const string UPS_TEXT = " UPS: ";
30        private const string FPS_TEXT = " | FPS: ";
31
32        private GraphicsDeviceManager graphics;
33        private SpriteBatch spriteBatch;
34        private InputHandler input;
35        private RenderHelper renderer;
36        private Camera camera;
37        private Level level;
38       
39        #region Counter
40        private readonly TimeSpan ONE_SECOND = TimeSpan.FromSeconds(1.0);
41        private TimeSpan currentTime = TimeSpan.Zero;
42        private int frames = 0, updates = 0;
43        #endregion
44
45        public Game()
46        {
47            graphics = new GraphicsDeviceManager(this);
48            Content.RootDirectory = "Content";
49            input = new InputHandler(Window);
50        }
51
52        /// <summary>
53        /// Allows the game to perform any initialization it needs to before starting to run.
54        /// This is where it can query for any required services and load any non-graphic
55        /// related content.  Calling base.Initialize will enumerate through any components
56        /// and initialize them as well.
57        /// </summary>
58        protected override void Initialize()
59        {
60            graphics.PreferredBackBufferWidth = WIDTH;
61            graphics.PreferredBackBufferHeight = HEIGHT;
62            graphics.SynchronizeWithVerticalRetrace = true;
63            graphics.ApplyChanges();
64            Window.Title = TITLE;
65            IsMouseVisible = true;
66
67            InitializeInput();
68
69            base.Initialize();
70        }
71
72        private void InitializeInput()
73        {
74            input.RegisterKeyboardKey(Keys.Escape);
75        }
76
77        /// <summary>
78        /// LoadContent will be called once per game and is the place to load
79        /// all of your content.
80        /// </summary>
81        protected override void LoadContent()
82        {
83            spriteBatch = new SpriteBatch(GraphicsDevice);
84            renderer = new RenderHelper(spriteBatch);
85
86            Resources.LoadResources(Content, renderer);
87        }
88
89        /// <summary>
90        /// UnloadContent will be called once per game and is the place to unload
91        /// all content.
92        /// </summary>
93        protected override void UnloadContent()
94        {
95            // TODO: Unload any non ContentManager content here
96        }
97
98        protected override void BeginRun()
99        {
100            base.BeginRun();
101
102            camera = new Camera();
103            level = new Level(128, 128);
104        }
105
106        /// <summary>
107        /// Allows the game to run logic such as updating the world,
108        /// checking for collisions, gathering input, and playing audio.
109        /// </summary>
110        /// <param name="gameTime">Provides a snapshot of timing values.</param>
111        protected override void Update(GameTime gameTime)
112        {
113            input.Update();
114            camera.Update();
115
116            if (InputHandler.HasKeyBeenPressed(Keys.Escape))
117                this.Exit();
118
119            updates++;
120            currentTime += gameTime.ElapsedGameTime;
121            if (currentTime >= ONE_SECOND)
122            {
123                currentTime = TimeSpan.Zero;
124                Window.Title = new StringBuilder(TITLE).Append(FPS_TEXT).Append(frames).Append(UPS_TEXT).Append(updates).ToString();
125                frames = updates = 0;
126            }
127
128            base.Update(gameTime);
129        }
130
131        /// <summary>
132        /// This is called when the game should draw itself.
133        /// </summary>
134        /// <param name="gameTime">Provides a snapshot of timing values.</param>
135        protected override void Draw(GameTime gameTime)
136        {
137            GraphicsDevice.Clear(Color.Black);
138
139            renderer.BeginRender();
140
141            level.RenderBackground(camera, renderer);
142           
143            renderer.EndRender();
144
145            frames++;
146            base.Draw(gameTime);
147        }
148    }
149}
Note: See TracBrowser for help on using the repository browser.