Days 7/8 - Can´t Create Secundary Buffer

It seems as I have the same problem of iniciating the fiels in Waveformat struct.

But I´ve correct.

1
2
3
4
5
6
7
WAVEFORMATEX WaveFormat = {};
			WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
			WaveFormat.nChannels = 2;
			WaveFormat.nSamplesPerSec = samplesPerSecond;
			WaveFormat.wBitsPerSample = 16;
			WaveFormat.nBlockAlign = (WaveFormat.nChannels*WaveFormat.wBitsPerSample) / 8;
			WaveFormat.nAvgBytesPerSec = WaveFormat.nBlockAlign*WaveFormat.nBlockAlign;


Also, the calling of Win32DirectSoundInit, seems equal.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
			int samplesPersecond = 44000;
			int squareWaveCounter = 0;

			int Hz = 440;
			int squareWavePeriod = 48000/440;
			uint32 runningIndexBuffer = 0;
			int bytesPersample =  sizeof(int16) * 2;
			int SecondaryBufferSize = samplesPersecond*bytesPersample;

			Win32InitDSound(Window, samplesPersecond, SecondaryBufferSize);




My code is very close to Casys, I add my stile a bit and encapsulate a little. I will preorder de game as soon I fixe my Paypal account.

https://github.com/ThadeuFerreira...master/code/main_handmadehero.cpp
I think I may have ran into the same problem I had when first doing this bit. Initialize your DirectSound object, like this:

1
LPDIRECTSOUND DirectSound = 0;


If you pull DirectSoundCreate(0, &DirectSound, 0) into a temporary variable, like so:

1
2
3
4
5
6
7
//Get a DirectSound object!
direct_sound_create *DirectSoundCreate = (direct_sound_create *) GetProcAddress(DSoundLibrary, "DirectSoundCreate");

LPDIRECTSOUND DirectSound;
HRESULT Err = DirectSoundCreate(0, &DirectSound, 0);
if (SUCCEEDED(Err))
{


Does it succeed? My compiler caused the SUCCEEDED macro to _not_ succeed unless I either initialized the pointer to zero, or assigned the result of the DirectSoundCreate call to a local. Might not be your problem, but maybe!

Edited by drjeats on
Probably this is the error:
1
int samplesPersecond = 44000;

44000 is very weird samples per second. You typically want to specify there 44100 or 48000.
No changes, it still fails...

:(
1
WaveFormat.nAvgBytesPerSec = WaveFormat.nBlockAlign*WaveFormat.nBlockAlign;

This line is wrong. It should be:
1
WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;

Also this:
1
*sampleOut = (int16)region2;

is supposed to be:
1
sampleOut = (int16*) region2;

and this should be obvious:
1
2
3
uint8 Blue ;
uint8 Green ;
uint8 Red = 0;


In general your code is very inconsistent and completely full of typos. This will come and bite you later on and result in really strange bugs or just straight up crashes like it did this time. People might be unwilling to help you if half of the variables are misspelled and randomly have different capitalisation in different parts of the program. Eventually even you will have trouble reading your own code.

And it's not just about names, there are also multiple numerical constants that are wrong just like the sample rate, as mmozeiko pointed out. Variable names "only" impact code readability, but this is actual broken functionality.

Edited by Dghelneshi on
Thank you Dghelneshi, so obvious copy and paste error!!!

Well, I know how inconcsistent my code is. It´s not much different from Casey´s. And I will fix latter, if I have time.

My usual style is different. I like encapsulating and using classes as much as possible. I also like smaller variable names and a hungarian-esc type of prefixing.

Thank you again.

Edited by ThadeuMelo on