场景图错误

问题描述 投票:0回答:1

所以我有一个视觉工作室项目的场景图,我和一群人正在研究游戏概念。我对此很新,所以任何建议都非常感谢。

这是场景图的头文件:

#include "Category.hpp" //4
#include <SFML/System/NonCopyable.hpp> //5
#include <SFML/Graphics/Transformable.hpp> //6
#include <SFML/Graphics/Drawable.hpp> //7
#include <vector> //8
#include <set> //9
#include <memory> //10
#include <utility> //11

struct Command; //17
class CommandQueue; /18

class SceneNode : public sf::Transformable, public sf::Drawable, private sf::NonCopyable //20
{
public: //22
typedef std::unique_ptr<SceneNode> Ptr; //23
typedef std::pair<SceneNode*, SceneNode*> Pair; //24


public: //27
explicit                SceneNode(Category::Type category = Category::None); //28

void                    attachChild(Ptr child); //30
Ptr                     detachChild(const SceneNode& node); //31

void                    update(sf::Time dt, CommandQueue& commands); //33

sf::Vector2f            getWorldPosition() const; //35
sf::Transform           getWorldTransform() const; //36

void                    onCommand(const Command& command, sf::Time dt); //38
virtual unsigned int    getCategory() const; //39

void                    checkSceneCollision(SceneNode& sceneGraph, std::set<Pair>& collisionPairs); //41
void                    checkNodeCollision(SceneNode& node, std::set<Pair>& collisionPairs); //42
void                    removeWrecks(); //43
virtual sf::FloatRect   getBoundingRect() const; //44
virtual bool            isMarkedForRemoval() const; //45 
virtual bool            isDestroyed() const; //46


private: //49
virtual void            updateCurrent(sf::Time dt, CommandQueue& commands); //50
void                    updateChildren(sf::Time dt, CommandQueue& commands); //51

virtual void            draw(sf::RenderTarget& target, sf::RenderStates states) const; //53
virtual void            drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; //54
void                    drawChildren(sf::RenderTarget& target, sf::RenderStates states) const; //55
void                    drawBoundingRect(sf::RenderTarget& target, sf::RenderStates states) const; //56


private: //59
std::vector<Ptr>        mChildren; //60
SceneNode*              mParent; //61
Category::Type          mDefaultCategory; //62
};

bool collision(const SceneNode& lhs, const SceneNode& rhs); //65
float distance(const SceneNode& lhs, const SceneNode& rhs); //66

////.cpp文件

#include "Scene Graph.h" //1
#include <SFML/Graphics/RectangleShape.hpp> //2
#include <SFML/Graphics/RenderTarget.hpp> //3


#include <algorithm> //6
#include <cassert>  //7
#include <cmath>  //8

SceneNode::SceneNode(Category::Type category) //11
: mChildren() //12
, mParent(nullptr)  //13
, mDefaultCategory(category) //14
{ //15

} //17

void SceneNode::attachChild(Ptr child) //19
{
child->mParent = this; //21
mChildren.push_back(std::move(child)); //22
}

SceneNode::Ptr SceneNode::detachChild(const SceneNode& node) //25
{
auto found = std::find_if(mChildren.begin(), mChildren.end(), [&](Ptr& p) { return p.get() == &node; }); //27
assert(found != mChildren.end()); //28

Ptr result = std::move(*found); //30
result->mParent = nullptr; //31
mChildren.erase(found); //32
return result; //33
}

void SceneNode::updateCurrent(sf::Time dt, CommandQueue& commands) //36
{
updateCurrent(dt, commands); //38
updateChildren(dt, commands); //39
}

void SceneNode::updateCurrent(sf::Time, CommandQueue&) //42
{
//do nothing by default //44
}

void SceneNode::updateChildren(sf::Time dt, CommandQueue& commands) //47
{

for(Ptr& child : mChildren) //50
    child->update(dt, commands); //51
}

void SceneNode::draw(sf::RenderTarget& target, sf::RenderStates states) const //54
{
//apply transform of current node //56
states.transform *= getTransform(); /57

//Draw node and children with changed transform //59
drawCurrent(target, states); //60
drawChildren(target, states); //61


}

void SceneNode::drawCurrent(sf::RenderTarget&, sf::RenderStates) const //66
{
//do nothing by default //68
}

void SceneNode::drawChildren(sf::RenderTarget& target, sf::RenderStates states) const //71
{
for(const Ptr& child : mChildren) //73
    child->draw(target, states); //74
}

void SceneNode::drawBoundingRect(sf::RenderTarget& target, sf::RenderStates) const //77
{
sf::FloatRect rect = getBoundingRect(); //79
sf::RectangleShape shape; //80
shape.setPosition(sf::Vector2f(rect.left, rect.top)); //81
shape.setSize(sf::Vector2f(rect.width, rect.height)); //82
shape.setFillColor(sf::Color::Transparent); //83
shape.setOutlineColor(sf::Color::Green); //84
shape.setOutlineThickness(1.f); //85
target.draw(shape);  //86
}

sf::Vector2f SceneNode::getWorldPosition() const //89
{
return getWorldTransform() * sf::Vector2f(); //91
}

sf::Transform SceneNode::getWorldTransform() const //94
{
sf::Transform transform = sf::Transform::Identity; //96

for (const SceneNode* node = this; node != nullptr; node = node->mParent) //98
    transform = node->getTransform() * transform; //99

return transform; //101
}

void SceneNode::onCommand(const Command& command, sf::Time dt) //104
{
//command current node, if category matches //106
if (command.category & getCategory()) //107
    command.action(*this, dt); //108

//command children //110
for(Ptr& child : mChildren) //111
child->onCommand(command, dt); //112
}

unsigned int SceneNode::getCategory() const //115
{
return mDefaultCategory; //117
}

void SceneNode::checkSceneCollision(SceneNode& sceneGraph, std::set<Pair>& collisionPairs) //120
{
checkNodeCollision(sceneGraph, collisionPairs); //122

for(Ptr& child : sceneGraph.mChildren) //124
    checkSceneCollision(*child, collisionPairs); //125
}

void SceneNode::checkNodeCollision(SceneNode& node, std::set<Pair>& collisionPairs) //128
{
if (this != &node && collision(*this, node) && !isDestroyed() && !node.isDestroyed()) //130
    collisionPairs.insert(std::minmax(this, &node)); //131

for(Ptr& child : mChildren) //133
    child->checkNodeCollision(node, collisionPairs); //134
}

void SceneNode::removeWrecks() //137
{
// Remove all children which request so //139
auto wreckfieldBegin = std::remove_if(mChildren.begin(), mChildren.end(), std::mem_fn(&SceneNode::isMarkedForRemoval)); //140
mChildren.erase(wreckfieldBegin, mChildren.end()); //141

// Call function recursively for all remaining children //143
std::for_each(mChildren.begin(), mChildren.end(), std::mem_fn(&SceneNode::removeWrecks)); //144
}

sf::FloatRect SceneNode::getBoundingRect() const //147
{
return sf::FloatRect(); //149
}

bool SceneNode::isMarkedForRemoval() const //152
{
// By default, remove node if entity is destroyed //154
return isDestroyed(); //155
}

bool SceneNode::isDestroyed() const //158
{
// By default, scene node needn't be removed //160
return false; //161
}

bool collision(const SceneNode& lhs, const SceneNode& rhs) //164
{
return lhs.getBoundingRect().intersects(rhs.getBoundingRect()); //166
}

float distance(const SceneNode& lhs, const SceneNode& rhs) //169
{
return length(lhs.getWorldPosition() - rhs.getWorldPosition()); //171
}

我得到的错误列如下: 1)不允许不完整类型.cpp第107,108行 2)namespace std没有成员“mem_fn”.cpp第140,144行 3)未找到标识符“长度”.cpp第171行 4)使用未定义类型“命令”.cpp第108,109行

c++ sfml
1个回答
1
投票

看起来你没有为<functional>的定义包含std::mem_fn标头,虽然我在你的场景图头文件中看到了Command的前向声明,但似乎没有针对定义Command的头文件的include指令。

© www.soinside.com 2019 - 2024. All rights reserved.