/
main.cpp
187 lines (171 loc) · 4.28 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
#include "glut_headers.h"
#include <iostream>
#include "Game.h"
#include "Text.h"
#include "Pantalla.h"
#include <mmsystem.h>
float mat_ambient_diffuse [] = {0.0,0.0,0.0,1.0};
float mat_specular [] = {0.0,0.0,0.0,1.0};
float mat_emission [] = {0.0,0.0,0.0,1.0};
float mat_shininess = 0.4;
float focus_emission [] = {0.0,0.0,0.0,1.0};
float light_ambient [] = {0.0,0.2,0.0,1.0};
float light_diffuse_specular [] = {0.6,0.2,0.067,0.0};
float light_pos [] = {0.0,0.0,2.0,1.0};
float spot_dir [] = {0.0,0.0,-1.0};
float spot_cutoff = 50.0;
float spot_exponent = 5.0;
int rot_x[2] = {300,1.0}; //rot_x
int rot_y[2] = {0,0}; //rot_y
int rot_z[2] = {0,0}; //rot_z
int width = 1000;
int height = 800;
boolean start_screen = true;
int timer = 0;
Game* game;
Game* game2;
Pantalla* inicio;
void reshape(int x, int y)
{
width = x;
height = y;
glViewport(0, 0, width, height);
game->setDimensions(width/2, height);
game2->setDimensions(width/2, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.5,28.5, 31.5, -1.5, -512, 512);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
void menu(int option)
{
switch (option)
{
case 1:
if(!game->hasEnded()) {
game->pause();
game2->pause();
}
break;
case 2:
game2 = new Game();
game = new Game();
break;
case 0:
exit(0);
break;
}
}
void init()
{
glEnable(GL_DEPTH_TEST);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_FALSE);
glEnable(GL_LIGHTING);
GLfloat globalAmbient[ ] = { 1.00, 1.00, 1.00, 0.0 };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient);
glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE, mat_ambient_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialf(GL_FRONT,GL_SHININESS,mat_shininess);
glClearColor(0.0,0.0,0.0,0.0);
glutCreateMenu(menu);
glutAddMenuEntry("pause/unpause (p)", 1);
glutAddMenuEntry("restart game (r)", 2);
glutAddMenuEntry("quit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void initObjects()
{
game = new Game();
game->setKeys(1000+97, 1000+119, 1000+100, 1000+115);
game2 = new Game();
game2->setKeys(100, 101, 102,103);
inicio = new Pantalla();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(start_screen) {
reshape(width, height);
inicio->draw();
} else if((game->isPlaying() && game2->isPlaying) && (!game->hasEnded() || !game2->hasEnded())) {
glPushMatrix();
game->setPoint(0, 0);
game->draw();
glPopMatrix();
glPushMatrix();
game2->setPoint(width/2, 0);
game2->draw();
glPopMatrix();
} else if(!game->isPlaying() || game2->isPlaying()) {
reshape(width, height);
Text::drawText("Juego en pausa, presione \"p\" para resumir o \"r\" para reiniciar");
} else if(game->hasEnded() && game2->hasEnded()) {
reshape(width, height);
glPushMatrix();
Text::drawText("Fin del juego, presione \"r\" para reiniciar");
glTranslatef(0, 1, 0);
if(game->score > game2->score) {
Text::drawText("Gane el jugador 1 con puntuación de:");
glTranslatef(0, 1, 0);
Text::drawInt(game->score);
} else {
Text::drawText("Gane el jugador 2 con puntuación de:");
glTranslatef(0, 1, 0);
Text::drawInt(game2->score);
}
glPopMatrix();
}
glutSwapBuffers();
}
void special(int key, int x, int y){
if(key == 1114)
{
initObjects();
reshape(width, height);
} else if(key == 1105) {
game->playSound();
start_screen = false;
}
game->keyListener(key, x, y);
game2->keyListener(key, x, y);
glutPostRedisplay();
}
void key(unsigned char key, int x, int y)
{
// Lets try all keys as equals
// +1000 normal keys.
special(key+1000, x, y);
}
static void idle(void)
{
glutPostRedisplay();
}
void myTimer(int valor)
{
if(game->isPlaying() && !game->hasEnded()) {
game->update();
game2->update();
valor = game->speed;
}
glutPostRedisplay();
glutTimerFunc(valor,myTimer,valor);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(width,height);
glutInitWindowPosition(100,50);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Pacman");
initObjects();
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutIdleFunc(idle);
glutTimerFunc(350,myTimer,350);
glutMainLoop();
return EXIT_SUCCESS;
}