C ++ SFML雪碧没有被通过类绘制(C++ SFML Sprite not being draw

2019-10-20 14:39发布

(不,我的意思不是显示一个精灵非常基本的,我根本想不出更好的标题,因为我不完全了解什么错误。)

简单地说,我有一个精灵,当被绘制到屏幕上,会显示不出来,但如果我在做临时精灵做一些测试int main()并将其绘制到屏幕上,他们表现出了完美的罚款。 所以,我知道我做错了我的类的设计或东西,因为他们是在精灵将不会显示。

(注:我所有的类都是的命名空间下rpg::我会剪下来,以节省空间,但我可能会使用它在我的代码不加思考-所以要去除可能发生的任何混淆)

码:

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;
}

我知道有一吨的代码在这里,但我相信,至少大部分是相关的。 我没有丝毫的想法,为什么在图形组件精灵不显示,但临时精灵工作完全正常,即使这两个精灵纹理指向相同的纹理。 我的猜测是,我做错了什么与类内的抽奖功能,但它们看起来没什么问题。 我认为这是我的另一件事是,对象的精灵不会再让纹理出于某种原因,但我做到设置其质地为rpg::PlayerTexture在其HandleEvents()函数,以便它设置纹理肯定的,但它还是不解决它。 我是在一个完全丧失这一点,它可能是简单的东西。

感谢您抽出宝贵时间,通过这一切阅读,如果你能,我很想解释,为什么这个问题是问题。 谢谢

Answer 1:

当你声明一个类的成员静态这意味着不管如何创建类的许多对象,只有一个静态成员的副本。 您需要将其声明为“外部”。 extern sf::Texture PlayerTexture



文章来源: C++ SFML Sprite not being drawn through classes
标签: c++ sfml