Classes declared as pointers, causing problem

Started by
4 comments, last by johnnyBravo 20 years, 4 months ago
In the directsound playsound example in directx 9 sdk, they have a class called CSoundManager that is called like this: CSoundManager * g_pSoundManager; g_pSoundManager = new CSoundManager(); g_pSoundManager->Initialize( hWnd ) ; Ive been messing around with the sample code and making it much smaller. The problem is i dont want to declare the CSoundManager as a pointer. But as such: CSoundManager g_pSoundManager; g_pSoundManager.Initialize( hWnd ) ; But when I do this directsound will not initialise. Heres my class code:

class CSoundManager
{
public:
	static LPDIRECTSOUND8 m_pDS;
	CSoundManager()
	{
		m_pDS = NULL;
	}

	~CSoundManager()
	{
		if(m_pDS)
		{ 
			(m_pDS)->Release(); 
			(m_pDS)=NULL;
		}
	}

	void Initialize( HWND  hWnd   )
	{
		DirectSoundCreate8( NULL, &m_pDS, NULL );
		m_pDS->SetCooperativeLevel( hWnd, DSSCL_PRIORITY );
		LPDIRECTSOUNDBUFFER pDSBPrimary = NULL;

		DSBUFFERDESC dsbd;
		ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
		dsbd.dwSize        = sizeof(DSBUFFERDESC);
		dsbd.dwFlags       = DSBCAPS_PRIMARYBUFFER;
		dsbd.dwBufferBytes = 0;
		dsbd.lpwfxFormat   = NULL;
		m_pDS->CreateSoundBuffer( &dsbd, &pDSBPrimary, NULL ) ;
		
		WAVEFORMATEX wfx;
		ZeroMemory( &wfx, sizeof(WAVEFORMATEX) ); 
		wfx.wFormatTag      = (WORD) WAVE_FORMAT_PCM; 
		wfx.nChannels       =  2; 
		wfx.nSamplesPerSec  = 22050; 
		wfx.wBitsPerSample  = (WORD) 16; 
		wfx.nBlockAlign     = (WORD) (wfx.wBitsPerSample / 8 * wfx.nChannels);
		wfx.nAvgBytesPerSec = (DWORD) (wfx.nSamplesPerSec * wfx.nBlockAlign);

		pDSBPrimary->SetFormat(&wfx);
		
		if(pDSBPrimary)
		{ 
			(pDSBPrimary)->Release(); 
			(pDSBPrimary)=NULL;
		}	
	}
};
LPDIRECTSOUND8 CSoundManager::m_pDS=NULL;
thanks [edited by - johnnyBravo on November 26, 2003 11:12:38 PM]
Advertisement
Three things:

1) It shouldn''t make a difference.
2) What do you mean by ''directsound will not initialise''?
2) Why don''t you want to use pointers?
well if i change the class declaration from not using pointers , the speakers click and nothing plays, as ive got another class that loads and plays sounds

i don''t want to use pointers as i have a wrapper that i want the declaration of every class consistent. As i dont use pointers in the declartion of any of my classes.
It is probably a scope problem. I''m guessing your class that you instantiate goes out of scope and thus deletes the interfaces that are actually playing the sounds. (Note: Never actually done anything in DirectSound).
Try creating the instance globally (a global variable of CSoundManager) and then playing it, does anything happen?
-----------------------------Final Frontier Trader
wow it works, thankyou!

You are welcome.
-----------------------------Final Frontier Trader

This topic is closed to new replies.

Advertisement