diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 414cbd4c..81048b88 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -385,6 +385,8 @@ Game::~Game() { world.end(); //must die before selection because of referencers + BaseColorPickEntity::resetUniqueColors(); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] aiInterfaces.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,aiInterfaces.size()); delete videoPlayer; @@ -1830,6 +1832,7 @@ void Game::update() { if(keepFactions == false) { world.end(); + world.cleanup(); world.clearTileset(); @@ -1840,6 +1843,7 @@ void Game::update() { aiInterfaces.clear(); gui.end(); //selection must be cleared before deleting units world.end(); //must die before selection because of referencers + BaseColorPickEntity::resetUniqueColors(); // MUST DO THIS LAST!!!! Because objects above have pointers to things like // unit particles and fade them out etc and this end method deletes the original // object pointers. @@ -1864,6 +1868,8 @@ void Game::update() { soundRenderer.stopAllSounds(fadeMusicMilliseconds); world.endScenario(); + BaseColorPickEntity::resetUniqueColors(); + Renderer &renderer= Renderer::getInstance(); renderer.endScenario(); world.clearTileset(); diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index f3bb7435..b3d8b92e 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -6879,104 +6879,6 @@ void Renderer::selectUsingColorPicking(Selection::UnitContainer &units, if(units.empty() == true && withObjectSelection == true) { colorPickObject(obj, posDown,posUp); } - -/* - int x1 = posDown.x; - int y1 = posDown.y; - int x2 = posUp.x; - int y2 = posUp.y; - - int x = min(x1,x2); - int y = min(y1,y2); - int w = max(x1,x2) - min(x1,x2); - int h = max(y1,y2) - min(y1,y2); - if(w < 1) { - w = 1; - } - if(h < 1) { - h = 1; - } - - const Metrics &metrics= Metrics::getInstance(); - x= (x * metrics.getScreenW() / metrics.getVirtualW()); - y= (y * metrics.getScreenH() / metrics.getVirtualH()); - - w= (w * metrics.getScreenW() / metrics.getVirtualW()); - h= (h * metrics.getScreenH() / metrics.getVirtualH()); - - PixelBufferWrapper::begin(); - - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - //GLint view[]= {0, 0, metrics.getVirtualW(), metrics.getVirtualH()}; - //gluPickMatrix(x, y, w, h, view); - gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane); - loadGameCameraMatrix(); - - //render units to find which ones should be selected - //printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - vector rendererUnits = renderUnitsFast(false, true); - //printf("In [%s::%s] Line: %d rendererUnits = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,rendererUnits.size()); - - vector rendererObjects; - if(withObjectSelection == true) { - rendererObjects = renderObjectsFast(false,true,true); - } - //pop matrices - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - - // Added this to ensure all the selection calls are done now - // (see http://www.unknownroad.com/rtfm/graphics/glselection.html section: [0x4]) - glFlush(); - - // uncomment this for debugging color picking to see what is colored - //GraphicsInterface::getInstance().getCurrentContext()->swapBuffers(); - - PixelBufferWrapper::end(); - - vector rendererModels; - if(rendererUnits.empty() == false) { - copy(rendererUnits.begin(), rendererUnits.end(), std::inserter(rendererModels, rendererModels.begin())); - } - if(rendererObjects.empty() == false) { - copy(rendererObjects.begin(), rendererObjects.end(), std::inserter(rendererModels, rendererModels.begin())); - } - - vector pickedList = BaseColorPickEntity::getPickedList(x,y,w,h, rendererModels); - //printf("In [%s::%s] Line: %d pickedList = %d models rendered = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,pickedList.size(),rendererModels.size()); - - //glPopAttrib(); - if(pickedList.empty() == false) { - units.reserve(pickedList.size()); - for(unsigned int i = 0; i < pickedList.size(); ++i) { - int index = pickedList[i]; - //printf("In [%s::%s] Line: %d searching for selected object i = %d index = %d units = %d objects = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,index,rendererUnits.size(),rendererObjects.size()); - - if(rendererObjects.empty() == false && index < rendererObjects.size()) { - Object *object = rendererObjects[index]; - //printf("In [%s::%s] Line: %d searching for selected object i = %d index = %d [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,index,object); - - if(object != NULL) { - obj = object; - if(withObjectSelection == true) { - //printf("In [%s::%s] Line: %d found selected object [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,obj); - return; - } - } - } - else { - index -= rendererObjects.size(); - Unit *unit = rendererUnits[index]; - if(unit != NULL && unit->isAlive()) { - units.push_back(unit); - } - } - } - } -*/ } void Renderer::colorPickUnits(Selection::UnitContainer &units, diff --git a/source/glest_game/gui/gui.cpp b/source/glest_game/gui/gui.cpp index 4d8d9d7a..fb7b69bf 100644 --- a/source/glest_game/gui/gui.cpp +++ b/source/glest_game/gui/gui.cpp @@ -437,8 +437,9 @@ void Gui::giveOneClickOrders(){ } void Gui::giveDefaultOrders(int x, int y) { - //compute target + //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + const Unit *targetUnit= NULL; Vec2i targetPos; if(computeTarget(Vec2i(x, y), targetPos, targetUnit) == false) { @@ -446,6 +447,7 @@ void Gui::giveDefaultOrders(int x, int y) { return; } giveDefaultOrders(targetPos.x,targetPos.y,targetUnit,true); + //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); } void Gui::givePreparedDefaultOrders(int x, int y){ @@ -457,7 +459,9 @@ void Gui::giveDefaultOrders(int x, int y,const Unit *targetUnit, bool paintMouse //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); bool queueKeyDown = isKeyDown(queueCommandKey); Vec2i targetPos=Vec2i(x, y); + //give order + //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); std::pair result= commander->tryGiveCommand(&selection, targetPos, targetUnit, queueKeyDown); //graphical result diff --git a/source/glest_game/type_instances/object.cpp b/source/glest_game/type_instances/object.cpp index 57188c2a..5e6952a6 100644 --- a/source/glest_game/type_instances/object.cpp +++ b/source/glest_game/type_instances/object.cpp @@ -72,6 +72,8 @@ Object::~Object() { } delete resource; resource = NULL; + + recycleUniqueColor(); } void Object::end() { diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index bc883e2f..6a1306cd 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -564,6 +564,8 @@ Unit::~Unit() { delete mutexCommands; mutexCommands=NULL; + recycleUniqueColor(); + #ifdef LEAK_CHECK_UNITS Unit::mapMemoryList.erase(this); #endif diff --git a/source/shared_lib/include/graphics/model.h b/source/shared_lib/include/graphics/model.h index 52019117..632902c2 100644 --- a/source/shared_lib/include/graphics/model.h +++ b/source/shared_lib/include/graphics/model.h @@ -252,6 +252,11 @@ public: virtual ~BaseColorPickEntity() {} static const int COLOR_COMPONENTS = 4; + + struct ColorPickStruct { + unsigned char color[COLOR_COMPONENTS]; + }; + static void init(int bufferSize); static void beginPicking(); static void endPicking(); @@ -263,10 +268,17 @@ public: string getColorDescription() const; virtual string getUniquePickName() const = 0; -private: - unsigned char uniqueColorID[COLOR_COMPONENTS]; + static void resetUniqueColors(); - static unsigned char nextColorID[COLOR_COMPONENTS]; +protected: + + void recycleUniqueColor(); + +private: + ColorPickStruct uniqueColorID; + + static ColorPickStruct nextColorID; + static vector nextColorIDReuseList; static Mutex mutexNextColorID; static auto_ptr pbo; diff --git a/source/shared_lib/sources/graphics/model.cpp b/source/shared_lib/sources/graphics/model.cpp index 848a98dc..77171cfb 100644 --- a/source/shared_lib/sources/graphics/model.cpp +++ b/source/shared_lib/sources/graphics/model.cpp @@ -1459,40 +1459,64 @@ PixelBufferWrapper::~PixelBufferWrapper() { } //unsigned char BaseColorPickEntity::nextColorID[COLOR_COMPONENTS] = {1, 1, 1, 1}; -unsigned char BaseColorPickEntity::nextColorID[COLOR_COMPONENTS] = { 1, 1, 1 }; +BaseColorPickEntity::ColorPickStruct BaseColorPickEntity::nextColorID = { 1, 1, 1 }; +vector BaseColorPickEntity::nextColorIDReuseList; Mutex BaseColorPickEntity::mutexNextColorID; auto_ptr BaseColorPickEntity::pbo; +void BaseColorPickEntity::recycleUniqueColor() { + MutexSafeWrapper safeMutex(&mutexNextColorID); + nextColorIDReuseList.push_back(uniqueColorID); +} + +void BaseColorPickEntity::resetUniqueColors() { + MutexSafeWrapper safeMutex(&mutexNextColorID); + nextColorID.color[0] = 1; + nextColorID.color[1] = 1; + nextColorID.color[2] = 1; + nextColorIDReuseList.clear(); +} + BaseColorPickEntity::BaseColorPickEntity() { MutexSafeWrapper safeMutex(&mutexNextColorID); - uniqueColorID[0] = nextColorID[0]; - uniqueColorID[1] = nextColorID[1]; - uniqueColorID[2] = nextColorID[2]; + // Reuse old colors + if(nextColorIDReuseList.empty() == false) { + uniqueColorID.color[0] = nextColorIDReuseList.back().color[0]; + uniqueColorID.color[1] = nextColorIDReuseList.back().color[1]; + uniqueColorID.color[2] = nextColorIDReuseList.back().color[2]; + nextColorIDReuseList.pop_back(); + + return; + } + + uniqueColorID.color[0] = nextColorID.color[0]; + uniqueColorID.color[1] = nextColorID.color[1]; + uniqueColorID.color[2] = nextColorID.color[2]; //uniqueColorID[3] = nextColorID[3]; - const int colorSpacing = 2; + const int colorSpacing = 8; - if(nextColorID[0] + colorSpacing <= 255) { - nextColorID[0] += colorSpacing; + if(nextColorID.color[0] + colorSpacing <= 255) { + nextColorID.color[0] += colorSpacing; } else { - nextColorID[0] = 1; - if(nextColorID[1] + colorSpacing <= 255) { - nextColorID[1] += colorSpacing; + nextColorID.color[0] = 1; + if(nextColorID.color[1] + colorSpacing <= 255) { + nextColorID.color[1] += colorSpacing; } else { - nextColorID[1] = 1; - if(nextColorID[2] + colorSpacing <= 255) { - nextColorID[2] += colorSpacing; + nextColorID.color[1] = 1; + if(nextColorID.color[2] + colorSpacing <= 255) { + nextColorID.color[2] += colorSpacing; } else { //printf("Color rolled over on 3rd level!\n"); - nextColorID[0] = 1; - nextColorID[1] = 1; - nextColorID[2] = 1; + nextColorID.color[0] = 1; + nextColorID.color[1] = 1; + nextColorID.color[2] = 1; // nextColorID[2] = 1; @@ -1517,7 +1541,7 @@ void BaseColorPickEntity::init(int bufferSize) { string BaseColorPickEntity::getColorDescription() const { char szBuf[100]=""; - snprintf(szBuf,100,"%d.%d.%d",uniqueColorID[0],uniqueColorID[1],uniqueColorID[2]); + snprintf(szBuf,100,"%d.%d.%d",uniqueColorID.color[0],uniqueColorID.color[1],uniqueColorID.color[2]); string result = szBuf; return result; } @@ -1669,9 +1693,9 @@ vector BaseColorPickEntity::getPickedList(int x,int y,int w,int h, bool BaseColorPickEntity::isUniquePickingColor(unsigned char *pixel) const { bool result = false; - if( uniqueColorID[0] == pixel[0] && - uniqueColorID[1] == pixel[1] && - uniqueColorID[2] == pixel[2]) { + if( uniqueColorID.color[0] == pixel[0] && + uniqueColorID.color[1] == pixel[1] && + uniqueColorID.color[2] == pixel[2]) { //uniqueColorID[3] == pixel[3]) { result = true; } @@ -1681,9 +1705,9 @@ bool BaseColorPickEntity::isUniquePickingColor(unsigned char *pixel) const { void BaseColorPickEntity::setUniquePickingColor() const { - glColor3ub(uniqueColorID[0], - uniqueColorID[1], - uniqueColorID[2]); + glColor3ub(uniqueColorID.color[0], + uniqueColorID.color[1], + uniqueColorID.color[2]); /* glColor3f( uniqueColorID[0] / 255.0f, uniqueColorID[1] / 255.0f,