From 958184e018f0068dd6c55fe36b1c130d67062052 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 7 Sep 2010 21:01:22 +0000 Subject: [PATCH] - more memory cleanup --- source/glest_game/ai/path_finder.cpp | 49 +++++++++++++------ source/glest_game/ai/path_finder.h | 12 ++++- source/glest_game/game/game.cpp | 4 +- source/glest_game/type_instances/unit.cpp | 2 +- source/glest_game/type_instances/unit.h | 8 +-- source/glest_game/types/resource_type.cpp | 12 ++++- source/glest_game/types/resource_type.h | 1 + source/glest_game/types/tech_tree.cpp | 7 ++- source/glest_game/types/unit_type.cpp | 1 + source/shared_lib/include/graphics/vec.h | 42 ++++++++++++++++ .../shared_lib/sources/graphics/particle.cpp | 3 +- source/shared_lib/sources/util/conversion.cpp | 8 +-- 12 files changed, 118 insertions(+), 31 deletions(-) diff --git a/source/glest_game/ai/path_finder.cpp b/source/glest_game/ai/path_finder.cpp index 2a7ca499..0abc390e 100644 --- a/source/glest_game/ai/path_finder.cpp +++ b/source/glest_game/ai/path_finder.cpp @@ -40,30 +40,41 @@ const int PathFinder::pathFindRefresh= 10; PathFinder::PathFinder(){ - nodePool= NULL; + //nodePool= NULL; + nodePool.clear(); + map=NULL; } PathFinder::PathFinder(const Map *map){ - nodePool= NULL; + //nodePool= NULL; + nodePool.clear(); + map=NULL; init(map); } void PathFinder::init(const Map *map){ - if(nodePool != NULL) { - delete [] nodePool; - nodePool = NULL; - } - nodePool= new Node[pathFindNodesMax]; + //if(nodePool != NULL) { + // delete [] nodePool; + // nodePool = NULL; + //} + //nodePool= new Node[pathFindNodesMax]; + nodePool.resize(pathFindNodesMax); + this->map= map; } PathFinder::~PathFinder(){ - delete [] nodePool; - nodePool = NULL; + //delete [] nodePool; + //nodePool = NULL; + nodePool.clear(); + map=NULL; } TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos){ + if(map == NULL) { + throw runtime_error("map == NULL"); + } //route cache UnitPathInterface *path= unit->getPath(); if(finalPos==unit->getPos()) { @@ -148,6 +159,10 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos){ Chrono chrono; chrono.start(); + if(map == NULL) { + throw runtime_error("map == NULL"); + } + nodePoolCount= 0; const Vec2i finalPos= computeNearestFreePos(unit, targetPos); @@ -163,8 +178,9 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos){ assert(firstNode!=NULL);; firstNode->next= NULL; firstNode->prev= NULL; - firstNode->pos= unit->getPos(); - firstNode->heuristic= heuristic(unit->getPos(), finalPos); + const Vec2i unitPos = unit->getPos(); + firstNode->pos= unitPos; + firstNode->heuristic= heuristic(unitPos, finalPos); firstNode->exploredCell= true; openNodes.push_back(firstNode); @@ -188,7 +204,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos){ node= *it; //b3) if minHeuristic is the finalNode, or the path is no more explored => path was found - if(node->pos==finalPos || !node->exploredCell){ + if(node->pos == finalPos || node->exploredCell == false) { pathFound= true; break; } @@ -200,7 +216,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos){ for(int i=-1; i<=1 && !nodeLimitReached; ++i){ for(int j=-1; j<=1 && !nodeLimitReached; ++j){ Vec2i sucPos= node->pos + Vec2i(i, j); - if(!openPos(sucPos) && map->aproxCanMove(unit, node->pos, sucPos)){ + if(openPos(sucPos) == false && map->aproxCanMove(unit, node->pos, sucPos)){ //if node is not open and canMove then generate another node Node *sucNode= newNode(); if(sucNode!=NULL){ @@ -257,7 +273,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos){ currNode= firstNode; for(int i=0; currNode->next!=NULL && inext, i++){ - path->push(currNode->next->pos); + path->add(currNode->next->pos); } } @@ -271,7 +287,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos){ } PathFinder::Node *PathFinder::newNode(){ - if(nodePoolCountgetPos(); diff --git a/source/glest_game/ai/path_finder.h b/source/glest_game/ai/path_finder.h index 501844f2..532341a3 100644 --- a/source/glest_game/ai/path_finder.h +++ b/source/glest_game/ai/path_finder.h @@ -34,7 +34,14 @@ class Unit; class PathFinder { public: - struct Node{ + class Node { + public: + Node() : pos(0,0) { + next=NULL; + prev=NULL; + heuristic=0.0; + exploredCell=false; + } Vec2i pos; Node *next; Node *prev; @@ -51,7 +58,8 @@ public: private: Nodes openNodes; Nodes closedNodes; - Node *nodePool; + //Node *nodePool; + std::vector nodePool; int nodePoolCount; const Map *map; diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index e1f253bf..d7815716 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -51,6 +51,8 @@ Game::Game(Program *program, const GameSettings *gameSettings): // GameConstants::cameraFps= 50; GameConstants::updateFps= 40; GameConstants::cameraFps= 100; + captureAvgTestStatus = false; + lastRenderLog2d = 0; quitTriggeredIndicator = false; originalDisplayMsgCallback = NULL; @@ -1502,7 +1504,7 @@ void Game::render2d(){ perfLogging = true; } - string str; + string str=""; std::map factionDebugInfo; if( renderer.getShowDebugUI() == true || diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 8a5c18b6..e8d7e3f4 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -59,7 +59,7 @@ void UnitPathBasic::incBlockCount() { blockCount++; } -void UnitPathBasic::push(const Vec2i &path){ +void UnitPathBasic::add(const Vec2i &path){ pathQueue.push_back(path); } diff --git a/source/glest_game/type_instances/unit.h b/source/glest_game/type_instances/unit.h index 454eb2a8..efa94a0b 100644 --- a/source/glest_game/type_instances/unit.h +++ b/source/glest_game/type_instances/unit.h @@ -107,7 +107,7 @@ public: virtual void clear() = 0; virtual void clearBlockCount() = 0; virtual void incBlockCount() = 0; - virtual void push(const Vec2i &path) = 0; + virtual void add(const Vec2i &path) = 0; //virtual Vec2i pop() = 0; virtual std::string toString() const = 0; @@ -129,7 +129,7 @@ public: virtual void clear(); virtual void clearBlockCount() { blockCount = 0; } virtual void incBlockCount(); - virtual void push(const Vec2i &path); + virtual void add(const Vec2i &path); Vec2i pop(); virtual std::string toString() const; @@ -156,9 +156,9 @@ public: virtual void clear() {list::clear(); blockCount = 0;} /**< clear the path */ virtual void clearBlockCount() { blockCount = 0; } virtual void incBlockCount() {++blockCount;} /**< increment block counter */ - virtual void push(const Vec2i &pos) {push_front(pos);} /**< push onto front of path */ + virtual void push(Vec2i &pos) {push_front(pos);} /**< push onto front of path */ bool empty() const {return list::empty();} /**< is path empty */ - void push(Vec2i &pos) {push_front(pos);} /**< push onto front of path */ + virtual void add(const Vec2i &pos) { push_front(pos);} /**< push onto front of path */ #if 0 diff --git a/source/glest_game/types/resource_type.cpp b/source/glest_game/types/resource_type.cpp index 539165e5..996fb7f0 100644 --- a/source/glest_game/types/resource_type.cpp +++ b/source/glest_game/types/resource_type.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2008 Martiņo Figueroa +// Copyright (C) 2001-2008 Martio Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published @@ -28,6 +28,16 @@ namespace Glest{ namespace Game{ // class ResourceType // ===================================================== +ResourceType::ResourceType() { + resourceClass=rcTech; + tilesetObject=0; + resourceNumber=0; + interval=0; + defResPerPatch=0; + recoup_cost = false; + model = NULL; +} + void ResourceType::load(const string &dir, Checksum* checksum){ string path, str; diff --git a/source/glest_game/types/resource_type.h b/source/glest_game/types/resource_type.h index 26687f95..7168c195 100644 --- a/source/glest_game/types/resource_type.h +++ b/source/glest_game/types/resource_type.h @@ -47,6 +47,7 @@ private: Model *model; public: + ResourceType(); void load(const string &dir, Checksum* checksum); //get diff --git a/source/glest_game/types/tech_tree.cpp b/source/glest_game/types/tech_tree.cpp index 497653fc..65ac1533 100644 --- a/source/glest_game/types/tech_tree.cpp +++ b/source/glest_game/types/tech_tree.cpp @@ -195,10 +195,13 @@ const FactionType *TechTree::getType(const string &name) const{ } const ResourceType *TechTree::getTechResourceType(int i) const{ - for(int j=0; jgetResourceNumber()==i && rt->getClass()==rcTech) + if(rt == NULL) { + throw runtime_error("rt == NULL"); + } + if(rt->getResourceNumber() == i && rt->getClass() == rcTech) return getResourceType(j); } diff --git a/source/glest_game/types/unit_type.cpp b/source/glest_game/types/unit_type.cpp index 7167ca84..11a66f2f 100644 --- a/source/glest_game/types/unit_type.cpp +++ b/source/glest_game/types/unit_type.cpp @@ -57,6 +57,7 @@ UnitType::UnitType(){ light= false; multiSelect= false; armorType= NULL; + rotatedBuildPos=0; for(int i=0; ix= v.x; this->y= v.y; } + template + explicit Vec2(Vec2 &v){ + this->x= v.x; + this->y= v.y; + } Vec2(T x, T y){ this->x= x; @@ -66,6 +72,12 @@ public: return reinterpret_cast(this); } + Vec2 & operator=(const Vec2 &v) { + this->x= v.x; + this->y= v.y; + return *this; + } + bool operator ==(const Vec2 &v) const{ return x==v.x && y==v.y; } @@ -201,6 +213,13 @@ public: this->z= v.z; } + template + explicit Vec3(Vec3 &v){ + this->x= v.x; + this->y= v.y; + this->z= v.z; + } + Vec3(T x, T y, T z){ this->x= x; this->y= y; @@ -221,6 +240,13 @@ public: return reinterpret_cast(this); } + Vec3 & operator=(const Vec3 &v) { + this->x= v.x; + this->y= v.y; + this->z= v.z; + return *this; + } + bool operator ==(const Vec3 &v) const{ return x==v.x && y==v.y && z==v.z; } @@ -387,6 +413,14 @@ public: this->w= v.w; } + template + explicit Vec4(Vec4 &v){ + this->x= v.x; + this->y= v.y; + this->z= v.z; + this->w= v.w; + } + Vec4(T x, T y, T z, T w){ this->x= x; this->y= y; @@ -416,6 +450,14 @@ public: return reinterpret_cast(this); } + Vec4 & operator=(const Vec4 &v) { + this->x= v.x; + this->y= v.y; + this->z= v.z; + this->w= v.w; + return *this; + } + bool operator ==(const Vec4 &v) const{ return x==v.x && y==v.y && z==v.z && w==v.w; } diff --git a/source/shared_lib/sources/graphics/particle.cpp b/source/shared_lib/sources/graphics/particle.cpp index c828ad8f..1842df61 100644 --- a/source/shared_lib/sources/graphics/particle.cpp +++ b/source/shared_lib/sources/graphics/particle.cpp @@ -369,6 +369,7 @@ UnitParticleSystem::UnitParticleSystem(int particleCount): ParticleSystem(partic fixed=false; rotation=0.0f; relativeDirection=true; + relative=false; cRotation= Vec3f(1.0f,1.0f,1.0f); fixedAddition = Vec3f(0.0f,0.0f,0.0f); @@ -437,7 +438,7 @@ void UnitParticleSystem::initParticle(Particle *p, int particleIndex){ p->speed= p->speed * speed; p->accel= Vec3f(0.0f, -gravity, 0.0f); - if(!relative){ + if(relative == false) { p->pos= Vec3f(pos.x+x+offset.x, pos.y+random.randRange(-radius/2, radius/2)+offset.y, pos.z+y+offset.z); } else diff --git a/source/shared_lib/sources/util/conversion.cpp b/source/shared_lib/sources/util/conversion.cpp index c1e5d5be..e50affc5 100644 --- a/source/shared_lib/sources/util/conversion.cpp +++ b/source/shared_lib/sources/util/conversion.cpp @@ -105,25 +105,25 @@ string boolToStr(bool b){ } string intToStr(int i){ - char str[strSize]; + char str[strSize]=""; sprintf(str, "%d", i); return str; } string intToHex(int i){ - char str[strSize]; + char str[strSize]=""; sprintf(str, "%x", i); return str; } string floatToStr(float f,int precsion){ - char str[strSize]; + char str[strSize]=""; sprintf(str, "%.*f", precsion,f); return str; } string doubleToStr(double d,int precsion){ - char str[strSize]; + char str[strSize]=""; sprintf(str, "%.*f", precsion,d); return str; }