Question on shapes

Mike Parker aldacron at gmail.com
Tue May 17 06:17:02 UTC 2022


On Tuesday, 17 May 2022 at 05:08:30 UTC, matheus wrote:

>
> In D there would be a better way to do such thing?
>

Nothing really specific to D, but for one or two properties, you 
might just add them as function parameters with default values:

```d
void draw(float scale = 1.0f);
```

If you have a number of them (scale, color, blend state, etc.), 
then you might add them as members of the `Shape` class. You 
could then expand on that with a single draw function in the 
`Shape` class that handles the actual drawing, and the subclasses 
would then call that internally after, e.g., setting up any 
vertex buffers or whatever specific to the shapes.

```d
class Shape {
    private:
       float scale;
       RGBA color;
       DrawBuffer buffer; // some API-specific vertex buffer or 
whatever

    protected:
       void drawImpl() { // get the shape on screen }

    public:
       abstract void draw();
}

class Circle {
    override void draw() {
       // set up buffer
       ...
       drawImpl();
}
```

Or you could have a `DrawProperties` struct independent of the 
`Shape` hierarchy that you can fill out and pass to every draw 
call. Or set global properties in the renderer and draw objects 
that have the same properties all at once.

There are several ways to go about it.


More information about the Digitalmars-d-learn mailing list