alessio211734 said:
I wrote my spline class, now I think that it's not good put a draw method
in this class because I would separate the spline data from the rendering
that depend on the library that I could use.
class Spline
{
Spline(std::vector<Point3d>...)
....
}
What's the best solution to separate rendering from spline data.
Here are my 0,02€.
A quick and dirty solution would be to define a class/function that would
act as an operator that, when passing a Spline object as an operand, would
churn out the code needed to render the spline object. If you go through
this route then it would be in your best interests to implement this through
a strategy pattern, dedicated to render the graphical objects in your scene.
You could pull off something like:
<pseudo-ish code>
class ObjectRenderingStrategy
{
public:
/* snip other members... */
virtual void spline(Spline const &) = 0;
};
/* a rendering strategy intended to render objects in a bland way */
class BlandRenderingConcreteStrategy
: public ObjectRenderingStrategy
{
public:
void spline(Spline const &);
};
void
BlandRenderingConcreteStrategy::spline(Spline const &spline)
{
// code to render a spline here
}
/* a rendering strategy intended to render objects in an awesome way */
class AwesomeRenderingConcreteStrategy
: public ObjectRenderingStrategy
{
public:
void spline(Spline const &);
};
void
AwesomeRenderingConcreteStrategy::spline(Spline const &spline)
{
// code to render a spline here
}
// now, the render loop
void some_sort_of_rendering_routine(void)
{
BlandRenderingConcreteStrategy bland;
AwesomeRenderingConcreteStrategy awesome;
ObjectRenderingStrategy *render = NULL;
/* whether you wish to render your splines the bland way or the awesome
way, the only thing you need to do is assign ObjectRenderingStrategy *render
to an object of one of of those strategies. This means that if you wish to
implement an entirely new way of rendering your graphical objects then all
you really need to do is define a new concrete strategy class and assign a
pointer to an object of that type. It may look needlessly complicated but it
will save a hefty amount of time in the medium and long run */
for(Spline &spline: my_list_of_spline_objects)
{
render->(spline); /* et voilá. spline rendered according to
the current render policy */
}
}
</pseudo-ish code>
In contrast, a more pristine solution would consist in implementing a model-
view-controller design pattern.
http://en.wikipedia.org/wiki/Model–view–controller
There are other alternatives, but this approach tends to be a bit more
forgiving to design changes in your code.
Hope this helps,
Rui Maciel