-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovingBlocks.cpp
More file actions
485 lines (446 loc) · 16.4 KB
/
Copy pathmovingBlocks.cpp
File metadata and controls
485 lines (446 loc) · 16.4 KB
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
/*
* File: movingBlocks.cpp
* Author: Philipp Jährling
*
* Created on 9. Januar 2012, 00:05
*/
//Standartbibliotheken
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
//Eigene
#include "helper.hpp"
#include "Block.hpp"
#include "Field.hpp"
#include "Texture.hpp"
//C-Code
extern "C" {
#include <GL/glut.h>
#include <GL/gl.h>
}
using namespace std ;
/*******************************************************************************
********************************** VARIABLEN ***********************************
*******************************************************************************/
// Fenster
int wnd_width = 1024; //800;
int wnd_height = 768; //600;
// Szene
float rotXStart = 40.0;
float rotYStart = -90.0;
float rotX = 0; //rotXStart;
float rotY = 0; //rotYStart;
float rotStep = 2.0;
bool camMod = false;
bool menuMod = true;
bool done = false;
int focusIndex = 1;
int menuIndex = 1;
int level = 1;
GLfloat light_position1[] = { 1.0, 1.0, 0.0, 0.0 };
//GLfloat light_position2[] = { 0.0, 1.0, 1.0, 0.0 }; //von links im Spiel
GLfloat light_position2[] = { -1.0, 1.0, 0.0, 0.0 };
GLfloat light_position3[] = { 0.0, 0.0, 1.0, 0.0 };
char* fnBigBlock = (char*)"blockBigObject.obj";
char* fnSmallBlock = (char*)"blockSmallObject.obj";
char* fnBox = (char*)"boxObject.obj";
char* fnSkyboxTex[] = { (char*)"texture/brightday/skyboxRight.rgb"
, (char*)"texture/brightday/skyboxFront.rgb"
, (char*)"texture/brightday/skyboxLeft.rgb"
, (char*)"texture/brightday/skyboxBack.rgb"
, (char*)"texture/brightday/skyboxTop.rgb"
, (char*)"texture/brightday/skyboxBottom.rgb" };
//Texturen
Texture* texWood = NULL;
Texture* texWood2 = NULL;
Texture* texWood3 = NULL;
Texture* texGreyStone = NULL;
Texture* texRedStone = NULL;
Texture* texOcherStone = NULL;
Texture* texMenu = NULL;
Texture* texLevelDone = NULL;
Texture* texSkyBox[6];
//Logik
Field* field = NULL;
//Anzeige-Objekte
Drawable* box = NULL;
Drawable* menu = NULL;
Drawable* levelDone = NULL;
map<int, Block*> mBlockObjects;
/*******************************************************************************
*************************** ANIMATIONS-FUNKTIONEN ******************************
*******************************************************************************/
void TurnCamBack(int value) {
if (rotY != rotYStart) {
rotY = (rotY < rotYStart) ? (rotY + rotStep) : (rotY - rotStep);
glutPostRedisplay();
glutTimerFunc(10, TurnCamBack, 0);
} else {
if (rotX != rotXStart) {
rotX = (rotX < rotXStart) ? (rotX + rotStep) : (rotX - rotStep);
glutPostRedisplay();
glutTimerFunc(10, TurnCamBack, 0);
}
}
}
void AnimateMenu(int value) {
float rotEnd = menuIndex * 90.0;
if (rotEnd == 270 && rotX == 0) { rotX = 360; }
if (rotEnd == 0 && rotX == 270) { rotX = -90; }
//cout << "rotEnd: " << rotEnd << " / rotX: " << rotX << endl;
if (menuMod == true && rotX != rotEnd) {
if ( rotX < rotEnd ) {
//nach oben drehen
rotX = (rotX == 360) ? (0 + rotStep) : (rotX + rotStep);
} else {
rotX = (rotX == 0) ? (360 - rotStep) : (rotX - rotStep);
}
glutPostRedisplay();
glutTimerFunc(10, AnimateMenu, 0);
}
}
/*******************************************************************************
************************************** UTILS ***********************************
*******************************************************************************/
void generateBlockObjects() {
vector< vector<int> > vBlocksData;
vBlocksData = field->getElementList();
int index = 0;
int posHori = 0;
int posVert = 0;
int length = 0;
int turned = 0; //0 -> false / 1 -> true
bool focused = false;
for (int i = 0; i < vBlocksData.size(); i++) {
index = vBlocksData[i].at(0); //erster Wert = index
posHori = vBlocksData[i].at(1); //zweiter Wert = horizontales Feld
posVert = vBlocksData[i].at(2); //dritter Wert = vertikales Feld
length = vBlocksData[i].at(3); //vierter Wert = länge des Elements
turned = vBlocksData[i].at(4); //fünfter Wert = ist Stein gedreht*/
//Erzeugen
if (index == 1) {
mBlockObjects[index] = new Block(fnSmallBlock, texRedStone, texOcherStone, false, (bool)turned) ;
} else {
if (length == 2) {
mBlockObjects[index] = new Block(fnSmallBlock, texGreyStone, texOcherStone, false, (bool)turned) ;
} else {
mBlockObjects[index] = new Block(fnBigBlock, texGreyStone, texOcherStone, true, (bool)turned) ;
}
if (!focused) {
mBlockObjects[index]->setFocus(true);
focusIndex = index;
focused = true;
}
}
//mBlockObjects[index]->setShine(true, GL_LIGHT3, GL_LIGHT4);
//An richtige Position bringen
mBlockObjects[index]->setTopLeftCorner();
for (int h = 0; h < posHori; h++) {
mBlockObjects[index]->moveRight();
}
for (int v = 0; v < posVert; v++) {
mBlockObjects[index]->moveDown();
}
//testing
/*for (int j = 0; j < vBlocksData[i].size(); j++) { cout << vBlocksData[i][j] << endl; }*/
}
}
void InitLevel() {
//davor Level setzten, danach Redisplay
done = false;
field->loadLevel(level);
generateBlockObjects();
rotX = 0;
rotY = 0;
TurnCamBack(0);
camMod = false;
menuMod = false;
}
void initMenu(int val) {
done = false;
menuMod = true;
rotX = menuIndex * 90;
mBlockObjects.clear();
glutPostRedisplay();
}
/*******************************************************************************
***************************** ANZEIGE-FUNKTIONEN *******************************
*******************************************************************************/
void InitGL(void) {
glClearColor( 0.0, 0.0, 0.0, 0.0 ); // Hintergrundfarbe
glEnable( GL_NORMALIZE );
glEnable(GL_DEPTH_TEST);
initLight();
//Texturen
texWood = new Texture((char*)"texture/wood1.rgb", 1);
texWood2 = new Texture((char*)"texture/wood2.rgb", 1);
texWood3 = new Texture((char*)"texture/wood3.rgb", 1);
texGreyStone = new Texture((char*)"texture/stone_grey1.rgb", 2);
texOcherStone = new Texture((char*)"texture/stone_ocher1.rgb", 3);
texRedStone = new Texture((char*)"texture/stone_red1.rgb", 4);
texMenu = new Texture((char*)"texture/stone_menu.rgb", 5);
texLevelDone = new Texture((char*)"texture/stone_done.rgb", 6);
texWood->initTexture();
texWood2->initTexture();
texWood3->initTexture();
texGreyStone->initTexture();
texOcherStone->initTexture();
texRedStone->initTexture();
texMenu->initTexture();
texLevelDone->initTexture();
//Skybox
for (int i = 0; i < 6; i++) {
texSkyBox[i] = new Texture( fnSkyboxTex[i], (7 + i) );
texSkyBox[i]->initTexture();
}
//Feld
field = new Field();
//Objekte
box = new Drawable(fnBox, texWood);
menu = new Drawable(fnBigBlock, texMenu);
levelDone = new Drawable(fnBigBlock, texLevelDone);
initMenu(0);
}
void Resize(int width, int height) {
glViewport( 0, 0, (GLint)width, (GLint)height );
wnd_width = width;
wnd_height= height;
}
void Display(void) {
// Projektionsmatrix einstellen
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); // Puffer leeren
// Transformation der Ansicht
glFrustum(-1, 1, -1, 1, 4, 100);
// In ModelMode gehen
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
if (menuMod == true) {
//Skybox
skyboxShow(texSkyBox, 0, 0);
glPushMatrix();
glEnable(GL_LIGHTING); // --> Beleuchtung AN
glLightfv(GL_LIGHT0, GL_POSITION, light_position2);
glLightfv(GL_LIGHT1, GL_POSITION, light_position3);
menuShow(menu, rotX);
glPopMatrix();
// Menü
} else {
//Skybox
skyboxShow(texSkyBox, rotX, rotY);
//Spielfeld + Elemente
glPushMatrix();
glEnable(GL_LIGHTING); // --> Beleuchtung AN
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// Verschiebung in z-Richtung nach hinten (rauszoomen)
glTranslatef ( 0.0, 0.0, -20.0 );
glRotatef ( rotX, 1.0, 0.0, 0.0 ); // Rotation um die x-Achse
glRotatef ( rotY, 0.0, 1.0, 0.0 ); // Rotation um die y-Achse
glLightfv(GL_LIGHT0, GL_POSITION, light_position1);
glLightfv(GL_LIGHT1, GL_POSITION, light_position2);
//Objekte zeichen
box->draw();
for( map<int, Block*>::iterator it = mBlockObjects.begin(), itEnd = mBlockObjects.end(); it != itEnd; ++it) {
(*it).second->draw();
}
glPopMatrix();
if (done == true) {
glPushMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTranslatef ( 0.0, -1.0, -8.0 );
glRotatef ( rotX - rotXStart + 15.0, 1.0, 0.0, 0.0 ); // Rotation um die x-Achse
glRotatef ( rotY + rotYStart, 0.0, 1.0, 0.0 ); // Rotation um die y-Achse
glRotatef ( 5.0, 0.0, 1.0, 0.0 ); // Rotation um die y-Achse
levelDone->draw();
glPopMatrix();
}
}
// Anzeigen
glFlush();
}
/*******************************************************************************
********************************** STEUERUNG ***********************************
*******************************************************************************/
void menuAction () {
if (menuIndex > 0) {
level = menuIndex;
InitLevel();
glutPostRedisplay();
} else {
exit(0);
}
}
//PFEILTASTEN - Ansicht drehen, Menu-Auswahl oder Stein bewegen
void SpecialKeys(int key, int x, int y) {
switch (key) {
//PFEIL OBEN
case GLUT_KEY_UP:
if (menuMod == true) {
menuIndex = (menuIndex < 3) ? menuIndex+1 : 0;
//cout << "MenuIndex: " << menuIndex << endl;
AnimateMenu(0);
} else if (camMod == true) {
rotX += rotStep;
glutPostRedisplay();
} else {
if (field->moveUp(focusIndex) == 1) {
mBlockObjects[focusIndex]->moveUp();
glutPostRedisplay();
}
}
break;
//PFEIL UNTEN
case GLUT_KEY_DOWN:
if (menuMod == true) {
menuIndex = (menuIndex > 0) ? menuIndex-1 : 3;
//cout << "MenuIndex: " << menuIndex << endl;
AnimateMenu(0);
} else if (camMod == true) {
rotX -= rotStep;
glutPostRedisplay();
} else {
if (field->moveDown(focusIndex) == 1) {
mBlockObjects[focusIndex]->moveDown();
glutPostRedisplay();
}
}
break;
//PFEIL LINKS
case GLUT_KEY_LEFT:
if (camMod == true) {
rotY += rotStep;
glutPostRedisplay();
} else if (menuMod == false) {
if (field->moveLeft(focusIndex) == 1) {
mBlockObjects[focusIndex]->moveLeft();
glutPostRedisplay();
}
}
break;
//PFEIL RECHTS
case GLUT_KEY_RIGHT:
if (camMod == true) {
rotY -= rotStep;
glutPostRedisplay();
} else if (menuMod == false) {
int res = field->moveRight(focusIndex);
if (res == 1) {
mBlockObjects[focusIndex]->moveRight();
glutPostRedisplay();
} else if (res == 2) {
mBlockObjects[focusIndex]->moveRight();
done = true;
glutPostRedisplay();
glutTimerFunc(2500, initMenu, 0);
}
}
break;
//DEFAULT
default:
return;
}
//cout << "Rotation: X=" << rotX << " / " << "Y=" << rotY << endl ;
}
//NROMALE TASTEN - Stein auswählen, Menu-Punkt auswählen und Modus wechseln
void Keyboard(unsigned char key, int x, int y) {
int newIndex = 0;
switch ((int)key) {
case 101: // e --> Modus wechseln
if (menuMod == false) {
camMod = (camMod == true) ? false : true;
TurnCamBack(0);
}
break;
case 13: // Enter --> in Menu = Auswahl
if ( menuMod == true ) {
menuAction();
}
case 119: // w --> Fokus nach "oben" verschieben (-x)
newIndex = field->focusUp(focusIndex);
if (newIndex != 0) {
mBlockObjects[focusIndex]->setFocus(false);
mBlockObjects[newIndex]->setFocus(true);
focusIndex = newIndex;
glutPostRedisplay();
}
break;
case 97: // a --> Fokus nach "links" verschieben (+z)
newIndex = field->focusLeft(focusIndex);
if (newIndex != 0) {
mBlockObjects[focusIndex]->setFocus(false);
mBlockObjects[newIndex]->setFocus(true);
focusIndex = newIndex;
glutPostRedisplay();
}
break;
case 115: // s --> Fokus nach "unten" verschieben (+x)
newIndex = field->focusDown(focusIndex);
if (newIndex != 0) {
mBlockObjects[focusIndex]->setFocus(false);
mBlockObjects[newIndex]->setFocus(true);
focusIndex = newIndex;
glutPostRedisplay();
}
break;
case 100: // d --> Fokus nach "rechts" verschieben (-z)
newIndex = field->focusRight(focusIndex);
if (newIndex != 0) {
mBlockObjects[focusIndex]->setFocus(false);
mBlockObjects[newIndex]->setFocus(true);
focusIndex = newIndex;
glutPostRedisplay();
}
break;
case 113: // q --> zurück zum Menü
if (menuMod == false) {
initMenu(0);
}
break;
case 49: // q --> zurück zum Menü
if (menuMod == false) {
box->setTexture(texWood);
glutPostRedisplay();
}
break;
case 50: // q --> zurück zum Menü
if (menuMod == false) {
box->setTexture(texWood2);
glutPostRedisplay();
}
break;
case 51: // q --> zurück zum Menü
if (menuMod == false) {
box->setTexture(texWood3);
glutPostRedisplay();
}
break;
default:
break;
}
//cout << "Keyboard:" << (int)key << endl ;
}
/*******************************************************************************
************************************** MAIN ************************************
*******************************************************************************/
int main(int argc, char **argv) {
glutInit( &argc, argv ); // Initialsierung der GLUT-Bibliothek
glutInitDisplayMode( GLUT_SINGLE | // einfacher Puffer
//GLUT_DOUBLE | // doppelter Puffer
GLUT_DEPTH | // Tiefen Puffer vorhanden
GLUT_RGB ); // Farbpuffer mit Rot,Grün und Blau
glutInitWindowSize( wnd_width,wnd_height );
glutInitWindowPosition( 100,100 );
glutCreateWindow( "We call it Blocks" );
InitGL();
//Festlegen der Funktionen die GLUT verwenden soll
glutDisplayFunc( &Display ); // zeichnen des Bildes
glutReshapeFunc( &Resize ); // Größenänderungen des Fensters
glutKeyboardFunc( &Keyboard ); // Buchstaben etc.
glutSpecialFunc( &SpecialKeys ); // Pfeiltasten etc.
glutMainLoop(); // Glut-Endlosschleife
return 0;
}