|
|
|
|
@@ -60,7 +60,7 @@ DEBUG_PLATFORM_READ_ENTIRE_FILE(debugReadEntireFile) {
|
|
|
|
|
if (ReadFile(fileHandle, result.contents, (DWORD)fileSize.QuadPart, &bytesRead, NULL) && (fileSize32 == (uint32)bytesRead)) {
|
|
|
|
|
result.contentsSize = fileSize32;
|
|
|
|
|
} else {
|
|
|
|
|
debugFreeFileMemory(result.contents);
|
|
|
|
|
debugFreeFileMemory(ctx, result.contents);
|
|
|
|
|
result.contents = NULL;
|
|
|
|
|
// logging
|
|
|
|
|
}
|
|
|
|
|
@@ -139,14 +139,6 @@ internal void win32DrawBufferInWindow(Win32OffscreenBuffer *buffer, HWND window)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Win32GameCode {
|
|
|
|
|
HMODULE gameCodeLib;
|
|
|
|
|
GameUpdateAndRenderFn *updateAndRender;
|
|
|
|
|
GameGetSoundSamplesFn *getSoundSamples;
|
|
|
|
|
bool isValid;
|
|
|
|
|
FILETIME lastWriteTime;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
inline FILETIME win32GetLastWriteTime(char *filename) {
|
|
|
|
|
FILETIME lastWriteTime = {};
|
|
|
|
|
WIN32_FIND_DATA findData;
|
|
|
|
|
@@ -202,9 +194,11 @@ internal void win32LoadXInput() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void win32ProcessKeyboardKeypress(GameButtonState *newState, bool32 isDown) {
|
|
|
|
|
if (newState->endedDown != isDown) {
|
|
|
|
|
newState->endedDown = isDown;
|
|
|
|
|
newState->halfTransitionCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void win32ProcessXInputDigitalButton(DWORD xInputButtonState, GameButtonState *oldState, GameButtonState *newState, DWORD buttonBit) {
|
|
|
|
|
newState->endedDown = (xInputButtonState & buttonBit) == buttonBit;
|
|
|
|
|
@@ -258,12 +252,7 @@ internal void win32InitSound(HWND window, int32 samplesPerSec, int bufferSize) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LRESULT mainWindowCallback(
|
|
|
|
|
HWND window,
|
|
|
|
|
UINT message,
|
|
|
|
|
WPARAM wParam,
|
|
|
|
|
LPARAM lParam
|
|
|
|
|
) {
|
|
|
|
|
LRESULT mainWindowCallback(HWND window, UINT message, WPARAM wParam, LPARAM lParam) {
|
|
|
|
|
LRESULT result = 0;
|
|
|
|
|
switch (message) {
|
|
|
|
|
case WM_SIZE: {
|
|
|
|
|
@@ -360,18 +349,24 @@ internal void win32FillSoundBuffer(Win32SoundOutput *soundOutput, DWORD byteToLo
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void win32BeginRecordingInput(Win32State *win32State, int inputRecordingIndex) {
|
|
|
|
|
win32State->inputRecordingIndex = inputRecordingIndex;
|
|
|
|
|
char *filename = "replay.ipt";
|
|
|
|
|
win32State->recordingHandle = CreateFileA(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL);
|
|
|
|
|
DWORD bytesToWrite = (DWORD)win32State->gameMemoryTotalSize;
|
|
|
|
|
Assert(bytesToWrite < 0xFFFFFFFF);
|
|
|
|
|
DWORD bytesWritten;
|
|
|
|
|
WriteFile(win32State->recordingHandle, win32State->gameMemoryBlock, bytesToWrite, &bytesWritten, NULL);
|
|
|
|
|
internal Win32ReplayBuffer* win32GetReplayBuffer(Win32State *state, int unsigned index) {
|
|
|
|
|
Win32ReplayBuffer *result = &state->replayBuffers[index - 1];
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void win32BeginRecordingInput(Win32State *state, int inputRecordingIndex) {
|
|
|
|
|
Win32ReplayBuffer *replayBuffer = win32GetReplayBuffer(state, inputRecordingIndex);
|
|
|
|
|
if (replayBuffer->memoryBlock) {
|
|
|
|
|
state->inputRecordingIndex = inputRecordingIndex;
|
|
|
|
|
state->recordingHandle = replayBuffer->fileHandle;
|
|
|
|
|
LARGE_INTEGER filePosition;
|
|
|
|
|
filePosition.QuadPart = state->gameMemoryTotalSize;
|
|
|
|
|
SetFilePointerEx(state->recordingHandle, filePosition, NULL, FILE_BEGIN);
|
|
|
|
|
CopyMemory(replayBuffer->memoryBlock, state->gameMemoryBlock, state->gameMemoryTotalSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void win32EndRecordingInput(Win32State *win32State) {
|
|
|
|
|
CloseHandle(win32State->recordingHandle);
|
|
|
|
|
win32State->inputRecordingIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -380,18 +375,19 @@ internal void win32RecordInput(Win32State *win32State, GameInput *newInput) {
|
|
|
|
|
WriteFile(win32State->recordingHandle, newInput, sizeof(*newInput), &bytesWritten, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void win32BeginInputPlayback(Win32State *win32State, int inputPlayingIndex) {
|
|
|
|
|
win32State->inputPlayingIndex = inputPlayingIndex;
|
|
|
|
|
char *filename = "replay.ipt";
|
|
|
|
|
win32State->playbackHandle = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
|
|
|
|
|
DWORD bytesToRead = (DWORD)win32State->gameMemoryTotalSize;
|
|
|
|
|
Assert(bytesToRead < 0xFFFFFFFF);
|
|
|
|
|
DWORD bytesRead;
|
|
|
|
|
ReadFile(win32State->playbackHandle, win32State->gameMemoryBlock, bytesToRead, &bytesRead, NULL);
|
|
|
|
|
internal void win32BeginInputPlayback(Win32State *state, int inputPlayingIndex) {
|
|
|
|
|
Win32ReplayBuffer *replayBuffer = win32GetReplayBuffer(state, inputPlayingIndex);
|
|
|
|
|
if (replayBuffer->memoryBlock) {
|
|
|
|
|
state->inputPlayingIndex = inputPlayingIndex;
|
|
|
|
|
state->playbackHandle = replayBuffer->fileHandle;
|
|
|
|
|
LARGE_INTEGER filePosition;
|
|
|
|
|
filePosition.QuadPart = state->gameMemoryTotalSize;
|
|
|
|
|
SetFilePointerEx(state->playbackHandle, filePosition, NULL, FILE_BEGIN);
|
|
|
|
|
CopyMemory(state->gameMemoryBlock, replayBuffer->memoryBlock, state->gameMemoryTotalSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void win32EndInputPlayback(Win32State *win32State) {
|
|
|
|
|
CloseHandle(win32State->playbackHandle);
|
|
|
|
|
win32State->inputPlayingIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -449,11 +445,13 @@ internal void win32ProcessPendingMessages(Win32State *win32State, GameController
|
|
|
|
|
} else if (VKCode == VK_SPACE) {
|
|
|
|
|
} else if (VKCode == 'L') {
|
|
|
|
|
if (isDown) {
|
|
|
|
|
if (win32State->inputRecordingIndex == 0) {
|
|
|
|
|
if (win32State->inputRecordingIndex == 0 && win32State->inputPlayingIndex == 0) {
|
|
|
|
|
win32BeginRecordingInput(win32State, 1);
|
|
|
|
|
} else {
|
|
|
|
|
} else if (win32State->inputRecordingIndex != 0 && win32State->inputPlayingIndex == 0) {
|
|
|
|
|
win32EndRecordingInput(win32State);
|
|
|
|
|
win32BeginInputPlayback(win32State, 1);
|
|
|
|
|
} else if (win32State->inputRecordingIndex == 0 && win32State->inputPlayingIndex != 0) {
|
|
|
|
|
win32EndInputPlayback(win32State);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -483,12 +481,6 @@ inline real32 win32GetSecondsElapsed(LARGE_INTEGER start, LARGE_INTEGER end) {
|
|
|
|
|
return (real32)(end.QuadPart - start.QuadPart) / (real32)globalPerfCountFrequency;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// int monitorRefreshHz = 60;
|
|
|
|
|
// int gameUpdateHz = monitorRefreshHz / 2;
|
|
|
|
|
|
|
|
|
|
#define monitorRefreshHz 60
|
|
|
|
|
#define gameUpdateHz (monitorRefreshHz / 2)
|
|
|
|
|
|
|
|
|
|
internal void win32DebugDrawVertical(Win32OffscreenBuffer *buffer, int x, int top, int bottom, int32 color) {
|
|
|
|
|
uint8 *pixel = (uint8 *)buffer->memory + top * buffer->pitch + x*buffer->bytesPerPixel;
|
|
|
|
|
for (int y = top; y < bottom; y++) {
|
|
|
|
|
@@ -503,29 +495,6 @@ inline void win32DrawSoundBufferMarker(Win32OffscreenBuffer *buffer, Win32SoundO
|
|
|
|
|
win32DebugDrawVertical(buffer, x, top, bottom, color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void win32DebugSyncDisplay(Win32OffscreenBuffer *buffer, int markerCount, Win32DebugTimeMarker *markers, Win32SoundOutput *soundOutput, real32 targetSecondsPerFrame) {
|
|
|
|
|
int padX = 16;
|
|
|
|
|
int padY = 16;
|
|
|
|
|
|
|
|
|
|
int top = padY;
|
|
|
|
|
int bottom = buffer->height - padY;
|
|
|
|
|
int renderWidth = buffer->width - 2 *padX;
|
|
|
|
|
|
|
|
|
|
real32 pxPerSoundBufferEntry = (real32)renderWidth / (real32)soundOutput->secondaryBufferSize;
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
for (int markerIndex = 0; markerIndex < markerCount; markerIndex++) {
|
|
|
|
|
Win32DebugTimeMarker *thisMarker = &markers[markerIndex];
|
|
|
|
|
real32 alpha = ((real32)(markerIndex + 1) / (real32)markerCount);
|
|
|
|
|
int x = padX + (int)(pxPerSoundBufferEntry * (real32)thisMarker->writeCursor);
|
|
|
|
|
uint8 *pixel = (uint8 *)buffer->memory + top * buffer->pitch + x*buffer->bytesPerPixel;
|
|
|
|
|
uint32 newPixel = (uint32)((real32)alpha * 0xFFFFFFFF + (1.0f - alpha) * (*(uint32 *)pixel));
|
|
|
|
|
win32DrawSoundBufferMarker(buffer, soundOutput, pxPerSoundBufferEntry, padX, top, bottom, thisMarker->playCursor, newPixel);
|
|
|
|
|
win32DrawSoundBufferMarker(buffer, soundOutput, pxPerSoundBufferEntry, padX, top, bottom, thisMarker->writeCursor, 0x00FF0000);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void catStrings(size_t sourceACount, char *sourceA, size_t sourceBCount, char *sourceB, size_t destCount, char *dest) {
|
|
|
|
|
for (int i = 0; i < sourceACount; i++) {
|
|
|
|
|
*dest++ = *sourceA++;
|
|
|
|
|
@@ -536,27 +505,34 @@ void catStrings(size_t sourceACount, char *sourceA, size_t sourceBCount, char *s
|
|
|
|
|
*dest++ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLine, int commandShow) {
|
|
|
|
|
char exeFileName[MAX_PATH];
|
|
|
|
|
DWORD sizeOfFilename = GetModuleFileNameA(NULL, exeFileName, sizeof(exeFileName));
|
|
|
|
|
char *onePastLastSlash = exeFileName;
|
|
|
|
|
for (char *scan = exeFileName; *scan; scan++) {
|
|
|
|
|
void win32GetExeFilename(Win32State *win32State) {
|
|
|
|
|
DWORD sizeOfFilename = GetModuleFileNameA(NULL, win32State->exeFilename, sizeof(win32State->exeFilename));
|
|
|
|
|
win32State->onePastLastExeFilenameSlash = win32State->exeFilename;
|
|
|
|
|
for (char *scan = win32State->exeFilename; *scan; scan++) {
|
|
|
|
|
if (*scan == '\\') {
|
|
|
|
|
onePastLastSlash = scan + 1;
|
|
|
|
|
win32State->onePastLastExeFilenameSlash = scan + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char sourceGameCodeDLLFilename[] = "handmade.dll";
|
|
|
|
|
void win32GetFullPathToLocalFile(Win32State *state, char *filename, int filenameSize, char *maxPathBuffer) {
|
|
|
|
|
catStrings(state->onePastLastExeFilenameSlash - state->exeFilename, state->exeFilename, filenameSize, filename, (size_t)MAX_PATH, maxPathBuffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define WINDOW_WIDTH 1280
|
|
|
|
|
#define WINDOW_HEIGHT 720
|
|
|
|
|
|
|
|
|
|
int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLine, int commandShow) {
|
|
|
|
|
Win32State win32State = {};
|
|
|
|
|
win32GetExeFilename(&win32State);
|
|
|
|
|
|
|
|
|
|
char *sourceGameCodeDLLFilename = "handmade.dll";
|
|
|
|
|
char sourceGameCodeDLLFullPath[MAX_PATH];
|
|
|
|
|
catStrings(onePastLastSlash - exeFileName, exeFileName,
|
|
|
|
|
sizeof(sourceGameCodeDLLFilename) - 1, sourceGameCodeDLLFilename,
|
|
|
|
|
sizeof(sourceGameCodeDLLFullPath) - 1, sourceGameCodeDLLFullPath);
|
|
|
|
|
win32GetFullPathToLocalFile(&win32State, sourceGameCodeDLLFilename, sizeof(sourceGameCodeDLLFullPath) - 1, sourceGameCodeDLLFullPath);
|
|
|
|
|
|
|
|
|
|
char tempGameCodeDLLFilename[] = "handmade_temp.dll";
|
|
|
|
|
char tempGameCodeDLLFullPath[MAX_PATH];
|
|
|
|
|
catStrings(onePastLastSlash - exeFileName, exeFileName,
|
|
|
|
|
sizeof(tempGameCodeDLLFilename) - 1, tempGameCodeDLLFilename,
|
|
|
|
|
sizeof(tempGameCodeDLLFullPath) - 1, tempGameCodeDLLFullPath);
|
|
|
|
|
win32GetFullPathToLocalFile(&win32State, tempGameCodeDLLFilename, sizeof(tempGameCodeDLLFullPath) - 1, tempGameCodeDLLFullPath);
|
|
|
|
|
|
|
|
|
|
LARGE_INTEGER performanceFrequencyResult;
|
|
|
|
|
QueryPerformanceFrequency(&performanceFrequencyResult);
|
|
|
|
|
@@ -570,40 +546,45 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
|
|
|
|
|
windowClass.lpfnWndProc = &mainWindowCallback;
|
|
|
|
|
windowClass.hInstance = instance;
|
|
|
|
|
windowClass.lpszClassName = "HandmadeHeroWindowClass";
|
|
|
|
|
|
|
|
|
|
resizeDIBSection(&globalBackBuffer, 1280, 720);
|
|
|
|
|
|
|
|
|
|
real32 targetSecondsPerFrame = 1.0f / (real32)gameUpdateHz;
|
|
|
|
|
resizeDIBSection(&globalBackBuffer, WINDOW_WIDTH, WINDOW_HEIGHT);
|
|
|
|
|
|
|
|
|
|
if (RegisterClass(&windowClass)) {
|
|
|
|
|
DWORD windowFlags = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
|
|
|
|
|
RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
|
|
|
|
|
AdjustWindowRectEx(&rect, windowFlags, false, NULL);
|
|
|
|
|
HWND window = CreateWindowExA(
|
|
|
|
|
NULL,
|
|
|
|
|
windowClass.lpszClassName,
|
|
|
|
|
"Handmade Hero",
|
|
|
|
|
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
|
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
|
windowFlags,
|
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
|
rect.right - rect.left,
|
|
|
|
|
rect.bottom - rect.top,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
instance,
|
|
|
|
|
NULL
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (window) {
|
|
|
|
|
int monitorRefreshHz = 60;
|
|
|
|
|
int win32RefreshRate = GetDeviceCaps(GetDC(window), VREFRESH);
|
|
|
|
|
if (win32RefreshRate > 1) {
|
|
|
|
|
monitorRefreshHz = win32RefreshRate;
|
|
|
|
|
}
|
|
|
|
|
real32 gameUpdateHz = monitorRefreshHz / 2.0f;
|
|
|
|
|
real32 targetSecondsPerFrame = 1.0f / (real32)gameUpdateHz;
|
|
|
|
|
|
|
|
|
|
HH_CTRLW = GlobalAddAtomA("HH_CTRLW");
|
|
|
|
|
RegisterHotKey(window, HH_CTRLW, MOD_CONTROL, 'W');
|
|
|
|
|
|
|
|
|
|
globalRunning = true;
|
|
|
|
|
Win32State win32State = {};
|
|
|
|
|
|
|
|
|
|
GameInput input[2] = {};
|
|
|
|
|
GameInput *oldInput = &input[0];
|
|
|
|
|
GameInput *newInput = &input[1];
|
|
|
|
|
|
|
|
|
|
int debugTimeMarkerIndex = 0;
|
|
|
|
|
Win32DebugTimeMarker debugMarkers[gameUpdateHz / 2] = {};
|
|
|
|
|
|
|
|
|
|
win32LoadXInput();
|
|
|
|
|
|
|
|
|
|
// sound test
|
|
|
|
|
@@ -612,7 +593,7 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
|
|
|
|
|
soundOutput.runningSampleIndex = 0;
|
|
|
|
|
soundOutput.bytesPerSample = sizeof(int16)*2;
|
|
|
|
|
soundOutput.secondaryBufferSize = soundOutput.samplesPerSecond*soundOutput.bytesPerSample;
|
|
|
|
|
soundOutput.safetyBytes = (soundOutput.samplesPerSecond * soundOutput.bytesPerSample / gameUpdateHz) / 2;
|
|
|
|
|
soundOutput.safetyBytes = (int)((soundOutput.samplesPerSecond * soundOutput.bytesPerSample / gameUpdateHz) / 2.0f);
|
|
|
|
|
|
|
|
|
|
int16 *samples = (int16*)VirtualAlloc(NULL, soundOutput.secondaryBufferSize, MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
|
|
|
|
|
|
@@ -623,7 +604,7 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
|
|
|
|
|
#endif
|
|
|
|
|
GameMemory gameMemory = {};
|
|
|
|
|
gameMemory.permanentStorageSize = Megabytes(64);
|
|
|
|
|
gameMemory.transientStorageSize = Gigabytes((uint64)4);
|
|
|
|
|
gameMemory.transientStorageSize = Gigabytes((uint64)1);
|
|
|
|
|
gameMemory.debugFreeFileMemory = debugFreeFileMemory;
|
|
|
|
|
gameMemory.debugWriteEntireFile = debugWriteEntireFile;
|
|
|
|
|
gameMemory.debugReadEntireFile = debugReadEntireFile;
|
|
|
|
|
@@ -634,6 +615,25 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
|
|
|
|
|
gameMemory.permanentStorage = win32State.gameMemoryBlock;
|
|
|
|
|
gameMemory.transientStorage = ((uint8 *)gameMemory.permanentStorage + gameMemory.permanentStorageSize);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ArrayCount(win32State.replayBuffers); i++) {
|
|
|
|
|
// Filename
|
|
|
|
|
char prefix[] = "replay-";
|
|
|
|
|
char suffix[] = ".hmr";
|
|
|
|
|
const int totalLen = ArrayCount(prefix) + 1 + ArrayCount(suffix);
|
|
|
|
|
char replayFilename[totalLen] = {};
|
|
|
|
|
char digit[] = {(char)(48 + i)};
|
|
|
|
|
catStrings(ArrayCount(prefix) - 1, prefix, 1, digit, ArrayCount(replayFilename) - 1, replayFilename);
|
|
|
|
|
catStrings(ArrayCount(prefix), replayFilename, ArrayCount(suffix) - 1, suffix, ArrayCount(replayFilename) - 1, replayFilename);
|
|
|
|
|
win32GetFullPathToLocalFile(&win32State, replayFilename, sizeof(replayFilename) - 1, win32State.replayBuffers[i].replayFilename);
|
|
|
|
|
Win32ReplayBuffer *replayBuffer = &win32State.replayBuffers[i];
|
|
|
|
|
|
|
|
|
|
replayBuffer->fileHandle = CreateFileA(replayBuffer->replayFilename, GENERIC_WRITE | GENERIC_READ, NULL, NULL, CREATE_ALWAYS, NULL, NULL);
|
|
|
|
|
DWORD maxSizeLow = (win32State.gameMemoryTotalSize & 0xFFFFFFFF);
|
|
|
|
|
DWORD maxSizeHigh = (win32State.gameMemoryTotalSize >> 32);
|
|
|
|
|
replayBuffer->memoryMap = CreateFileMappingA(replayBuffer->fileHandle, NULL, PAGE_READWRITE, maxSizeHigh, maxSizeLow, NULL);
|
|
|
|
|
replayBuffer->memoryBlock = MapViewOfFile(replayBuffer->memoryMap, FILE_MAP_ALL_ACCESS, NULL, NULL, win32State.gameMemoryTotalSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
win32InitSound(window, soundOutput.samplesPerSecond, soundOutput.secondaryBufferSize);
|
|
|
|
|
win32ClearBuffer(&soundOutput);
|
|
|
|
|
globalSecondaryBuffer->Play(0, 0, DSBPLAY_LOOPING);
|
|
|
|
|
@@ -641,7 +641,6 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
|
|
|
|
|
LARGE_INTEGER lastWorkCounter = win32GetWallClock();
|
|
|
|
|
LARGE_INTEGER flipWallClock = win32GetWallClock();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DWORD audioLatencyBytes = 0;
|
|
|
|
|
real32 audioLatencySeconds = 0;
|
|
|
|
|
bool soundIsValid = false;
|
|
|
|
|
@@ -666,6 +665,18 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
|
|
|
|
|
|
|
|
|
|
win32ProcessPendingMessages(&win32State, newKeyboardController);
|
|
|
|
|
|
|
|
|
|
POINT mousePos;
|
|
|
|
|
GetCursorPos(&mousePos);
|
|
|
|
|
ScreenToClient(window, &mousePos);
|
|
|
|
|
newInput->mouseX = mousePos.x;
|
|
|
|
|
newInput->mouseY = mousePos.y;
|
|
|
|
|
newInput->mouseZ = 0; // mouse wheel
|
|
|
|
|
win32ProcessKeyboardKeypress(&newInput->mouseButtons[0], GetKeyState(VK_LBUTTON) & (1 << 15));
|
|
|
|
|
win32ProcessKeyboardKeypress(&newInput->mouseButtons[1], GetKeyState(VK_MBUTTON) & (1 << 15));
|
|
|
|
|
win32ProcessKeyboardKeypress(&newInput->mouseButtons[2], GetKeyState(VK_RBUTTON) & (1 << 15));
|
|
|
|
|
win32ProcessKeyboardKeypress(&newInput->mouseButtons[3], GetKeyState(VK_XBUTTON1) & (1 << 15));
|
|
|
|
|
win32ProcessKeyboardKeypress(&newInput->mouseButtons[4], GetKeyState(VK_XBUTTON2) & (1 << 15));
|
|
|
|
|
|
|
|
|
|
XINPUT_STATE controllerState;
|
|
|
|
|
int maxControllerCount = XUSER_MAX_COUNT;
|
|
|
|
|
if (maxControllerCount > ArrayCount(newInput->controllers) - 1) {
|
|
|
|
|
@@ -728,6 +739,8 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ThreadContext threadCtx = {};
|
|
|
|
|
|
|
|
|
|
GameOffscreenBuffer videoBuffer = {};
|
|
|
|
|
videoBuffer.memory = globalBackBuffer.memory;
|
|
|
|
|
videoBuffer.width = globalBackBuffer.width;
|
|
|
|
|
@@ -742,20 +755,25 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
|
|
|
|
|
win32PlaybackInput(&win32State, newInput);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
game.updateAndRender(&gameMemory, &videoBuffer, newInput);
|
|
|
|
|
if (game.updateAndRender) {
|
|
|
|
|
game.updateAndRender(&threadCtx, &gameMemory, &videoBuffer, newInput);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LARGE_INTEGER audioWallClock = win32GetWallClock();
|
|
|
|
|
real32 fromBeginToAudioSeconds = win32GetSecondsElapsed(flipWallClock, audioWallClock);
|
|
|
|
|
|
|
|
|
|
DWORD playCursor = 0;
|
|
|
|
|
DWORD writeCursor = 0;
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(globalSecondaryBuffer->GetCurrentPosition(&playCursor, &writeCursor))) {
|
|
|
|
|
if (!soundIsValid) {
|
|
|
|
|
soundOutput.runningSampleIndex = writeCursor / soundOutput.bytesPerSample;
|
|
|
|
|
soundIsValid = true;
|
|
|
|
|
}
|
|
|
|
|
DWORD byteToLock = (soundOutput.runningSampleIndex*soundOutput.bytesPerSample) % soundOutput.secondaryBufferSize;
|
|
|
|
|
|
|
|
|
|
DWORD expectedSoundBytesPerFrame = (soundOutput.samplesPerSecond * soundOutput.bytesPerSample) / gameUpdateHz;
|
|
|
|
|
DWORD expectedFrameBoundaryByte = playCursor + expectedSoundBytesPerFrame;
|
|
|
|
|
DWORD expectedSoundBytesPerFrame = (DWORD)((soundOutput.samplesPerSecond * soundOutput.bytesPerSample) / gameUpdateHz);
|
|
|
|
|
real32 secondsLeftUntilFlip = targetSecondsPerFrame - fromBeginToAudioSeconds;
|
|
|
|
|
DWORD expectedBytesUntilFlip = (DWORD)((secondsLeftUntilFlip/targetSecondsPerFrame) * (real32)expectedSoundBytesPerFrame);
|
|
|
|
|
DWORD expectedFrameBoundaryByte = playCursor + expectedBytesUntilFlip;
|
|
|
|
|
|
|
|
|
|
DWORD safeWriteCursor = writeCursor;
|
|
|
|
|
if (safeWriteCursor < playCursor) {
|
|
|
|
|
@@ -785,25 +803,7 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
|
|
|
|
|
soundBuffer.samplesPerSecond = soundOutput.samplesPerSecond;
|
|
|
|
|
soundBuffer.sampleCount = bytesToWrite / soundOutput.bytesPerSample;
|
|
|
|
|
soundBuffer.samples = samples;
|
|
|
|
|
game.getSoundSamples(&gameMemory, &soundBuffer);
|
|
|
|
|
#if HANDMADE_INTERNAL
|
|
|
|
|
Assert(debugTimeMarkerIndex < ArrayCount(debugMarkers));
|
|
|
|
|
|
|
|
|
|
Win32DebugTimeMarker *marker = &debugMarkers[debugTimeMarkerIndex++];
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
game.getSoundSamples(&threadCtx, &gameMemory, &soundBuffer);
|
|
|
|
|
win32FillSoundBuffer(&soundOutput, byteToLock, bytesToWrite, &soundBuffer);
|
|
|
|
|
} else {
|
|
|
|
|
soundIsValid = false;
|
|
|
|
|
@@ -827,10 +827,6 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
|
|
|
|
|
|
|
|
|
|
lastWorkCounter = win32GetWallClock();
|
|
|
|
|
|
|
|
|
|
#if HANDMADE_INTERNAL
|
|
|
|
|
win32DebugSyncDisplay(&globalBackBuffer, ArrayCount(debugMarkers), debugMarkers, &soundOutput, targetSecondsPerFrame);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
win32DrawBufferInWindow(&globalBackBuffer, window);
|
|
|
|
|
flipWallClock = win32GetWallClock();
|
|
|
|
|
|
|
|
|
|
|