00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include "System.h"
00013
#include <OpenCAL/Object.h>
00014
#include <OpenCAL/Actor.h>
00015
#include <OpenCAL/Agent.h>
00016
#include <OpenCAL/Renderer.h>
00017
#include <OpenCAL/Picker.h>
00018
#include <OpenCAL/ObjectIterator.h>
00019
using namespace OpenCAL;
00020
00021
#include <OpenCAL/NullParentException.h>
00022
using Exceptions::NullParentException;
00023
00024
using namespace std;
00025
00026
00027
00028
#define forAllObjects(it)\
00029
ObjectList::iterator it;\
00030
for(it = m_objects.begin(); it != m_objects.end(); ++it)
00031
00032
#define forAllActors(it)\
00033
ActorList::iterator it;\
00034
for(it = m_actors.begin(); it != m_actors.end(); ++it)
00035
00036
#define forAllAgents(it)\
00037
AgentList::iterator it;\
00038
for(it = m_agents.begin(); it != m_agents.end(); ++it)
00039
00040
#define forAllSystems(it)\
00041
SystemList::iterator it;\
00042
for(it = m_systems.begin(); it != m_systems.end(); ++it)
00043
00044
00045
00046
00047
00048
00049 System::System(
System *parent)
00050 : m_destructing(false), m_parent(parent), m_initialized(false),
00051 m_draw(true), m_enabled(true), m_accuracyHint(0.0f),
00052 m_selectedObject(0), m_renderer(0), m_picker(0)
00053 {
00054
#ifdef VERBOSE
00055
Debug::print(
"System constructor", 2);
00056
#endif // VERBOSE
00057
00058
00059
00060
00061
00062
00063
#ifdef VERBOSE
00064
Debug::print(
"Parent is given, adding to the the parent's system list...", 2);
00065
#endif // VERBOSE
00066
00067
if(parent)
00068 parent->
addSystem(
this);
00069 }
00070
00071 System::~System()
00072 {
00073
#ifdef VERBOSE
00074
Debug::print(
"System destructor", 2);
00075
00076 Debug::stream <<
"Cleaning up ";
00077 Debug::stream << m_objects.size() <<
" object(s), ";
00078 Debug::stream << m_actors.size() <<
" actor(s), ";
00079 Debug::stream << m_agents.size() <<
" agent(s) and ";
00080 Debug::stream << m_systems.size() <<
" subsystem(s)." << endl;
00081 Debug::printStream();
00082
#endif // VERBOSE
00083
00084
m_destructing =
true;
00085
00086 forAllSystems(system)
00087 delete *system;
00088
00089 forAllObjects(object)
00090 delete *object;
00091
00092 forAllActors(actor)
00093 delete *actor;
00094
00095 forAllAgents(agent)
00096 delete *agent;
00097
00098 if(m_parent)
00099 m_parent->deleteSystem(this);
00100 }
00101
00102
00103
00104
00105
00106
00107 void System::render()
00108 {
00109
if(!m_renderer)
00110 {
00111 cerr <<
"No renderer available!!" << endl;
00112
return;
00113 }
00114
00115
if(!isInitialized())
00116 {
00117
#ifdef VERBOSE
00118
Debug::initWarning(
"system");
00119
#endif // VERBOSE
00120
initialize();
00121 }
00122
00123 forAllSystems(system)
00124 {
00125
if((*system)->getDraw())
00126 {
00127 (*system)->setRenderer(m_renderer);
00128 (*system)->render();
00129 }
00130 }
00131
00132 forAllActors(actor)
00133 {
00134
if((*actor)->getDraw())
00135 (*actor)->render();
00136 }
00137
00138 forAllObjects(object)
00139 {
00140
if((*object)->getDraw())
00141 (*object)->render();
00142 }
00143 }
00144
00145
00146
00147
00148
00149
00150 void System::renderPicking()
00151 {
00152
unsigned int i;
00153
00154
00155
00156
if(!m_picker)
00157 {
00158 cerr <<
"No picker available!!" << endl;
00159
return;
00160 }
00161
00162
if(!isInitialized())
00163
initialize();
00164
00165
for(i = 0; i < m_systems.size(); ++i)
00166 {
00167
if(m_systems[i]->getDraw())
00168 {
00170 m_systems[i]->setPicker(m_picker);
00171
00172 m_picker->
pushName(i);
00173 m_systems[i]->renderPicking();
00174 m_picker->
popName();
00175 }
00176 }
00177
00178 m_picker->
pushName(0);
00179
for(i = 0; i < m_objects.size(); ++i)
00180 {
00181
if(m_objects[i]->getDraw())
00182 {
00183 m_picker->
loadName(i);
00184 m_objects[i]->renderPicking();
00185 }
00186 }
00187 m_picker->
popName();
00188 }
00189
00190
Object *System::pick(
int x,
int y)
00191 {
00192
00193
00194
if(!m_picker)
00195 {
00196 cerr <<
"No picker available!!" << endl;
00197
return 0;
00198 }
00199
00200
if(!isInitialized())
00201
initialize();
00202
00203
return m_picker->
pick(
this, x, y);
00204 }
00205
00206
00207
00208
00209
00210
00211 void System::initialize()
00212 {
00213
#ifdef VERBOSE
00214
Debug::print(
"System initialize", 2);
00215
#endif // VERBOSE
00216
00217
if(m_renderer)
00218 m_renderer->
initialize();
00219
00220 forAllSystems(system)
00221 {
00222 (*system)->setRenderer(m_renderer);
00223 (*system)->initialize();
00224 }
00225
00226 forAllObjects(object)
00227 (*object)->initialize();
00228
00229 forAllActors(actor)
00230 (*actor)->initialize();
00231
00232 forAllAgents(agent)
00233 (*agent)->initialize();
00234
00235 m_initialized =
true;
00236 }
00237
00238
void System::step(
float deltaSeconds)
00239 {
00240
#ifdef VERBOSE
00241
Debug::stream <<
"System step (" << deltaSeconds <<
" seconds)" << endl;
00242 Debug::printStream(Debug::maxLevel);
00243
#endif // VERBOSE
00244
00245
00246
00247
if(!isInitialized())
00248 {
00249
#ifdef VERBOSE
00250
Debug::initWarning(
"system");
00251
#endif // VERBOSE
00252
initialize();
00253 }
00254
00255 forAllActors(actor)
00256 {
00257
if((*actor)->isEnabled())
00258 (*actor)->execute(deltaSeconds);
00259 }
00260
00261 forAllSystems(system)
00262 {
00263
if((*system)->isEnabled())
00264 (*system)->step(deltaSeconds);
00265 }
00266
00267 forAllAgents(agent)
00268 {
00269
if((*agent)->isEnabled())
00270 (*agent)->satisfy();
00271 }
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 void System::deleteObject(
Object *object)
00448 {
00454
if(
m_destructing)
return;
00455
00456 forAllObjects(it)
00457
if(*it == object)
00458 {
00459 m_objects.erase(it);
00460
return;
00461 }
00462
00463
#ifdef VERBOSE
00464
Debug::print(
"Trying to delete an object that isn't in the list!");
00465
#endif // VERBOSE
00466
00468 }
00469
00470
00471 void System::deleteActor(
Actor *actor)
00472 {
00478
if(
m_destructing)
return;
00479
00480 forAllActors(it)
00481
if(*it == actor)
00482 {
00483 m_actors.erase(it);
00484
return;
00485 }
00486
00487
#ifdef VERBOSE
00488
Debug::print(
"Trying to delete an actor that isn't in the list!");
00489
#endif // VERBOSE
00490
00492 }
00493
00494 void System::deleteAgent(
Agent *agent)
00495 {
00501
if(
m_destructing)
return;
00502
00503 forAllAgents(it)
00504
if(*it == agent)
00505 {
00506 m_agents.erase(it);
00507
return;
00508 }
00509
00510
#ifdef VERBOSE
00511
Debug::print(
"Trying to delete an agent that isn't in the list!");
00512
#endif // VERBOSE
00513
00515 }
00516
00517 void System::deleteSystem(
System *system)
00518 {
00525
if(
m_destructing)
return;
00526
00527 forAllSystems(it)
00528
if(*it == system)
00529 {
00530 m_systems.erase(it);
00531
return;
00532 }
00533
00534
#ifdef VERBOSE
00535
Debug::print(
"Trying to delete a system that isn't in the list!");
00536
#endif // VERBOSE
00537
00539 }