-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
343 lines (319 loc) · 8.42 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
#include <GL/glut.h>
#include <math.h>
#include <string.h>
// constants.hpp includes all the constants
#include "constants.hpp"
// functions.hpp includes all the function declarations
#include "functions.hpp"
// utility.hpp includes all the utility functions
#include "utility.hpp"
// objects.hpp includes all objects in the background
#include "objects.hpp"
// Rendering objects
void renderObjects() {
if(sceneMode==0) {
// Morning Dusk
if(pos<=600)
renderDawn();
// Morning
if(pos>600 && pos<=1200)
renderMorning();
// Evening
if(pos>1200 && pos<=1800)
renderEvening();
// Night
if(pos>1800 && pos<2400)
renderNight();
}
if(sceneMode==1)
renderDawn();
if(sceneMode==2)
renderMorning();
if(sceneMode==3)
renderEvening();
if(sceneMode==4)
renderNight();
}
// Function to lock the position of the screen (pausing function)
void lockPosition() {
float newPos;
float newPlanetX;
float newPlanetY;
float newVehiclePos;
float newCloudOneSpeed;
float newCloudTwoSpeed;
float newCloudThreeSpeed;
if(lock==1) {
newPos = pos;
newPlanetX = planetX;
newPlanetY = planetY;
newVehiclePos = vehiclePos;
newCloudOneSpeed = cloudOneSpeed;
newCloudTwoSpeed = cloudTwoSpeed;
newCloudThreeSpeed = cloudThreeSpeed;
lock=2;
}
else if(lock==2) {
pos = newPos;
planetX = newPlanetX;
planetY = newPlanetY;
vehiclePos = newVehiclePos;
cloudOneSpeed = newCloudOneSpeed;
cloudTwoSpeed = newCloudTwoSpeed;
cloudThreeSpeed = newCloudThreeSpeed;
}
else if(lock==0)
return;
}
void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT);
// If scene is paused, lock the positions of the screen
if(pause==1)
lockPosition();
else {
pos+=0.25;
// 1st cloud speed
if(cloudOneSpeed <= 2.0)
cloudOneSpeed += 0.000035;
else
cloudOneSpeed = CLOUD_ONE_DEFAULT_SPEED;
// 2nd cloud speed
if(cloudTwoSpeed <= 2.0)
cloudTwoSpeed += 0.00015;
else
cloudTwoSpeed = CLOUD_TWO_DEFAULT_SPEED;
// 3rd cloud speed
if(cloudThreeSpeed <= 2.0)
cloudThreeSpeed += 0.000005;
else
cloudThreeSpeed = CLOUD_THREE_DEFAULT_SPEED;
// 1st phase - quarter of the screen
if(pos==600)
pos=601.0;
// 2nd phase - half of the screen
if(pos==1200)
pos=1201.0;
// 3rd phase - 3/4th of the screen
if(pos==1800)
pos=1801.0;
// 4th phase - end of the screen
if(pos==2400)
resetScene();
// Render background objects
renderObjects();
// Dawn & Morning
if(pos<=1200)
planetY += 0.000025;
// Evening & Night
if(pos>1200 && pos<2400)
planetY -= 0.000025;
// Entire screen
if(pos>=0 && pos<=2400) {
planetX += 0.0001;
vehiclePos += carSpeed;
}
}
if(pause==1) {
// Render background objects behind the pause screen
renderObjects();
// Render the pause screen
renderPauseScreen();
}
glutPostRedisplay();
glFlush();
}
void displayString(float x, float y, char* string, int fontType, float r, float g, float b) {
int len, i;
glColor3f(r, g, b);
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
if(fontType == 1)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, string[i]);
if(fontType == 2)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
if(fontType == 3)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, string[i]);
if(fontType == 4)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, string[i]);
}
}
void renderTitleScreen() {
glClear(GL_COLOR_BUFFER_BIT);
displayString(0.32, 0.33, projectTitle, 1, 1.0, 1.0, 1.0);
displayString(0.37, 0.26, controlTitle, 1, 1.0, 1.0, 1.0);
displayString(0.30, 0.24, commandDecrease, 2, 1.0, 1.0, 1.0);
displayString(0.30, 0.22, commandIncrease, 2, 1.0, 1.0, 1.0);
displayString(0.30, 0.2, commandPause, 2, 1.0, 1.0, 1.0);
displayString(0.33, 0.18, commandReset, 2, 1.0, 1.0, 1.0);
displayString(0.29, 0.16, comandSceneModes, 2, 1.0, 1.0, 1.0);
displayString(0.31, 0.14, commandQuit, 2, 1.0, 1.0, 1.0);
displayString(0.31, 0.1, commandStart, 1, 1.0, 1.0, 1.0);
renderTitleScreenObjects(0.71, 0.84, 1);
glFlush();
}
void renderPauseScreen() {
renderPauseDialogBox();
displayString(0.33, 0.25, pauseTitle, 1, 0.0, 0.0, 0.0);
displayString(0.34, 0.2, pauseText, 2, 0.0, 0.0, 0.0);
}
void renderSceneTitle(float x, float y, char* sceneTitle) {
renderSceneTitleBox();
displayString(x, y, sceneTitle, 1, 0.0, 0.0, 0.0);
}
void resetScene() {
pos = 0;
planetX = PLANET_X_DEFAULT;
planetY = PLANET_Y_DEFAULT;
vehiclePos = DEFAULT_VEHICLE_POS;
cloudOneSpeed = CLOUD_ONE_DEFAULT_SPEED;
cloudTwoSpeed = CLOUD_TWO_DEFAULT_SPEED;
cloudThreeSpeed = CLOUD_THREE_DEFAULT_SPEED;
}
void handleDisplay() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-0.05, 0.85, -0.05, 0.40);
// Title screen view
if(view==0) {
glClearColor(0, 0.17, 0.37, 1.0);
renderTitleScreen();
}
// Scenary screen view
if(view==1) {
glClearColor(1.0, 1.0, 1.0, 1.0);
renderScene();
}
}
void pauseScene() {
if(pause==1) {
pause=0;
}
else {
pause=1;
lock=0;
}
}
void handleKey(unsigned char key, int x, int y) {
if(key=='s') {
// Set mode to scenary mode
view = 1;
// Re-render the screen
glutPostRedisplay();
}
if(key=='d') {
carSpeed += 0.0002;
if(carSpeed>=0.01)
carSpeed=0.01;
}
if(key=='a') {
carSpeed -= 0.0002;
if(carSpeed<=-0.0002)
carSpeed=-0.0002;
}
if(key=='p') {
pauseScene();
}
if(key=='r') {
resetScene();
}
if(key=='q') {
// Set mode to title screen mode
view = 0;
// Re-render the screen
glutPostRedisplay();
}
// Number 1
if(key==49) {
resetScene();
sceneMode=0;
}
// Number 2
if(key==50) {
resetScene();
sceneMode=1;
}
// Number 3
if(key==51) {
resetScene();
sceneMode=2;
}
// Number 4
if(key==52) {
resetScene();
sceneMode=3;
}
// Number 5
if(key==53) {
resetScene();
sceneMode=4;
}
}
void handleModesMenu(int option) {
resetScene();
switch(option) {
// Default scene
case 0:
sceneMode=0;
break;
// Dawn scene
case 1:
sceneMode=1;
break;
// Morning scene
case 2:
sceneMode=2;
break;
// Evening scene
case 3:
sceneMode=3;
break;
// Night scene
case 4:
sceneMode=4;
break;
}
}
void handleMainMenu(int option) {
switch(option) {
case 0:
pauseScene();
break;
case 1:
resetScene();
break;
case 2:
// Set mode to title screen mode
view = 0;
// Re-render the screen
glutPostRedisplay();
break;
}
}
void callMainMenu() {
int modesMenu = glutCreateMenu(handleModesMenu);
glutAddMenuEntry("Default",0);
glutAddMenuEntry("Dawn",1);
glutAddMenuEntry("Morning",2);
glutAddMenuEntry("Evening",3);
glutAddMenuEntry("Night",4);
glutCreateMenu(handleMainMenu);
glutAddMenuEntry("Pause/Resume", 0);
glutAddSubMenu("Scene Modes", modesMenu);
glutAddMenuEntry("Reset Scene", 1);
glutAddMenuEntry("Exit", 2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (1200, 800);
glutInitWindowPosition (100, 100);
glutCreateWindow("Town Life Simulation");
glutKeyboardFunc(handleKey);
glutDisplayFunc(handleDisplay);
callMainMenu();
glutMainLoop();
return 0;
}