简体   繁体   中英

Function Calling Inheritance C++

Okay firstly Inheritance isn't my strong point that is why I am here. However i will explain the best i can.

I am calling a function from Class 1 in Class 2, Class 2 is derived from Class 1 and Class 2 is the base class for Class 3.

Upon execution of the program it is class 3 which is called in WinMain().

Ill show it code always makes sense that way:

void SRNTY_API Direct3D11::D3D11ResizeBuffers(HWND hwnd)
{
    RECT rect;
    GetWindowRect(hwnd, &rect);
    mRenderTargetWidth = rect.right - rect.left;
    mRenderTargetHeight = rect.bottom - rect.top;

    if (mDXGISwapChain != NULL)
    {
        assert(mD3D11DeviceContext);
        assert(mD3D11Device);
        assert(mDXGISwapChain);

        if (mD3D11DeviceContext)
        {
            mD3D11DeviceContext->ClearState();
        }
        if (mD3D11RenderTargetView)
        {
            mD3D11RenderTargetView->Release();
        }
        if (mD3D11DepthStencilView)
        {
            mD3D11DepthStencilView->Release();
        }

        if (FAILED(result = mDXGISwapChain->ResizeBuffers(1, mRenderTargetWidth, mRenderTargetHeight,
            DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH)))
        {
            SRNTY::ErrorHandler::Instance()->AddError(L"Direct3D11::D3D11ResizeBuffers() - ID3D11Device::CreateRenderTargetView() failed to create render target view!",
                SRNTY::ErrorHandler::ErrorHeaderSelect::EHS_ERROR);
        }

        if (FAILED(result = mDXGISwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mD3D11RenderTargetView)))
        {
            SRNTY::ErrorHandler::Instance()->AddError(L"Direct3D11::D3D11ResizeBuffers() - ID3D11Device::CreateDepthStencilView() failed to create depth stencil view!!",
                SRNTY::ErrorHandler::ErrorHeaderSelect::EHS_ERROR);
        }

        if (FAILED(result = mDXGISwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mD3D11DepthStencilView)))
        {
            SRNTY::ErrorHandler::Instance()->AddError(L"Direct3D11::D3D11ResizeBuffers() - ID3D11Device::CreateDepthStencilView() failed to create depth stencil view!!",
                SRNTY::ErrorHandler::ErrorHeaderSelect::EHS_ERROR);
        }

        D3D11_VIEWPORT viewport;
        ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

        viewport.TopLeftX = 0;
        viewport.TopLeftY = 0;
        viewport.Width = mRenderTargetWidth;
        viewport.Height = mRenderTargetHeight;
        viewport.MinDepth = 0.0f;
        viewport.MaxDepth = 1.0f;

        if ((mD3D11DeviceContext != NULL) && (mD3D11RenderTargetView != NULL) && (mD3D11DepthStencilView != NULL))
        {
            mD3D11DeviceContext->OMSetRenderTargets(1, &mD3D11RenderTargetView, mD3D11DepthStencilView);
            mD3D11DeviceContext->RSSetViewports(1, &viewport);
        }
    }
}

The above function is designed to re size the IDXGISwapChain* buffers. So this must be called on WM_EXITSIZEMOVE. This function would be be within Class 1 from the above blabba.

So the next class which uses the above functions class as a base now calls this on WM_EXITSIZEMOVE, here is the code:

LRESULT Window::MsgProc(__in HWND hWnd, __in UINT message,
    __in_opt WPARAM wParam, __in_opt LPARAM lParam)                 
{
    switch (message)
    {
    case WM_KEYDOWN:                                                                    
    {
        if (wParam == VK_ESCAPE)                                                        
        {
           PostQuitMessage(0);
        }
    }

    case WM_MENUCHAR:                                                                   
    {
        return MAKELRESULT(0, MNC_CLOSE);                                       
    } break;

    case WM_DESTROY:                                                                        
    {
        PostQuitMessage(0);                                                             
    } break;

    case WM_ENTERSIZEMOVE:                                                          
    {

    } break;

    case WM_EXITSIZEMOVE:                                                               
    {
        D3D11ResizeBuffers(hWnd);    // heres is the above function being called as mentioned on WM_EXITSIZEMOVE
    } break;

    case WM_GETMINMAXINFO:                                                          
    {

    } break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

Both of these classes then get added to a Engine class which is then added to a Application class. The application is the front facing class to WinMain().

Now what is happening isn't in fact the buffers being re sized, whats happening is Direct3D11::D3D11ResizeBuffers() is getting the window detentions from calling GetWindowRect(hwnd, &rect); and storing the results in RECT rect;.

However when we get to this line here mRenderTargetWidth = rect.right - rect.left;. It basically says mRenderTargetWidth "Exception thrown: write access violation. this was 0x8.". When i Debug the program i notice all the members of the Direct3D11 class say the following:

result Unable to read memory
mhWnd Unable to read memory
mRenderTargetWidth Unable to read memory
mRenderTargetHeight Unable to read memory
mDXGISwapChain Unable to read memory
mDXGIDevice Unable to read memory
mDXGIAdapter Unable to read memory
mDXGIFactory Unable to read memory
mD3D11Device Unable to read memory
mD3D11DeviceContext Unable to read memory

mD3D11RenderTargetView Unable to read memory
mD3D11DepthStencilView Unable to read memory

So in effect the class has diapered so like this->mD3D11Device is NULL.

It is very safe to say I am mega confused here. I really need help either explaining what i am doing wrong or suggestions as to how i can solve this problem would be greatly appreciated. If any needs any further information just ask and i will give.

Thanks for reading and i hope people can help :)

Ok i was just being very dumb and was able to solve the problem simply by creating a global object back in the header file of the Direct3D11 class:

extern SRNTY_API Direct3D11& pDirect3D11;

This enables me to then create an object in any cpp file like this:

Direct3D11& pDirect3D11 = Direct3D11::Direct3D11();

So im using the same instance every time, well i think thats it i dont really like thinking about instances and inheritance boggles my mind a bit.

But if anyone has the same problem this is the answer. Good luck to everyone else out there \\o/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM