-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrettyPixel.cpp
539 lines (471 loc) · 12.1 KB
/
PrettyPixel.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <cstring>
#include <fstream>
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <time.h>
//Level picture storage
#include "Pictures.h"
using namespace std;
/**************************************************************************/
//Global defaults
int
screenLength = 800, screenWidth, screenHeight, level = 0, siz = 0,
solBuffer = 5, score = 1000, picture[300][3], custPicture[300][3],
ind = 0;
float
rotateX = 0, rotateY = 0, goalX = 0, goalY = 0, cx, cy;
bool
pass = false, drawSC = false, custGame = false, firstG = true, epilepsyFlag = false, hard = false;
char
scoreText[7], levelText[3];
/**************************************************************************/
//Background operations
//Randomize z-coordinates
void RandomizeZ(int ray[][3], int siz) {
srand(time(NULL));
for (int i = 0; i < siz; ++i) {
ray[i][2] = (rand() % 600) - 300;
}
}
//Draw and store picture for custom game
void DrawCustomPixel(int x, int y) {
glPushMatrix();
cout << endl << ind;
glColor3f(1, 1, 1);
glPointSize(5);
glBegin(GL_POINTS);
glVertex3f(x, y, 0);
glEnd();
glutSwapBuffers();
glPopMatrix();
//Store points
custPicture[ind][0] = x,
custPicture[ind][1] = y,
custPicture[ind][2] = 0;
ind++;
}
//Custom Drawboard display
void CustomDraw() {
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(0.3, 0.69, 0.31);
glLineWidth(1);
glutWireCube(700);
glFlush();
glPopMatrix();
glutSwapBuffers();
}
//Text render
void DrawText(const char *text, int x, int y, int sparkle) {
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(x, y, 0);
if (sparkle == 1) {
glScalef(.5, .5, 0);
glLineWidth(5);
}
else if (sparkle == 2) {
glScalef(.15, .15, 0);
glLineWidth(2);
}
else if (sparkle == 3) {
glScalef(.3, .3, 0);
glLineWidth(3);
}
for (int i = 0; text[i] != '\0'; i++)
glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, (int)text[i]);
glutPostRedisplay();
glPopMatrix();
}
//Draw playground
void DrawPixel() {
glPushMatrix();
//Draw points
epilepsyFlag ? glColor3f((rand() % 100) / 100.0, (rand() % 100) / 100.0, (rand() % 100) / 100.0) : glColor4f(0.96, 0.26, 0.21, 1.0);
glPointSize(5);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_INT, 0, picture);
glDrawArrays(GL_POINTS, 0, siz);
glDisableClientState(GL_VERTEX_ARRAY);
//Draw lines
if (hard)
{
glColor3f(1, 1, 1);
glLineWidth(1);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_INT, 0, picture);
glDrawArrays(GL_LINES, 0, siz);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
}
glPopMatrix();
}
/**************************************************************************/
//Display functions
//Main game display
void Pretty() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPointSize(1);
glColor3f(1, 1, 1);
//Round details
sprintf(levelText, "%d", level);
DrawText("Level: ",
-70, (screenHeight / 2.2), 2);
DrawText(levelText,
30, (screenHeight / 2.2), 2);
sprintf(scoreText, "%d", score);
DrawText("Score: ",
-70, (screenHeight / 2.4), 2);
DrawText(scoreText,
30, (screenHeight / 2.4), 2);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity();
//Check solution
if (abs(goalX - rotateX) < solBuffer &&
abs(goalY - rotateY) < solBuffer) {
rotateX = goalX;
rotateY = goalY;
pass = true;
}
//Rotate playing field
glRotatef(goalX - rotateX, 1, 0, 0);
glRotatef(goalY - rotateY, 0, 1, 0);
//Display Image
DrawPixel();
glFlush();
glutSwapBuffers();
}
//Gameover display
void GameOver() {
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPointSize(1);
glColor3f(1.0, 0.54, 0.5);
DrawText("GameOver",
-(screenLength / 3.4), (screenHeight / 4), 1);
glColor3f(0.3, 0.69, 0.31);
DrawText("(m/M): MAIN MENU",
-(screenLength / 5.5), (screenHeight / 16), 2);
DrawText("(q/Q): EXIT",
-(screenLength / 5.5), 0, 2);
glColor3f(1.0, 0.54, 0.5);
DrawText("Your High Score: ",
-(screenLength / 2.3), -(screenHeight / 5), 3);
DrawText(scoreText,
(screenLength / 5), -(screenHeight / 5), 3);
glColor3f(1.0, 0.44, 0.26);
glBegin(GL_LINES);
glVertex3f(-(screenWidth / 2), (screenHeight / 2 - 40), 0);
glVertex3f((screenWidth / 2), (screenHeight / 2 - 40), 0);
glVertex3f((screenWidth / 2), -(screenHeight / 2 - 40), 0);
glVertex3f(-(screenWidth / 2), -(screenHeight / 2 - 40), 0);
glEnd();
glFlush();
glPopMatrix();
glutSwapBuffers();
}
//Splash screen display
void Splash() {
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0, 0.54, 0.5);
DrawText("PRETTY PIXEL",
-(screenLength / 2.7), (screenHeight / 4), 1);
glColor3f(0.3, 0.69, 0.31);
DrawText("(g/G): START GAME",
-(screenLength / 5), (screenHeight / 16), 2);
DrawText("(h/H): HELP",
-(screenLength / 5), 0, 2);
DrawText("(c/C): CUSTOM GAME",
-(screenLength / 5), -(screenHeight / 16), 2);
glColor3f(1.0, 0.54, 0.5);
DrawText("Computer Graphics Project",
-(screenLength / 2.05), -(screenHeight / 5), 3);
glColor3f(0.3, 0.69, 0.31);
DrawText("Shreyas N",
-(screenWidth / 2.5), -(screenHeight / 3.5), 2);
DrawText("1MV14CS130",
-(screenWidth / 2.5), -(screenHeight / 3), 2);
DrawText("Shashank S",
(screenWidth / 2.5) - 157, -(screenHeight / 3.5), 2);
DrawText("1MV14CS131",
(screenWidth / 2.5) - 157, -(screenHeight / 3), 2);
glLineWidth(10);
glColor3f(1.0, 0.44, 0.26);
glBegin(GL_LINES);
glVertex3f(-(screenWidth / 2), (screenHeight / 2 - 40), 0);
glVertex3f((screenWidth / 2), (screenHeight / 2 - 40), 0);
glVertex3f((screenWidth / 2), -(screenHeight / 2 - 40), 0);
glVertex3f(-(screenWidth / 2), -(screenHeight / 2 - 40), 0);
glEnd();
glFlush();
glPopMatrix();
score = 2000;
glutSwapBuffers();
}
//Help screen display
void Help() {
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glLineWidth(10);
glPointSize(1);
glColor3f(1.0, 0.44, 0.26);
glBegin(GL_LINES);
glVertex3f(-(screenWidth / 2), (screenHeight / 2 - 40), 0);
glVertex3f((screenWidth / 2), (screenHeight / 2 - 40), 0);
glVertex3f((screenWidth / 2), -(screenHeight / 2 - 40), 0);
glVertex3f(-(screenWidth / 2), -(screenHeight / 2 - 40), 0);
glEnd();
glColor3f(1.0, 1.0, 1.0);
DrawText("The rules are simple",
-(screenLength / 4), (screenHeight / 3), 2);
glColor3f(0.0, 1.0, 1.0);
DrawText("Move mouse to rotate the playing field",
-(screenLength / 2.1), (screenHeight / 16), 2);
DrawText("Left Mouse Button to advance to next level",
-(screenLength / 2.1), 0, 2);
DrawText("Right Mouse Button to exit the game",
-(screenLength / 2.1), -(screenHeight / 16), 2);
DrawText("(m/M) to go to menu, (g/G) to start game",
-(screenLength / 2.1), -(screenHeight / 8), 2);
DrawText("(t/T) to increase difficulty",
-(screenLength / 2.1), -(screenHeight / 5.2), 2);
glFlush();
glPopMatrix();
glutSwapBuffers();
}
/**************************************************************************/
//Level initialization
void Leveler() {
//Set random solution point
srand(time(NULL));
cx = (rand() % screenLength) - (screenLength / 2);
cy = (rand() % screenLength) - (screenLength / 2);
goalX = cy / (screenLength / 200);
goalY = cx / (screenLength / 200);
//Clear picture
memset(picture, 0, sizeof(int) * siz * 3);
//Level up
level++;
//Play Custom Game
if (custGame) {
siz = ind;
memcpy(picture, custPicture, sizeof(int) * siz * 3);
score = 1000;
level = 999;
custGame = false;
}
//Play default game
else {
//Load level picture
switch (level) {
case 1:
siz = sizeof(one) / sizeof(one[0]);
memcpy(picture, one, sizeof(int) * siz * 3);
break;
case 2:
siz = sizeof(two) / sizeof(two[0]);
memcpy(picture, two, sizeof(int) * siz * 3);
score += 1000;
break;
case 3:
siz = sizeof(three) / sizeof(three[0]);
memcpy(picture, three, sizeof(int) * siz * 3);
score += 1000;
break;
case 4:
siz = sizeof(four) / sizeof(four[0]);
memcpy(picture, four, sizeof(int) * siz * 3);
score += 1000;
break;
case 5:
siz = sizeof(five) / sizeof(five[0]);
memcpy(picture, five, sizeof(int) * siz * 3);
score += 1000;
break;
case 6:
siz = sizeof(six) / sizeof(six[0]);
memcpy(picture, six, sizeof(int) * siz * 3);
score += 1000;
break;
case 7:
siz = sizeof(seven) / sizeof(seven[0]);
memcpy(picture, seven, sizeof(int) * siz * 3);
score += 1000;
break;
default://Gameover
cout << " Score: " << score << "\nGame Over\n";
siz = sizeof(one) / sizeof(one[0]);
memcpy(picture, one, sizeof(int) * siz * 3);
level = 1;
score = 1000;
glutDisplayFunc(GameOver);
break;
}
}
if (level > 1)
cout << " Score: " << score;
cout << "\nLevel " << level;
//Randomize z coordinates
RandomizeZ(picture, siz);
}
//Window reshape callback
void Reshape(int w, int h) {
screenWidth = w;
screenHeight = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-w / 2, w / 2, -h / 2, h / 2, -w / 2, w / 2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/**************************************************************************/
//Input callbacks
//Passive motion callback
void MouseMove(int x, int y) {
//Normalization
x = x - (screenLength / 2),
y = (screenLength - y) - (screenLength / 2);
if (!drawSC) {
rotateX = -y / (screenLength / 200);
rotateY = x / (screenLength / 200);
if (--score < 0)
{
score = 1000;
glutDisplayFunc(GameOver);
}
glutPostRedisplay();
}
}
//Active motion callback
void MouseClickMove(int x, int y) {
//Normalization
x = x - (screenLength / 2),
y = (screenLength - y) - (screenLength / 2);
}
//Mouse click call back
void MouseClick(int button, int state, int x, int y) {
//Normalization
x = x - (screenLength / 2),
y = (screenLength - y) - (screenLength / 2);
switch (button) {
case GLUT_RIGHT_BUTTON://Right mouse button
if (state == GLUT_DOWN) {
if (!drawSC) {
cout << "\nGame Over\n";
//exit(0);
}
else {
//WriteCoord();
drawSC = false;
custGame = true;
glutSetWindowTitle("Pretty Pixel");
glutFullScreen();
glPointSize(1);
Leveler();
glutDisplayFunc(Pretty);
}
}
break;
case GLUT_LEFT_BUTTON://left mouse button
if (state == GLUT_DOWN && pass == true && !drawSC) {
//Level up
pass = false;
Leveler();
}
else if (state == GLUT_DOWN && drawSC &&
x > -350 && x < 350 && y > -350 && y < 350) {
//User drawing board
DrawCustomPixel(x, y);
glFlush();
}
break;
}
}
//Keyboard callback
void Keys(unsigned char key, int x, int y) {
switch (key)
{
case 'm':
case 'M':
//Splash screen
drawSC = false;
glutDisplayFunc(Splash);
break;
case 'h':
case 'H':
//Help screen
drawSC = false;
glutDisplayFunc(Help);
break;
case 'g':
case 'G':
//Game screen
drawSC = false;
//Level Bug fixed here
level = 0;
Leveler();
glutSetWindowTitle("Pretty Pixel");
glutDisplayFunc(Pretty);
break;
case 'f':
case 'F':
//Full screen toggle
glutFullScreenToggle();
break;
case 'c':
case 'C':
//Custom game screen
drawSC = true;
ind = 0;
glutReshapeWindow(800, 800);
glutSetWindowTitle("Pretty Draw");
glutDisplayFunc(CustomDraw);
break;
case 'e':
case 'E':
//Want some epilepsy?
epilepsyFlag ? epilepsyFlag = false : epilepsyFlag = true;
break;
case 't':
case 'T':
hard ? hard = false : hard = true;
break;
case 'q':
case 'Q':
//Quit game
exit(0);
break;
}
}
/**************************************************************************/
//Main
int main(int argc, char **argv) {
cout << "Hello World! Pretty Pixel\n";
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH) - screenLength) / 2,
(glutGet(GLUT_SCREEN_HEIGHT) - screenLength) / 2);
glutInitWindowSize(screenLength, screenLength);
glutCreateWindow("Pretty Pixel");
glutFullScreen();
glEnable(GL_DEPTH_TEST);
glutReshapeFunc(Reshape);
glutMouseFunc(MouseClick);
glutMotionFunc(MouseClickMove);
glutPassiveMotionFunc(MouseMove);
glutKeyboardFunc(Keys);
glutDisplayFunc(Splash);
glClearColor(0.08f, 0.08f, 0.08f, 1.0);
glutMainLoop();
return 0;
}