Archived

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

drcharm

Newbie Question on Defining Devices

Recommended Posts

Okay, I hope I can explain this well enough: I''m learning DirectX, and just trying to set up some simple things to take control of a device, put up a black screen... you know the drill, the straightforward stuff. So I got that to work without too much messing around, and I decided to try something a little more practical, putting my various functions (D3DEngine and D3DRender) into different source code files, just to familiarize myself with how such a thing is done. The problem that I''ve run into is with the defining of the devices that I use. In my D3DEngine code, I use the standard: LPDIRECT3D8 g_pD3D; LPDIRECT3DDEVICE8 g_pd3dDevice; to define the necessary things. That code works fine. But when I then try to use my D3DRender code (a separate source file), I run into a catch-22: If I put: LPDIRECT3DDEVICE8 g_pd3dDevice; in my code, it tells me that was already defined in D3DEngine.obj, if I don''t put it in, it tells me that I have an undeclared identifier g_pd3dDevice. I''ve tried various things: putting the two LPDIRECT commands in a header file, and directing both of the .cpp files to include that header, changing the g_pd3dDevice to g_pd3dDevicea (boy the computer didn''t like that one). So I think the answer is pretty basic, but I don''t know what it is. Thanks, Dr. Charm P.S. I suspect that I need a Cleanup function to release both of the above mentioned things, but I don''t know where to put it.

Share this post


Link to post
Share on other sites
everytime your compiler hits the LPDIRECT3DDEVICE g_D3D
line (and that other one) you''re creating an instance of
that object. So your getting multiple instances of the
same object with the same name...

You wanna be really careful how you design your files
because of this. If you really need multiple .cpp files
to be able to use the same global variables you can do
the following

#ifdef MAINFILE
#define EXTERN
#else
#define EXTERN extern
#endif

...

then declare all your global variables as
EXTERN LPDIRECT3DDEVICE g_D3D;

...etc...

now in ONE file (say the file with your WinMain)

#define MAINFILE
#include "global_defs.h"

...

and in every other file that you want to see those
globals just do the standard

#include "global_defs.h"

--Ravenshadow

Share this post


Link to post
Share on other sites
Thanks a lot!! I figured out the extern thing about an 10 minutes ago, but I like the global defining thing... Very satisfying when a program compiles and runs

Thanks again,

Dr. Charm

Share this post


Link to post
Share on other sites