Problem with Visual Studio 2015 when Platform Independence started (Day 11/12)

Hi,

First of all, this series is Awesome.

Now that i said that, down to the issue.

It all basically worked fine until the Platform things started, cause Visual Studio doesn't seem to like it when you include a ".cpp" file.
So i get warnings and errors all over the place cause it doesn't see the "typedef" and what not.

Compiling through the bat like Casey does works fine.

And i spent a lot of time trying to use Emacs instead, but it's just too advanced for me at my current state, and i love Visual Studio's AutoComplete cause my memory sucks and it's just so much faster for me, and it's easier to play around and guess when you can see the names.

I also have problems with it not always compiling the the changes for some reason when it's in the other file,
but it Might have to do with that it's broken or something ( i basically used the header files to "bypass" the issue).

Anyone got it working and can share their settings or something?

Thanks:)
What exactly do you mean by "Visual Studio doesn't seem to like it" ?
Do you see compiler errors or just intellisense errors (red squiggly lines) ?

If they are compiler errors then most likely you are trying to build .cpp files, but Casey is using unity build. He does not compile all .cpp files, just one main file. So you need to exclude from build all .cpp files that should not be built. Alternatively don't add any files to project, just click "Show All Files" button in Solution Explorer.

If you are talking about intellisense errors, then there is not much you can do. Visual Studio simply doesn't understand that you are trying to do unity build and is trying to parse .cpp files individually like they would be compiled independently. Obviously that will lead to various compiler errors and that's why it shows those red squiggly lines.
Actually just found that solution in another post here!

It works wonders if i exclude the file from compiling;D

Now i just need to try to understand this DirectSound buffer thingy, my math sucks so have a hard time with it,
i get the principle though.

I had XAudio2 Working alongside before as i wanted to see if i could get that working,
but i can't get it to work in this case where he makes the "Game" provide a certain amount of samples or something.
Cause XAudio2 from my understanding has no such thing as a circular buffer, it just has a Queue,

And adding things to the Queue doesn't work in that way (basically cause i guess it just adds the same object and rewrites it over and over as it's a pointer to memory).

But even if tht wasn't the case it would add Much more to the queue then what time has actually passed.

Would like to solve that if possible (to learn about this stuff).

Here is some Code. (I work in Float for Audio, cause, why not).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
real32 Samples[(48000 * 2)];
		game_sound_output_buffer SoundBuffer = {};
		if (GlobalDirectSoundBuffer)
		{
			SoundBuffer.SamplesPerSecond = SoundOutput.SamplesPerSecond;
			SoundBuffer.SampleCount = BytesToWrite / SoundOutput.BytesPerSample;
			SoundBuffer.Samples = Samples;
		}
		else
		{
			SoundBuffer.SamplesPerSecond = SoundOutput.SamplesPerSecond;
			SoundBuffer.SampleCount = 48000;
			SoundBuffer.Samples = Samples;
		}
		game_offscreen_buffer Buffer = {};
		Buffer.BitmapMemory = GlobalBackBuffer.BitmapMemory;
		Buffer.BitmapWidth = GlobalBackBuffer.BitmapWidth;
		Buffer.BitmapHeight = GlobalBackBuffer.BitmapHeight;
		Buffer.Pitch = GlobalBackBuffer.Pitch;

		GameUpdateAndRender(&Buffer, 0, 0, &SoundBuffer);

		if (SoundIsValid)
		{
			Win32FillSoundBuffer(&SoundOutput, BytesToLock, BytesToWrite, &SoundBuffer);
		}

		if (XAudio2System.SourceVoice)
		{

			XAUDIO2_VOICE_STATE VoiceStat;
			XAudio2System.SourceVoice->GetState(&VoiceStat);
			if (VoiceStat.BuffersQueued < 2)
			{

		
			//	continue;
			{

				XAUDIO2_BUFFER Buffer = {};
				Buffer.AudioBytes = (sizeof(Samples));
				Buffer.pAudioData = (byte*)Samples;
				XAudio2System.SourceVoice->SubmitSourceBuffer(&Buffer);
			}
			}
		}


:D

Edited by ZeroWalker on
The idea is to figure out how much latency is ok for your game audio and then every frame submit enough data to keep audio buffer filled. Typically that would be around one or two extra frames. So while you are processing game state & rendering next frame there is no underrun happening for audio buffer.

Btw if you don't care about supporting Windows XP then instead of XAudio2 you should use WASAPI that is part of Windows Core Audio. Because XAudio2 (and DirectSound) on Windows Vista+ is just a wrapper around Windows Core Audio. So it's just better to go directly to lowest level of audio API.

Here's my implementation for win32 layer that uses WASAPI: https://hero.handmade.network/forums/code-discussion/t/102 I don't remember how good it was, but that should give you idea how to start using WASAPI.
Here's another topic on WASAPI: https://hero.handmade.network/forums/code-discussion/t/456

If you move your audio thread in background thread then with WASAPI you can actually get very low latency (few hundred frames). Of course in this case you'll need to keep manual synchronization for audio state, because of race conditions with game thread when game thread want's to update something in audio state.
Ah, actually thought of trying Wasapi as i have at least Some small experience with it and knew you could get access to the audioclock.

But though XAudio2 would be more proper in Gaming sense, as i don't think i normally see games or emulators use Wasapi instead of XAudio2.


Thanks for the link will check it out:D
XAudio2 is just a higher level than the WASAPI. I use the WASAPI in my game, but for example in the game i work on at my job we use XAudio2 and have nothing but problems! Some people say the same thing about the WASAPI though, so pick your poison!
Well i got Wasapi working after playing around with it.
It's seems to be much simpler than DirectSound in the aspect of the buffer,
cause it has everything built in with the "Circular thing".


Though one thing Wasapi seems to be lacking is Resampling, which DirectSound has (basically cause it sends the data to Wasapi i guess).
XAudio2 also has that flexibility.


What are the problems you are having with XAudio2 if i may ask?
Not that i will understand probably, but is there something specific in the API which is problematic?
WASAPI has resampling. When you call IAudioClient::Initialize you can pass AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM flag. This will allow you to specify whatever rate you want. WASPI will resample (and convert channels) it to native sample rate / channel count automatically. Add AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY flag for better quality.

Edited by Mārtiņš Možeiko on Reason: typos
Oh nice, didn't find info about that, thanks:)!