Archived

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

Luke1

Best Way ? (Directx Related)

Recommended Posts

Luke1    122
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)

Share this post


Link to post
Share on other sites
taZman    122
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..

Share this post


Link to post
Share on other sites
Nypyren    12074
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

Share this post


Link to post
Share on other sites
RhoneRanger    100
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!

Share this post


Link to post
Share on other sites
Luke1    122
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.

Share this post


Link to post
Share on other sites
RhoneRanger    100
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]

Share this post


Link to post
Share on other sites
billybob    134
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.

Share this post


Link to post
Share on other sites
Luke1    122
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 externals
Error executing link.exe.

TOAB.exe - 3 error(s), 0 warning(s)


Any Ideas ?

Share this post


Link to post
Share on other sites
RhoneRanger    100
I dont understand totally how you are calling your funcitons

can you give me an example of a function that is having problems?

If you have lets say a DirectX class where you do all your setup etc, and in that class you have

public:
LPDIRECT3DDEVCICE8 Device;

And another class say SkyDome and you have a function calles

InitDome(int size, LPDIRECT3DDEVICE8 Device)
{
Device->CreatVertexBuffer();
...
}
You should have no problems passing the device to that function.

[edited by - RhoneRanger on March 8, 2003 10:58:35 AM]

Share this post


Link to post
Share on other sites
Luke1    122
Ok ive managed to get it working and this is what ive come up with(*note it works), feel free to tell me how i can improve it

*this is my actual code but its the same layout:
cEng.h

      
class cEng
{
public:
LPDIRECT3D8 D3D;
LPDIRECT3DDEVICE8 D3DDevice;

void cEng::SetVars(LPDIRECT3D8 g_D3D, LPDIRECT3DDEVICE8 g_D3DDevice)

HRESULT cEng::StartD3D(HWND Hwnd)

};

cEng.cpp

  
void cEng::SetVars(LPDIRECT3D8 g_D3D, LPDIRECT3DDEVICE8 g_D3DDevice)
{
D3D = g_D3D;
D3DDevice = g_D3DDevice;
}

HRESULT cEng::StartD3D(HWND Hwnd)
{
...
CreateDevice(...,...,D3DDevice)
...
}




[edited by - Luke1 on March 8, 2003 11:14:42 AM]

[edited by - Luke1 on March 8, 2003 11:15:30 AM]

Share this post


Link to post
Share on other sites
RhoneRanger    100
OKAY

I would make D3D private and Device public.

Look at this line

void cEng::SetVars(LPDIRECT3D8 g_D3D, LPDIRECT3DDEVICE8 g_D3DDevice)

since g_D3D and g_D3DDevice are already class members, you dont pass them via functions.

Here is how I would handle things based on your class
cEng.h

      

class cEng
{
private:
LPDIRECT3D8 D3D;

public:
cEng();
~cEng();

LPDIRECT3DDEVICE8 D3DDevice;
HRESULT StartD3D(HWND Hwnd);

};




EDIT: You should learn about classes before you try to do this!

[edited by - RhoneRanger on March 8, 2003 11:24:25 AM]

Share this post


Link to post
Share on other sites