Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

How do I get 2 class's to talk to each other with Direct3D objects?

This topic is 3143 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 am atempting to make 2 c++ class's and I need data from one into another. I have class A
class Draw
{
private:
	LPDIRECT3D9 d3d;
	LPDIRECT3DDEVICE9 d3ddev;
	LPDIRECT3DSURFACE9 backbuffer;
	LPD3DXSPRITE spriteobj;
public:
	Draw(HWND hwnd,int width,int height,bool fullscreen)
	{
		d3d = NULL;
		d3ddev = NULL;
		backbuffer = NULL;
		spriteobj = NULL;
		Direct3D_Init(hwnd,width,height,fullscreen);
	}

	bool Direct3D_Init(HWND,int,int,bool);
	void Direct3D_Shutdown();
	LPDIRECT3DSURFACE9 LoadSurface(string filename);
    void DrawSurface(LPDIRECT3DSURFACE9 dest, float x, float y, LPDIRECT3DSURFACE9 source);
    LPDIRECT3DTEXTURE9 LoadTexture(string filename, D3DCOLOR transcolor = D3DCOLOR_XRGB(0,0,0));

};

that needs to send data to class B
class Font
{

private:
	//font variables
LPD3DXFONT font;
LPDIRECT3DDEVICE9 *d3ddev;
LPD3DXSPRITE *spriteobj;

public:

	Font(string name, int size)
	{
		MakeFont(name,size);
	}
	//font functions
    LPD3DXFONT MakeFont(string name, int size);
    void FontPrint( int x, int y, string text, D3DCOLOR color = D3DCOLOR_XRGB(255,255,255));
	void getInfo(LPDIRECT3DDEVICE9 *d3ddev, LPD3DXSPRITE *spriteobj);
};

the data I need is the LPDIRECT3DDEVICE9 and LPD3DXSPRITE passed from a to b and also should I call a pointer or by reference? I am not sure I need to change it or just need the info of it Here are the class functions that need the data
LPD3DXFONT Font::MakeFont(string name, int size)
{
    LPD3DXFONT font = NULL;

    D3DXFONT_DESC desc = {
        size,                   //height
        0,                      //width
        0,                      //weight
        0,                      //miplevels
        false,                  //italic
        DEFAULT_CHARSET,        //charset
        OUT_TT_PRECIS,          //output precision
        CLIP_DEFAULT_PRECIS,    //quality
        DEFAULT_PITCH,          //pitch and family
        ""                      //font name
    };

    strcpy(desc.FaceName, name.c_str());

    D3DXCreateFontIndirect(d3ddev, &desc, &font);

    return font;
}

void Font::FontPrint(LPD3DXFONT font, int x, int y, string text, D3DCOLOR color)
{
    //figure out the text boundary
    RECT rect = { x, y, 0, 0 };
    font->DrawText( NULL, text.c_str(), text.length(), &rect, DT_CALCRECT, color); 

    //print the text
    font->DrawText(spriteobj, text.c_str(), text.length(), &rect, DT_LEFT, color); 
}

Share this post


Link to post
Share on other sites
Advertisement
I would either make a CreateFont() function in the Draw class (Which will create a new Font and set the sprite pointer), or make the Draw class a singleton or globally accessible. The former is much more preferable...

Share this post


Link to post
Share on other sites
Or you put a pointer to the device (Or any other thing you need) into the parameter list. When you use DirectX functions in general, the most want a device pointer from you... So, why not doing this either?

Share this post


Link to post
Share on other sites
Since your project will use the DX Font stuff, why not make your DirectX rendering class do *all* work which require the d3d device, and your font class only generate data for it?

Write a font drawing function that accepts coordinates, a string, and maybe a style.
Then have your font class generate that data.

That way you have all the functionality your Font class wants, and on top of it all, you can then use the new drawing function for any object that wants font output.

That way you don't have to implement it for every object that needs one.

Share this post


Link to post
Share on other sites
Yes, thats also a solution, but not a good one. For a small and tiny game it may works good, but whats when you have a class for drawing the HUD? How should this draw text then? And putting the HUD-class onto the general Draw-Class is also a bad idea. That would make becoming it too big and unreadable. The common solution would look like this:

(Typed right of my head)

class Font
{
public:
//Do initialization here
HRESULT InitFont(LPDIRECT3DDEVICE9 Device,Foo foo foo....);

//Render here
HRESULT RenderFont(D3DXVECTOR2 Position, blah blah);

//Needed variables
LPD3DXFONT font;
LPDIRECT3DDEVICE9* d3ddev;
LPD3DXSPRITE* spriteobj;

};



and to use it, you would have to do this:


//Class-decleration:
Font HudFont;
...

//Somewhere else
void MyGame::InitGame()
{
HudFont.InitFont(d3ddev,blah blah);
}

void MyGame::RenderGame()
{
SomeObject->RenderMe(&HudFont); //Render some object which needs access to the font
}


Share this post


Link to post
Share on other sites
thanks for all the help.

What is the syntax to send the data from draw to font class or do I need to get the data.

What I am trying to say is do I use the font class to get the data or do I use the drawclass to push the data?

Share this post


Link to post
Share on other sites
Just look at my examples. I show both ways there:

-How to use your Font-Class inside another class (&Font <= Pointer)
-How to pass the device to the font class

When you are inside some class and you need it, the function which could use it, could look like this:


void SomeClass::RenderMe(Font* HudFont, .....)
{
HudFont->DoSomething();
}



So, where is the problem?

Oh, and a tipp: I've set up a general struct which holds pointer to some very important things in my engine. Its easier to use such a thing than pass hundreds of parameters to a function. Every object gets the pointer to that struct and is able to access the things it holds. Anyways, you don't need such a struct. Not, when you don't have a large Actor-system or many classes which need many DX things.

Put some parameters in you function and it should be nice :)

Share this post


Link to post
Share on other sites
I have tryed a few things now and nothing works.

in the font class I am trying to have the pointer point to the device that the draw function returns

same goes with the sprite function

This is how I am trying to do it

draw class

LPDIRECT3DDEVICE9 get_device()
{return d3ddev;}

LPD3DXSPRITE get_sprObj()
{ return spriteobj;}



font class


Font()
{
*d3ddev = dd3.get_device();
*spriteobj = dd3.get_sprObj();
}


Share this post


Link to post
Share on other sites
Yes, this is right. But one thing:


LPDIRECT3DDEVICE9 *d3ddev;
LPD3DXSPRITE *spriteobj;

....

Font()
{
*d3ddev = dd3.get_device();
*spriteobj = dd3.get_sprObj();
}



Why *? The LPDIRECT3DDEVICE9 is already a pointer. Everything LP... with an uppercased name is a pointer. You don't need a pointer to a pointer here.

Share this post


Link to post
Share on other sites
This part of the code is giving me issues now


void Font::FontPrint( int x, int y, string text, D3DCOLOR color)
{
//figure out the text boundary
RECT rect = { x, y, 0, 0 };
font->DrawText( NULL, text.c_str(), text.length(), &rect, DT_CALCRECT, color);

//print the text
font->DrawText(spriteobj, text.c_str(), text.length(), &rect, DT_LEFT, color);
}



I call the above from here


void Game_Run(HWND window)
{
if(NewGame.Start())
{

normal.FontPrint(100,300,"Endgame");

NewGame.End();
}
}



its saying that the
font->DrawText( NULL, text.c_str(), text.length(), &rect, DT_CALCRECT, color);

has

Unhandled exception at 0x000e2935 in class_Test.exe: 0xC0000005: Access violation reading location 0x00000000.

I am not sure what this means?

I started everything properly I believe but it still not getting the data passed like it should

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!