/
main.c
executable file
·323 lines (247 loc) · 7.07 KB
/
main.c
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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "main.h"
#include "geometry.h"
#include "controls.h"
#include "lighting.h"
#include "colors.h"
#include "texture.h"
#include "util.h"
#include "projectile.h"
#include "sound.h"
int main(int argc, char **argv)
{
/*Initialize and create window.*/
glutInit(&argc, argv);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
global.windowID = glutCreateWindow("Assignment 2 - s3331571");
/*Load images to be 'transformed' into textures.*/
globalTexture.grass = loadImage("./Texture/grass.jpg");
globalTexture.metal = loadImage("./Texture/metal.jpg");
/*Load textures from the images.*/
globalTexture.grassT = loadTexture(globalTexture.grass);
globalTexture.metalT = loadTexture(globalTexture.metal);
glEnable(GL_TEXTURE_2D);
/*Initialize any variables.*/
initVariables();
initFiringVariables();
initPlayerBases();
initScores();
glEnable(GL_DEPTH_TEST);
/*Call helper method to initilize lighting.*/
initLighting();
/*Setup Callbacks.*/
glutDisplayFunc(display);
glutMotionFunc(mouseMove);
//glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutSpecialFunc(keyboardSpecial);
glutIdleFunc(idle);
/*Give random function a seed.*/
srand(time(NULL));
/*Calculate initial vertices.*/
storeStarBackground();
calcGeometryVertices();
setupAllMusic();
glutMainLoop();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*If split view is activated.*/
if(global.split)
{
/*Red*/
firstPersonView(90.0, -90.0, 0);
showTextDisplay(TRUE);
/*Blue*/
firstPersonView(-90.0, 90.0, 350);
}
else
{
glPushMatrix();
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glOrtho(MIN_AXIS_LEN, MAX_AXIS_LEN, MIN_AXIS_LEN,
MAX_AXIS_LEN, MIN_AXIS_LEN, MAX_AXIS_LEN);
renderScene();
glPopMatrix();
}
glutSwapBuffers();
}
/*Setup a viewport of a first person view from a base.*/
void firstPersonView(float baseRotation, float starRotation, int leftY)
{
glPushMatrix();
glTranslatef(0.0, 0.0, -2.3);
/*Rotate on x axis to get the flat ringworld.*/
glRotatef(-90.0, 1.0, 0.0, 0.0);
/*Rotate on z to get to a base.*/
glRotatef(baseRotation, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 700.0f/350.0f, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glViewport(0, leftY, 700, 350);
/*Draw the star background perspective.*/
glPushMatrix();
glRotatef(starRotation, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, -2.2);
drawStarBackground();
glPopMatrix();
renderScene();
glPopMatrix();
}
/*Render all the objects into the scene. With appropriate
*transformations applied.*/
void renderScene()
{
if(!global.split)
{
drawStarBackground();
showTextDisplay(FALSE);
}
/*Save matrix for rotation before calculating
how far to zoom in.*/
glMatrixMode(GL_PROJECTION);
glScalef(cameraVar.zoom, cameraVar.zoom, cameraVar.zoom);
glMatrixMode(GL_MODELVIEW);
if(!global.split)
rotateCamera();
drawSceneWorld();
drawSceneProjectiles();
drawSceneBases();
/*Print any errors that may pop up.*/
int error = glGetError();
if(error != GL_NO_ERROR)
{
printf("%s\n", gluErrorString(error));
exit(1);
}
/*Reset projection matrix to default
*so the text and star background does not
*get scaled next iteration.*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
/*Helper function to apply transformations
*to each base without too much duplicate code.*/
void drawSceneBase(float color[COLOR_SIZE], float x, float y)
{
glPushMatrix();
glRotatef(global.animateRing, 0.0, 0.0, 1.0);
glTranslatef(x, y, 0.0);
drawBase(color);
glPopMatrix();
}
/*Apply appropriate transformations and draw
*the player bases.*/
void drawSceneBases()
{
if(globalBase.redMid)
drawSceneBase(red, -MID_BASE_X, 0.0);
if(globalBase.redNorth)
drawSceneBase(red, -SIDE_BASE_X, SIDE_BASE_Y);
if(globalBase.redSouth)
drawSceneBase(red, -SIDE_BASE_X, -SIDE_BASE_Y);
if(globalBase.blueMid)
drawSceneBase(blue, MID_BASE_X, 0.0);
if(globalBase.blueNorth)
drawSceneBase(blue, SIDE_BASE_X, SIDE_BASE_Y);
if(globalBase.blueSouth)
drawSceneBase(blue, SIDE_BASE_X, -SIDE_BASE_Y);
}
/*Apply transformations and draw projectiles
*to the scene.*/
void drawSceneProjectiles()
{
glPushMatrix();
glRotatef(global.animateRing, 0.0, 0.0, 1.0);
drawProjectiles();
glPopMatrix();
}
/*Draw the rings and sun applying transformations.*/
void drawSceneWorld()
{
/*Draw the sun.*/
setSunMaterial();
drawSphereFilled();
/*Draw the rings.*/
glPushMatrix();
glRotatef(global.animateRing, 0.0, 0.0, 1.0);
setRingWorldMaterial();
drawRing(RING_SIZE, blue);
setOpenSailMaterial();
drawInnerRing(SAIL_SIZE, light_grey);
glPopMatrix();
}
/*Init player scores.*/
void initScores()
{
globalScore.red = 0;
globalScore.blue = 0;
}
/*Initialize the number of player bases.*/
void initPlayerBases()
{
globalScore.playerOneWon = FALSE;
globalScore.gameOver = FALSE;
/*Set a boolean testing whether a base is alive or not.*/
globalBase.redNorth = TRUE;
globalBase.redSouth = TRUE;
globalBase.redMid = TRUE;
globalBase.blueNorth = TRUE;
globalBase.blueSouth = TRUE;
globalBase.blueMid = TRUE;
}
/*Test whether a player won by checking which
*bases are still alive.*/
int playerWon()
{
/*The gameOver variable is used so the score only updates once rather
*than continually until the user hits the restart button.*/
/*Red lost.*/
if(!globalBase.redNorth && !globalBase.redSouth && !globalBase.redMid)
{
if(globalScore.gameOver == FALSE)
{
globalScore.blue++;
globalScore.gameOver = TRUE;
globalScore.playerOneWon = FALSE;
}
return TRUE;
}
/*Blue lost.*/
if(!globalBase.blueNorth && !globalBase.blueSouth && !globalBase.blueMid)
{
if(globalScore.gameOver == FALSE)
{
globalScore.red++;
globalScore.gameOver = TRUE;
globalScore.playerOneWon = TRUE;
}
return TRUE;
}
return FALSE;
}
/*Show the text display in either of the views.*/
void showTextDisplay(int firstPerson)
{
glPushMatrix();
glLoadIdentity();
if(!firstPerson)
glOrtho(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, MIN_AXIS_LEN, MAX_AXIS_LEN);
else
glOrtho(0, WINDOW_WIDTH / 2, 0, WINDOW_HEIGHT / 2, MIN_AXIS_LEN, MAX_AXIS_LEN);
if(playerWon())
drawTextPlayerWon();
else
drawTextToScreen();
glPopMatrix();
}