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.
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.
(My question is specifically about C++ implementation, not games)
I do not have access to the project but I made a quick UML diagram, I hope this will help clarifies a bit
Inheritance is built upon a is-a
relationship. What most people get wrong is that it's not a in perspective of the object itself, but upon the functionality that the object brings with it.
In your case, the functionality of the D3D class is to initialize and allocate the 3D engine context. Right?
The Demo and Shader classes do not extend that behavior, which means that there are no is-a
relationship.
So in your case, those classes requires the D3D class to be able to function. Another solution might be do not use the D3D class at all in Demo or Shader. It depends on if the D3D class provides any other functionality (methods) that the Demo or Shader classes use.
You should also be really careful with exposing fields in your classes since it can become a real headache when your application grows. Try instead to expose functionality through methods.