I have a class called Renderer that holds two callback objects;
void (*drawCall)(const sf::Drawable& drawable, const sf::RenderStates& states);
void (*drawPrimCall)(const sf::Vertex* vertices, unsigned int vertexCount,
sf::PrimitiveType type, const sf::RenderStates& states);
I would like to have a public function that binds these callbacks, it's templated and needs a class that has two draw methods each with their valid parameters, something like this:
template<typename T>
inline void setRenderer(T* instance)
{
using namespace std::placeholders;
drawCall = std::bind(&T::draw, instance, _1, _2);
drawPrimCall = std::bind(&T::draw, instance, _1, _2, _3, _4);
}
However, as you might imagine this does not work. I'm still learning about std::bind and I'm not sure about the use of placeholders, it's understood what I'm trying to do here.
I've tried looking for answers in Google but it has been a bit daunting, thanks for any help!
UPDATE:
Ok, so the answer given by @Holt works, after changing to std::function my callbacks look like this:
std::function<void(const sf::Drawable& drawable, const sf::RenderStates& states)> drawCall_fn;
std::function<void(const sf::Vertex* vertices, unsigned int vertexCount,
sf::PrimitiveType type, const sf::RenderStates& states)> drawPrimCall_fn;
Now for the setRenderer method due to constraints on external libraries I also need to pass a function reference, like so;
template<typename T>
inline void setRenderer(T* instance, void(T::*drawCall)(const sf::Drawable&, const sf::RenderStates&))
{
using namespace std::placeholders;
drawCall_fn = std::bind(drawCall, instance, _1, _2);
}
Such that when called I can do it like this:
renderer.setRenderer<sf::RenderTarget>(&window, &sf::RenderTarget::draw);
**Note: I'm only dealing with drawCall_fn for the moment as I can easily expand after.
UPDATE2: Added missing parameters for setRenderer drawCall.