1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Patrick Boenzli |
---|
15 | co-programmer: ... |
---|
16 | */ |
---|
17 | |
---|
18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GARBAGE_COLLECTOR |
---|
19 | |
---|
20 | #include "garbage_collector.h" |
---|
21 | |
---|
22 | #include "world.h" |
---|
23 | #include "world_entity.h" |
---|
24 | #include "null_parent.h" |
---|
25 | |
---|
26 | #include "list.h" |
---|
27 | #include "object_manager.h" |
---|
28 | |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | |
---|
32 | GarbageCollector* GarbageCollector::singletonRef = 0; |
---|
33 | |
---|
34 | /** |
---|
35 | \brief standard constructor |
---|
36 | \todo this constructor is not jet implemented - do it |
---|
37 | */ |
---|
38 | GarbageCollector::GarbageCollector () |
---|
39 | { |
---|
40 | this->setClassName ("GarbageCollection"); |
---|
41 | this->time = 0; |
---|
42 | this->delay = 1.5f; /* clean up all 5.0 seconds */ |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | /** |
---|
47 | \brief standard deconstructor |
---|
48 | |
---|
49 | */ |
---|
50 | GarbageCollector::~GarbageCollector () |
---|
51 | { |
---|
52 | // delete what has to be deleted here |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | \brief this returns the singleton reference this this class |
---|
58 | \returns singleton reference |
---|
59 | */ |
---|
60 | GarbageCollector* GarbageCollector::getInstance() |
---|
61 | { |
---|
62 | if( singletonRef == NULL) |
---|
63 | singletonRef = new GarbageCollector(); |
---|
64 | return singletonRef; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | /** |
---|
69 | \brief this sets the collection delay |
---|
70 | \param delay |
---|
71 | |
---|
72 | after this delay, the garbage collector starts its work and begins to collect unused object |
---|
73 | to delete them afterwards. only objects in the worldentity list from the world object are lookded |
---|
74 | at. |
---|
75 | */ |
---|
76 | void GarbageCollector::setCollectionDelay(float delay) |
---|
77 | { |
---|
78 | this->delay = delay; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /** |
---|
83 | \brief this foreces a garbage collection |
---|
84 | |
---|
85 | if this function is called, the gc tries to initiate the garbage collection routines. actually |
---|
86 | this should always work. |
---|
87 | */ |
---|
88 | void GarbageCollector::forceCollection() |
---|
89 | { |
---|
90 | /* just make the time slitely bigger than the delay */ |
---|
91 | this->time = this->delay + 1; |
---|
92 | /* and update, to get rid of the unused objects */ |
---|
93 | this->update(); |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | /** |
---|
98 | \brief this ticks the GarbageCollector to give it the time pulse |
---|
99 | \param the time passed since last tick |
---|
100 | |
---|
101 | like every other tick function eg. worldentity |
---|
102 | */ |
---|
103 | void GarbageCollector::tick(float time) |
---|
104 | { |
---|
105 | this->time += time; |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | /** |
---|
110 | \brief this updated the gargabe collection, if the time is ready |
---|
111 | |
---|
112 | |
---|
113 | */ |
---|
114 | void GarbageCollector::update() |
---|
115 | { |
---|
116 | if( this->time < this->delay) |
---|
117 | return; |
---|
118 | /* garbage collect */ |
---|
119 | PRINTF(3)("=============================\n"); |
---|
120 | PRINTF(3)("Processing Garbage Collection\n"); |
---|
121 | PRINTF(3)("=============================\n"); |
---|
122 | int counter = 0; |
---|
123 | WorldInterface* wi = WorldInterface::getInstance(); |
---|
124 | tList<WorldEntity>* list = wi->getEntityList(); |
---|
125 | |
---|
126 | tIterator<WorldEntity>* iterator = list->getIterator(); |
---|
127 | WorldEntity* entity = iterator->nextElement(); |
---|
128 | while( entity != NULL) |
---|
129 | { |
---|
130 | if( entity->isFinalized()) |
---|
131 | { |
---|
132 | PRINTF(4)("= finalizing object\n"); |
---|
133 | ++counter; |
---|
134 | |
---|
135 | /* first remove out of entity list */ |
---|
136 | list->remove(entity); |
---|
137 | /* second remove out of pnode tree */ |
---|
138 | entity->remove(); |
---|
139 | /* then finaly delete reference */ |
---|
140 | //delete entity; |
---|
141 | ObjectManager::getInstance()->addToDeadList(CL_TEST_BULLET, entity); |
---|
142 | ObjectManager::getInstance()->debug(); |
---|
143 | } |
---|
144 | entity = iterator->nextElement(); |
---|
145 | } |
---|
146 | |
---|
147 | PRINTF(3)("= collected %i unused objects\n", counter); |
---|
148 | PRINTF(3)("=============================\n"); |
---|
149 | |
---|
150 | /* reset time to count again up to this->delay */ |
---|
151 | this->time = 0; |
---|
152 | } |
---|