If I have a graph which consists of nodes like this:
class Graph
{
public:
...
int32_t width();
int32_t height();
const Node * getNode(int32_t height, int32_t width);
private:
std::vector<std::vector<Node>> data;
}
where each node is a struct with multiple member variables:
struct Node
{
int width, height;
int a; // should be in the top left corner when rendering rectengular node;
int b; // top right corner;
int c; // bottom left corner;
int d; // bottom right corner;
int e; // center of the node;
}
Should the renderer know how to render each node or is it nodes job to know how to draw itself?
For example, here is one way where the renderer does all the job:
class Renderer
{
public:
void render(const Graph & graph)
{
for(int32_t height = 0; height < graph.height(); ++height)
{
for(int32_t width = 0; width < graph.width (); ++width )
{
// Drawing rectangle which represents the node;
drawNodeRect(getPositionOfNodeRect());
// Calculating positions of each member of Node;
auto posOfA = doSomeCalculationsForA();
...
auto posOfE = doSomeCalculationsForE();
// Drawing each member;
renderNumber(posOfA, graph.getNode(width, height));
...
renderNumber(posOfE, graph.getNode(width, height));
}
}
}
}
Here what it would look like in the case where the node knows how draw itself:
struct Node
{
int a; // should be in the top left corner when rendering rectengular node;
...
void render()
{
// Drawing rectangle which represents the node;
drawNodeRect(getPositionOfNodeRect());
// Calculating positions of each member of Node;
auto posOfA = doSomeCalculationsForA();
...
auto posOfE = doSomeCalculationsForE();
// Drawing each member;
renderNumber(posOfA, graph.getNode(width, height));
...
renderNumber(posOfE, graph.getNode(width, height));
}
}
class Renderer
{
public:
void render(const Graph & graph)
{
for(int32_t height = 0; height < graph.height(); ++height)
{
for(int32_t width = 0; width < graph.width (); ++width )
{
graph.getNode(width, height).render();
}
}
}
}
Maybe to push it further, the graph could also know how to draw itself:
class Graph
{
public:
...
int32_t width();
int32_t height();
const Node * getNode(int32_t height, int32_t width);
void render()
{
for(int32_t height = 0; height < graph.height(); ++height)
{
for(int32_t width = 0; width < graph.width (); ++width )
{
graph.getNode(width, height).render();
}
}
}
private:
std::vector<std::vector<Node>> data;
}
class Renderer
{
public:
void render(const Graph & graph)
{
renderGraph(graph);
}
}
So basically my question again, Should every entity know how to render itself or is it renderer's job to figure out how to draw stuff? For better understanding but not really necessary, I am using SFML, where you can inherit from sf::Drawable which allows you to overload a function to make an object Drawable.
window.draw(...)
method multiple times(2 rects each node and 3 text), also each frame the methodwindow.display(...)
is called once. I am not sure how I can optimize that by using bigger patches. I mean I could save some space by only using one base drawable node and pass every node to it so that it sets up all texts and colors and shapes.