내 게임 세계의 모든 객체에는 그 객체를 시각적으로 나타내는 스프라이트 벡터가 있습니다. 내 문제는 내가 화면에 제대로 그릴 것 못할 것입니다 :스프라이트의 벡터에서 그리기 SFML
이 드로어 블 오브젝트입니다 매주 당김 상속 :
class Drawable {
private:
static vector<Drawable*> sprites;
protected:
vector<sf::Texture> myTextures;
vector<sf::Sprite> mySprites;
public:
Drawable();
static vector<Drawable*> getSprites();
void draw(sf::RenderWindow&) const;
};
와의 통화 당 :
vector<Drawable*> Drawable::drawables;
Drawable::Drawable() {
drawables.push_back(this);
}
vector<Drawable*> Drawable::getDrawables() {
return drawables;
}
void Drawable::draw(sf::RenderWindow& window) const {
for (auto sprite : mySprites) {
window.draw(sprite);
}
}
예 당김에서 상속 개체의 :
class Terrain : public Drawable {
private:
void loadSprite(string);
public:
Terrain(string);
};
과 .CPP :
Terrain::Terrain(string fileName) {
loadSprite(fileName);
}
void Terrain::loadSprite(string fileName) {
sf::Texture texture;
texture.loadFromFile(fileName);
myTextures.push_back(texture);
sf::Sprite sprite;
sprite.setTexture(texture);
mySprites.push_back(sprite);
}
이 경우 지형 스프라이트는 런타임 중에 흰색 상자에 불과합니다. loadSprite의 "texture"와 "sprite"var은 메소드가 범위를 벗어나면 파괴됩니다.
"텍스처"와 "스프라이트"를 지형 클래스에 저장하여 문제를 해결할 수 있음을 알고 있습니다. 하지만 이것은 내게 불필요한 것 같습니다. 대신에 mySprites 및 myTextures 벡터에 저장할 수 있습니까?
입력 해 주셔서 감사합니다. 나는 생각해 낸 방법을 시도했습니다. 그것은 스프라이트와 텍스처를 사용하는 각 객체에 스프라이트와 텍스처를 저장하는 것입니다. 그래서 terrain.h sf :: Texture texture, sf :: Sprite 스프라이트와 loadSprite에 두 개의 필드를 할당했을 것입니다. – Binbon
또한 mySprites와 myTextures가 포인터를 보관하도록 (복사 대신) 중복을 막을 수 있습니다. – Binbon