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
|
#include "MainWindow.h"
#include "types.h"
#include <iostream>
//#include <string>
#include <iostream>
#define WINDOW_TIMER 15
#define WALL_SIZE 0.5f
void f_onChangeSize(int w, int h) {
MainWindow::getInstance().onChangeSize(w, h);
}
void f_onDisplay() {
MainWindow::getInstance().onRenderScene();
}
void f_onTimer(int v) {
MainWindow::getInstance().onTimer();
}
void f_onNormalKeyPressed(unsigned char key, int x, int y)
{
MainWindow::getInstance().onNormalKeyPressed(key);
}
void f_onSpecialKeyPressed(int key, int x, int y)
{
MainWindow::getInstance().onSpecialKeyPressed(key);
}
void hex2fColor(uint color, float &out_red, float &out_green, float &out_blue)
{
float colors[3];
colors[0] = (float) ((color & 0x0000FF) >> 0) / 256.f;
colors[1] = (float) ((color & 0x00FF00) >> 8) / 256.f;
colors[2] = (float) ((color & 0xFF0000) >> 16) / 256.f;
out_blue = colors[0];
out_green = colors[1];
out_red = colors[2];
}
void f_doNothing() {}
MainWindow &MainWindow::getInstance()
{
static MainWindow s_mainWindow; // calling it static will create it if it's the first time and retrieve it otherwise.
return s_mainWindow;
}
void MainWindow::drawString(GLfloat x, GLfloat y, char *text, uint color, TextAlign align)
{
char *p;
float red, green, blue;
hex2fColor(color, red, green, blue);
glLineWidth(3.0);
glPushMatrix();
glTranslatef(x, y, 0);
glScalef(0.001, 0.001, 0.00);
if (align)
glTranslatef( ((float) align / 2) * glutStrokeLength(GLUT_STROKE_MONO_ROMAN, (const unsigned char *) text), 0, 0);
glColor3f(red, green, blue);
for (p = text; *p; p++)
glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, *p);
glPopMatrix();
}
void MainWindow::onChangeSize(int w, int h)
{
if (h == 0)
h = 1;
float ratio = w * 1.0f / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45.0f, ratio, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
m_width = w;
m_height = h;
//gluOrtho2D(0, 0, m_width, m_height);
}
void MainWindow::onRenderScene() {
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(m_pField)
{
Coordinates maxC = m_pField->getMaxCoordinates();
float GridStepX = 2*(1.f/(maxC.x+2*WALL_SIZE));
float GridStepY = 2*(1.f/(maxC.y+2*WALL_SIZE));
PlayerColor color;
float red, green, blue;
color = 0xFFFFFF;
hex2fColor(color, red, green, blue);
glColor3f(red, green, blue);
glBegin(GL_POLYGON);
glVertex2f(-1, -1);
glVertex2f( 1, -1);
glVertex2f( 1, 1);
glVertex2f(-1, 1);
glEnd();
color = emptyColors[0];
hex2fColor(color, red, green, blue);
glColor3f(red, green, blue);
glBegin(GL_POLYGON);
glVertex2f(-(maxC.x/(maxC.x+2*WALL_SIZE)), -(maxC.x/(maxC.x+2*WALL_SIZE)));
glVertex2f( (maxC.x/(maxC.x+2*WALL_SIZE)), -(maxC.x/(maxC.x+2*WALL_SIZE)));
glVertex2f( (maxC.x/(maxC.x+2*WALL_SIZE)), (maxC.x/(maxC.x+2*WALL_SIZE)));
glVertex2f(-(maxC.x/(maxC.x+2*WALL_SIZE)), (maxC.x/(maxC.x+2*WALL_SIZE)));
glEnd();
int lineX = 0;
int lineY = 0;
color = emptyColors[1];
hex2fColor(color, red, green, blue);
glColor3f(red, green, blue);
glBegin(GL_LINES);
for (lineX = 0; lineX < maxC.x; lineX += 10)
{
glVertex2f(2 * ((float) lineX / (maxC.x + 2*WALL_SIZE)) - 1, -1);
glVertex2f(2 * ((float) lineX / (maxC.x + 2*WALL_SIZE)) - 1, 1);
}
for (lineY = 0; lineY < maxC.y; lineY += 10)
{
glVertex2f(-1, 2 * ((float) lineY / (maxC.y + 2*WALL_SIZE)) - 1);
glVertex2f( 1, 2 * ((float) lineY / (maxC.y + 2*WALL_SIZE)) - 1);
}
glEnd();
for(int coordX = 0; coordX < maxC.x; ++coordX)
{
for(int coordY = 0; coordY < maxC.y; ++coordY)
{
Cell c = m_pField->getCell(Coordinates(coordX, coordY));
if (c.getState() != EMPTY_CELL)
{
switch(c.getState())
{
case BOOM:
color = 0xFFFFFF;
hex2fColor(color, red, green, blue);
glColor3f(red, green, blue);
break;
case PLAYER_WALL:
color = playerColors[c.getPlayer()->getNumber() % numColors];
color = (c.getPlayer()->isAlive() ? playerColors[c.getPlayer()->getNumber() % numColors] : emptyColors[2]);
hex2fColor(color, red, green, blue);
glColor3f(red/2, green/2, blue/2);
break;
case PLAYER:
color = playerColors[c.getPlayer()->getNumber() % numColors];
hex2fColor(color, red, green, blue);
glColor3f(red, green, blue);
break;
}
glBegin(GL_POLYGON);
float floatCoordX = 2 * ((float) (coordX+WALL_SIZE) / (maxC.x + 2*WALL_SIZE)) - 1;
float floatCoordY = 2 * ((float) (coordY+WALL_SIZE) / (maxC.y + 2*WALL_SIZE)) - 1;
glVertex2f(floatCoordX , floatCoordY );
glVertex2f(floatCoordX + GridStepX, floatCoordY );
glVertex2f(floatCoordX + GridStepX, floatCoordY + GridStepY);
glVertex2f(floatCoordX , floatCoordY + GridStepY);
glEnd();
}
}
}
if (! m_pGame->isRunning())
drawString(0, -0.02, (char*)"PAUSED", 0xffffff, center);
}
glutSwapBuffers();
}
void MainWindow::onTimer()
{
if(m_pGame)
{
int time = glutGet(GLUT_ELAPSED_TIME);
m_pGame->updateGame(time);
onRenderScene();
}
glutTimerFunc(WINDOW_TIMER, f_onTimer, 0);
}
KeyboardController* MainWindow::generateNewKeyboardController(Player* pPlayer, KeyCode leftKey, KeyCode rightKey)
{
KeyboardController* pKeyboardController = new KeyboardController(pPlayer, leftKey, rightKey);
m_vpKeyboardController.push_back(pKeyboardController);
return pKeyboardController;
}
void MainWindow::onNormalKeyPressed(uint8 key)
{
if (key >= 'A' && key <= 'Z')
{
onKeyPressed((KeyCode) (key - 'A' + KEY_A));
}
else if (key >= 'a' && key <= 'z')
{
onKeyPressed((KeyCode) (key - 'a'+ KEY_A));
}
else if (key >= '0' && key <= '9')
{
onKeyPressed((KeyCode) (key - '0' + KEY_0));
}
else if (key == 27)
{
exit(0);
}
if(m_pGame)
{
if (key == 13)
{
m_pGame->restart();
}else if (key == 32)
{
m_pGame->toggleGame();
}
}
}
void MainWindow::onSpecialKeyPressed(int key)
{
switch(key)
{
case GLUT_KEY_UP:
onKeyPressed(KEY_UP);
break;
case GLUT_KEY_DOWN:
onKeyPressed(KEY_DOWN);
break;
case GLUT_KEY_LEFT:
onKeyPressed(KEY_LEFT);
break;
case GLUT_KEY_RIGHT:
onKeyPressed(KEY_RIGHT);
break;
}
}
void MainWindow::onKeyPressed(KeyCode key)
{
for(std::vector<KeyboardController*>::iterator it = m_vpKeyboardController.begin();
it != m_vpKeyboardController.end();
++it)
{
(*it)->PressKey(key);
}
}
void MainWindow::init(int *argcp, char **argv){
glutInit(argcp, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
}
void MainWindow::create(std::string name, int x, int y, int width, int height)
{
glutInitWindowPosition(x, y);
glutInitWindowSize(width, height);
m_width = width;
m_height = height;
if (m_width && m_height)
{
glutCreateWindow(name.c_str());
gluOrtho2D(0, 0,m_width, m_height);
}
}
void MainWindow::startLoop()
{
// register callbacks
#ifdef __APPLE__
glutDisplayFunc(f_onDisplay);
#else
//glutReshapeFunc(f_onChangeSize);
//glutDisplayFunc(f_doNothing);
#endif
glutTimerFunc(WINDOW_TIMER, f_onTimer, 0);
//glutIdleFunc(f_onDisplay);
// enter GLUT event processing cycle
glutKeyboardFunc(f_onNormalKeyPressed);
glutSpecialFunc(f_onSpecialKeyPressed);
glutMainLoop();
}
MainWindow::MainWindow():
m_width(0), m_height(0)
{
m_pField = NULL;
m_pGame = NULL;
m_vpKeyboardController.clear();
}
// The field has the info of what to draw
void MainWindow::setField(Field* pField) {
m_pField = pField;
}
Field* MainWindow::getField() {
return(m_pField);
}
// The game needs to be updated whenever we want to update the display content.
void MainWindow::setGame(Game* pGame) {
m_pGame = pGame;
}
Game* MainWindow::getGame() {
return(m_pGame);
}
MainWindow::~MainWindow(void)
{
}
|