This commit is contained in:
2024-09-10 22:32:31 +01:00
parent 51c9509a13
commit 6e289f79fd
4 changed files with 86 additions and 60 deletions

View File

@@ -40,7 +40,7 @@ internal void renderWeirdGradient(GameOffscreenBuffer *buffer, int xOffset, int
} }
} }
internal void gameUpdateAndRender(GameMemory *memory, GameOffscreenBuffer *videoBuf, GameInput *input, GameSoundOutputBuffer *soundBuf) { internal void gameUpdateAndRender(GameMemory *memory, GameOffscreenBuffer *videoBuf, GameInput *input) {
Assert(sizeof(GameState) <= memory->permanentStorageSize); Assert(sizeof(GameState) <= memory->permanentStorageSize);
GameState *state = (GameState*)memory->permanentStorage; GameState *state = (GameState*)memory->permanentStorage;
@@ -61,26 +61,29 @@ internal void gameUpdateAndRender(GameMemory *memory, GameOffscreenBuffer *video
GameControllerInput *controllerInput = &input->controllers[controllerIndex]; GameControllerInput *controllerInput = &input->controllers[controllerIndex];
if (controllerInput->isAnalog) { if (controllerInput->isAnalog) {
state->toneHz = 440 + (int)(128.0f*controllerInput->stickAvgX); state->toneHz = 440 + (int)(128.0f*controllerInput->stickAvgX);
state->greenOffset -= (int)(4.0f*controllerInput->stickAvgX); state->greenOffset -= (int)(20.0f*controllerInput->stickAvgX);
state->blueOffset += (int)(4.0f*controllerInput->stickAvgY); state->blueOffset += (int)(20.0f*controllerInput->stickAvgY);
} else { } else {
if (controllerInput->stickRight.endedDown) { if (controllerInput->stickRight.endedDown) {
state->toneHz = 440 + 128; state->toneHz = 440 + 128;
state->greenOffset -= 4; state->greenOffset -= 10;
} else if (controllerInput->stickLeft.endedDown) { } else if (controllerInput->stickLeft.endedDown) {
state->toneHz = 440 - 128; state->toneHz = 440 - 128;
state->greenOffset += 4; state->greenOffset += 10;
} else { } else {
state->toneHz = 440; //state->toneHz = 440;
} }
if (controllerInput->stickUp.endedDown) { if (controllerInput->stickUp.endedDown) {
state->blueOffset += 4; state->blueOffset += 10;
} else if (controllerInput->stickDown.endedDown) { } else if (controllerInput->stickDown.endedDown) {
state->blueOffset -= 4; state->blueOffset -= 10;
} }
} }
} }
outputSineSound(soundBuf, state);
renderWeirdGradient(videoBuf, state->greenOffset, state->blueOffset); renderWeirdGradient(videoBuf, state->greenOffset, state->blueOffset);
} }
internal void gameGetSoundSamples(GameMemory *memory, GameSoundOutputBuffer *soundBuf) {
outputSineSound(soundBuf, (GameState*)memory->permanentStorage);
}

View File

@@ -125,7 +125,8 @@ struct GameState {
}; };
// === Game to platform services === // === Game to platform services ===
void gameUpdateAndRender(GameMemory *memory, GameInput *input, GameOffscreenBuffer *videoBuf, GameSoundOutputBuffer *soundBuf); internal void gameUpdateAndRender(GameMemory *memory, GameInput *input, GameOffscreenBuffer *videoBuf);
internal void gameGetSoundSamples(GameMemory *memory, GameSoundOutputBuffer *soundBuf);
// === Platform to game services === // === Platform to game services ===
#if HANDMADE_INTERNAL #if HANDMADE_INTERNAL

View File

@@ -367,7 +367,6 @@ inline real32 win32GetSecondsElapsed(LARGE_INTEGER start, LARGE_INTEGER end) {
// int monitorRefreshHz = 60; // int monitorRefreshHz = 60;
// int gameUpdateHz = monitorRefreshHz / 2; // int gameUpdateHz = monitorRefreshHz / 2;
#define framesOfAudioLatency 3
#define monitorRefreshHz 60 #define monitorRefreshHz 60
#define gameUpdateHz (monitorRefreshHz / 2) #define gameUpdateHz (monitorRefreshHz / 2)
@@ -395,11 +394,16 @@ internal void win32DebugSyncDisplay(Win32OffscreenBuffer *screenBuffer, int mark
int renderWidth = screenBuffer->width - 2 *padX; int renderWidth = screenBuffer->width - 2 *padX;
real32 pxPerSoundBufferEntry = (real32)renderWidth / (real32)soundOutput->secondaryBufferSize; real32 pxPerSoundBufferEntry = (real32)renderWidth / (real32)soundOutput->secondaryBufferSize;
int pitch = screenBuffer->width*screenBuffer->bytesPerPixel;
for (int markerIndex = 0; markerIndex < markerCount; markerIndex++) { for (int markerIndex = 0; markerIndex < markerCount; markerIndex++) {
Win32DebugTimeMarker *thisMarker = &markers[markerIndex]; Win32DebugTimeMarker *thisMarker = &markers[markerIndex];
win32DrawSoundBufferMarker(screenBuffer, soundOutput, pxPerSoundBufferEntry, padX, top, bottom, thisMarker->playCursor, 0xFFFFFFFF); real32 alpha = ((real32)(markerIndex + 1) / (real32)markerCount);
win32DrawSoundBufferMarker(screenBuffer, soundOutput, pxPerSoundBufferEntry, padX, top, bottom, thisMarker->writeCursor, 0xFFFF0000); int x = padX + (int)(pxPerSoundBufferEntry * (real32)thisMarker->writeCursor);
uint8 *pixel = (uint8 *)screenBuffer->memory + top * pitch + x*screenBuffer->bytesPerPixel;
uint32 newPixel = (uint32)((real32)alpha * 0xFFFFFFFF + (1.0f - alpha) * (*(uint32 *)pixel));
win32DrawSoundBufferMarker(screenBuffer, soundOutput, pxPerSoundBufferEntry, padX, top, bottom, thisMarker->playCursor, newPixel);
win32DrawSoundBufferMarker(screenBuffer, soundOutput, pxPerSoundBufferEntry, padX, top, bottom, thisMarker->writeCursor, 0x00FF0000);
} }
} }
@@ -456,7 +460,7 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
soundOutput.runningSampleIndex = 0; soundOutput.runningSampleIndex = 0;
soundOutput.bytesPerSample = sizeof(int16)*2; soundOutput.bytesPerSample = sizeof(int16)*2;
soundOutput.secondaryBufferSize = soundOutput.samplesPerSecond*soundOutput.bytesPerSample; soundOutput.secondaryBufferSize = soundOutput.samplesPerSecond*soundOutput.bytesPerSample;
soundOutput.latencySampleCount = framesOfAudioLatency * (soundOutput.samplesPerSecond / gameUpdateHz); soundOutput.safetyBytes = (soundOutput.samplesPerSecond * soundOutput.bytesPerSample / gameUpdateHz) / 3;
int16 *samples = (int16*)VirtualAlloc(NULL, soundOutput.secondaryBufferSize, MEM_COMMIT, PAGE_READWRITE); int16 *samples = (int16*)VirtualAlloc(NULL, soundOutput.secondaryBufferSize, MEM_COMMIT, PAGE_READWRITE);
@@ -478,11 +482,12 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
globalSecondaryBuffer->Play(0, 0, DSBPLAY_LOOPING); globalSecondaryBuffer->Play(0, 0, DSBPLAY_LOOPING);
LARGE_INTEGER lastWorkCounter = win32GetWallClock(); LARGE_INTEGER lastWorkCounter = win32GetWallClock();
LARGE_INTEGER flipWallClock = win32GetWallClock();
int64 lastCycleCount = __rdtsc(); int64 lastCycleCount = __rdtsc();
bool32 soundIsValid = false; DWORD audioLatencyBytes = 0;
DWORD lastPlayCursor = 0; real32 audioLatencySeconds = 0;
while (globalRunning) { while (globalRunning) {
GameControllerInput *oldKeyboardController = &oldInput->controllers[0]; GameControllerInput *oldKeyboardController = &oldInput->controllers[0];
@@ -561,33 +566,75 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
videoBuffer.memory = globalBackBuffer.memory; videoBuffer.memory = globalBackBuffer.memory;
videoBuffer.width = globalBackBuffer.width; videoBuffer.width = globalBackBuffer.width;
videoBuffer.height = globalBackBuffer.height; videoBuffer.height = globalBackBuffer.height;
gameUpdateAndRender(&gameMemory, &videoBuffer, newInput);
// Sound test DWORD playCursor = 0;
DWORD byteToLock = 0; DWORD writeCursor = 0;
DWORD targetCursor = lastPlayCursor; bool soundIsValid = false;
DWORD bytesToWrite = 0;
if (soundIsValid) { if (SUCCEEDED(globalSecondaryBuffer->GetCurrentPosition(&playCursor, &writeCursor))) {
byteToLock = (soundOutput.runningSampleIndex*soundOutput.bytesPerSample) % soundOutput.secondaryBufferSize; if (!soundIsValid) {
targetCursor = (lastPlayCursor + soundOutput.latencySampleCount*soundOutput.bytesPerSample) % soundOutput.secondaryBufferSize; soundOutput.runningSampleIndex = writeCursor / soundOutput.bytesPerSample;
if (byteToLock == targetCursor) { soundIsValid = true;
bytesToWrite = 0; }
} else if (byteToLock > targetCursor) { DWORD byteToLock = (soundOutput.runningSampleIndex*soundOutput.bytesPerSample) % soundOutput.secondaryBufferSize;
// TODO(dledda): episode 20 ~2:00, something's wrong though, gotta sort it out.
DWORD expectedSoundBytesPerFrame = (soundOutput.samplesPerSecond * soundOutput.bytesPerSample) / gameUpdateHz;
DWORD expectedFrameBoundaryByte = playCursor + expectedSoundBytesPerFrame;
DWORD safeWriteCursor = writeCursor;
if (safeWriteCursor < playCursor) {
safeWriteCursor += soundOutput.secondaryBufferSize;
}
Assert(safeWriteCursor >= playCursor);
safeWriteCursor += soundOutput.safetyBytes + 2000;
bool audioIsLowLatency = safeWriteCursor >= expectedFrameBoundaryByte;
DWORD targetCursor = 0;
if (audioIsLowLatency) {
targetCursor = expectedFrameBoundaryByte + expectedSoundBytesPerFrame;
} else {
targetCursor = writeCursor + expectedSoundBytesPerFrame + soundOutput.safetyBytes;
}
targetCursor %= soundOutput.secondaryBufferSize;
DWORD bytesToWrite = 0;
if (byteToLock > targetCursor) {
bytesToWrite = soundOutput.secondaryBufferSize - byteToLock + targetCursor; bytesToWrite = soundOutput.secondaryBufferSize - byteToLock + targetCursor;
} else { } else {
bytesToWrite = targetCursor - byteToLock; bytesToWrite = targetCursor - byteToLock;
} }
}
GameSoundOutputBuffer soundBuffer = {}; GameSoundOutputBuffer soundBuffer = {};
soundBuffer.samplesPerSecond = soundOutput.samplesPerSecond; soundBuffer.samplesPerSecond = soundOutput.samplesPerSecond;
soundBuffer.sampleCount = bytesToWrite / soundOutput.bytesPerSample; soundBuffer.sampleCount = bytesToWrite / soundOutput.bytesPerSample;
soundBuffer.samples = samples; soundBuffer.samples = samples;
gameGetSoundSamples(&gameMemory, &soundBuffer);
#if HANDMADE_INTERNAL
Assert(debugTimeMarkerIndex < ArrayCount(debugMarkers));
gameUpdateAndRender(&gameMemory, &videoBuffer, newInput, &soundBuffer); Win32DebugTimeMarker *marker = &debugMarkers[debugTimeMarkerIndex++];
if (soundIsValid) {
DWORD unwrappedWriteCursor = writeCursor;
if (unwrappedWriteCursor < targetCursor) {
unwrappedWriteCursor += soundOutput.secondaryBufferSize;
}
audioLatencyBytes = unwrappedWriteCursor - playCursor;
audioLatencySeconds = (((real32)audioLatencyBytes / (real32)soundOutput.bytesPerSample) / (real32)soundOutput.samplesPerSecond);
if (debugTimeMarkerIndex >= ArrayCount(debugMarkers)) {
debugTimeMarkerIndex = 0;
}
globalSecondaryBuffer->GetCurrentPosition(&marker->playCursor, &marker->writeCursor);
#endif
win32FillSoundBuffer(&soundOutput, byteToLock, bytesToWrite, &soundBuffer); win32FillSoundBuffer(&soundOutput, byteToLock, bytesToWrite, &soundBuffer);
} else {
soundIsValid = false;
} }
real32 secondsElapsedForFrame = win32GetSecondsElapsed(lastWorkCounter, win32GetWallClock()); real32 secondsElapsedForFrame = win32GetSecondsElapsed(lastWorkCounter, win32GetWallClock());
if (secondsElapsedForFrame < targetSecondsPerFrame) { if (secondsElapsedForFrame < targetSecondsPerFrame) {
if (sleepIsGranular) { if (sleepIsGranular) {
@@ -610,32 +657,7 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
#endif #endif
win32DrawBufferInWindow(&globalBackBuffer, window); win32DrawBufferInWindow(&globalBackBuffer, window);
flipWallClock = win32GetWallClock();
DWORD playCursor = 0;
DWORD writeCursor = 0;
if (SUCCEEDED(globalSecondaryBuffer->GetCurrentPosition(&playCursor, &writeCursor))) {
lastPlayCursor = playCursor;
if (!soundIsValid) {
soundOutput.runningSampleIndex = writeCursor / soundOutput.bytesPerSample;
soundIsValid = true;
}
} else {
soundIsValid = false;
}
#if HANDMADE_INTERNAL
{
Assert(debugTimeMarkerIndex < ArrayCount(debugMarkers));
Win32DebugTimeMarker *marker = &debugMarkers[debugTimeMarkerIndex++];
if (debugTimeMarkerIndex >= ArrayCount(debugMarkers)) {
debugTimeMarkerIndex = 0;
}
globalSecondaryBuffer->GetCurrentPosition(&marker->playCursor, &marker->writeCursor);
}
#endif
#if 0 #if 0
real64 msElapsed = (real32)(1000.0f*secondsElapsedForFrame) / globalPerfCountFrequency; real64 msElapsed = (real32)(1000.0f*secondsElapsedForFrame) / globalPerfCountFrequency;

View File

@@ -20,7 +20,7 @@ struct Win32SoundOutput {
int bytesPerSample; int bytesPerSample;
int secondaryBufferSize; int secondaryBufferSize;
real32 tSine; real32 tSine;
int latencySampleCount; int safetyBytes;
}; };
struct Win32DebugTimeMarker { struct Win32DebugTimeMarker {