C++ SFML Sprite 未通过类绘制 [英] C++ SFML Sprite not being drawn through classes

查看:71
本文介绍了C++ SFML Sprite 未通过类绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(不,我的意思不是显示精灵的基本知识.我只是想不出更好的标题,因为我不完全明白出了什么问题.)

(No, I do not mean the very basics of displaying a sprite. I simply could not think of a better title since I do not fully understand what is going wrong.)

简单地说,我有一个精灵,当它被绘制到屏幕上时,不会显示出来,但是如果我通过在 int main() 中制作临时精灵来做一些测试并将它们绘制到屏幕,它们显示得非常好.所以我知道我在类的设计上做错了什么,因为它们是精灵不会显示的地方.

Simply put, I have a sprite that, when being drawn to the screen, will not show up, but if I do some tests by making temporary sprites in int main() and draw them to the screen, they show up perfectly fine. So I know that I am doing something wrong with the design of my classes or something because they are where the sprites will not show.

(注意:我所有的类都在 rpg:: 的命名空间下我会将它删掉以节省空间,但我可能会不假思索地在我的代码中使用它 - 所以为了消除任何混淆这可能会发生)

(note: all of my classes are under the namespace of rpg:: I will cut it out to save space, but I might use it in my code without thinking - so to remove any confusion that might happen)

代码:

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

抽象组件.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

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

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() 函数,这样它肯定会设置纹理,但这也没有修复它.我对此完全不知所措,这可能很简单.

I know there is a ton of code here, but I believe that at least most of it is relevant. I haven't the slightest idea as to why the sprite in the graphics component doesn't show, but the temporary sprite works perfectly fine, even though both sprites' textures point to the same texture. My guess is that I did something wrong with the draw function inside of the classes, but they look fine to me. The other thing I think it my be is that the object's sprite isn't keeping the texture for some reason, but I made it set its texture to the rpg::PlayerTexture in its HandleEvents() function so that it would set the texture for sure, but that didn't fix it either. I am at a complete loss to this and it is probably something simple.

感谢您花时间阅读所有这些内容,如果您能够阅读,我希望您能解释为什么问题是问题所在.谢谢

Thanks for taking the time to read through all of this and, if you are able to, I would love an explanation as to why the problem is the problem. Thanks

推荐答案

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

When you declare a member of a class as static it means no matter how many objects of the class are created, there is only ONE copy of the static member. You need to declare it as "extern". extern sf::Texture PlayerTexture

这篇关于C++ SFML Sprite 未通过类绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆