Archived

This topic is now archived and is closed to further replies.

Cobra

OOP vs NON-OOP

Recommended Posts

Okay... I dont mean to start a flame war or anything, this is just a curious question. See.. I''ve built several engines in the past, ranging for q3 map loading engines, to height-mapped terrain engines e.t.c.... some in OOP, and others in a more C style. I''ve been working on my latest engine for a week now, have various model support in (3ds, md3, md2, mdl, ase) and basic sound support (wav, midi). e.t.c Its a very clean codebase e.t.c (had some stupid bugs along the way)... but even though it''s clean and well made, I''m thinking of rewriting it to try and get performance increase and hopefully even cleaner code. (seeing as it''s only rewriting a weeks work). I''m just wondering... when I rewrite this, should I write it in C style again, or in OOP form. (which I use in my engine anyway for certain things such as model loading). Just wanted to get a few peoples opinions on it before I go and restart. I''m looking for maximum performance here, as this engine has to surpass all of my old ones (and I''ll be laying the effects in a nice thick layer in this :D , its intended for machines in about 2 years from now.), gonna start working on a full game on it. e.t.c e.t.c Just really wanted to grab a few peoples opinions on whether I should stick to my more-used C form or make the entire engine OOP. Look forward to reading your opinions. ~Cobra~

Share this post


Link to post
Share on other sites
I think you should mix both styles. Some parts are easier to handle with objects (real game objects like models, etc.) while others are easier with simple functions and modules. I played a lot with these ideas and now it seems to me that the best way is to avoid the "wrapper mania" but still use classes to avoid playing with hand-made this pointers...

alexk7@alexk7.com

Share this post


Link to post
Share on other sites
I believe that when your project starts growing larger it really pays off to have it OOP. It makes it a lott more easy to read for yourself and for others. You don't have to work yourself through dozens of (global) functions to see the general program flow (if OOP is used corectly).
Might I suggest the book at relisoft: www.relisoft.com/book , it really changed my way of programming in c++, although I'm still reading it .

Edited by - shabaz on February 6, 2002 1:43:01 PM

Share this post


Link to post
Share on other sites
Thx for the replies thus far.. just to let you guys know, this is how the engine currently is.

Main code, initialising window e.t.c e.t.c is all done in C
BMP loading all done in C.
Wav and Midi sound handling all done in C (yes I know wav doesnt need anything but a lib linked in, but I wanted to pass it through the sound system in the engine and have each bit logged e.t.c so I wrote my own functions for it).
All model loading code is OOP
TGA loading code is C.
Controls e.t.c are all done in the main window code (such as in Nehe's. they arent held within a seperate OOP based function (I've actually seen this done b4.. not sure why they bothered))
Map loading code WILL be OOP.
All font-based stuff e.t.c is done in C

So basically I'm struggling on whether to keep it as it is (mixed) or move it all to OOP.

Anyway.. keep the replies coming, this stuff is all useful.
(btw.. Shabaz, thx for the link, I'm giving it a look through at the moment, its funny that a lot of the techniques ID-Software used in their code layout e.t.c are defined as "bad" in this hehe)

Edited by - Cobra on February 6, 2002 1:43:22 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster




OOP gives you 3 traits: polymorphism, inheritance, and data hiding. If you can''t exploit any of these there is no reason to do OOP.


Having said that, I have yet to program anything of size which I haven''t found a way to utilize these traits to make my code more flexible/clean/etc

Share this post


Link to post
Share on other sites
Guest Anonymous Poster




OOP gives you 3 traits: polymorphism, inheritance, and data hiding. If you can''t exploit any of these there is no reason to do OOP.


Having said that, I have yet to program anything of size which I haven''t found a way to utilize these traits to make my code more flexible/clean/etc

Share this post


Link to post
Share on other sites
It''ll also depend on what you''re comfortable with. If you''re not used to thinking in OO on larger projects and designing systems with this approach, you''ll probably end up making a bit of a hash of it at your first couple of attempts and end up with a poor OO model with lots of inter-class coupling and dependencies and possibly slow code.
If straight C is your strong point, then you can do a pretty good approximation of OO in C by using static functions and variables (encapsulation) and only prototyping the functions you want others to use in your header files. OK, granted you don''t get polymorphism, but writing clearly defined C modules with clearly defined interfaces is still a good thing.

You''ve already spent time developing a codebase, review it, and if you think it will scale with your project, go with it as is for now.

Just my $0.02 ...



Slightly shrimpy smell == unsafe breadbin

Share this post


Link to post
Share on other sites
In terms of pure speed, C code will sometimes be faster, depending on how you handle it. But I find OOP far neater. To make my code "clean", I use OOP. I use basically pure OOP for my latest project Literally zero global functions (besides WndProc and WinMain of course).

One interesting thing people rarely take note of is that nearly everyone prefers the syntax of OpenGL to that of Direct3D, and Direct3D is object-oriented whereas OpenGL is procedural. So the OOP approach is not necessarily the cleanest. Of course, the actual OpenGL code must be messy as hell, I''m sure, but the interface itself is very neat. But with OpenGL, you aren''t exposed to any variables at all, and that''s what makes it clean-looking.

~CGameProgrammer( );

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''ve never coded in straight C. Started with a few years of Smalltalk and going on six years of C++. What are some examples of things that can be coded "cleaner" in C vs. C++ (ignoring the C is faster than C++ argument). Just curious.

Share this post


Link to post
Share on other sites
quote:
Original post by CGameProgrammer
I use basically pure OOP for my latest project Literally zero global functions (besides WndProc and WinMain of course).



I''ve got you beat! My WndProc is a static class function!

As for "to OO or not to OO", it''s really a matter of taste, both methods can accomplish the same ends. The important thing with object design is doing it effectivly. I''ve seen too much "Object-Oriented" code that was nothing but procedural code with the functions defined on classes instead of globally.



"So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable."
- Tool
------------------------------

Share this post


Link to post
Share on other sites
it really depends on the size of the project. if it''s something simple, like just a variation of nehe''s base code (which it isn''t) then C will do. but get anywhere beyond that, and C++ becomes a lot more flexible and powerful... I, and I''m sure many people here, have seen what bad C can do to code... seeing programs where the main cpp is 7000 lines +.

But, obviously, the project is only as good as it''s planning. A well planned C project will always beat a poorly planned C++ project, and the other way around.

I''d say that anything that has potential to get over a total of about 4000 lines should really be written in 100% C++.
a good example, in my opinion, of what a nightmare can come from not doing this, is the Quake2 code.. Yes, the code is fast, and it is obviously well planned, but I just get the feeling that if it was true C++, it would have been half the size, and infinitly easier to read, and given id more time to work on the game.

(just look at how every enemy in Q2 has it''s own unique code, yet, for example, in HL, everything is based from a generic base entity...)

my current app, if it was C, would likly be around 30,000 lines. yet due to the way I''ve built it, it''s around 12,000 at the moment. and everything is independant of each other, so no errors occur with one object effecting another... something that makes debugging A LOT simpler.
In fact, I''d doubt it''d be possible without severly crippling how it works if it were C.

Share this post


Link to post
Share on other sites
just a quick one, as i am in dire need of breakfast

you say you are targetting machines 2 years from now... well, we all know how ridiculously fast things can change in that time with computers You''re looking at about 2 evolutions of hardware (regarding CPU and graphics cards), so its safe to say things will be kickin pretty hard in two years time. Over that time, compiler technology is also likely to improve.

With that in mind, I think its safe to say that you should really focus on clean and well-structured design, and make the move to OO, as it really is a boon for handling complexity. The performance difference between C and C++ is infinitesmally small (compilers are improving)... it all really hinges on the quality of your design... a well designed and implemented OO system will kick seven shades of shit out of a badly written old-school one.

And as many have said before me, you will probably find it much easier to design and manage a large project (such as a game engine) using an OO approach.

Share this post


Link to post
Share on other sites
Thx for all your input guys. I''ve made a decision in what I''m going to do.

Firstly.. im gonna work for another week or so on the engine without changing anything.
Then... i''m going to rewrite it all in OOP.
It should prove to be a fair ol'' challenge, and will hopefully reap some nice consequences, even if it is just cleaner code.

Thx for all the replies. :D

*runs off to code like a maniac*

~Cobra~

Share this post


Link to post
Share on other sites
WayfarerX : You have 0 global variables. What about things like EPSILON and PI...?
Any tips how to get rid of global functions like optimized sin/cos/sqrt.. I realy hate global functions but I don''t know what else to do...

There are more worlds than the one that you hold in your hand...

Share this post


Link to post
Share on other sites
quote:
Original post by _DarkWIng_
WayfarerX : You have 0 global variables. What about things like EPSILON and PI...?


Those arent variables. They are constants.
quote:

Any tips how to get rid of global functions like optimized sin/cos/sqrt.. I realy hate global functions but I don''t know what else to do...


You can make them static methods of a class. In certain languages(C#/Java), you have to do this, since they dont support freestanding functions. In languages like C++ its more a matter of taste. It has no impacts on performance whatsoever.




Once there was a time when all people believed in God and the church ruled. This time is called the Dark Ages.

Share this post


Link to post
Share on other sites