Here's some code showing where I'm stuck.
What I'm trying to do is first put various derived classes of Dude (such as RedDude here) into a vector to sort them by position, and then put everything from vector into a vector of sf::Drawables (along with some other stuff like the background which I don't want mixed with the "Dudes") so that I can draw them.
Problem is I don't really understand what to put into the base class:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
#include "AssetManager.h"
class Dude : public sf::Drawable, public sf::Transformable
{
public:
void Draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
target.draw(*THING, states);
}
private:
std::unique_ptr<sf::Drawable> THING;
};
class RedDude : public Dude
{
public:
bool load()
{
red_dude = sf::Sprite(AssetManager::GetTexture("Media/red_dude.png"));
red_dude.setTextureRect(sf::IntRect(0, 0, 96, 140));
red_dude.setScale(1, 1);
red_dude.setOrigin(48, 124);
return true;
}
virtual void draw(sf::RenderTarget& target, sf::RenderStates states)const
{
states.transform *= getTransform();
states.texture = &red_dude_tex;
target.draw(red_dude, states);
}
sf::Sprite red_dude;
sf::Texture red_dude_tex;
};
std::vector<std::unique_ptr<Dude>> Dudes;
std::vector<std::reference_wrapper<const sf::Drawable>> World;
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML");
std::unique_ptr<RedDude> d1 = std::make_unique<RedDude>();
d1->load();
d1->setPosition(50,50);
Dudes.push_back(std::move(d1));
World.push_back(std::move(Dudes[0]));
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(World[0]);
window.display();
}
return 0;
}
-------
trying to compile this code gives me an error at this line:
World.push_back(std::move(Dudes[0]));
... says that argument type doesn't match the object type.