[3647] | 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 | |
---|
[3688] | 18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GARBAGE_COLLECTOR |
---|
[3647] | 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" |
---|
[4289] | 27 | #include "object_manager.h" |
---|
[3647] | 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; |
---|
[3685] | 42 | this->delay = 1.5f; /* clean up all 5.0 seconds */ |
---|
[3647] | 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 | |
---|
[3664] | 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 | */ |
---|
[3647] | 76 | void GarbageCollector::setCollectionDelay(float delay) |
---|
[3664] | 77 | { |
---|
| 78 | this->delay = delay; |
---|
| 79 | } |
---|
[3647] | 80 | |
---|
| 81 | |
---|
[3664] | 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 | */ |
---|
[3647] | 88 | void GarbageCollector::forceCollection() |
---|
| 89 | { |
---|
[3664] | 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(); |
---|
[3647] | 94 | } |
---|
| 95 | |
---|
[3664] | 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 | */ |
---|
[3647] | 103 | void GarbageCollector::tick(float time) |
---|
| 104 | { |
---|
| 105 | this->time += time; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | |
---|
[3664] | 109 | /** |
---|
| 110 | \brief this updated the gargabe collection, if the time is ready |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | */ |
---|
[3647] | 114 | void GarbageCollector::update() |
---|
| 115 | { |
---|
| 116 | if( this->time < this->delay) |
---|
| 117 | return; |
---|
| 118 | /* garbage collect */ |
---|
[3669] | 119 | PRINTF(3)("=============================\n"); |
---|
| 120 | PRINTF(3)("Processing Garbage Collection\n"); |
---|
| 121 | PRINTF(3)("=============================\n"); |
---|
| 122 | int counter = 0; |
---|
[3647] | 123 | WorldInterface* wi = WorldInterface::getInstance(); |
---|
| 124 | tList<WorldEntity>* list = wi->getEntityList(); |
---|
[3661] | 125 | |
---|
| 126 | tIterator<WorldEntity>* iterator = list->getIterator(); |
---|
| 127 | WorldEntity* entity = iterator->nextElement(); |
---|
| 128 | while( entity != NULL) |
---|
| 129 | { |
---|
| 130 | if( entity->isFinalized()) |
---|
| 131 | { |
---|
[3669] | 132 | PRINTF(4)("= finalizing object\n"); |
---|
| 133 | ++counter; |
---|
[3662] | 134 | |
---|
[3647] | 135 | /* first remove out of entity list */ |
---|
[3662] | 136 | list->remove(entity); |
---|
[3647] | 137 | /* second remove out of pnode tree */ |
---|
[3669] | 138 | entity->remove(); |
---|
[3670] | 139 | /* then finaly delete reference */ |
---|
[4289] | 140 | //delete entity; |
---|
| 141 | ObjectManager::getInstance()->addToDeadList(CL_TEST_BULLET, entity); |
---|
| 142 | ObjectManager::getInstance()->debug(); |
---|
[3661] | 143 | } |
---|
| 144 | entity = iterator->nextElement(); |
---|
| 145 | } |
---|
[3670] | 146 | |
---|
[3669] | 147 | PRINTF(3)("= collected %i unused objects\n", counter); |
---|
| 148 | PRINTF(3)("=============================\n"); |
---|
| 149 | |
---|
[3647] | 150 | /* reset time to count again up to this->delay */ |
---|
| 151 | this->time = 0; |
---|
| 152 | } |
---|