-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameWindow.cs
125 lines (95 loc) · 3.07 KB
/
GameWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using FPSTetris.GameStates;
using FPSTetris.WinForms;
using System.Diagnostics;
namespace FPSTetris
{
public partial class GameWindow : Form
{
#region Constructor
private GameWindow()
{
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
_formState = new FormState();
GameStatesManager = new GameStatesManager();
}
#endregion
#region Singleton
private static GameWindow _instance;
public static GameWindow Instance
{
get
{
if (_instance == null)
_instance = new GameWindow();
return _instance;
}
}
#endregion
#region Attributes and Properties
public bool GameRunning { get; private set; }
public GameStatesManager GameStatesManager { get; private set; }
public bool FullScreen { get; private set; }
#endregion
#region Private Fields
private readonly FormState _formState;
#endregion
#region Overriden Methods
protected override void OnFormClosing(FormClosingEventArgs e)
{
GameRunning = false;
GameStatesManager.FinalizeManager();
base.OnFormClosing(e);
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
GameStatesManager.InitializeManager();
StartMainLoop();
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
if (GameStatesManager.CurrentGameState != null)
GameStatesManager.CurrentGameState.Render(e.Graphics);
}
#endregion
#region Private Methods
private void StartMainLoop()
{
GameRunning = true;
Stopwatch timer = new Stopwatch();
timer.Start();
double lastTime = timer.ElapsedMilliseconds;
while (GameRunning)
{
double gameTime = timer.ElapsedMilliseconds;
float elapsedTime = (float)(gameTime - lastTime);
lastTime = gameTime;
Application.DoEvents();
if (GameStatesManager.CurrentGameState != null)
GameStatesManager.CurrentGameState.Update(elapsedTime);
Refresh();
}
timer.Stop();
}
#endregion
#region Public Methods
public void ToggleScreenMode()
{
FullScreen = !FullScreen;
if (FullScreen)
_formState.Maximize(this);
else
_formState.Restore(this);
}
#endregion
}
}