samedi 28 novembre 2020

I can't maintain state of a base class amongst subclasses

Summary

My goal is to write a rendering engine using DirectX 11. I plan to write it in an OOP fashion. The plan is to write separate classes. Specifically one class for shader where I can load, compile, bind and set it for usage. Second class for creating vertex buffer, where I can load vertices, bind buffers and set it for rendering.



My Attempts

I wrote one base class `A` for initializing DirectX and maintain 2 objects, `m_device` and `m_device_context`. Then as I wrote `2` subclasses `B` and `C`.

At class B I create and bind vertex buffers and At C I compile and bind vertex shaders. Both B and C uses m_device object to create objects and m_device_context to bind/set them.

In subclass B, I use subclass C to compile and bind shaders. Using subclass B I initialize Base class. but at C subclass I get memory Access violation on m_device. this is probably because I have to reinitialize base class but I can't have different instances of DirectX objects.


Question

I have read that global variable or objects are not recommended but How do I solve this problem, How do I maintain global objects that I will need thought the project?



(My question is specifically about C++ implementation, not games)

Aucun commentaire:

Enregistrer un commentaire