I'm really enjoying this series, very educational and entertaining.
I just wanted to throw a couple of ideas I've added to the base code so far.
Debug messages
I use a simple macro for my debug messages, so it appears more clearly in my debug output. The other benefit is I can #if it out of the release build and also switch between platforms by defining it in the platform code. Nothing amazingly novel, but I like it :)
1 2 3 4 5 6 7 8 9 10 11 | #if WIN32_DEBUG #define DFAIL(msg) OutputDebugStringA("[-] "msg"\n") #define DSUCCESS(msg) OutputDebugStringA("[+] "msg"\n") #elif LINUX_DEBUG .... #endif .... XInputLib = LoadLibraryA("xinput9_1_0.dll"); if (XInputLib){ DFAIL("No XInput, using stubs"); .... |
Sound Buffer
I tweaked the audio loop so it used pointers to do the counting, rather than a separate index.
It also switches between regions with in the same loop.
I'm sure this will be fixed by Casey further down the line but I thought it'd be nice to share.
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 47 48 49 50 51 52 53 | .... void Win32FillSoundBuffer(win32_direct_sound * so, DWORD ByteToLock, DWORD BytesToWrite){ VOID * Region1; VOID * Region2; DWORD Region1Size; DWORD Region2Size; if (SUCCEEDED(GlobalSecondaryBuffer->Lock(ByteToLock, BytesToWrite, &Region1, &Region1Size, &Region2, &Region2Size, 0 //flags ))){ int16_t *SampleOut = (int16_t *)Region1; int16_t *Region1End = (int16_t*)((int8_t*)Region1 + Region1Size); int16_t *Region2End = (int16_t*)((int8_t*)Region2 + Region2Size); while (SampleOut != Region2End){ real32 SinV = sinf(so->t_sin); int16_t v = (uint16_t)(SinV * 1000); *SampleOut++ = v; //Left Channel *SampleOut++ = v; //Right Channel so->t_sin += 2.0f*Pi32*1.0f / (real32)so->WavePeriod; ++so->RunningSampleIndex; if (SampleOut == Region1End){ SampleOut = (int16_t *)Region2; } } GlobalSecondaryBuffer->Unlock(Region1, Region1Size, Region2, Region2Size); } else{ DFAIL("GlobalSecondaryBuffer->Lock"); } } ..... DWORD playCursor; DWORD writeCursor; if (SUCCEEDED(GlobalSecondaryBuffer->GetCurrentPosition(&playCursor, &writeCursor))){ DWORD ByteToLock = (so.RunningSampleIndex * so.BytesPerSample) % so.SecondaryBufferSize; DWORD TargetCursor = (playCursor + (so.LatencySampleCount*so.BytesPerSample)) % so.SecondaryBufferSize; DWORD BytesToWrite; if (ByteToLock > TargetCursor){ BytesToWrite = (so.SecondaryBufferSize - ByteToLock); BytesToWrite += TargetCursor; } else{ BytesToWrite = TargetCursor - ByteToLock; } Win32FillSoundBuffer(&so, ByteToLock, BytesToWrite); } |