Direct2D - Nothing is being drawn to the screen, something wrong in the way I'm handling arrays in C++

StackOverflow https://stackoverflow.com/questions/4752514

سؤال

Hello everyone and thank you for looking. This is a follow up to the original question posted here.

I have a base class that I define thusly:

class DrawableShape
{
public:
    virtual HRESULT DrawShape(ID2D1HwndRenderTarget* m_pRenderTarget)
    {
        return S_OK;
    }
};

I have two classes that extend this class, both are similar, so I'm listing one:

class MyD2DEllipse : public DrawableShape
{
private:
    D2D1_ELLIPSE data;
public:
    MyD2DEllipse();
    HRESULT DrawShape(ID2D1HwndRenderTarget* m_pRenderTarget);
};

The DrawShape function is implemented like this:

HRESULT MyD2DEllipse::DrawShape(ID2D1HwndRenderTarget* m_pRenderTarget)
{
    HRESULT hr = E_FAIL;
    ID2D1SolidColorBrush* m_pBrush;
    hr = m_pRenderTarget->CreateSolidColorBrush(
                D2D1::ColorF(D2D1::ColorF::OrangeRed),
                &m_pBrush
                );
    m_pRenderTarget->DrawEllipse(&data, m_pBrush, 10.f);
    return hr;
}

I want to draw a random number of ellipses and rectangles to the screen, so I first find out those random numbers, create an array of DrawableShape with that size (since I can't allocate objects dynamically in C++), replace the parent objects with the child objects, and then call the draw function on the array, randomly again. Here's what my code looks like:

    HRESULT Demo::OnRender()
    {
        HRESULT hr = S_OK;
        hr = CreateDeviceResources();

        if (SUCCEEDED(hr))
        {
            m_pRenderTarget->BeginDraw();
            m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
            m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));

            // Decide on # of primitives
        randEllipse = 1 + (rand() % 5);
        randRectangle = 1 + (rand() % 5);
        totalPrimitives = randEllipse + randRectangle;

        DrawableShape *shapes;
        shapes = new MyShape[totalPrimitives];

        for (int i=0; i<randEllipse; i++)
        {
            MyEllipse ellipse1;
            shapes[i] = ellipse1;
        }
        for (int i=randEllipse; i<(randEllipse + randRectangle); i++)
        {
            MyRectangle rect1;
            shapes[i] = rect1;
        }

        for (int i=0; i<totalPrimitives; i++)
        {
            hr = shapes[i].DrawMyShape(m_pRenderTarget);
        }
        hr = m_pRenderTarget->EndDraw();
    }
}

That should've worked, but it doesn't. Also, after writing this out, I realize that I'm better off creating the array in some sort of init function, and calling the draw on the array in the OnRender function. Please help!!


EDIT: Okay I've got the shapes working with pointers, the problem is the construction of the array. So I have something like this:

    MyD2DRectangle rect1;
    MyD2DEllipse ell1;

    DrawableShape *shape1 = &rect1;
    DrawableShape *shape2 = &ell1;
    shape1->DrawShape(m_pRenderTarget);
    shape2->DrawShape(m_pRenderTarget);

That seems to work by itself. How can I create the array of DrawableShape without slicing?

هل كانت مفيدة؟

المحلول

shapes is an array of MyShape instances. When you say shapes[i] = ellipse1; or shapes[i] = rect1;, you are losing the subclass data as part of this assignment, which is known as slicing in C++.

As such, each call to shapes[i].DrawMyShape(m_pRenderTarget); is just returning S_OK as defined in MyShape.

In order to properly use polymorphism in C++, you need to use pointers or references to MyShape instances (usually allocated using new). If you are not allowed to do this (homework?), then you need to find a way to do this without polymorphism.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top