C ++ SFML Sprite不是通过类绘制的

时间:2014-07-30 20:34:56

标签: c++ sfml

(不,我不是指显示精灵的基础知识。我根本无法想到一个更好的标题,因为我不完全明白出了什么问题。)

简单地说,我有一个精灵,当被绘制到屏幕上时,不会显示,但如果我通过在int main()制作临时精灵并将它们绘制到屏幕上进行一些测试,它们就会出现非常好。所以我知道我在设计类或其他东西时出错了,因为它们是精灵不会显示的地方。

(注意:我的所有类都在rpg::的命名空间下我会将其删除以节省空间,但我可能会在我的代码中不加思索地使用它 - 所以要消除可能发生的任何混淆)< / p>

代码:

rpg.h:

static sf::Texture PlayerTexture;

/*
    NOTE: Loads in All Textures for use
          --This works properly (tested)
*/
static void SetUp()
{
    if(!PlayerTexture.loadFromFile("C:/Program Files (x86)/Terentia/Files/Textures/player.png"))
    {
        std::cout<<"Error: texture failed to load..."<<std::endl;
    }
}

AbstractComponents.h

//Abstract class for handling graphics
class GraphicsComponent : public rpg::Component, public sf::Drawable
{
protected:
    sf::Sprite mySprite;

public:
    virtual void HandleEvents(GameObject& object, sf::RenderWindow& screen, rpg::EventList& events) = 0;

//Copy Constructor
GraphicsComponent(const GraphicsComponent& me) {
    mySprite = me.mySprite; }

GraphicsComponent& GraphicsComponent::operator= (const GraphicsComponent &me){
    mySprite = me.mySprite; }

GraphicsComponent() {}
virtual ~GraphicsComponent() {}
};

PlayerGraphicsComponent.h

class PlayerGraphicsComponent : public rpg::GraphicsComponent
{

public:

virtual void Receive(rpg::Message message);

virtual void HandleEvents(GameObject& object, sf::RenderWindow& screen, rpg::EventList& events);

PlayerGraphicsComponent() { mySprite.setTexture(rpg::PlayerTexture); }
~PlayerGraphicsComponent();

private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    //Draw sprite onto the screen.
    target.draw(mySprite, states);

}//needed to make drawable
};

GameObject.h

class GameObject : public rpg::ContainerObject, public sf::Drawable
{
public:
sf::Vector2f myPosition;

rpg::Movable CanMove;

GameObject();
GameObject(std::shared_ptr<InputComponent> input,
            std::shared_ptr<GraphicsComponent> graphics,
            std::shared_ptr<PhysicsComponent> physics )
:   myInput(input),
    myGraphics(graphics),
    myPhysics(physics)
{myPosition.x = 0;
myPosition.y = 0;}

//Copy Constructor
GameObject(const GameObject& me) {
    myInput = me.myInput;
    myGraphics = me.myGraphics;
    myPhysics = me.myPhysics;
    myPosition = me.myPosition; }

GameObject& GameObject::operator= (const GameObject &me);

~GameObject();

void HandleEvents(sf::RenderWindow& screen, rpg::EventList& events)
{
    myInput->HandleEvents(*this, screen, events);
    myPhysics->HandleEvents(*this, screen, events);
    myGraphics->HandleEvents(*this, screen, events);
}

private:
std::shared_ptr<InputComponent>     myInput;
std::shared_ptr<GraphicsComponent>  myGraphics;
std::shared_ptr<PhysicsComponent>   myPhysics;


private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    target.draw(*myGraphics, states);

}//needed to make drawable
};http://stackoverflow.com/questions/13899077/c-sfml-display-glitch-with-sprite-member-changed-by-mouseclick

的main.cpp

int main()
{
//Loads in all textures, info, etc
rpg::SetUp(); 

//Create Player Object
ptrGameObject player = rpg::CreatePlayer();

//Create render window and give it it's info
sf::RenderWindow screen;
screen.create(sf::VideoMode(800, 640), "Terentia", sf::Style::Close);

screen.setVerticalSyncEnabled(1); // Setting max framerate to 60 (Facultative)
screen.setFramerateLimit(60); // Setting max framerate to 60 (Facultative)

rpg::EventList eventList;

while(screen.isOpen())
{
    sf::Event events;
    while(screen.pollEvent(events))
    {
        eventList.push_back(events);
        if (events.type == sf::Event::Closed)
        {
            screen.close();
        }
    }

    //Handle Object Events here
        player->HandleEvents(screen, eventList);
    //End Object Handle Events

    eventList.clear();

    //temp sprite for test
    std::shared_ptr<sf::Sprite> tempSprite(new sf::Sprite());

    tempSprite->setTexture(rpg::PlayerTexture);

    screen.clear();

    //Doesn't draw to screen
    screen.draw(*player);

    //Does draw to screen.
    screen.draw(*tempSprite);

    screen.display();

}

return 0;
}

我知道这里有很多代码,但我相信至少大部分代码都是相关的。我没有想到为什么图形组件中的精灵没有显示,但临时精灵工作得非常好,即使两个精灵都是如此。纹理指向相同的纹理。我的猜测是我在类中的draw函数做错了,但它们看起来很好。我认为我的另一件事是对象的精灵由于某种原因没有保留纹理,但是我把它的纹理设置为rpg::PlayerTexture中的HandleEvents()。功能,以便它确定设置纹理,但也没有修复它。我完全失去了这一点,这可能很简单。

感谢您花时间阅读所有这些内容,如果您能够,我会很乐意解释为什么问题出在问题上。感谢

1 个答案:

答案 0 :(得分:0)

当您将类的成员声明为static时,意味着无论创建了多少个类的对象,都只有一个静态成员的副本。您需要将其声明为&#34; extern&#34;。 extern sf::Texture PlayerTexture