Archived

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

CoderTCD

is it effective to use OOP in game programming?

Recommended Posts

I want to use OOP (classes constructors destructors etc) in game programming but I''m anxcious that it could slow down the game. What do you think about it? I don''t see any OOP in game programming examples so I wish to know if anyone''s using it.

Share this post


Link to post
Share on other sites
If done right, it''s no slower than C. Just learn good OOP practices and you''ll be fine.

For small games it doesn''t make much difference at all.

Share this post


Link to post
Share on other sites
The consensus seems to be that the most vital part of a game engine is what happens at the lowest levels (efficiency of parsing for collision detection, primitive submittal to the rendering pipeline, etc.)

Programming in C++ at its worst involves some extra memory references, which aren''t enough in today''s machines to make that much of a difference. You can go overboard with OOP and overdo the necessity for extra memory references, and there are ways to optimize, but on its own it doesn''t make enough of an impact to be a concern.

That''s my 2 cents.

Share this post


Link to post
Share on other sites
Why would using OOP slow down your program? I'm using Classes (well, just one :D ), and it helps that all the code I need have been packed away in a bunch of functions, which have been in turn been packed away in a Class.

Though that said, I am experiencing some slow down probs myself right now - I think it's something to do with how my main loop/getmessage code is done (thread on this coming soon). I cannot believe it's summat to do with my using a class.

Of course I might be wrong.





"Sweetie! Get Mommy's bazooka!"

[edited by - Mister Stewart on September 1, 2002 2:09:52 PM]

Share this post


Link to post
Share on other sites
quote:


Why would using OOP slow down your program?




Generally you will be doing:

CGame *MyGame=new CGame;

to access any of the methods/properties will require the CPU to traverse a pointer to get to the class.

Also

CGame *MyGame=new CDerivedGame;

you also have the overhead of using the VMT (virtual method table) to make use of polymorphism, which is slower as it''s another pointer traversal.

Also

You might come unstuck with multiple inheritance (less of an efficiency problem but one which may slow down your coding if you are unaware of the consequences).

I like OO techniques in game coding. Understanding OO is essential to doing it well. OO does not slow down a program (if it did do you think Microsoft would have based DirectX on COM - a OO technology) by a lot but there IS an overhead (generally n-noticeable on todays machines)

Neil

WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!

Share this post


Link to post
Share on other sites
*g* Perhaps Half-Life with its unstable & ugly engine isn''t the best example to show the benefits of OOP ;-)

_________________________
"Reality is merely an illusion, albeit a very persistent one." (Albert Einstein)
My Homepage

Share this post


Link to post
Share on other sites
Unreal Tournament''s code is highly object oriented. Actually, I believe OOP has been used for the majority of the games released in the last 4 years.

You don''t have to traverse more pointers with C++ than you would do with C. And if you do, there''s a very very very high probability that it wont impact the performance of your program at all.

Regarding virtual methods for polymorphism... It''s a feature like any other, and you choose whether you use it or not. However, if you need a C equivalent for polymorphism you''ll have to write it on your own, and again there''s a very high probability that your code will be less efficient than the C++ compiler''s.

Share this post


Link to post
Share on other sites
Games are ideal for OOP! I wrote one myself with Tile,Unit and GameEvent classes.

''Your theory of a donut shaped universe is intriguing Homer'' - Stephen Hawking

Share this post


Link to post
Share on other sites
quote:
Original post by randomZ
*g* Perhaps Half-Life with its unstable & ugly engine isn''t the best example to show the benefits of OOP ;-)

_________________________
"Reality is merely an illusion, albeit a very persistent one." (Albert Einstein)
My Homepage



huh? HL is an excellent engine even if it''s out-dated, it still has the biggest mod community in the world, it wouldn''t if it was unstable and ugly.

Share this post


Link to post
Share on other sites
Pretty sure that Carmack went all C++ for Q3, and is using for his current projects.



"With my feet upon the ground I lose myself between the sounds and open wide to suck it in, I feel it move across my skin. I''m reaching up and reaching out. I''m reaching for the random or what ever will bewilder me, what ever will bewilder me. And following our will and wind we may just go where no one''s been. We''ll ride the spiral to the end and may just go where no one''s been." - Maynard James Keenan
[TheBlackJester ]
[Wildfire Studios ]

Share this post


Link to post
Share on other sites
quote:
Original post by TheBlackJester
Pretty sure that Carmack went all C++ for Q3, and is using for his current projects.



Quake 3 = pure Classic C.
Doom 3 = everything C++, except the main graphics engine which is still pure Classic C.

Share this post


Link to post
Share on other sites
For what most people use C for, the same speed and codesize will come out from using C++. Most of the C++ standard is about improving coding habits and effeciency.

This thread could quickly become a C VS. C++ debate. I suggest at least looking at the first couple of chapters in Eckel''s "Thinking in C++ book" because it is free, very small, and easy to read. He starts off explaining C++ from a C perspective and I think it will help the original poster make his decision.

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

Share this post


Link to post
Share on other sites
Learn OOP right and it owns classic C without classes (if your project has any kind of size that is). Also if you are working more people on a project itis MUCH easier when using OOP (define interfaces etc. and program out from them..)

Share this post


Link to post
Share on other sites
OOP is just a different style of programming, it won''t affect the speed that much (at least not on today''s machines.) It just depends on your game''s design: if its poor, its performance will decrease and probability of bugs will increase. If you have a good design, then you wouldn''t notice much difference than coding it in straight C.

Share this post


Link to post
Share on other sites
Probably key use of OO techniques is re-useability.
For example in my engine:



TGraphic (Abstract)
|
|
TMesh-----------
| |
| |
TSkinnedMesh TProgressiveMesh



I have a "standard" TGraphic class which acquires a D3D Deice from my D3D class, and contains a VB and an IB for any graphic object. Each new level adds functionality (TSkinnedMesh uses TMeshes load routine and some other code to get skining info, but uses the exact same rendering code).

OO is not the be all and end all, but CAN (and I find IS) useful (especially if you plan on ever re-using any of your old code).

Neil

WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!


[edited by - thedo on September 2, 2002 3:24:36 AM]

[edited by - thedo on September 2, 2002 3:25:06 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by thedo
Probably key use of OO techniques is re-useability.


Sssshh... Sabreman might hear you


"When you know the LORD you have no need for masturbation!"

Share this post


Link to post
Share on other sites