-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphicsForm.cs
302 lines (269 loc) · 10.3 KB
/
GraphicsForm.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using System.Diagnostics;
using System;
using System.IO;
using System.Collections.Generic;
namespace Graphics {
public partial class GraphicsForm : Form {
public static bool gameStarted = false;
public static int sanityCount = 5;
public static List<int> skybox_waitingTimes = new List<int> { 10, 30, 30, 30, 30, 30, 30 };
string currentScreen;
StartScreen startScreen = new StartScreen();
LoadScreen loadScreen = new LoadScreen();
Renderer renderer = new Renderer();
Thread MainLoopThread;
List<String> garbageMessages = new List<string>(){
"You have found the key, I'm impressed!",
"Garbage. Garbage everywhere.",
"Nothing of use here.",
"People can be very messy.",
"You're going to be very smelly."
};
MP3_player stepsOut = new MP3_player();
MP3_player stepsIn = new MP3_player();
bool stepsOn = false;
float deltaTime;
public GraphicsForm(string currScreen)
{
currentScreen = currScreen;
InitializeComponent();
#region Full-Screen
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Size = GetScreen().Size;
simpleOpenGlControl1.Size = this.Size;
#endregion
simpleOpenGlControl1.InitializeContexts();
Cursor.Hide();
MoveCursor();
initialize();
deltaTime = 0.005f;
#region Threads
if (Thread.CurrentThread.Name == "")
Thread.CurrentThread.Name = "Main";
MainLoopThread = new Thread(MainLoop);
MainLoopThread.Name = "MainLoop Thread";
MainLoopThread.Start();
#endregion
}
void initialize() {
if (currentScreen == "start")
startScreen.Initialize();
if (currentScreen == "load")
loadScreen.Initialize();
if (currentScreen == "renderer")
{
//loadScreen.Initialize();
renderer.Initialize();
stepsOut.open(Renderer.projectPath + @"\Sounds\forest_footsteps.mp3");
stepsIn.open(Renderer.projectPath + @"\Sounds\footsteps_house.mp3");
}
}
void MainLoop() {
if (currentScreen == "start")
startScreen.Draw();
while (true) {
try {
//if (!Renderer.PrevoiuslyLoaded[Renderer.currentSkyboxID])
// loadScreen.Draw();
renderer.Draw();
renderer.Update(deltaTime);
}
catch { }
simpleOpenGlControl1.Refresh();
}
}
private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e) {
try {
if (currentScreen == "start")
startScreen.Draw();
if (currentScreen == "load")
loadScreen.Draw();
if (currentScreen == "renderer") {
//if (!Renderer.PrevoiuslyLoaded[Renderer.currentSkyboxID])
// loadScreen.Draw();
renderer.Draw();
renderer.Update(deltaTime);
}
}
catch { }
}
private void simpleOpenGlControl1_KeyPress(object sender, KeyPressEventArgs e) {
if (e.KeyChar == (char)(32) || e.KeyChar == (char)(13))//space or enter
{
if (currentScreen == "start")
{
if (StartScreen.currentBtnIDX == 0) //Play
{
gameStarted = true;
MainLoopThread.Abort();
startScreen.CleanUp();
GraphicsForm rendererForm = new GraphicsForm("renderer");
this.Hide();
rendererForm.ShowDialog();
this.Close();
}
else //Exit
{
if (gameStarted)
{
//loadScreen.CleanUp();
renderer.CleanUp();
}
else
startScreen.CleanUp();
MainLoopThread.Abort();
this.Close();
}
}
}
if (currentScreen == "renderer") {
if (e.KeyChar == (char)(27))
{
renderer.pauseScreen = !renderer.pauseScreen;
}
if(e.KeyChar == (char)(32))
{
if (Renderer.lostSanity || renderer.pauseScreen)
{
gameStarted = false;
MainLoopThread.Abort();
renderer.CleanUp();
GraphicsForm startForm = new GraphicsForm("start");
this.Hide();
startForm.ShowDialog();
this.Close();
}
if (Renderer.text_shown)
{
Renderer.text_shown = false;
}
}
float speed = 3f;
if (Renderer.currentSkyboxID == 0)
speed = 1.5f;
if (e.KeyChar == 'a')
Renderer.cam.Strafe(-speed);
if (e.KeyChar == 'd')
Renderer.cam.Strafe(speed);
if (e.KeyChar == 's')
Renderer.cam.Walk(-speed);
if (e.KeyChar == 'w')
Renderer.cam.Walk(speed);
if (Camera.Move && e.KeyChar != 'e')
WalkSound();
//if (e.KeyChar == 'z')
// Renderer.cam.Fly(-speed);
//if (e.KeyChar == 'c')
// Renderer.cam.Fly(speed);
if (e.KeyChar == 'e') {
try {
modelType currentInteractionType = renderer.InteractiveCheck();
#region Garbage interaction
if (currentInteractionType == modelType.GARBAGE) {
//if (Renderer.playerHasKey)
// MessageBox.Show(garbageMessages[0]);
//else
//{
// Random random = new Random();
// MessageBox.Show(garbageMessages[random.Next(1, garbageMessages.Count - 1)]);
//}
}
#endregion
}
catch { }
}
}
}
float prevX, prevY;
private void simpleOpenGlControl1_MouseMove(object sender, MouseEventArgs e) {
if (currentScreen == "renderer") {
float speed = 0.05f;
float delta = e.X - prevX;
if (delta > 2)
Renderer.cam.Yaw(-speed);
else if (delta < -2)
Renderer.cam.Yaw(speed);
delta = e.Y - prevY;
if (delta > 2)
Renderer.cam.Pitch(-speed);
else if (delta < -2)
Renderer.cam.Pitch(speed);
MoveCursor();
}
}
private void MoveCursor() {
this.Cursor = new Cursor(Cursor.Current.Handle);
Point p = PointToScreen(simpleOpenGlControl1.Location);
Cursor.Position = new Point(simpleOpenGlControl1.Size.Width / 2 + p.X, simpleOpenGlControl1.Size.Height / 2 + p.Y);
Cursor.Clip = new Rectangle(this.Location, this.Size);
prevX = simpleOpenGlControl1.Location.X + simpleOpenGlControl1.Size.Width / 2;
prevY = simpleOpenGlControl1.Location.Y + simpleOpenGlControl1.Size.Height / 2;
}
private void simpleOpenGlControl1_MouseClick(object sender, MouseEventArgs e)
{
//MessageBox.Show(e.X.ToString() + ", " + e.Y.ToString());
if (currentScreen == "start") {
if (startScreen.startButtonClicked(e.X, e.Y)) {
gameStarted = true;
this.Hide();
MainLoopThread.Abort();
startScreen.CleanUp();
GraphicsForm rendererForm = new GraphicsForm("renderer");
rendererForm.ShowDialog();
this.Close();
}
}
}
private void GraphicsForm_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
MainLoopThread.Abort();
}
catch { }
}
private void simpleOpenGlControl1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
StartScreen.currentBtnIDX++;
StartScreen.currentBtnIDX %= StartScreen.buttonsCount;
}
if (e.KeyCode == Keys.Up)
{
StartScreen.currentBtnIDX--;
StartScreen.currentBtnIDX %= StartScreen.buttonsCount;
StartScreen.currentBtnIDX += StartScreen.buttonsCount;
StartScreen.currentBtnIDX %= StartScreen.buttonsCount;
}
}
public void WalkSound()
{
if (!stepsOn)
{
stepsOn = true;
if (Renderer.currentSkyboxID == 0)
stepsOut.PlayLooping();
else
{
stepsOut.stop();
stepsIn.PlayLooping();
}
}
}
private void simpleOpenGlControl1_KeyUp(object sender, KeyEventArgs e)
{
stepsOn = false;
//if (Renderer.currentSkyboxID == 0)
stepsOut.stop();
//else
stepsIn.stop();
}
public Rectangle GetScreen() {
return Screen.FromControl(this).Bounds;
}
}
}