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