- Titi, fixed the remove object so its done in the Object destructor (using an interface)

This commit is contained in:
Mark Vejvoda 2011-02-15 04:34:19 +00:00
parent 02979a7430
commit 2357e2cfd1
6 changed files with 24 additions and 9 deletions

View File

@ -90,6 +90,8 @@ Game::Game(Program *program, const GameSettings *gameSettings):
speed= sNormal; speed= sNormal;
showFullConsole= false; showFullConsole= false;
Object::setStateCallback(&gui);
Logger &logger= Logger::getInstance(); Logger &logger= Logger::getInstance();
logger.showProgress(); logger.showProgress();
@ -99,6 +101,7 @@ Game::Game(Program *program, const GameSettings *gameSettings):
Game::~Game() { Game::~Game() {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
Object::setStateCallback(NULL);
thisGamePtr = NULL; thisGamePtr = NULL;
if(originalDisplayMsgCallback != NULL) { if(originalDisplayMsgCallback != NULL) {
NetworkInterface::setDisplayMessageFunction(originalDisplayMsgCallback); NetworkInterface::setDisplayMessageFunction(originalDisplayMsgCallback);

View File

@ -993,7 +993,7 @@ bool Gui::computeTarget(const Vec2i &screenPos, Vec2i &targetPos, const Unit *&t
} }
} }
void Gui::removeObject(Object* o){ void Gui::removingObjectEvent(Object* o){
if(getSelectedResourceObject()==o){ if(getSelectedResourceObject()==o){
selectedResourceObject=NULL; selectedResourceObject=NULL;
} }

View File

@ -92,7 +92,7 @@ public:
/// In game GUI /// In game GUI
// ===================================================== // =====================================================
class Gui{ class Gui : public ObjectStateInterface {
public: public:
static const int maxSelBuff= 128*5; static const int maxSelBuff= 128*5;
static const int upgradeDisplayIndex= 8; static const int upgradeDisplayIndex= 8;
@ -143,6 +143,9 @@ public:
void init(Game *game); void init(Game *game);
void end(); void end();
// callback when tileset objects are removed from the world
virtual void removingObjectEvent(Object* o) ;
//get //get
Vec2i getPosObjWorld() const {return posObjWorld;} Vec2i getPosObjWorld() const {return posObjWorld;}
const UnitType *getBuilding() const; const UnitType *getBuilding() const;
@ -151,7 +154,6 @@ public:
const Display *getDisplay() const {return &display;} const Display *getDisplay() const {return &display;}
const Selection *getSelection() const {return &selection;} const Selection *getSelection() const {return &selection;}
const Object *getSelectedResourceObject() const {return selectedResourceObject;} const Object *getSelectedResourceObject() const {return selectedResourceObject;}
void removeObject(Object* o) ;
const SelectionQuad *getSelectionQuad() const {return &selectionQuad;} const SelectionQuad *getSelectionQuad() const {return &selectionQuad;}
CardinalDir getSelectedFacing() const {return selectedBuildingFacing;} CardinalDir getSelectedFacing() const {return selectedBuildingFacing;}

View File

@ -26,6 +26,7 @@ using namespace Shared::Util;
namespace Glest{ namespace Game{ namespace Glest{ namespace Game{
ObjectStateInterface *Object::stateCallback=NULL;
// ===================================================== // =====================================================
// class Object // class Object
@ -49,7 +50,10 @@ Object::Object(ObjectType *objectType, const Vec3f &pos, const Vec2i &mapPos) {
Object::~Object(){ Object::~Object(){
Renderer &renderer= Renderer::getInstance(); Renderer &renderer= Renderer::getInstance();
renderer.removeObjectFromQuadCache(this); renderer.removeObjectFromQuadCache(this);
//renderer.getGame()->getGui()->removeObject(this); if(stateCallback) {
stateCallback->removingObjectEvent(this);
//renderer.getGame()->getGui()->removeObject(this);
}
delete resource; delete resource;
} }

View File

@ -31,6 +31,13 @@ using Shared::Graphics::Vec3f;
/// A map object: tree, stone... /// A map object: tree, stone...
// ===================================================== // =====================================================
class Object;
class ObjectStateInterface {
public:
virtual void removingObjectEvent(Object *object) = 0;
};
class Object { class Object {
private: private:
ObjectType *objectType; ObjectType *objectType;
@ -41,10 +48,14 @@ private:
int lastRenderFrame; int lastRenderFrame;
Vec2i mapPos; Vec2i mapPos;
static ObjectStateInterface *stateCallback;
public: public:
Object(ObjectType *objectType, const Vec3f &pos, const Vec2i &mapPos); Object(ObjectType *objectType, const Vec3f &pos, const Vec2i &mapPos);
~Object(); ~Object();
static void setStateCallback(ObjectStateInterface *value) { stateCallback=value; }
void setHeight(float height) {pos.y= height;} void setHeight(float height) {pos.y= height;}
const ObjectType *getType() const {return objectType;} const ObjectType *getType() const {return objectType;}

View File

@ -1028,11 +1028,6 @@ void UnitUpdater::updateHarvest(Unit *unit) {
//if resource exausted, then delete it and stop //if resource exausted, then delete it and stop
if (r->decAmount(1)) { if (r->decAmount(1)) {
const ResourceType *rt = r->getType(); const ResourceType *rt = r->getType();
//TODO:
// the following line should be done in Object::~Object(), but I don't know how (titi)!
this->game->getGui()->removeObject(sc->getObject());
sc->deleteResource(); sc->deleteResource();
unit->getFaction()->removeResourceTargetFromCache(unitTargetPos); unit->getFaction()->removeResourceTargetFromCache(unitTargetPos);