Jump to content
  • Advertisement
Sign in to follow this  
hypno_

C++ classes accessing each other

This topic is 4774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a question regarding code design in C++. Let's assume I want to code a small tile-based game and thus I create a class CTile for tile handling (loading, saving, displaying) and one class CGfx to wrap my graphics stuff (initializing video mode, creating a surface to draw on (SDL)). Now the method for displaying a tile on the screen would look like this: void CTile::drawTile( int tilenr, int x, int y, SDL_Surface * targetSurface ); And an actual call in the game code would then look like drawTile( 2, 100, 150, Gfx.getScreen() ); But what if I want to have this method like this as I always want to draw to the screen? void CTile::drawTile( int tilenr, int x, int y ); Because CTile doesn't know about the existing surface in CGfx.. Do I have to write a class (let's say CGame) that incorporates an instance of the other two classes and put a method in CGame that automatically draws a tile on the surface that is defined in CGfx? I know this would work, but I think it's a bit awkward. There has to be a more elegant way - that I obviously don't know about yet. Or is the first definition of the drawTile method the "proper" way to do it?

Share this post


Link to post
Share on other sites
Advertisement
You could have a SDL_Surface pointer as a member of CTile, and in an initialization method set that pointer as the surface you want to.


void CTile::Init(SDL_Surface *targetSurface)
{
m_targetSurface = targetSurface;
}


Then you could just use m_targetSurface instead of passing a pointer.

Share this post


Link to post
Share on other sites
Are CTiles graphical components or are they logical components? Ie. do they just represent graphical tiles that are drawn or do they contain information related to the game logic?

If they're graphical elements, then you probably should have a class for the graphical engine. Assuming CGfx is that class, then CTiles belong to the CGfx object. So either you place the drawTile() function in CGfx, make a pointer to CGfx in CTile or just pass the surface as a parameter to drawTile().

If CTiles are logical components, then you should have a function for drawing tiles in CGfx that queries the relevant information needed for drawing tiles from the CTiles class.

Alternatively you could have two tile classes, one for representing the logical tile and the other for the graphical tile.

Having both in the same class isn't probably a good idea, unless your game is going to be very simple.


Just noticed that you pass coordinates and tilenr (what ever that is) to drawTile(). I think you should have those stored in the tile object.

Share this post


Link to post
Share on other sites
sigh, this rabid OO stuff is killing me.

Ok here is what I did. I have a graphics namespace, and it has the following function:

void Draw(IDirect3DTexture9* image, int x, int y, unsigned int color);

you can drop the color, my program needs it.

Then in my Territory (I am making a RISK like game) I simply call the draw function. That is all that there is to it. Setting up 10 levels of indirection that only serve to confuse you is not good programming practice. When you only have one screen and never expect more, then that screen is fundamentally a global resource. It is a global conceptually. Your code should match the concept, and so code that does not treat it as a global resource is denying the fundamental truth of your program, and thus inferior. Having hundreds of globals is kind of messy and so I use namespaces to help organize.

In summary: use namespaces rather than objects.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tera_Dragon
You could have a SDL_Surface pointer as a member of CTile, and in an initialization method set that pointer as the surface you want to.


Think I'll stick with that, thanks Tera_Dragon.

FlowingOoze: You made some good points there imho, I am indeed contemplating to put the tile drawing function into the CGfx class. Originally I wanted the tile class to incorporate graphics and logic.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!