2017-08-13 55 views
0

我正在圖形引擎上工作。該引擎有一個std::vector可繪製。 Drawable是一個對象,它包含一個Model和一個DrawableObject,該對象繼而保存來自2D或3D模型的着色器程序和一堆頂點。添加新的Drawables進展順利,當我嘗試刪除Drawable時會出現問題。最後一個Drawable將總是被刪除,倒數第二個將更改其值。錯誤的元素被刪除後,使用std :: erase成功的方法

代碼

Drawable.h

class Drawable 
{ 
public: 
    Drawable& operator=(const Drawable& other) 
    { 
     Drawable tmp(other); 
     std::swap(model, other.model); 
     std::swap(drawableObject, other.drawableObject); 
     return *this; 
    } 

    Drawable(domain::Model& model, DrawableObject& drawableObject) : 
     model(model), 
     drawableObject(drawableObject) 
    {} 

    domain::Model& model; 
    DrawableObject& drawableObject; 
}; 

game.cpp

void Game::init_game() 
{ 
    human = domain::Model(glm::vec3(0, 0, -3)); 
    moveables.push_back(&human); 
    room = domain::Model(glm::vec3(0, 0, -10)); 
    props.push_back(&room); 
    cube = domain::Model(glm::vec3(0, 0, 0)); 
    props.push_back(&cube); 
} 

void Game::init_graphics_engine() 
{ 
    // ... load graphics models 

    // add drawables 
    graphicsEngine->add(cube, Drawable::CUBE); 
    graphicsEngine->add(human, Drawable::HUMAN); 
    graphicsEngine->add(room, Drawable::ROOM); 
    graphicsEngine->add(topDownScene->cursor, Drawable::MARKER); 
} 

graphics_engine/engine.cpp

void Engine::add(domain::Model& model, unsigned int object) 
{ 
    auto drawableObject = drawableObjects[object]; 
    // make sure not to add a model that already is represented 
    auto it = std::find_if(drawables.begin(), drawables.end(), [&model](Drawable& drawable) {return &drawable.model == &model;}); 
    if(drawableObject && it == drawables.end()) 
     drawables.push_back(Drawable(model, *drawableObject)); 
} 

void Engine::remove(domain::Model& model) 
{ 
    auto predicate = [&model](Drawable& drawable) 
    { 
     return &drawable.model == &model; 
    }; 
    drawables.erase(std::remove_if(drawables.begin(), drawables.end(), predicate), drawables.end()); 
} 

場景

這是現場的樣子,當我啓動應用程序:

scene on startup

這是現場的樣子試圖刪除的小「人」的立方體後中間:

enter image description here

的代碼刪除最後繪製對象,這是白色標記,而不是「人」立方體,並改變了房間的z位置。這幾乎總是發生,它刪除最後一個元素並將第二個元素的z更改爲最後一個元素。它只有在init方法中最後添加'人類'多維數據集時纔有效。

斷點

刪除對象之前:

enter image description here

刪除對象後:

enter image description here

這是正確的。

離開remove方法,並且考慮看看在渲染循環:

enter image description here

不知何故改變。

+3

等一下,你的拷貝賦值操作符是否修改右邊?和'tmp'有什麼關係? – NPE

+0

@NPE [copy-and-swap idiom](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom)。 – LogicStuff

+0

@LogicStuff:這就是我猜測它的*意思是*,但不是如果你仔細閱讀代碼(除非我沒有看到某些東西,當然這也是一種可能性)。 – NPE

回答

0

我將類成員改爲指針。現在它可以工作。評論是正確的,我沒有對tmp變量做任何事情。

class Drawable 
{ 
public: 
    Drawable& operator=(const Drawable& other) 
    { 
     this->model = other.model; 
     this->drawableObject = other.drawableObject; 
     return *this; 
    } 

    Drawable(domain::Model* model, std::shared_ptr<DrawableObject> drawableObject) : 
     model(model), 
     drawableObject(drawableObject) 
    {} 

    domain::Model* model; 
    std::shared_ptr<DrawableObject> drawableObject; 
}; 
相關問題