Archived

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

DirectSound Failing

This topic is 5266 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

Ive read a book and some tutorials on DIrectSound with DirectX8 and i was trying to create my own class for it...everything seems right from what i see in this book but it fails when i call the QueryInterface function the main
#include <windows.h>									
#include <stdio.h>	
#include <dinput.h>	
#include "InputCore.h"
#include "SoundCore.h"			

//Global

char *g_WindowClass = "Game";
char *g_WindowName = "Game";

HWND g_hWnd;
cInput KeyboardInput, MouseInput;
cSound Sound;

#define KEYDOWN(name, key) (name[key] & 0x80)
#define MouseButtonState(x) ((MouseInput.MouseState.rgbButtons[x] & 0x80) ? true : false)

//Main Prototypes

HWND CreateNewWindow(HINSTANCE, char *, char *, int, int);
bool RegisterWindow(HINSTANCE, char *);
bool UnRegisterWindow(HINSTANCE, char *);
bool Init();
bool PreFrame();
bool PerFrame();
bool PostFrame();

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)			
{
    MSG msg;

	//Regeister New Window

	RegisterWindow(hInst, g_WindowClass);

	//Create New Window

	CreateNewWindow(hInst, g_WindowClass, g_WindowName, 800, 600);

	DirectInput8Create(hInst,DIRECTINPUT_VERSION, IID_IDirectInput8,(void **) &KeyboardInput.g_DI8, NULL);
	DirectInput8Create(hInst,DIRECTINPUT_VERSION, IID_IDirectInput8,(void **) &MouseInput.g_DI8, NULL);

	KeyboardInput.g_DID8 = KeyboardInput.KeyboardInit(g_hWnd, KeyboardInput.g_DI8);
	MouseInput.g_DID8 = MouseInput.MouseInit(g_hWnd, MouseInput.g_DI8);

	if(Sound.SoundInit(g_hWnd) == false)
		MessageBox(g_hWnd, "Test", "Test", MB_OK);

	if(Sound.SoundCreate() == false)
		MessageBox(g_hWnd, "Test", "Test", MB_OK);

	Sound.g_Sound->Release();

	while(msg.message != WM_QUIT)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))	
		{													
			TranslateMessage (&msg);				
			DispatchMessage (&msg);				
		}

		KeyboardInput.ReadDevice((void*)KeyboardInput.KeyState, 256);
		MouseInput.ReadDevice((void*)&MouseInput.MouseState, sizeof(DIMOUSESTATE));
	}
	
	UnRegisterWindow(hInst, g_WindowClass);

	return msg.wParam;					 
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    switch (iMsg)							
    {
	case WM_DESTROY:
		PostQuitMessage(0);				
		break;									
	}													

	return DefWindowProc (hwnd, iMsg, wParam, lParam);
}

//Create a New Widnow

HWND CreateNewWindow(HINSTANCE hInst,char *WindowClass, char *WindowClassName, int WindowWidth, int WindowHeight)
{
	HWND hWnd;

	 hWnd = CreateWindow (WindowClass, WindowClassName, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,	
						 CW_USEDEFAULT, WindowWidth, WindowHeight, NULL, NULL, hInst, NULL);
	 
	ShowWindow(hWnd, SW_NORMAL);					
    UpdateWindow(hWnd);	

	g_hWnd = hWnd;

	return hWnd;
}

//Register New Window

bool RegisterWindow(HINSTANCE hInst, char *WindowClass)
{
	WNDCLASSEX  wndclass;

    wndclass.cbSize        = sizeof (wndclass);	
    wndclass.style         = CS_HREDRAW | CS_VREDRAW;	
    wndclass.lpfnWndProc   = WndProc;	
    wndclass.cbClsExtra    = 0;						
    wndclass.cbWndExtra    = 0;					
    wndclass.hInstance     = hInst;			
    wndclass.hIcon         = LoadIcon (NULL, IDI_WINLOGO);
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
										
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wndclass.lpszMenuName  = NULL;					
    wndclass.lpszClassName = WindowClass;				
    wndclass.hIconSm       = LoadIcon (NULL, IDI_WINLOGO);	
	RegisterClassEx (&wndclass);		

	return true;
}

//UnRegesterWindow

bool UnRegisterWindow(HINSTANCE hInst, char *WindowClass)
{
	UnregisterClass(WindowClass,hInst);

	return true;
}
this is the class for it now i know the problem is in the SoundCreate Function with the QUery Interface part..the problem is i dont know why. When in the main and call the SOund Create it returns false.
#include <dsound.h>

#ifndef _SoundCore_H_
#define _SoundCore_H_

class cSound
{
	public:
		IDirectSound8 *g_Sound;
		IDirectSoundBuffer *g_SoundBuffer;
		IDirectSoundBuffer8 *g_SecSoundBuffer8;
		IDirectSoundNotify8 *g_SoundNot;

		DSBUFFERDESC g_dsbd;
		WAVEFORMATEX g_wavex;

		cSound();
		~cSound();

		bool SoundInit(HWND hWnd);
		bool SoundShutdown();
		bool SoundCreate();
		bool SoundPlayPri();
		bool SoundPlaySec(int position);
		bool SoundStop();
		bool SoundLock();
		bool SoundSetVolume(long);
		bool SoundSetPan(long);
		bool SoundSetFreq(long);
		bool SoundRestore();
		bool SoundQue(IDirectSoundBuffer8 *);
};

cSound::cSound()
{
}

cSound::~cSound()
{	
}

bool cSound::SoundShutdown()
{
	g_Sound->Release();
	g_SoundBuffer->Release();
		
	g_SecSoundBuffer8->Release();

	return true;
}

bool cSound::SoundInit(HWND hWnd)
{
	ZeroMemory(&g_dsbd, sizeof(DSBUFFERDESC));
	ZeroMemory(&g_wavex, sizeof(WAVEFORMATEX));

	g_dsbd.dwSize = sizeof(DSBUFFERDESC);
	g_dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME;
	g_dsbd.dwBufferBytes = 0;
	g_dsbd.lpwfxFormat = NULL;

	g_wavex.wFormatTag = WAVE_FORMAT_PCM;
	g_wavex.nChannels = 1;
	g_wavex.nSamplesPerSec = 22050;
	g_wavex.wBitsPerSample = 16;
	g_wavex.nBlockAlign = (g_wavex.wBitsPerSample/8) * g_wavex.nChannels;
	g_wavex.nAvgBytesPerSec = g_wavex.nSamplesPerSec * g_wavex.nBlockAlign;

	if(FAILED(DirectSoundCreate8(NULL, &g_Sound, NULL)))
		return false;

	if(FAILED(g_Sound->SetCooperativeLevel(hWnd, DSSCL_PRIORITY)))
		return false;

	if(FAILED(g_Sound->CreateSoundBuffer(&g_dsbd, &g_SoundBuffer, NULL)))
		return false;

	if(FAILED(g_SoundBuffer->SetFormat(&g_wavex)))
		return false;

	return true;
}

bool cSound::SoundPlayPri()
{
	if(FAILED(g_SoundBuffer->Play(0,0, DSBPLAY_LOOPING)))
		return false;

	return true;
}

bool cSound::SoundPlaySec(int position)
{
	if(SUCCEEDED(g_SecSoundBuffer8->SetCurrentPosition(position)))
		if(FAILED(g_SecSoundBuffer8->Play(0,0,0)))
			return false;
	else
		return false;

	return true;
}

bool cSound::SoundStop()
{
	if(FAILED(g_SoundBuffer->Stop()))
		return false;
	if(FAILED(g_SecSoundBuffer8->Stop()))
		return false;

	return true;
}

bool cSound::SoundCreate()
{
	if(FAILED(g_Sound->CreateSoundBuffer(&g_dsbd, &g_SoundBuffer, NULL)))
		return false;

	 if(FAILED(g_SoundBuffer->QueryInterface(IID_IDirectSoundBuffer8,(void**)&g_SecSoundBuffer8))) 
		return false;

	 return true;
}

bool cSound::SoundLock()
{
	char *SoundPtr;
	DWORD Size;

	if(FAILED(g_SecSoundBuffer8->Lock(0,0, (void**)&SoundPtr, (DWORD*)&Size, NULL, 0, DSBLOCK_ENTIREBUFFER)))
	{
		for(DWORD num = 0; num < Size; num++)
			SoundPtr[num] = rand() & 65536;
	}
	else
		return false;

	if(FAILED(g_SecSoundBuffer8->Unlock((void*)SoundPtr, Size, NULL, 0)))
		return false;

	return true;
}

bool cSound::SoundSetVolume(long num)
{
	if(FAILED(g_SecSoundBuffer8->SetVolume(num)))
		return false;
	
	return true;
}

bool cSound::SoundSetPan(long num)
{
	if(FAILED(g_SecSoundBuffer8->SetPan(num)))
		return false;

	return true;
}

bool cSound::SoundSetFreq(long num)
{
	if(FAILED(g_SecSoundBuffer8->SetFrequency(num)))
		return false;

	return true;
}

bool cSound::SoundRestore()
{
	if(FAILED(g_SecSoundBuffer8->Restore()))
		return false;

	return true;
}

bool cSound::SoundQue(IDirectSoundBuffer8 *Not)
{
	if(FAILED(Not->QueryInterface(IID_IDirectSoundNotify8, (void**)&g_SoundNot)))
		return false;

	return true;
}
#endif

Share this post


Link to post
Share on other sites
Nice work, but why go through all that trouble? Screw it, go with DirectX 6 DirectSound and use DSLoadSoundBuffer. You want the bare minimum for including DirectX sound in your game? Here it is

DirectSoundDemo.cpp EXE

Here is dsutil.cpp | dsutil.h you''ll need also

This is using WAV sounds in resources, for files its pretty much the same except you include the file name in DSLoadSoundBuffer function.

Phil P

Share this post


Link to post
Share on other sites
Just call CoInitialize(NULL); at the start of your program

---------------------------------------------
If God with me, Who against me?
Sitio de desarrollo de videojuegos en español
Digital Moon Studio...

There is no spoon

Share this post


Link to post
Share on other sites