Right, another day, another tech-file update. Unfortunately, work on my 3D engine Manic Motion has been very low-profile since my last update. Reason for this is my transition from being a student to being an employee. I now have a job as software engineer at Philips Electron Optics.
The good news is that in a very short time, I have learnt quite a lot about a number of subjects I wasnít exactly good at in the past. One of these things is object design, and I would like to share some thoughts on this subject with you. Maybe a lot of this is old-hat for some or even most people out there, but Iím going to write it down anyway. Itís general programming stuff, so no fancy 3D algorithms. The reason to put it in my tech-file and not in the tutorials file is that Iím on a rather steep learning-curve at the moment, and itís quite possible the views I present here will change in the near future. But I wanted to write something about it anyway, because writing forces me to think clearly. A tech-file is probably a better place to make mistakes than a tutorial .
In my last update, I said that I consider good object-design to be very important. This was wrong. At my new job, some 30 people are working on a project consisting of several hundreds of code modules, and thousands (!) of objects. Good object design is not just very important here, it is absolutely vital to produce functioning software. And out of these 30 people, thereís a group of 3 working full-time on architecture and design. This indicates how important architecture and design are according to management.
First of all, there is a difference between Object Oriented Programming (OOP) and an Object Oriented Language (OOL). C++ is an OOL. It provides the tools and the framework in which you, the programmer, may or may not choose to work Object Oriented. OOP on the other hand is a general way of thinking which enables us, humble mortals, to design,manage and gain insight in complex systems.
One of the most important lessons I have learnt in the past few weeks is why to use OOP in the first place. My personal answer at the moment is: Because it allows you to write code which is derived from a high-level design.
A high level design is something like this (Donít take it too seriously please):
This diagram is very superficial and simple, yet it already provides information on the class-structure which will eventually emerge for this game-engine. You can see the complete engine consists of a number of sub-engines, each with their own, well-defined task. Now, as you all know, a 3D engine can also be sub-divided into even smaller blocks. This subdividing into ever smaller blocks is iterative, and the final logical step is the transition to an actual class-structure. That is the power of OOP: You can think about and work on a design in abstract concepts, plot relations, assign responsibilities, and finally make an easy translation to the actual implementation in an OOL. This is very different from traditional programming, where there was a huge gap to cross between the logical/ structural design and the actual implementation.
OOP is built on a few concepts. In my view the first and foremost concept is Encapsulation. Encapsulation may be the most important concept in OOP, yet to explain it in words can be difficult. One way of explaining encapsulation is that an object only does the things which are its responsibility. For example: When you write a 3D engine, youíd have an object responsible for rendering and a separate object responsible for actually blitting the rendered scene to the screen. Ideally, this means you can port your engine to other platforms by simply adapting/rewriting the blitting object, while the rendering object remains unchanged. A wel encapsulated object resembles a black-box: You put something in, and something else comes out, but how it works internally is completely unclear. Also, you are completely unable to reach inside and change the behaviour of the black-box. An example:
Well, this text is getting quite lengthy already, so Iíll just stop here. If you find this interesting, please let me know, and I might write some more on the subject. But bear in mind I am by no means an expert on OOP.
Download The Sample Program: ManicMotion.exe