Best Way ? (Directx Related)

Started by
14 comments, last by Luke1 21 years, 1 month ago
I am making classes for my game and i ran into a minor problem. I am making each class a seperate source file(.cpp). My problem is declaring the d3d and d3ddevice variables. How would i declare them so my main.cpp and classes can use them ? (i know this is simple but i havent done this in awhile)
Advertisement
From all I have read so far...I believe you should really try and stay away from OOP in game code...For one reason the overhead and another what you have run into...Remember the basis of OOP is ''encapsulation''...Only let client code see what it needs to...Keep everything else contained privately..
hah... this has nothing to do with OOP...


When you declare a global variable that you want to use between .cpp files, you need to say "extern" with the variable declaration in all files but one.

main.cpp:

int global_int;


blarg.cpp:

extern int global_int;


blarg2.cpp:

extern int global_int;



you can also put the "extern int global_int" in a .h file which all of those files may include.

500*1
Why cant you just pass them via function calling?

For example, in a SkyDome class.

HRESULT SkyDome::CreateSkydome(int radius, int NumDivisions, LPDIRECT3DDEVICE9 Device)
{

}

Or in a terrain class

HRESULT Terrain::CreateLandPatch(int sizeofpatch, int Divisions, LPDIRECT3DDEVICE9 Device)
{
}

OR you could pass them through the constructor.

LIke in your .h file declare a LPDIRECT3DDEVICE9 pDevice;

Then you could override the constructor

MYClass::Myclass(LPDIRECT3DDEVICE9 Device)
{
pDevice=Device;
}


I go for the first alternative, because then I just pass the poionter throughout the game.

I have seen some pass it via the constructor, and I think that is not such a good idea.

Good luck! And if any other questions post here!
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
Thankyou everyone who posted, i appreciate your input and i ended up using extern for my code. Its just easier to do that instead of passing variables everytime i call a function.
umm, okay.

You might run into problems at further develooment of your engine.
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
how so ?
Which is better....

To have your Device well contained, so you know exaclty where it is and what it is doing, or having it globally defined in every source file just waiting to be accessed at any given time?

If you have LPDIRECT3DDEVICE9 as a global variable, in EVERY source, you are just asking for huge problems.

If you have your Device as a public member in your DXWrapper, you are much better off.

Take a look at all the examples in the SDK, or any tutorials written by decent programmers (Andy Pikes tuts in my mind are really good!)



[edited by - RhoneRanger on March 7, 2003 12:01:34 AM]
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
usually you''ll end up with one singleton ''kernel'' in your engine, if you keep the device there, you can have a function like this:

class YourSingletonKernel{   ...   LPDIRECT3D9 D3D;   LPDIRECT3DDEVICE9 Device;   ...public:   inline LPDIRECT3D9 GetD3D() { return D3D; }   inline LPDIRECT3DDEVICE9 GetDevice() { return Device; }}; 


you can also use a static global instance of the singleton to get even less overhead on the call.

static YourSingletonKernel * Kernel = 0;...//in your YourSingletonKernel''s init function (should be if its a singleton)void YourSingletonKernel::Init(){   Kernel = this;   ...} 


then anywhere you need the device, you can go Kernel->GetDevice(). i''m pretty sure it will be inlined, eleminating the function overhead. if it doesn''t get inlined, you can try making the LPDIRECT3DDEVICE9 and LPDIRECT3D9 static.
Ok im trying to organize my classes to have the variables to be called at the function like you suggested but when i did that i got this error:


  --------------------Configuration: TOAB - Win32 Debug--------------------Linking...cEngine.obj : error LNK2001: unresolved external symbol "struct IDirect3D8 * g_pD3D" (?g_pD3D@@3PAUIDirect3D8@@A)cEngine.obj : error LNK2001: unresolved external symbol "struct IDirect3DDevice8 * g_pD3DDevice" (?g_pD3DDevice@@3PAUIDirect3DDevice8@@A)Debug/TOAB.exe : fatal error LNK1120: 2 unresolved externalsError executing link.exe.TOAB.exe - 3 error(s), 0 warning(s)  


Any Ideas ?

This topic is closed to new replies.

Advertisement