From 2c359bae0fb3fe6910869011a89224e099743cf6 Mon Sep 17 00:00:00 2001 From: Ledda Date: Thu, 12 Sep 2024 23:05:44 +0100 Subject: [PATCH] update --- misc/build.bat | 4 +-- src/handmade.cpp | 10 +------ src/win32_handmade.cpp | 63 +++++++++++++++++++++++++++++++++++------- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/misc/build.bat b/misc/build.bat index 687c4ad..709ba73 100644 --- a/misc/build.bat +++ b/misc/build.bat @@ -16,8 +16,8 @@ set commonCompilerFlags=^ pushd .\build -cl %commonCompilerFlags% -Fe:handmade.dll ..\src\handmade.cpp -Fmhandmade.map /link /DLL /EXPORT:gameGetSoundSamples /EXPORT:gameUpdateAndRender -cl %commonCompilerFlags% -Fe:handmade_win32.exe ..\src\win32_handmade.cpp -Fmwin32_handmade.map /link %commonLinkerFlags% +cl %commonCompilerFlags% ..\src\handmade.cpp -Fmhandmade.map -LD /link -incremental:no -EXPORT:gameGetSoundSamples -EXPORT:gameUpdateAndRender +cl %commonCompilerFlags% -Fe:handmade_win32.exe ..\src\win32_handmade.cpp -Fmwin32_handmade.map /link -incremental:no %commonLinkerFlags% popd exit /b diff --git a/src/handmade.cpp b/src/handmade.cpp index 0648053..ac4e70b 100644 --- a/src/handmade.cpp +++ b/src/handmade.cpp @@ -24,7 +24,7 @@ internal void renderWeirdGradient(GameOffscreenBuffer *buffer, int xOffset, int for (int x = 0; x < buffer->width; x++) { uint8 blue = (uint8)(x + xOffset); uint8 green = (uint8)(y + yOffset); - *pixel++ = (green << 16) | blue; + *pixel++ = (green << 8) | blue; } row += pitch; } @@ -77,11 +77,3 @@ extern "C" GAME_UPDATE_AND_RENDER(gameUpdateAndRender) { extern "C" GAME_GET_SOUND_SAMPLES(gameGetSoundSamples) { outputSineSound(soundBuf, (GameState*)memory->permanentStorage); } - -#if HANDMADE_WIN32 -#include "windows.h" - -BOOL DllMain() { - return TRUE; -} -#endif diff --git a/src/win32_handmade.cpp b/src/win32_handmade.cpp index b6324ee..a06fec6 100644 --- a/src/win32_handmade.cpp +++ b/src/win32_handmade.cpp @@ -143,13 +143,27 @@ struct Win32GameCode { GameUpdateAndRenderFn *updateAndRender; GameGetSoundSamplesFn *getSoundSamples; bool isValid; + FILETIME lastWriteTime; }; -internal Win32GameCode win32LoadGameCode() { +inline FILETIME win32GetLastWriteTime(char *filename) { + FILETIME lastWriteTime = {}; + WIN32_FIND_DATA findData; + HANDLE findHandle = FindFirstFileA(filename, &findData); + if (findHandle != INVALID_HANDLE_VALUE) { + lastWriteTime = findData.ftLastWriteTime; + FindClose(findHandle); + } + return lastWriteTime; +} + +internal Win32GameCode win32LoadGameCode(char *filename, char *tempname) { Win32GameCode result = {}; - CopyFile("handmade.dll", "handmade_temp.dll", FALSE); - HMODULE gameCodeLib = LoadLibrary("handmade_temp.dll"); + result.lastWriteTime = win32GetLastWriteTime(filename); + + CopyFile(filename, tempname, FALSE); + HMODULE gameCodeLib = LoadLibrary(tempname); result.gameCodeLib = gameCodeLib; if (gameCodeLib) { @@ -453,7 +467,38 @@ internal void win32DebugSyncDisplay(Win32OffscreenBuffer *screenBuffer, int mark } } +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++; + } + for (int i = 0; i < sourceBCount; i++) { + *dest++ = *sourceB++; + } + *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++) { + if (*scan == '\\') { + onePastLastSlash = scan + 1; + } + } + + char sourceGameCodeDLLFilename[] = "handmade.dll"; + char sourceGameCodeDLLFullPath[MAX_PATH]; + catStrings(onePastLastSlash - exeFileName, exeFileName, + sizeof(sourceGameCodeDLLFilename) - 1, 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); + LARGE_INTEGER performanceFrequencyResult; QueryPerformanceFrequency(&performanceFrequencyResult); globalPerfCountFrequency = performanceFrequencyResult.QuadPart; @@ -506,7 +551,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) / 3; + soundOutput.safetyBytes = (soundOutput.samplesPerSecond * soundOutput.bytesPerSample / gameUpdateHz) / 2; int16 *samples = (int16*)VirtualAlloc(NULL, soundOutput.secondaryBufferSize, MEM_COMMIT, PAGE_READWRITE); @@ -539,16 +584,14 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin real32 audioLatencySeconds = 0; bool soundIsValid = false; - Win32GameCode game = win32LoadGameCode(); - uint32 loadCounter = 0; + Win32GameCode game = win32LoadGameCode(sourceGameCodeDLLFullPath, tempGameCodeDLLFullPath); int64 lastCycleCount = __rdtsc(); while (globalRunning) { - if (loadCounter++ > 60) { + FILETIME newWriteTime = win32GetLastWriteTime(sourceGameCodeDLLFullPath); + if (CompareFileTime(&newWriteTime, &game.lastWriteTime) != 0) { win32UnloadGameCode(&game); - game = win32LoadGameCode(); - loadCounter = 0; - // TODO(dledda): handmade hero episode 22 (from the beginning) + game = win32LoadGameCode(sourceGameCodeDLLFullPath, tempGameCodeDLLFullPath); } GameControllerInput *oldKeyboardController = &oldInput->controllers[0];