Skip to main content
gamedev.net
🔒 Locked

Getting a struct from a singleton.

Started by DesCr Jun 26, 2004 at 5:29 PM 2 replies 950+ views
Original Post
DesCr
DesCr
This problem is beginning to frustrate me. I've been trying to make my engine more OO, so lots of things are becoming objects. Direct3DObject is a singleton, and lxRender is a class that's dependant on objects from Direct3DObject. In lxRender I create a method D3D which is a pointer to Direct3DObject's only instance.

class Direct3DObject
{
	static Direct3DObject m_Direct3DObject;

  protected:
	Direct3DObject();

  public:
	static Direct3DObject* GetDirect3DObject();

	
	~Direct3DObject();

	IDirect3D9*				g_pD3D;			// Direct3D interface
	IDirect3DDevice9*		g_pD3DDevice;	// Graphics adapter.
	IDirect3DVertexBuffer9*	g_pVB;			// Buffer to hold verticies.

	// A square vertex.
	struct SQUAREVERTEX
	{
		float x, y, z, rhw; // The transformed position for the vertex.
		D3DCOLOR color;     // The vertex color (32 bit AARRGGBB).
		float u, v;			// The texture coordinates of the vertex.
	};

	// Our custom FVF, which describes our custom vertex structure
	const DWORD D3DFVF_CUSTOMVERTEX;

	HRESULT D3DInit(HWND hWnd);
	HRESULT D3DInitVB();
	IDirect3DTexture9 *LoadTexture(char *fileName);

};

class lxRenderer
{
	Direct3DObject*		m_pD3D;

  public:

	lxRenderer();
	void BeginScene();
};

// Implementation
lxRenderer::lxRenderer() :
m_pD3D(Direct3DObject::GetDirect3DObject())
{
}

void lxRenderer::BeginScene()
{
	

	if( NULL == m_pD3D->g_pD3DDevice)
		return;

    // Clear the backbuffer
    m_pD3D->g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
    
    // Begin the scene
    if( SUCCEEDED( m_pD3D->g_pD3DDevice->BeginScene()))
    {
		// Draw the triangles in the vertex buffer. This is broken into a few
		// steps. We are passing the vertices down a "stream", so first we need
		// to specify the source of that stream, which is our vertex buffer. Then	
		// we need to let D3D know what vertex shader to use. Full, custom vertex
		// shaders are an advanced topic, but in most cases the vertex shader is
		// just the FVF, so that D3D knows what type of vertices we are dealing
		// with. Finally, we call DrawPrimitive() which does the actual rendering
		// of our geometry (in this case, just one triangle).
		m_pD3D->g_pD3DDevice->SetStreamSource( 0, m_pD3D->g_pVB, 0, sizeof(m_pD3D->SQUAREVERTEX) );
		m_pD3D->g_pD3DDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
		m_pD3D->g_pD3DDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 1 );

		// Set the display mode to whatever the desktop settings are.
		D3DDISPLAYMODE d3ddm;
		m_pD3D->g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
	}
}
I'm getting an error from (the second box):
m_pD3D->g_pD3DDevice->SetStreamSource( 0, m_pD3D->g_pVB, 0, sizeof(m_pD3D->SQUAREVERTEX) );
I'm getting this error pointing to that line:
error C2273: 'function-style cast' : illegal as right side of '->' operator
And I know it has to do with the SQUAREVERTEX, I just don't know what I'm not understanding >:(
wild_pointer
wild_pointer
Try

m_pD3D->g_pD3DDevice->SetStreamSource( 0, m_pD3D->g_pVB, 0, sizeof(m_pD3D::SQUAREVERTEX) );


SQUAREVERTEX isn't an actual member so you cant use pointer to member operator. Instead, you use the scope resolution operator to get access to the nested class.
[size=2]
DesCr
DesCr
(proceeds to slap himself)

I feel so stupid, but thanks for the help! Anyways, one correction I should make on your fix (in case anyone else reading this is having similar problems)...

m_pD3D->g_pD3DDevice->SetStreamSource(0, m_pD3D->g_pVB, 0, sizeof(Direct3DObject::SQUAREVERTEX));


That's what worked. When using m_pD3D::SQUAREVERTEX, it couldn't resolve m_pD3D as a namespace.
wild_pointer
wild_pointer
Quote:
Original post by DesCr
(proceeds to slap himself)

I feel so stupid, but thanks for the help! Anyways, one correction I should make on your fix (in case anyone else reading this is having similar problems)...

m_pD3D->g_pD3DDevice->SetStreamSource(0, m_pD3D->g_pVB, 0, sizeof(Direct3DObject::SQUAREVERTEX));


That's what worked. When using m_pD3D::SQUAREVERTEX, it couldn't resolve m_pD3D as a namespace.


Whoops.
[size=2]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.