Archived

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

Tszafran

Game Engine Layout

Recommended Posts

Hi, Im working on a game engine and have always read about making it into smaller classes to handle doing stuff. So I want to create a couple classes, one for Graphics, one for Sound, one for Input, and some other smaller ones like Log files, etc... I want to put all this classes together into one big one called Engine. The problem is if I make all those classes (sound, graphics, etc...) and make a new class called CEngine and make a instance of the sounds, graphics, etc... classes as members of CEngine those classes cant interact with eachother. How would I get around this? I want CGraphics to get information from CSound but they dont interact without alot of errors. What do I do?

Share this post


Link to post
Share on other sites
If I understand your problem correctly, you have trouble when one of your classes needs to access the private members of another class. If this is absolutely necessary, you can have your classes declare each other as friends:



class CGraphics
{
// blah
friend CSound;
// blah
};



This code allows CSound to access CGraphics'' private data.




Things are not what they are.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
While making them friends would work, gateway functions (I think thats what thier called) would be better. Something like:

int getX();
void setX( int newX );

this allows you to keep abstraction. If you want to change the way one of the objects work latter on and all the other objects are accessing its private data, you could be in a lot of trouble. This way so long as you leave the interface for your gateway functions as they are, the implementation of the object can be changed latter on, and you won''t get screwed.

Share this post


Link to post
Share on other sites
Both are interesting ideas, but I want all the classes to be able to interact with eachother (CGraphics, CSound, CInput, CLog, CMap). If I do Anonymous Poster suggestion the CGraphics class doesnt know what CMouse is,

say member function..

void CGraphics::DrawCursor(void)
{
CGraphics::Blit(Mouse_Surface,CInput.GetMouseX, CInput.GetMouseY);
}


Doesnt work.

Share this post


Link to post
Share on other sites
Having your graphics module talk to your sound module is a bad thing. Graphics should just do graphics. Sound should just do sound. When you couple them together like that, you make code that''s harder to maintain and harder to extend.

Your engine class should take care of the cross module interaction, keeping your modules cleanly separated.

Take care,
Bill

Share this post


Link to post
Share on other sites
It's true: if your classes all have to interact with each other, it is often a sign of bad design. I have used the "one class for graphics, one class for sound" approach before, and found that a clean design lets you avoid having your classes messing with each other's data.

That said, if you follow Anonymous Poster's technique,



void CGraphics::DrawCursor(void)
{
CGraphics::Blit(Mouse_Surface,CInput.GetMouseX(), CInput.GetMouseY());
}



should definitely work. Is there any particular error that it gives you?




Things are not what they are.


[edited by - myme15 on May 4, 2002 6:09:36 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just in case you dont understand the other anon poster
int X would be private and setX and getX public.

Share this post


Link to post
Share on other sites