Sfoglia il codice sorgente

update

tags/day-25
Ledda 2 mesi fa
parent
commit
2c359bae0f
3 ha cambiato i file con 56 aggiunte e 21 eliminazioni
  1. +2
    -2
      misc/build.bat
  2. +1
    -9
      src/handmade.cpp
  3. +53
    -10
      src/win32_handmade.cpp

+ 2
- 2
misc/build.bat Vedi File

@@ -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



+ 1
- 9
src/handmade.cpp Vedi File

@@ -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

+ 53
- 10
src/win32_handmade.cpp Vedi File

@@ -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];


Caricamento…
Annulla
Salva