-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
476 lines (371 loc) · 13.6 KB
/
Main.cpp
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#define NOMINMAX
#include <cml/cml.h>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "Player.cpp"
#include "Enemy.cpp"
#include <list>
#include "BoundingBox.h"
#include "Projectile.cpp"
void Update();
void Draw();
void DrawCube(float size);
void DrawCube2(float size, cml::vector3f colour);
void DrawFace(float size);
void Initialize();
#if 0
static std::string toString(float value);
#endif
void MouseMovement();
void ProcessKeyboard();
void ProcessMouse(sf::Event Event);
void UpdateProjectiles();
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Space Ship Demo", sf::Style::Fullscreen);
sf::Clock Clock;
sf::Clock updateClock;
//sf:String Text;
//sf::Font Arial;
SpaceShip::Player player(cml::vector3f(0,0,100));
std::vector<SpaceShip::Enemy> enemies;
bool initMousePos = false;
typedef std::vector<SpaceShip::Projectile> ProjectileVector;
ProjectileVector projectiles;
int main()
{
/*
if (!Arial.LoadFromFile("arial.ttf"))
return EXIT_FAILURE;
//Text("Hello SFML", Arial, 50);
Text.SetText("Hello");
Text.SetFont(MyFont);
Text.SetSize(50);
*/
Initialize();
while (window.IsOpened())
{
if (updateClock.GetElapsedTime() > 1.f/60.f) //update a set number of times per second, set time to avoid players moving at different speeds on different computers
{
Update();
updateClock.Reset();
}
Draw(); //draws as fast as possible
}
return EXIT_SUCCESS;
}
void Update()
{
sf::Event Event;
while (window.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
window.Close();
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
if (Event.Type == sf::Event::MouseMoved)
{
player.mouseX = Event.MouseMove.X;
player.mouseY = Event.MouseMove.Y;
}
//stores the players mouse pos initially, otherwise it takes it as (0,0)
//and calculates that the player makes a big spin at the start of the game
//making him upside down
if (!initMousePos)
{
const sf::Input& input = window.GetInput();
player.lastMouseX = input.GetMouseX();
player.lastMouseY = input.GetMouseY();
initMousePos = true;
}
ProcessMouse(Event);
}
ProcessKeyboard();
UpdateProjectiles();
}
void UpdateProjectiles()
{
for(ProjectileVector::size_type i = 0; i < projectiles.size(); i++)
{
projectiles[i].SetPosition(projectiles[i].GetPosition() + projectiles[i].velocity);
for (ProjectileVector::size_type k = 0; k < enemies.size(); k++)
{
if (projectiles[i].boundingBox.Intersects(enemies[k].boundingBox))
{
enemies[k].colour = cml::vector3f(enemies[k].colour[0] + 0.1f, enemies[k].colour[1] - 0.1f, enemies[k].colour[2] - 0.1f);
projectiles.erase(projectiles.begin() + i);
k = enemies.size();
}
else for(unsigned int j = 0; j < 3; j++)
if(projectiles[i].GetPosition()[j] > 1000 || projectiles[i].GetPosition()[j] < -1000)
{
projectiles.erase(projectiles.begin() + i);
k = enemies.size();
j = 3;
}
}
}
}
void ProcessKeyboard()
{
const sf::Input& input = window.GetInput();
if (input.IsKeyDown(sf::Key::Escape))
window.Close();
float speed = 6.f;
//key press events
// Escape key : exit
//if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
// window.Close();
if (input.IsKeyDown(sf::Key::W))
{
//if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::W))
//{
float xrotrad, yrotrad;
yrotrad = (player.rotY / 180 * 3.141592654f);
xrotrad = (player.rotX / 180 * 3.141592654f);
player.position[0] += float(sin(yrotrad)) * speed ;
player.position[2] -= float(cos(yrotrad)) * speed;
player.position[1] -= float(sin(xrotrad)) * speed;
}
if (input.IsKeyDown(sf::Key::S))
{
float xrotrad, yrotrad;
yrotrad = (player.rotY / 180 * 3.141592654f);
xrotrad = (player.rotX / 180 * 3.141592654f);
player.position[0] -= float(sin(yrotrad)) * speed;
player.position[2] += float(cos(yrotrad)) * speed;
player.position[1] += float(sin(xrotrad)) * speed;
}
if (input.IsKeyDown(sf::Key::D))
{
float yrotrad;
yrotrad = (player.rotY / 180 * 3.141592654f);
player.position[0] += float(cos(yrotrad)) * speed;// * 0.2f;
player.position[2] += float(sin(yrotrad)) * speed;// * 0.2f;
}
if (input.IsKeyDown(sf::Key::A))
{
float yrotrad;
yrotrad = (player.rotY / 180 * 3.141592654f);
player.position[0] -= float(cos(yrotrad)) * speed;// * 0.2f;
player.position[2] -= float(sin(yrotrad)) * speed;// * 0.2f;
}
}
void Draw()
{
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Apply some transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glPushMatrix();
//glTranslatef(0.f, 0.f, 0.f);
/*
glRotatef(Clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
glRotatef(Clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
glRotatef(Clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
*/
//draw outer surrounding box of maze
//DrawCube(5.f);
//glPopMatrix();
//translate camera to player position
glRotatef(player.rotX,1.0,0.0,0.0); //rotate our camera on the x-axis (left and right)
glRotatef(player.rotY,0.0,1.0,0.0); //rotate our camera on the y-axis (up and down)
glTranslatef(-player.position[0], -player.position[1], -player.position[2]);
//Draw enemy1
glPushMatrix();
glTranslatef(enemies[0].position[0], enemies[0].position[1], enemies[0].position[2]);
glRotatef(enemies[0].cubeRotX, 1.f, 0.f, 0.f);
glRotatef(enemies[0].cubeRotY, 0.f, 1.f, 0.f);
glRotatef(enemies[0].cubeRotZ, 0.f, 0.f, 1.f);
DrawCube2(1.f, enemies[0].colour);
glPopMatrix();
glPushMatrix();
glTranslatef(enemies[1].position[0], enemies[1].position[1], enemies[1].position[2]);
glRotatef(enemies[1].cubeRotX, 1.f, 0.f, 0.f);
glRotatef(enemies[1].cubeRotY, 0.f, 1.f, 0.f);
glRotatef(enemies[1].cubeRotZ, 0.f, 0.f, 1.f);
DrawCube2(1.f, enemies[1].colour);
glPopMatrix();
for(ProjectileVector::size_type i = 0; i < projectiles.size(); i++)
{
glPushMatrix();
glTranslatef(projectiles[i].GetPosition()[0], projectiles[i].GetPosition()[1], projectiles[i].GetPosition()[2]);
DrawCube2(0.25f, projectiles[i].colour);
glPopMatrix();
}
//glPushMatrix();
//DrawFace(1.f);
//DrawCube2(1.f, cml::vector3f(0.3f, 0.9f, 0.6f));
//glPopMatrix();
/*
glPushMatrix();
glTranslatef(player.position[0], player.position[1], player.position[2]);
//glTranslatef(0.f, 0.f, -200.f);
DrawCube(1.f);
glPopMatrix();
*/
/*
float Xvalue = player.position[0];
std::string coordX = toString(Xvalue);
float Yvalue = player.position[1];
std::string coordY = toString(Yvalue);
float Zvalue = player.position[2];
std::string coordZ = toString(Zvalue);
sf::String text("Player coords: " + coordX +","+ coordY +","+ coordZ, sf::Font::GetDefaultFont());
window.Draw(text);*/
window.Display();
}
void DrawCube2(float size)
{
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-50.f * size, -50.f * size, -50.f * size);
glVertex3f(-50.f * size, 50.f * size, -50.f * size);
glVertex3f( 50.f * size, 50.f * size, -50.f * size);
glVertex3f( 50.f * size, -50.f * size, -50.f * size);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-50.f * size, -50.f * size, 50.f * size);
glVertex3f(-50.f * size, 50.f * size, 50.f * size);
glVertex3f( 50.f * size, 50.f * size, 50.f * size);
glVertex3f( 50.f * size, -50.f * size, 50.f * size);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.f * size, -50.f * size, -50.f * size);
glVertex3f(-50.f * size, 50.f * size, -50.f * size);
glVertex3f(-50.f * size, 50.f * size, 50.f * size);
glVertex3f(-50.f * size, -50.f * size, 50.f * size);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(50.f * size, -50.f * size, -50.f * size);
glVertex3f(50.f * size, 50.f * size, -50.f * size);
glVertex3f(50.f * size, 50.f * size, 50.f * size);
glVertex3f(50.f * size, -50.f * size, 50.f * size);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-50.f * size, -50.f * size, 50.f * size);
glVertex3f(-50.f * size, -50.f * size, -50.f * size);
glVertex3f( 50.f * size, -50.f * size, -50.f * size);
glVertex3f( 50.f * size, -50.f * size, 50.f * size);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-50.f * size, 50.f * size, 50.f * size);
glVertex3f(-50.f * size, 50.f * size, -50.f * size);
glVertex3f( 50.f * size, 50.f * size, -50.f * size);
glVertex3f( 50.f * size, 50.f * size, 50.f * size);
glEnd();
}
void DrawFace(float size, cml::vector3f colour)
{
glBegin(GL_QUADS);
glColor3f(colour[0], colour[1], colour[2]);
glVertex3f(-45.f * size, -45.f * size, -50.f * size);
glVertex3f(-45.f * size, 45.f * size, -50.f * size);
glVertex3f( 45.f * size, 45.f * size, -50.f * size);
glVertex3f( 45.f * size, -45.f * size, -50.f * size);
//bottom line
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.f * size, -50.f * size, -50.f * size);
glVertex3f(-50.f * size, -45.f * size, -50.f * size);
glVertex3f( 50.f * size, -45.f * size, -50.f * size);
glVertex3f( 50.f * size, -50.f * size, -50.f * size);
//top line
//glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.f * size, 50.f * size, -50.f * size);
glVertex3f(-50.f * size, 45.f * size, -50.f * size);
glVertex3f( 50.f * size, 45.f * size, -50.f * size);
glVertex3f( 50.f * size, 50.f * size, -50.f * size);
//left line
glVertex3f(-50.f * size, -50.f * size, -50.f * size);
glVertex3f(-50.f * size, 50.f * size, -50.f * size);
glVertex3f(-45.f * size, 50.f * size, -50.f * size);
glVertex3f(-45.f * size, -50.f * size, -50.f * size);
//right line
glVertex3f(50.f * size, -50.f * size, -50.f * size);
glVertex3f(50.f * size, 50.f * size, -50.f * size);
glVertex3f(45.f * size, 50.f * size, -50.f * size);
glVertex3f(45.f * size, -50.f * size, -50.f * size);
glEnd();
}
void DrawCube2(float size, cml::vector3f colour)
{
glPushMatrix();
DrawFace(size, colour);
glRotatef(90, 0.f, 1.f, 0.f);
DrawFace(size, colour);
glRotatef(90, 0.f, 1.f, 0.f);
DrawFace(size, colour);
glRotatef(90, 0.f, 1.f, 0.f);
DrawFace(size, colour);
glPopMatrix();
glPushMatrix();
glRotatef(90, 1.f, 0.f, 0.f);
DrawFace(size, colour);
glRotatef(180, 1.f, 0.f, 0.f);
DrawFace(size, colour);
glPopMatrix();
}
void Initialize()
{
window.EnableKeyRepeat(false);
window.UseVerticalSync(true);
enemies.push_back(SpaceShip::Enemy(cml::vector3f(200,0,-200), cml::vector3f(0.3f, 1.f, 0.3f), SpaceShip::BoundingBox(cml::vector3f(200,0,-200), 100, 100, 100)));//cml::vector3f(50,50,50), cml::vector3f(-50,-50,-50)));
enemies.push_back(SpaceShip::Enemy(cml::vector3f(-200,0,-200), cml::vector3f(0.3f, 1.f, 0.3f), SpaceShip::BoundingBox(cml::vector3f(-200,0,-200), 100, 100, 100)));//cml::vector3f(50,50,50), cml::vector3f(-50,-50,-50)));
/*
if (!initMousePos)
{
const sf::Input& input = window.GetInput();
player.lastMouseX = input.GetMouseX();
player.lastMouseY = input.GetMouseY();
initMousePos = true;
}*/
// Set color and depth clear value
glClearDepth(1.f);
glClearColor(0.3f, 0.7f, 0.7f, 0.f);
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 2000.f);
/*
const sf::Input& input = window.GetInput();
player.lastMouseX = input.GetMouseX();
player.lastMouseY = input.GetMouseY();
*/
}
#if 0
static std::string toString(float value)
{
std::ostringstream stream;
stream << value;
return stream.str();
}
#endif
void ProcessMouse(sf::Event Event)
{
if (Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left)
{
cml::vector3f initialVector = cml::vector3f(0,0,-5); //initial vector to transform, projectiles have speed of 5
//convert player's rotation to radians and create rotation matrices
cml::matrix44f_r xrotMatrix;
cml::matrix_rotation_world_x(xrotMatrix, cml::rad(-player.rotX));
cml::matrix44f_r yrotMatrix;
cml::matrix_rotation_world_y(yrotMatrix, cml::rad(-player.rotY));
cml::matrix44f_r zrotMatrix;
cml::matrix_rotation_world_z(zrotMatrix, cml::rad(-player.rotZ));
//multiply the three matrices together into one final matrix
cml::matrix44f_r finalMatrix(cml::operator *(cml::operator *(xrotMatrix, yrotMatrix), zrotMatrix));
//xrotMatrix cml::operator * yrotMatrix cml::operator * zrotMatrix;
//cml::operator *
//apply the matrix rotation information to the vector to make it face in the right direction
initialVector = cml::transform_vector(finalMatrix, initialVector);
//add projectile with the velocity in initialVector
projectiles.push_back(SpaceShip::Projectile(player.position, cml::vector3f(1.f,1.f,1.f), SpaceShip::BoundingBox(player.position, 25,25,25), initialVector));
}
MouseMovement();
}
void MouseMovement()
{
float diffx = player.mouseX - player.lastMouseX; //check the difference between the current x and the last x position
float diffy = player.mouseY - player.lastMouseY; //check the difference between the current y and the last y position
player.lastMouseX = player.mouseX; //set lastx to the current x position
player.lastMouseY = player.mouseY; //set lasty to the current y position
player.rotX += diffy; //set the xrot to xrot with the addition of the difference in the y position
player.rotY += diffx; //set the xrot to yrot with the addition of the difference in the x position
}