Archived

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

AlienCharm

When to create a class?

Recommended Posts

I''m learning from LaMothe''s Windows Gurus book so I''m new to game programming. It seems most everything is created using structs, but I think he programs mostly towards the "C" way. I was wondering when would you want/need to create a class for something? For something liek pong I''m sure I could create a class for the paddle and the ball having it contain all it''s variables and draw functions and everything else. In the book he has functions that do everything and would just create a struct for the ball and for the paddle.

Share this post


Link to post
Share on other sites
You'r right about the paddle and ball classes for the pong game, but you might want to find some tutorials on OOP (Object Oriented Programming). What you just described is one aspect of OOP, namely encapsulation. OOP is much more: inheritance, encapsulation, data hiding, abstraction, etc.
I assume you know C so it might be a good time to learn a bit about C++ or Java. Maybe Java is better since it's not as complex as C++ and it forces you to work Object Oriented. It also has a friendlier syntax.

Little tutorial about OOP: click.
Very useful site about c++: click.
I'm sure you can find lot's more searching with google or something.

[edit] typo [/edit]


[edited by - Rule on January 6, 2004 10:41:31 AM]

Share this post


Link to post
Share on other sites
Although structs and classes are basicly the same in C++ (the one difference is that struct members default to public, while class members default to private), they are used differently. Structs tend to be used for objects that just have plain member data and little functionality (ie: structs usually have little or no methods). While on the other hand classes are for objects that are lively and that have operations performed on them.

In his ''Tricks'' book, Andre LaMothe programs in plain old C, that''s why he uses structs to hold the data and functions to operate on these structs. And yes, like you have mentioned, in C++ you would just have classes for the ball and paddle, with both the data and functions together in the class.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I personally don''t mind programming things with just C if there is not much complexity involved. When complexity is involved I usually perfer to go the OO way.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
From a very general point of view, you create a class when you feel it''s needed to treat a group of operations and attributes as an object.. This may sound really vague because it really is..

In fact, for a large complicated team project, most of the times is spent on understanding the problem and decide which classes to create/write.. etc.. You don''t actually spend a lot of time implementing comparing to the time that you are sitting down doing requirement analysis and system design (gasp!)

Share this post


Link to post
Share on other sites
OOP is a style of programming.

There is nothing technically wrong with doing everything using just structs and functions that act on those structs. There are still large scale programs being written today which don''t use classes at all. I can''t find it now but I thought I found a modern 4x game written in straight C - maybe the sequl to "Stars!"?

And furthermore, when it comes to Pong there isn''t any real advantage in using classes (other than for educational purposes in teaching yourself OOP), because the project is so small using classes adds complexity rather than removes it.

However, in larger projects OOP can make life much easier, because it allows you to think about classes written by other programmers (or written by yourself at a different time) as black boxes, i.e. you don''t have to worry about the inner working of this other class you call on whilst you work on a different object which does something else.

I''m still a beginner, and am only a couple of months into my first large scale project, so above I''m just saying what I''ve read. However, OOP definitely does seem to make intuitive sense to me - I''m sure it''s been helpful to think in terms of squadrons and units and projectiles and such like (in another game this could alternatively be paddles and balls) rather than in terms of functions and variables all the time.

I don''t know you''re own level of experience, but you''re not really going to learn about how to use classes from a game programming book - if you want to learn an introduction to using OOP in C++, read a good book on it. I thought Accelerated C++ was great, if very technical in style in comparison to the fluffy style of writing you find in Sams books.

Share this post


Link to post
Share on other sites
I know it''s been said many times before, but Object Orientation is a pradigm, a way of thinking and of organizing code. That''s why comments like this on are meaningless:

quote:

I personally don''t mind programming things with just C if there is not much complexity involved. When complexity is involved I usually perfer to go the OO way.



Nothing prevents you from uing an object oriented paradigm in C. Look at the source for Quake2 for an example of what I''m saying. Q2 is object oriented, not procedural. You don''t need built-in support for classes and inheritance to have an object oriented design.

As to the original question, you only need to make a class when it makes sense. C++ prgrammers often complain that Java ''forces you to use classes'' (i.e. everything is a class in Java - no global functions). Yet one of the most common mistakes C++ programmers make is forcing everything into classes!

Using a strategy game as an example, it would make sense to create a class which represents a Unit (archer, swordsman, wizard). Each player will have multiple units, so a unit class will ease unit management. On the other hand, do you need a display class? Most people tend to thing so, so they make a global display object, or a display singleton. I think it''s pointless. A few functions wrapped in a namespace makes more sense. At least I think so.

So the general rule I follow is: if there will be multiple instances, you have a godd candidate for a class or a struct; if there will only be one instance, you don''t need a class.

Share this post


Link to post
Share on other sites
I''ve take''n several classes at school on C++ and Java so I understand the OOP thing. They have us write a lot of them, thats pretty much all we use. Then I start looking at (beginner) game code and all I see is a bunch of structs and no classes so it threw me off. I honestly don''t think I''ve ever written a struct before for any of my classes.

After coming across something like:

typedef struct ALIEN_OBJ_TYP
{
LPDIRECTDRAWSURFACE7 frames[3];
int x,y;
int velocity;
int current_frame;
int counter;
} ALIEN_OBJ, *ALIEN_OBJ_PTR;

I relize it would take much longer to make it into a seperate class then it would to just write it like that.

Share this post


Link to post
Share on other sites