Ledda пре 4 месеци
родитељ
комит
374bfc8bf1
5 измењених фајлова са 91 додато и 29 уклоњено
  1. +6
    -0
      HidCerberus.Lib.log
  2. +1
    -1
      misc/build.bat
  3. +20
    -2
      src/handmade.cpp
  4. +7
    -1
      src/handmade.h
  5. +57
    -25
      src/win32_handmade.cpp

+ 6
- 0
HidCerberus.Lib.log Прегледај датотеку

@@ -0,0 +1,6 @@
2024-07-08 21:41:59.339 HidCerberus.Lib:HidGuardianOpen [Information]: Sending add request: http://localhost:26762/api/v1/hidguardian/whitelist/add/7440
2024-07-09 18:33:47.443 HidCerberus.Lib:HidGuardianOpen [Information]: Sending add request: http://localhost:26762/api/v1/hidguardian/whitelist/add/7156
2024-07-09 18:33:47.854 HidCerberus.Lib:HidGuardianClose [Information]: Sending remove request: http://localhost:26762/api/v1/hidguardian/whitelist/remove/7156
2024-07-09 18:34:16.211 HidCerberus.Lib:HidGuardianOpen [Information]: Sending add request: http://localhost:26762/api/v1/hidguardian/whitelist/add/4240
2024-07-09 18:34:16.316 HidCerberus.Lib:HidGuardianClose [Information]: Sending remove request: http://localhost:26762/api/v1/hidguardian/whitelist/remove/4240
2024-07-09 18:34:23.477 HidCerberus.Lib:HidGuardianOpen [Information]: Sending add request: http://localhost:26762/api/v1/hidguardian/whitelist/add/7712

+ 1
- 1
misc/build.bat Прегледај датотеку

@@ -1,5 +1,5 @@
@echo off
mkdir .\build
if NOT EXIST .\build mkdir .\build
pushd .\build
pwd
cl -FC -Zi -Fe:handmade.exe ..\src\win32_handmade.cpp user32.lib Gdi32.lib


+ 20
- 2
src/handmade.cpp Прегледај датотеку

@@ -1,5 +1,7 @@
#include "handmade.h"

#define PI32 3.141592653589f

void debug_printf(wchar_t* format, ...) {
int bufsize = wcslen(format)*10;
wchar_t *output = (wchar_t*)malloc(bufsize);
@@ -10,6 +12,21 @@ void debug_printf(wchar_t* format, ...) {
free(output);
}

internal void outputSound(GameSoundOutputBuffer *soundBuffer) {
local_persist real32 tSine;
int16 toneVolume = 3000;
int toneHz = 440;
int wavePeriod = soundBuffer->samplesPerSecond/toneHz;

int16 *sampleOut = soundBuffer->samples;
for (int sampleIndex = 0; sampleIndex < soundBuffer->sampleCount; sampleIndex++) {
int16 sampleValue = (int16)(sin(tSine) * (real32)toneVolume);
*sampleOut++ = sampleValue;
*sampleOut++ = sampleValue;
tSine += 2.0f * PI32 / (real32)wavePeriod;
}
}

internal void renderWeirdGradient(GameOffscreenBuffer *buffer, int xOffset, int yOffset) {
int bytesPerPixel = 4;
int pitch = buffer->width*bytesPerPixel;
@@ -25,6 +42,7 @@ internal void renderWeirdGradient(GameOffscreenBuffer *buffer, int xOffset, int
}
}

internal void gameUpdateAndRender(GameOffscreenBuffer *buffer, GameInput *input) {
renderWeirdGradient(buffer, input->xOffset, input->yOffset);
internal void gameUpdateAndRender(GameOffscreenBuffer *videoBuf, GameInput *input, GameSoundOutputBuffer *soundBuf) {
outputSound(soundBuf);
renderWeirdGradient(videoBuf, input->xOffset, input->yOffset);
}

+ 7
- 1
src/handmade.h Прегледај датотеку

@@ -33,12 +33,18 @@ struct GameInput {
int yOffset;
};

struct GameSoundOutputBuffer {
int samplesPerSecond;
int sampleCount;
int16 *samples;
};

struct GameOffscreenBuffer {
void *memory;
int width;
int height;
};

void gameUpdateAndRender();
void gameUpdateAndRender(GameOffscreenBuffer *videoBuf, GameInput *input, GameSoundOutputBuffer *soundBuf);

// Platform to game services

+ 57
- 25
src/win32_handmade.cpp Прегледај датотеку

@@ -18,8 +18,6 @@ struct Win32WindowDimensions {
int height;
};

#define PI32 3.141592653589f

global ATOM HH_CTRLW;
global bool running;
global Win32OffscreenBuffer globalBackBuffer;
@@ -41,6 +39,8 @@ global XInputSetStateFn *XInputSetStateDyn = XInputSetStateStub;

typedef HRESULT WINAPI DirectSoundCreateFn(LPCGUID pcGuidDevice, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter);

#define stackAlloc(size, type) (type*)_alloca(size*sizeof(type))

internal void resizeDIBSection(Win32OffscreenBuffer *buffer, int width, int height) {
if (buffer->memory) {
VirtualFree(buffer->memory, NULL, MEM_RELEASE);
@@ -228,7 +228,28 @@ struct Win32SoundOutput {
int latencySampleCount;
};

internal void win32FillSoundBuffer(Win32SoundOutput *soundOutput, DWORD byteToLock, DWORD bytesToWrite) {
internal void win32ClearBuffer(Win32SoundOutput *soundOutput) {
VOID *region1;
DWORD region1Size;
VOID *region2;
DWORD region2Size;
HRESULT lock = globalSecondaryBuffer->Lock(0, soundOutput->secondaryBufferSize, &region1, &region1Size, &region2, &region2Size, 0);
if (SUCCEEDED(lock)) {
int16 *destSamples = (int16*)region1;
for (DWORD byteIndex = 0; byteIndex < region1Size; byteIndex++) {
// *destSamples++ = 0;
}
destSamples = (int16*)region2;
for (DWORD byteIndex = 0; byteIndex < region2Size; byteIndex++) {
//*destSamples++ = 0;
}
globalSecondaryBuffer->Unlock(region1, region1Size, region2, region2Size);
} else {
// no lock!
}
}

internal void win32FillSoundBuffer(Win32SoundOutput *soundOutput, DWORD byteToLock, DWORD bytesToWrite, GameSoundOutputBuffer *sourceBuffer) {
VOID *region1;
DWORD region1Size;
VOID *region2;
@@ -236,21 +257,18 @@ internal void win32FillSoundBuffer(Win32SoundOutput *soundOutput, DWORD byteToLo
HRESULT lock = globalSecondaryBuffer->Lock(byteToLock, bytesToWrite, &region1, &region1Size, &region2, &region2Size, 0);
if (SUCCEEDED(lock)) {
DWORD region1SampleCount = region1Size/soundOutput->bytesPerSample;
int16 *sampleOut = (int16*)region1;
int16 *destSamples = (int16*)region1;
int16 *sourceSamples = sourceBuffer->samples;
for (DWORD sampleIndex = 0; sampleIndex < region1SampleCount; sampleIndex++) {
int16 sampleValue = (int16)(sin(soundOutput->tSine) * (real32)soundOutput->toneVolume);
*sampleOut++ = sampleValue;
*sampleOut++ = sampleValue;
soundOutput->tSine += 2.0f * PI32 / (real32)soundOutput->wavePeriod;
*destSamples++ = *sourceSamples++;
*destSamples++ = *sourceSamples++;
soundOutput->runningSampleIndex++;
}
DWORD region2SampleCount = region2Size/soundOutput->bytesPerSample;
sampleOut = (int16*)region2;
destSamples = (int16*)region2;
for (DWORD sampleIndex = 0; sampleIndex < region2SampleCount; sampleIndex++) {
int16 sampleValue = (int16)(sin(soundOutput->tSine) * (real32)soundOutput->toneVolume);
*sampleOut++ = sampleValue;
*sampleOut++ = sampleValue;
soundOutput->tSine += 2.0f * PI32 / (real32)soundOutput->wavePeriod;
*destSamples++ = *sourceSamples++;
*destSamples++ = *sourceSamples++;
soundOutput->runningSampleIndex++;
}
globalSecondaryBuffer->Unlock(region1, region1Size, region2, region2Size);
@@ -294,6 +312,8 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
MSG message;
running = true;

win32LoadXInput();

// graphics test
int xOffset = 0;
int yOffset = 0;
@@ -309,10 +329,10 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
soundOutput.secondaryBufferSize = soundOutput.samplesPerSecond*soundOutput.bytesPerSample;
soundOutput.latencySampleCount = soundOutput.samplesPerSecond / 15.0f;

win32LoadXInput();
win32InitSound(window, soundOutput.samplesPerSecond, soundOutput.secondaryBufferSize);
int16 *samples = (int16*)VirtualAlloc(NULL, soundOutput.secondaryBufferSize, MEM_COMMIT, PAGE_READWRITE);

win32FillSoundBuffer(&soundOutput, 0, soundOutput.latencySampleCount*soundOutput.bytesPerSample);
win32InitSound(window, soundOutput.samplesPerSecond, soundOutput.secondaryBufferSize);
win32ClearBuffer(&soundOutput);
globalSecondaryBuffer->Play(0, 0, DSBPLAY_LOOPING);

LARGE_INTEGER lastCounter;
@@ -367,23 +387,25 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
soundOutput.toneHz = 220 + (stickYNorm + 1.0)/2.0 * 220.0;
soundOutput.wavePeriod = soundOutput.samplesPerSecond / soundOutput.toneHz;

GameOffscreenBuffer buffer = {};
buffer.memory = globalBackBuffer.memory;
buffer.width = globalBackBuffer.width;
buffer.height = globalBackBuffer.height;
GameOffscreenBuffer videoBuffer = {};
videoBuffer.memory = globalBackBuffer.memory;
videoBuffer.width = globalBackBuffer.width;
videoBuffer.height = globalBackBuffer.height;

GameInput input = {};
input.xOffset = xOffset;
input.yOffset = yOffset;
gameUpdateAndRender(&buffer, &input);

// Sound test
DWORD playCursor;
DWORD writeCursor;
DWORD byteToLock;
DWORD targetCursor;
DWORD bytesToWrite;
bool soundIsValid = true;
if (SUCCEEDED(globalSecondaryBuffer->GetCurrentPosition(&playCursor, &writeCursor))) {
DWORD byteToLock = (soundOutput.runningSampleIndex*soundOutput.bytesPerSample) % soundOutput.secondaryBufferSize;
DWORD targetCursor = (playCursor + soundOutput.latencySampleCount*soundOutput.bytesPerSample) % soundOutput.secondaryBufferSize;
DWORD bytesToWrite;
byteToLock = (soundOutput.runningSampleIndex*soundOutput.bytesPerSample) % soundOutput.secondaryBufferSize;
targetCursor = (playCursor + soundOutput.latencySampleCount*soundOutput.bytesPerSample) % soundOutput.secondaryBufferSize;
if (byteToLock == targetCursor) {
bytesToWrite = 0;
} else if (byteToLock > targetCursor) {
@@ -391,9 +413,19 @@ int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prevInstance, PSTR commandLin
} else {
bytesToWrite = targetCursor - byteToLock;
}
win32FillSoundBuffer(&soundOutput, byteToLock, bytesToWrite);
soundIsValid = true;
}

GameSoundOutputBuffer soundBuffer = {};
soundBuffer.samplesPerSecond = soundOutput.samplesPerSecond;
soundBuffer.sampleCount = bytesToWrite / soundOutput.bytesPerSample;
soundBuffer.samples = samples;

gameUpdateAndRender(&videoBuffer, &input, &soundBuffer);

if (soundIsValid) {
win32FillSoundBuffer(&soundOutput, byteToLock, bytesToWrite, &soundBuffer);
}
win32DrawBufferInWindow(&globalBackBuffer, window);

LARGE_INTEGER endCounter;


Loading…
Откажи
Сачувај