Archived

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

Silex

hate hate hate classes

Recommended Posts

Silex    140
Okay now I have your attention, I have a pretty general design question for my terrain engine. I have my whole shiznit working from within the d3dapp class supplied with the dx sdk. The terrain engine is composed of two classes: a fat one that does general stuff which has an array of little ones that handle the details. Now, both the fat and the little classes need to get their gritty hands on the camera class that''s defined in d3dapp class. That''s what I''m not sure how to do. I started out giving the fat and little classes pointers to the camera that I initialized in their respective constructors, but I''m wondering if there''s a better way to do this. Anyway help would be super sweet, thanks!

Share this post


Link to post
Share on other sites
paul8262    122
I''m not completely sure i understand you properly.

I don''t use the D3D framework app structure,but played with doing things with it in the beginning.

If you give me some more details on what problems your having,and the way youv''e written it(just give me an idea in pseudo)i''ll try to help out if i can.

Paul.

Share this post


Link to post
Share on other sites
Silex    140
Yeah that wasn't all that clear I must admit. Basically, my problem is with variable scope. You don't really need to know anything about the d3dapp since its just a wrapper for API calls and windows stuff. Anyway, in this d3dapp I have my terrain class that I call to render that to the screen; however, it needs access to a camera class that's also declared in the wrapper class. That alone would be trivial, but the terrain class uses another class that also needs this information from the wrapper, and this other class is declared in the terrain class.

Lemme try pseudo that...


class wrapper
{
class camera;
class terrain;
};

class terrain
{
class helper;
};


...so both terrain and helper need easy access to helper. And so my problem specifically is I'm trying to let all terrain and helper member functions have access to camera while avoiding pointer dereferencing hell because I need to access it often and therefore efficiently.

[edited by - Silex on January 24, 2004 11:44:16 PM]

Share this post


Link to post
Share on other sites
paul8262    122
I'm still not totaly positive of the advice i should give.
(it's just difficult without seeing it in front of me).
maybe its just me

In my engine,my main header file(most stuff is accessable)i do something like:

#include windows.h>
#include DirectX stuff...util,file,font,Input,DPlay..etc
#include <Python stuff here...>
#include <tokamak physics here..>

#include Language stuff here...
#include VectorMath.h ...
#include Timer.h...
#include windows.h...
#include Geomitry.h...
#include BSPLoader.h...
#include DX9FX.h...
#include WorldCollision.h...
#include triggerSys...
#include Network.h..
#include Lightwave.h..
#include 3dsMax.h...
#include ASM.h...
#include Console.h...
#include WeaponAct.h...

etc etc....

Loads more specific functions and stuff here..

a class that is derived from..

class Win32Application
{
etc...
};

I make my D3DDevice extern(as is a lot of stuff)
and in the main implementation file,something similar:

Timer _Timer;
3DSMax _3DSMax;
DInput _DIput;
BSPLoader _BSPLoader;
Geomitry _Geomitry;
ASM _ASM;

etc etc...

You could probably do something like this with what youv'e done already,using the D3DFramework app,let me know this isn't clear to you,what i mean,and i'll try to explain it better tommorow(i'm a bit tired now).

Paul.


[edited by - paul8262 on January 24, 2004 12:01:56 AM]

Share this post


Link to post
Share on other sites
Raptor85    180
well, since you''ve already committed to using the sample framework i guess it being a little more cluttered couldnt hurt...

just store a pointer to your camera class in whatever classes you need it in. if you are worried about speed you shouldnt use the sample framework.

___________________
-Nicholas Anton
-raptor85.siigna.net

Share this post


Link to post
Share on other sites
marijnh    182
I have actually been running into similar problems - there are a few ''environment'' classes that I need to have access to from a whole lot of other classes. An example would be the enemies in any kind of action game - these are objects that need to have access to their environment to determine their behaviour. I could just give each and every one of them some pointers - one to the object that contains all other characters in the game, and one to the game map, and maybe some more. But that seems a little ugly to me. I am also hestitant to have global pointers or instances of these ''environment'' classes because that would cause problems with their lifetime.

Share this post


Link to post
Share on other sites
rileyriley    235
I am also running into a similar problem of sligtly smaller magnitude - I''ve separated my game code and my graphics code.. but guess what, my game code would like to use the graphics code occasionally! I think I''m going to settle for making an extra pointer to the graphics system as well. I''ve made the graphics a reference-counted system anyway, might as well have more than one pointer to it I guess

Share this post


Link to post
Share on other sites
AndyTX    806
You can use persistant pointers in each class (although you have to be careful with this, because if you ever change what they are pointing to (ie. reallocate)... lots of trouble!

You can also pass in the pointers when needed. You can bundle a lot of this stuff up in a structure or class of "common" pointers/game objects and pass that to routines when needed. No real performance hit to this unless in an extremely tight loop (where you''d use another method or inline functions).

Or, you can use a singleton class for the "common" structure, or all of the objects that you need. This one works pretty well for game resources (images, etc.) but no so well for some other things.

Usually if you think about it, and put some away-from-computer time into designing your class heirarchy, the "best" way of approaching the "data sharing" in an explicit "data-hiding" architecture (ie. modular object orientation) becomes more obvious.

Share this post


Link to post
Share on other sites
Trienco    2555
i guess its a common problem. take a console in a typical game. the game needs the console to ouput stuff (logs, errors), at the same time the console might want to "use" the game (change resolution, turn statistics on/off). so either your console would store commands in some way the game understands and the game would have to check if theres a command waiting or the console already stores some function pointers and calls them itself. or you implement a whole messaging system so every module can send messages to other modules.

btw. for my terrain i was passing a reference to the camera. the terrain only needed it for culling, so i didnt want to force someone to call a cull methode and pass the camera as argument all the time. if you have even more functions that need camera info (setting up shaders or whatever) it might be better than a bunch of functions with a whole lot of parameters. at the same you risk that the pointer will become invalid in the meantime (its less easy to forget about it, if you need it as a parameter all the time).

that seems like a situation without a perfect or "clean" solution.. except maybe changing the whole design to something that avoids this kind of situation.

Share this post


Link to post
Share on other sites
VladR    722
quote:
that seems like a situation without a perfect or "clean" solution.. except maybe changing the whole design to something that avoids this kind of situation.
Exactly ! It`s a design problem. Therefore you need to redesign it. It sucks, I know. I`ve spent last 4 days redesigning the classes in my engine so that I don`t have to supply 10 parameters(pointers) to each function. It`s just design fault. But believe me, you have to clean it, because sooner or later, your class .cpp file shall have about 5000 lines and compiling it each time shall cause you a grief, so you`ll have to clean it.
And you know what ? It`s much easier to read such clean code now, bugs are easier to find, because they are clearly in separate files, not in one file (as is the case when you have 2 classes in one header/cpp file).


VladR
Avenger 3D game

Share this post


Link to post
Share on other sites
Raptor85    180
yes, to get a clean solution you should start over and take a few months to design. If you run into this problem, you have to rethink your class layout and find another way (usually a much harder way) to accomplish the same task.

___________________
-Nicholas Anton
-raptor85.siigna.net

Share this post


Link to post
Share on other sites
paul8262    122
Hey there Sunray,
Thanks for taking an interest in my compile times

Luckily,i''m mostly playing with scripting and putting together the gameplay of my game now.

I gave up reading articles and books on the best way to do something a while ago,and tried it my own way,and have ended up with an engine with everything i need(for now at least)to be able to make my game move forward.

Note,that i said ''something like this'',not my engine is exactly like this.It may not be the best way,but all the features i needed or was missing from other engines i searched for to make my game,my engine has those features,and they work well.

Level editor,Facial animation,realtime self-shadowing shadows,trigger system,collision detection,fully embedded python scripting,physics,decent fog,lighting,weather sytem,many formats for actors/audio,video as texture,and much more that i wanted in an engine that i ahd bought.Now i don''t neeed to pay a big fee to use one,because i have my own

So i''m happy
I think if i didn''t do it my own way,i''d still be reading those articles and books,without anything to show for it still,and be spending too much time hovering around forums on the net,i only do that now mostly when i''m bored

Hope you work out your problem with classes.

Paul.

Share this post


Link to post
Share on other sites