CNullDriver

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Cppuser
Posts: 12
Joined: Thu Sep 28, 2006 8:38 pm

CNullDriver

Post by Cppuser »

Hi,

i'm trying to understand the code and ideas of implementations behind the irrlicht project.
I didn't really understand why the Videodriverclasses like CD3D9Driver is
derived from CNullDriver.

What is the idea behind CNullDriver?

Why is it not enough deriving from IVideoDriver?
Like CD3D9Driver : public IVideoDriver

Maybee some of you can help me understanding this.

Thank you
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

The reason is code reuse simply. CNullDriver does the generic part of the implementation of the IVideoDriver interface. Else this interface had to be implemented inside a header, which were acceptable for smaller functions, but not for 40k big source. Other alternative were to implement those functions in each of the drivers, but such code duplication is bad for obvious reasons.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

But you have to be really careful with this. The camera nodes also do this inheritance and thus the interface of a specialized camera cannot be extended that easily.
And for the drivers some additional stubs would also help, e.g. in preparation for DirectX10 (many methods are shared between all those drivers) and for OpenGL derivates such as OpenGL-ES.
Cppuser
Posts: 12
Joined: Thu Sep 28, 2006 8:38 pm

Post by Cppuser »

CNullDriver does the generic part of the implementation of the IVideoDriver interface. Else this interface had to be implemented inside a header
But why is the generic part in CD3D9Driver.cpp not enough?

Because not every VideoDriver supports all the IVideoDriver methods?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Unfortunately (no luckily :lol: ) all video drivers must implement all methods of the interface. That's what interfaces are good for :wink:
Cppuser
Posts: 12
Joined: Thu Sep 28, 2006 8:38 pm

Post by Cppuser »

Well, why it's not enough in the CD3D9Driver.cpp then?

Why having the methods in CNullDriver.cpp and additionally in CD3D9Driver.cpp ?

I don't get it ...
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

Because you then had to write the same code in the opengl driver and the software drivers. Why write the same code four times, when a single time suffices?

General info on this topic:
http://en.wikipedia.org/wiki/Code_reuse

The code reuse technique used in this case:
http://en.wikipedia.org/wiki/Implementation_inheritance

Hope this makes it clearer.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Because some methods are exactly the same for all drivers. And if you call IVideoDriverObject->redundantMethod() it searches in the specific driver class first, then in its parent and so on. That way it's best to put the methods the nearest to the root as possible. The interfaces are kept as small as possible (as said before) so the NullDriver is the best solution for larger methods for all drivers. It's simply the way it's done in OO programming.
Cppuser
Posts: 12
Joined: Thu Sep 28, 2006 8:38 pm

Post by Cppuser »

Saturn wrote:Because you then had to write the same code in the opengl driver and the software drivers. Why write the same code four times, when a single time suffices?
???
But the code is already written in DirectX.cpp files in OpenGL.cpp etc..
So the virtual prototypes of IVideoDriver can get their bodies from directx.cpp or opengl.cpp etc..
Why need a seperate Class CNullDriver for this?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

first of all NullDriver is a separate driver you can choose for rendering (which would need many implementations, though). But if you put the implementations in d3d8, d3d9, ogl, and software1/2 you'd have 5 implementations (duplicated code) instead of just 1 in NullDriver.
Cppuser
Posts: 12
Joined: Thu Sep 28, 2006 8:38 pm

Post by Cppuser »

Sorry,i don't understand why the dependency to CNullDriver is necessary.

And why do you speak about duplicates? The interface methods must exist in every driver so you have to write the functionbodies for each driver either ...

Anyway thx for trying to explain i really don't understand it ...
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

No you do not need to implement them again if a superclass already implements them (and they are declared virtual). Please take some time to understand this structure, it's quite important (also to understand which method is called in the code).
Xaron
Posts: 310
Joined: Sun Oct 16, 2005 7:39 am
Location: Germany
Contact:

Post by Xaron »

BTW: I use the null driver for my server code as I don't need to render all the stuff there but need to use Irrlicht. ;)

Regards - Xaron
Cppuser
Posts: 12
Joined: Thu Sep 28, 2006 8:38 pm

Post by Cppuser »

hybrid wrote:No you do not need to implement them again if a superclass already implements them (and they are declared virtual).
BUT THEY ARE implemented again and again ...
Have a look at the DirectX.cpp and opengl.cpp files.
And you will see that they all have a function body with driverdependent code ...

So,why you always say these methods only implemented in CNullDriver? Thats not true,all the methods got code in the driverXXX.cpp files too!

I really see no difference between these cpp files and the nulldriver cpp file.
The Nulldriver just have the same methods implemented like the directx and opengl driver.cpp files.
So it's only just another classname,thats all i can see ...
Cppuser
Posts: 12
Joined: Thu Sep 28, 2006 8:38 pm

Post by Cppuser »

Ok,maybee i got it now.

Some of the functionality is called by all the drivers in NullDriver.
Because some of the methods contains little stuff that all drivers need the same way.

Is that the reason why nulldriver class exists?
Post Reply