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


Code Help!! I must be tired...

Recommended Posts

I''m having troubles with access to certain classes, and unresolved externals. Scene.h + Scene.cpp are my files front end to the common files framework of direct3d (d3dapp.cpp, d3dutil.cpp, etc.) I have that part working fine! Then I created graphix.h and graphix.cpp, with holds functions for locking/unlocking vertex/index buffers. But they don''t have access to d3dapp.cpp and then can resolve m_pd3dDevice. Here are some *snippets* for your further understanding.

#ifndef SCENE_H
#define SCENE_H

#include <stdio.h>
#include <math.h>
#include <d3dx8.h>
#include "Graphix.h"
#include "D3DCommon\D3DApp.h"
#include "D3DCommon\D3DFont.h"
#include "D3DCommon\D3DUtil.h"
#include "D3DCommon\DXUtil.h"
#include "MetaBallCode\MarchingCubes.h"
#include "MetaBallCode\Metaballs.h"

class cScene : public CD3DApplication
	// Font for drawing text

    CD3DFont* m_pFont;
    CD3DFont* m_pFontSmall;

	// Class for Quick Graphics Functions

	cGraphix* m_pGfx;
	// Class for Metaballs Code

	CMetaballs *m_pMetaballs;

	// Scene

	LPDIRECT3DVERTEXBUFFER8 m_pVB;			// Buffer to hold vertices

	IDirect3DTexture8 *m_pTexture;			// Texture Resource


// Create buffers

  hr = m_pGfx->CreateVertexBuffer(MAX_VERTICES, sizeof(SVertex), 
		                          FVF_VERTEX, D3DPOOL_DEFAULT);
  if( FAILED(hr) )
	return E_FAIL;

  hr = m_pGfx->CreateIndexBuffer(MAX_INDICES, D3DPOOL_DEFAULT);
  if( FAILED(hr) )
	return E_FAIL;
The above works fine and those files compile great, but when it comes to graphix.cpp and graphix.h they don''t have access correctly.

#include <d3d8.h>

class cGraphix
	cGraphix(); // Constructor

    static cGraphix *GetInstance();

	HRESULT CreateVertexBuffer(UINT nLenght, UINT nVertexSize, DWORD FVF, D3DPOOL Pool);
	int     LockVertexBuffer(UINT nVertices, BYTE **ppVertices);
	void    UnlockVertexBuffer();

	HRESULT CreateIndexBuffer(UINT nLength, D3DPOOL Pool);
	int     LockIndexBuffer(UINT nIndices, BYTE **ppIndices);
	void    UnlockIndexBuffer();
	void    SetIndexOffset(UINT nOffset);

	IDirect3D8       *GetD3D();
	IDirect3DDevice8 *GetD3DDevice();

#include "graphix.h"

// Initialize static member

cGraphix *cGraphix::m_pGraphics = 0;


	m_pVertexBuffer = 0;
	m_pIndexBuffer  = 0;

	m_pGraphics  = this;


cGraphix *cGraphix::GetInstance()
	return m_pGraphics;

IDirect3D8 *cGraphix::GetD3D()
	return m_pd3d;


IDirect3DDevice8 *cGraphix::GetD3DDevice()
	return m_pd3dDevice;


HRESULT cGraphix::CreateVertexBuffer(UINT nLength, UINT nVertexSize, 
									  DWORD FVF, D3DPOOL Pool)

	hr = m_pd3dDevice->CreateVertexBuffer(nLength*nVertexSize,
                             D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY, FVF,
                             Pool, &m_pVertexBuffer);
	if( FAILED(hr) )
		return E_FAIL;

	// Prepare for rendering with this vertex buffer

	m_pd3dDevice->SetStreamSource(0, m_pVertexBuffer, nVertexSize);
	m_nVertexSize   = nVertexSize;
	m_nUsedVertices = nLength;
	m_nMaxVertices  = nLength;

	return S_OK;
Can someone please tell me what im doing wrong please, I really need the help here. I have tried giving graphix.h access to #include "D3DCommon\D3DApp.h" #include "D3DCommon\D3DFont.h" #include "D3DCommon\D3DUtil.h" #include "D3DCommon\DXUtil.h" and then supplying class cGraphix : public CD3DApplication but that doesn''t work, mind you everything compiles, but the program gives a memory access error, and I think im duplicating the device variables and accessing one and not the true one. Ah so mind boggling! Please help me! -Carandiru

Share this post

Link to post
Share on other sites
Your cGraphix class looks like a singleton. However it is not a singleton. It looks like that at initialisation you create a new cGraphix class. On the second call of the cGraphix class, a second instance of cGraphix is created but the first would not be released and the static member is overwritten with the adress of the second instance !
A correct singleton implementation is:

class cGraphix {
static cGraphix* instance; //from your code exerpt, I do not know if you declared the instance static

cGraphix(void): m_pVertexBuffer(0), m_pIndexBuffer(0);

static cGraphix* GetInstance(void);

cGraphix* cGraphix::instance = 0;

cGraphix* cGraphix::GetInstance(void)
if (instance == 0) //this is what your code seems to lack

instance = new cGraphix();
return instance;

Then the singleton call from your scene object is:

m_pGfx = cGraphix::GetInstance();

Hope that helps.
Ghostly yours,

Edit: put the constructor in the private part of cGraphix class where it should be.

[edited by - Red Ghost on July 16, 2002 3:32:48 AM]

Share this post

Link to post
Share on other sites