forked from skeeto/pixelcity
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Entity.cpp
460 lines (347 loc) · 11.6 KB
/
Entity.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
/*-----------------------------------------------------------------------------
Entity.cpp
Copyright (c) 2005 Shamus Young
All Rights Reserved
-------------------------------------------------------------------------------
An entity is any renderable stationary object in the world. This is an
abstract class. This module gathers up the Entities, sorts them by
texture use and location, and then stores them in OpenGL render lists
for faster rendering.
-----------------------------------------------------------------------------*/
#ifdef WINDOWS
#include <windows.h>
#endif
#include <math.h>
#include <GL/gl.h>
#include <vector>
#include <algorithm>
#include "Camera.h"
#include "Entity.h"
#include "Macro.h"
#include "Math.h"
#include "Render.h"
#include "Texture.h"
#include "World.h"
#include "Visible.h"
#include "Win.h"
#include "time_util.h"
class entity
{
private:
class RefCounter {
public:
RefCounter(CEntity* o) : _obj(o), _refs(1) {};
~RefCounter() {delete _obj;};
void AddRef() {_refs++;};
void Release() {if (--_refs == 0) delete this;};
CEntity& operator*() const {return *_obj;};
CEntity* operator->() const {return _obj;};
operator bool() const {return (!!_obj);};
private:
CEntity* _obj;
int _refs;
// not callable
RefCounter() : _obj(NULL) {};
RefCounter(const RefCounter& rhs) : _obj(NULL) {};
RefCounter& operator=(const RefCounter& rhs) {return *this;};
};
public:
entity() {rc = NULL;};
entity(CEntity* o) : rc(new RefCounter(o)) {};
entity(const entity& rhs) : rc(rhs.rc) {if(rc) rc->AddRef();};
~entity() {rc->Release();}
entity& operator=(const entity& rhs) {
if(rc)
rc->Release();
rc = rhs.rc;
if(rc)
rc->AddRef();
return *this;
};
CEntity& operator*() { return **rc; };
RefCounter& operator->() { return *rc; };
bool operator<(const entity& rhs) const {
if(!rhs.rc)
return true;
if(!rc)
return false;
if ((*rc)->Alpha () && !(*rhs.rc)->Alpha ())
return true;
if (!(*rc)->Alpha () && (*rhs.rc)->Alpha ())
return false;
return (*rc)->Texture () < (*rhs.rc)->Texture ();
};
private:
RefCounter *rc;
};
struct cell
{
unsigned list_textured;
unsigned list_flat;
unsigned list_flat_wireframe;
unsigned list_alpha;
GLvector pos;
};
typedef std::vector<entity> ent_list_t;
static cell cell_list[GRID_SIZE][GRID_SIZE];
static ent_list_t entity_list;
static bool sorted;
static bool compiled;
static int polycount;
static int compile_x;
static int compile_y;
static int compile_count;
static int compile_end;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
/*static int do_compare (const void *arg1, const void *arg2 )
{
struct entity* e1 = (struct entity*)arg1;
struct entity* e2 = (struct entity*)arg2;
if (e1->object->Alpha () && !e2->object->Alpha ())
return 1;
if (!e1->object->Alpha () && e2->object->Alpha ())
return -1;
if (e1->object->Texture () > e2->object->Texture ())
return 1;
else if (e1->object->Texture () < e2->object->Texture ())
return -1;
return 0;
}*/
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void add (CEntity* b)
{
entity_list.push_back(entity(b));
polycount = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static void do_compile ()
{
ent_list_t::iterator i;
int x, y;
if (compiled)
return;
x = compile_x;
y = compile_y;
//Changing textures is pretty expensive, and thus sorting the entites so that
//they are grouped by texture used can really improve framerate.
//qsort (entity_list, entity_count, sizeof (struct entity), do_compare);
//sorted = true;
//Now group entites on the grid
//make a list for the textured objects in this region
if (!cell_list[x][y].list_textured)
cell_list[x][y].list_textured = glGenLists(1);
glNewList (cell_list[x][y].list_textured, GL_COMPILE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
for (i = entity_list.begin(); i < entity_list.end(); ++i) {
GLvector pos = (*i)->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && !(*i)->Alpha ()) {
glBindTexture(GL_TEXTURE_2D, (*i)->Texture ());
(*i)->Render ();
}
}
glEndList();
//Make a list of flat-color stuff (A/C units, ledges, roofs, etc.)
if (!cell_list[x][y].list_flat)
cell_list[x][y].list_flat = glGenLists(1);
glNewList (cell_list[x][y].list_flat, GL_COMPILE);
glEnable (GL_CULL_FACE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
for (i = entity_list.begin(); i < entity_list.end(); ++i) {
GLvector pos = (*i)->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && !(*i)->Alpha ()) {
(*i)->RenderFlat (false);
}
}
glEndList();
//Now a list of flat-colored stuff that will be wireframe friendly
if (!cell_list[x][y].list_flat_wireframe)
cell_list[x][y].list_flat_wireframe = glGenLists(1);
glNewList (cell_list[x][y].list_flat_wireframe, GL_COMPILE);
glEnable (GL_CULL_FACE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
for (i = entity_list.begin(); i < entity_list.end(); ++i) {
GLvector pos = (*i)->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && !(*i)->Alpha ()) {
(*i)->RenderFlat (true);
}
}
glEndList();
//Now a list of stuff to be alpha-blended, and thus rendered last
if (!cell_list[x][y].list_alpha)
cell_list[x][y].list_alpha = glGenLists(1);
glNewList (cell_list[x][y].list_alpha, GL_COMPILE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
glDepthMask (false);
glEnable (GL_BLEND);
glDisable (GL_CULL_FACE);
for (i = entity_list.begin(); i < entity_list.end(); ++i) {
GLvector pos = (*i)->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && (*i)->Alpha ()) {
glBindTexture(GL_TEXTURE_2D, (*i)->Texture ());
(*i)->Render ();
}
}
glDepthMask (true);
glEndList();
//now walk the grid
compile_x++;
if (compile_x == GRID_SIZE) {
compile_x = 0;
compile_y++;
if (compile_y == GRID_SIZE)
compiled = true;
compile_end = GetTimeInMillis ();
}
compile_count++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool EntityReady ()
{
return compiled;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float EntityProgress ()
{
return (float)compile_count / (GRID_SIZE * GRID_SIZE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityUpdate ()
{
int stop_time;
if (!TextureReady ()) {
sorted = false;
return;
}
if (!sorted) {
std::sort (entity_list.begin(), entity_list.end());
sorted = true;
}
//We want to do several cells at once. Enough to get things done, but
//not so many that the program is unresponsive.
if (LOADING_SCREEN) { //If we're using a loading screen, we want to build as fast as possible
stop_time = GetTimeInMillis () + 100;
while (!compiled && GetTimeInMillis () < stop_time)
do_compile ();
} else //Take it slow
do_compile ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityRender ()
{
int polymode[2];
bool wireframe;
int x, y;
int elapsed;
//Draw all textured objects
glGetIntegerv (GL_POLYGON_MODE, &polymode[0]);
wireframe = polymode[0] != GL_FILL;
if (RenderFlat ())
glDisable (GL_TEXTURE_2D);
//If we're not using a loading screen, make the wireframe fade out via fog
if (!LOADING_SCREEN && wireframe) {
elapsed = 6000 - WorldSceneElapsed ();
if (elapsed >= 0 && elapsed <= 6000)
RenderFogFX ((float)elapsed / 6000.0f);
else
return;
}
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
if (Visible (x,y))
glCallList (cell_list[x][y].list_textured);
}
}
//draw all flat colored objects
glBindTexture(GL_TEXTURE_2D, 0);
glColor3f (0, 0, 0);
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
if (Visible (x, y)) {
if (wireframe)
glCallList (cell_list[x][y].list_flat_wireframe);
else
glCallList (cell_list[x][y].list_flat);
}
}
}
//draw all alpha-blended objects
glBindTexture(GL_TEXTURE_2D, 0);
glColor3f (0, 0, 0);
glEnable (GL_BLEND);
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
if (Visible (x,y)) {
glCallList (cell_list[x][y].list_alpha);
}
}
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityClear ()
{
entity_list.clear();
compile_x = 0;
compile_y = 0;
compile_count = 0;
compiled = false;
sorted = false;
int x, y;
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
glNewList (cell_list[x][y].list_textured, GL_COMPILE);
glEndList();
glNewList (cell_list[x][y].list_alpha, GL_COMPILE);
glEndList();
glNewList (cell_list[x][y].list_flat_wireframe, GL_COMPILE);
glEndList();
glNewList (cell_list[x][y].list_flat, GL_COMPILE);
glEndList();
}
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int EntityCount ()
{
return entity_list.size();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityInit (void)
{
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int EntityPolyCount (void)
{
if (!sorted)
return 0;
if (polycount)
return polycount;
for (ent_list_t::iterator i = entity_list.begin(); i < entity_list.end(); ++i)
polycount += (*i)->PolyCount ();
return polycount;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CEntity::CEntity (void)
{
add (this);
}
void CEntity::Render (void)
{
}
void CEntity::RenderFlat (bool wireframe)
{
}
void CEntity::Update (void)
{
}