-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapview.cpp
executable file
·385 lines (322 loc) · 11.2 KB
/
mapview.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
#include "main.hpp"
#include <cmath>
struct { float r,g,b; } hotkeyColors[10] = {
{ 1.0, 0.6, 0.6 },
{ 0.6, 0.6, 1.0 },
{ 0.7, 1.0, 0.7 },
{ 0.5, 1.0, 1.0 },
{ 1.0, 1.0, 0.4 },
{ 1.0, 0.6, 1.0 },
{ 0.1, 1.0, 0.1 },
{ 1.0, 0.7, 0.1 },
{ 0.5, 1.0, 0.0 },
{ 0.7, 0.7, 0.0 },
};
Image mapviewSelectedScrapyard("selection.png"),
mapviewFriendlyScrapyard("mapview_scrapyard_friendly.png"),
mapviewNeutralScrapyard("mapview_scrapyard_neutral.png"),
mapviewHostileScrapyard("mapview_scrapyard_hostile.png");
// mapviewRallyPoint("explode1.png");
void ScreenView::drawMapView()
{
typedef std::set<Model::Unit*> UnitList;
UnitList units;
const float pixel = 0.125;
unsigned short **fog = model->getFog(playerId);
int left = mapCameraX;
int top = mapCameraY;
int right = left+(screenWidth/mapViewTileSize)+1;
int bottom = top+(screenHeight/mapViewTileSize)+1;
if(left<0) left=0;
if(top<0) top=0;
if(right >= (int)model->getSizeX()) right = model->getSizeX()-1;
if(bottom >= (int)model->getSizeY()) bottom = model->getSizeY()-1;
glPushMatrix();
glScalef(mapViewTileSize, mapViewTileSize, 1.0);
glTranslatef(-(int)mapCameraX, -(int)mapCameraY, 0);
// Draw map tiles at less-than-full brightness, so that the map will
// fade into the background and make unit symbols &c easier to see.
// Draw all tiles top-to-bottom, taking note of relevant units along the way.
Image prevImage;
glBegin(GL_QUADS);
for(int yi=top; yi<=bottom; yi++)
for(int xi=left; xi<=right; xi++)
{
Model::Tile *tile = model->getTile(xi, yi);
if(fog[yi][xi] > 0)
{
glColor3ub(170,170,170);
Model::Unit *u = model->getUnitAt(xi, yi, true);
if(u) units.insert(u);
u = model->getUnitAt(xi, yi, false);
if(u) units.insert(u);
} else {
glColor3ub(100,100,100);
}
//drawImage(xi, yi, 1.0, 1.0, tile->image());
Image texture = tile->image();
if(!(texture == prevImage)) {
glEnd();
texture.bind();
glBegin(GL_QUADS);
}
glTexCoord2f(0.0, 0.0); glVertex2f(xi, yi );
glTexCoord2f(1.0, 0.0); glVertex2f(xi+1.0f, yi );
glTexCoord2f(1.0, 1.0); glVertex2f(xi+1.0f, yi+1.0f);
glTexCoord2f(0.0, 1.0); glVertex2f(xi, yi+1.0f);
}
glEnd();
// Draw scrapyards
std::vector<Model::Scrapyard*> vec;
model->getScrapyardList(vec);
for(unsigned ii=0; ii<vec.size(); ii++) {
const Model::Scrapyard *s = vec[ii];
int topLeftX = s->center.x - s->size.x/2;
int topLeftY = s->center.y - s->size.y/2;
if(ii==selectedScrapyard) {
glColor3ub(255,255,255);
drawImage(topLeftX, topLeftY, s->size.x, s->size.y, mapviewSelectedScrapyard);
/*int rallyX = s->rally.x - 2;
int rallyY = s->rally.y - 2;
drawImage(rallyX,rallyY,4,4, mapviewRallyPoint);*/
} else if(s->owner == playerId) {
glColor3ub(130,130,130);
drawImage(topLeftX, topLeftY, s->size.x, s->size.y, mapviewFriendlyScrapyard);
} else if(s->owner == 0) {
glColor3ub(130,130,130);
drawImage(topLeftX, topLeftY, s->size.x, s->size.y, mapviewNeutralScrapyard);
} else {
glColor3ub(130,130,130);
drawImage(topLeftX, topLeftY, s->size.x, s->size.y, mapviewHostileScrapyard);
}
if(s->owner==playerId && !(s->rally == s->center)) {
glColor3f(0.0, 0.7, 0.0);
glLineWidth(1.0);
glEnable(GL_LINE_SMOOTH);
glBindTexture(GL_TEXTURE_2D, 0);
float vertexX = s->rally.x - s->center.x,
vertexY = s->rally.y - s->center.y,
vertexLen = sqrt(vertexX*vertexX + vertexY*vertexY);
float perpendicularX = -vertexY/vertexLen,
perpendicularY = vertexX/vertexLen;
glBegin(GL_LINES);
glVertex2f(s->center.x, s->center.y);
glVertex2f(s->rally.x, s->rally.y);
glVertex2f(s->rally.x, s->rally.y);
glVertex2f(s->rally.x-vertexX/vertexLen+perpendicularX, s->rally.y-vertexY/vertexLen+perpendicularY);
glVertex2f(s->rally.x, s->rally.y);
glVertex2f(s->rally.x-vertexX/vertexLen-perpendicularX, s->rally.y-vertexY/vertexLen-perpendicularY);
glEnd();
glDisable(GL_LINE_SMOOTH);
}
}
// Draw unit selection boxes
glLineWidth(1.0);
glColor3f(selectionBoxRed, selectionBoxGreen, selectionBoxBlue);
glBindTexture(GL_TEXTURE_2D, 0);
glBegin(GL_LINES);
for(UnitList::iterator ii=units.begin(); ii!=units.end(); ii++)
{
Model::Unit *u = *ii;
Model::Unit *border;
float x = u->getX(),
y = u->getY();
if(selection.find(u->getId())==selection.end())
continue;
x = pixel*std::floor(x*8.0); // Round (x,y) to the nearest pixel
y = pixel*std::floor(y*8.0);
// The goal here is to draw a bounding shape around groups of selected
// units; that is, something like
// +----+
// |UUUU|
// |U|U++
// +-+-+
// where the line is one pixel outside the unit's boundaries.
//
// For moving units, this is too complex to deal with; un that case,
// simply draw a border all around every unit.
//
// For stationary units, look at each of the unit's borders. If there
// is no unit on that border, that unit is moving, or that unit is
// not selected, a line needs to be drawn on that border.
int left, right, top, bottom;
if(u->moving)
{
left = right = top = bottom = 1;
} else
{
border = model->getUnitAt((unsigned)x, (unsigned)(y-0.5), u->type->flying);
top = !border || border->moving || selection.find(border->getId())==selection.end();
border = model->getUnitAt((unsigned)(x+1.5), (unsigned)y, u->type->flying);
right = !border || border->moving || selection.find(border->getId())==selection.end();
border = model->getUnitAt((unsigned)x, (unsigned)(y+1.5), u->type->flying);
bottom = !border || border->moving || selection.find(border->getId())==selection.end();
border = model->getUnitAt((unsigned)(x-0.5), (unsigned)y, u->type->flying);
left = !border || border->moving || selection.find(border->getId())==selection.end();
}
// Corner pixels complicate things. If the rule was 'a corner pixel is
// drawn if either of the adjoining edges is drawn' (as would happen if
// we simply handled each of the four edges independently), then this
// would happen:
// --------
// |UUUuuu|
// |UUUuuu|
// |UUU|uu|
// |uu-----
// |uuu|
// |uuu|
// -----
// The correct answer is to include a corner pixel iff _both_ the
// adjoining edges are drawn. And to do this, the most efficient
// solution turns out to be to just permute every possible
// combination of edges to draw, and write separate code for each.
// (switch is O(1), so there is no performance penalty for this.)
int mode = (top) | (right<<1) | (bottom<<2) | (left<<3);
switch(mode)
{
case 0: // none
break;
case 1: // top
glVertex2f(x, y); //top
glVertex2f(x+1.0, y);
break;
case 2: // right
glVertex2f(x+1.0, y ); //right
glVertex2f(x+1.0, y+1.0);
break;
case 3: // right, top
glVertex2f(x+1.0, y ); //right
glVertex2f(x+1.0, y+1.0);
glVertex2f(x, y); //top
glVertex2f(x+1.0+pixel, y);
break;
case 4: // bottom
glVertex2f(x+1.0, y+1.0+pixel); //bottom
glVertex2f(x, y+1.0+pixel);
break;
case 5: // bottom, top
glVertex2f(x, y); //top
glVertex2f(x+1.0, y);
glVertex2f(x+1.0, y+1.0+pixel); //bottom
glVertex2f(x, y+1.0+pixel);
break;
case 6: // bottom, right
glVertex2f(x+1.0+pixel, y+1.0+pixel); //bottom
glVertex2f(x, y+1.0+pixel);
glVertex2f(x+1.0, y ); //right
glVertex2f(x+1.0, y+1.0);
break;
case 7: // bottom, right, top
glVertex2f(x+1.0+pixel, y+1.0+pixel); //bottom
glVertex2f(x, y+1.0+pixel);
glVertex2f(x+1.0, y-pixel); //right
glVertex2f(x+1.0, y+1.0);
glVertex2f(x, y); //top
glVertex2f(x+1.0+pixel, y);
break;
case 8: // left
glVertex2f(x-pixel, y ); //left
glVertex2f(x-pixel, y+1.0);
break;
case 9: // left, top
glVertex2f(x-pixel, y ); //left
glVertex2f(x-pixel, y+1.0);
glVertex2f(x-pixel, y); //top
glVertex2f(x+1.0, y);
break;
case 10: // left, right
glVertex2f(x-pixel, y ); //left
glVertex2f(x-pixel, y+1.0);
glVertex2f(x+1.0, y); //right
glVertex2f(x+1.0, y+1.0);
break;
case 11: // left, right, top
glVertex2f(x-pixel, y ); //left
glVertex2f(x-pixel, y+1.0);
glVertex2f(x+1.0, y-pixel); //right
glVertex2f(x+1.0, y+1.0);
glVertex2f(x-pixel, y); //top
glVertex2f(x+1.0+pixel, y);
break;
case 12: // left, bottom
glVertex2f(x-pixel, y ); //left
glVertex2f(x-pixel, y+1.0);
glVertex2f(x+1.0, y+1.0+pixel); //bottom
glVertex2f(x-pixel, y+1.0+pixel);
break;
case 13: // left, bottom, top
glVertex2f(x-pixel, y ); //left
glVertex2f(x-pixel, y+1.0);
glVertex2f(x+1.0, y+1.0+pixel); //bottom
glVertex2f(x-pixel, y+1.0+pixel);
glVertex2f(x-pixel, y); //top
glVertex2f(x+1.0, y);
break;
case 14: // left, bottom, right
glVertex2f(x-pixel, y ); //left
glVertex2f(x-pixel, y+1.0);
glVertex2f(x+1.0+pixel, y+1.0+pixel); //bottom
glVertex2f(x-pixel, y+1.0+pixel);
glVertex2f(x+1.0, y); //right
glVertex2f(x+1.0, y+1.0);
break;
case 15: // left, bottom, right, top
glVertex2f(x-pixel, y ); //left
glVertex2f(x-pixel, y+1.0);
glVertex2f(x+1.0+pixel, y+1.0+pixel); //bottom
glVertex2f(x-pixel, y+1.0+pixel);
glVertex2f(x+1.0, y-pixel); //right
glVertex2f(x+1.0, y+1.0);
glVertex2f(x-pixel, y); //top
glVertex2f(x+1.0+pixel, y);
break;
}
}
glEnd();
bool blinkOff = ((int)(getTime()*3.0) % 2) ? true : false;
// Draw units
for(UnitList::iterator ii=units.begin(); ii!=units.end(); ii++)
{
Model::Unit *u = *ii;
float x = u->getX(),
y = u->getY();
x = pixel*std::floor(x*8.0); // Round (x,y) to the nearest pixel
y = pixel*std::floor(y*8.0);
if(showUnitStatus) {
float fraction = (float)u->hp / u->type->maxHP;
if(fraction > 0.5)
glColor3f((1.0-fraction)*2, 1.0, 0.0);
else
glColor3f(1.0, fraction*2, 0.0);
}
else if(u->owner != playerId)
glColor3f(1.0, 0.2, 0.2);
else {
glColor3f(1.0, 1.0, 1.0);
// Check hotkeys
for(unsigned ii=0; ii<10; ii++) {
if(hotkeys[ii].find(u->getId()) != hotkeys[ii].end()) {
glColor3f(hotkeyColors[ii].r, hotkeyColors[ii].g, hotkeyColors[ii].b);
break;
}
}
// Check fuel
if(u->fuel == 0 && u->type->usesFuel && blinkOff)
glColor3f(0.0, 0.0, 0.0);
}
drawImage(x, y, 1.0, 1.0, u->mapViewImage());
}
// Draw a selection box
if(dragging) {
glColor3f(selectionBoxRed, selectionBoxGreen, selectionBoxBlue);
glBindTexture(GL_TEXTURE_2D, 0);
glLineWidth(2.0);
glBegin(GL_LINE_LOOP);
glVertex2f(mouseX, mouseY);
glVertex2f(dragStartX, mouseY);
glVertex2f(dragStartX, dragStartY);
glVertex2f(mouseX, dragStartY);
glEnd();
}
glPopMatrix();
}