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