我正在尝试为SFML项目编写场景图。我有头文件和.cpp文件,并且大多数情况下一切正常,除了我不断收到的一个错误。命名空间“ sf”没有成员“ Time”。我浏览了各种论坛,并尝试在头文件中引用时间,但没有任何效果。非常感谢您使此工作正常进行的技巧。
//头文件
#pragma once
#include <SFML/Graphics/Drawable.hpp>
#include <vector>
#include <memory>
class SceneNode : public sf::Transformable, public sf::Drawable, private sf::NonCopyable
{
public:
typedef std::unique_ptr<SceneNode> Ptr;
public:
SceneNode();
void attachChild(Ptr child);
Ptr detachChild(const SceneNode& node);
void update(sf::Time dt);
sf::Vector2f getWorldPosition() const;
sf::Transform getWorldTransform() const;
private:
virtual void updateCurrent(sf::Time dt);
void updateChildren(sf::Time dt);
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
void drawChildren(sf::RenderTarget& target, sf::RenderStates states) const;
private:
std::vector<Ptr> mChildren;
SceneNode* mParent;
};
//主文件
#include "SceneGraph.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <assert.h>
#include <algorithm>
#include <cassert>
using namespace std;
SceneNode::SceneNode()
: mChildren()
, mParent(nullptr)
{
}
void SceneNode::attachChild(Ptr child)
{
child->mParent = this;
mChildren.push_back(std::move(child));
}
SceneNode::Ptr SceneNode::detachChild(const SceneNode& node)
{
auto found = std::find_if(mChildren.begin(), mChildren.end(), [&](Ptr& p {
return p.get() == &node; });
assert(found != mChildren.end());
Ptr result = std::move(*found);
result->mParent = nullptr;
mChildren.erase(found);
return result;
}
void SceneNode::update(sf::Time dt)
{
updateCurrent(dt);
updateChildren(dt);
}
void SceneNode::updateCurrent(sf::Time)
{
//Do nothing by default
}
void SceneNode::updateChildren(sf::Time dt)
{
for_each(Ptr& child, mChildren)
child->update(dt);
}
void SceneNode::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
//apply transform of current node
states.transform *= getTransform();
//draw node and children with changed transform
drawCurrent(target, states);
drawChildren(target, states);
}
void SceneNode::drawCurrent(sf::RenderTarget&, sf::RenderStates) const
{
//do nothing by default
}
void SceneNode::drawChildren(sf::RenderTarget& target, sf::RenderStates states) const
{
for_each(const Ptr& child, mChildren)
child->draw(target, states);
}
sf::Vector2f SceneNode::getWorldTransform() const
{
sf::Transform transform = sf::Transform::Identity;
for (const SceneNode* node = this; node != nullptr; node = node->mParent)
transform = node->getTransform() * transform;
return transform;
}