add small directsound demo

This commit is contained in:
Zack Buhman 2025-12-21 23:04:31 -06:00
parent 55062cba98
commit 47ce3ee503
3 changed files with 78 additions and 5 deletions

View File

@ -14,7 +14,7 @@ CFLAGS += -Wno-error=unused-function
CFLAGS += -Wno-error=unused-variable CFLAGS += -Wno-error=unused-variable
CFLAGS += -DUNICODE CFLAGS += -DUNICODE
LDFLAGS += -Wl,--subsystem,windows -mwindows -mconsole -municode -ld3d9 -ld3dx9 -lwinmm -ldinput8 -ldxguid LDFLAGS += -Wl,--subsystem,windows -mwindows -mconsole -municode -ld3d9 -ld3dx9 -lwinmm -ldinput8 -ldxguid -ldsound -luuid
OPT = -Og OPT = -Og

View File

@ -4,6 +4,10 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <dinput.h> #include <dinput.h>
#include <dsound.h>
#include <assert.h>
#include <comdef.h>
struct CUSTOMVERTEX struct CUSTOMVERTEX
{ {
@ -29,6 +33,8 @@ IDirect3DVertexShader9 * g_pVertexShader = NULL;
IDirectInput8 * g_pDI = NULL; IDirectInput8 * g_pDI = NULL;
IDirectInputDevice8 * g_pdiDevice; IDirectInputDevice8 * g_pdiDevice;
IDirectSound8 * g_pDS = NULL;
HRESULT InitDirect3D(HWND hwnd) HRESULT InitDirect3D(HWND hwnd)
{ {
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
@ -188,14 +194,78 @@ HRESULT UpdateInput()
return S_OK; return S_OK;
} }
HRESULT InitDirectSound(HWND hwnd)
{
HRESULT hr;
hr = DirectSoundCreate8(NULL,
&g_pDS,
NULL);
if (FAILED(hr)) {
fprintf(stderr, "DirectSoundCreate8\n");
return hr;
}
hr = g_pDS->SetCooperativeLevel(hwnd, DSSCL_PRIORITY);
if (FAILED(hr)) {
fprintf(stderr, "SetCooperativeLevel\n");
return hr;
}
LPDIRECTSOUNDBUFFER pDsb = NULL;
WAVEFORMATEX wfx;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 2;
wfx.nSamplesPerSec = 44100;
wfx.nBlockAlign = 4;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
wfx.wBitsPerSample = 16;
DSBUFFERDESC desc = {};
desc.dwSize = (sizeof (DSBUFFERDESC));
desc.dwFlags = DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
desc.dwBufferBytes = 4 * wfx.nAvgBytesPerSec;
desc.guid3DAlgorithm = DS3DALG_DEFAULT;
desc.lpwfxFormat = &wfx;
hr = g_pDS->CreateSoundBuffer(&desc, &pDsb, NULL);
if (FAILED(hr)) {
fprintf(stderr, "CreateSoundBuffer\n");
_com_error err(hr);
LPCTSTR errMsg = err.ErrorMessage();
fprintf(stderr, "%ls\n", errMsg);
fflush(stderr);
return hr;
}
void * pBuffer;
DWORD dwBufferSize;
pDsb->Lock(0, desc.dwBufferBytes,
&pBuffer, &dwBufferSize,
NULL, NULL, 0);
FILE * fp = fopen("shire.pcm", "rb");
assert(fp != NULL);
fread(pBuffer, 1, dwBufferSize, fp);
fclose(fp);
pDsb->Unlock(pBuffer, dwBufferSize, NULL, 0);
pDsb->Play(0, 0, DSBPLAY_LOOPING);
while (1);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{ {
if (0) {
HRESULT hr; HRESULT hr;
hr = InitDirectInput(hInstance); hr = InitDirectInput(hInstance);
if (FAILED(hr)) if (FAILED(hr))
return 0; return 0;
if (0) {
while (true) { while (true) {
hr = UpdateInput(); hr = UpdateInput();
if (FAILED(hr)) if (FAILED(hr))
@ -229,6 +299,9 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
return 0; return 0;
} }
InitDirectSound(hwnd);
return 0;
if (!SUCCEEDED(InitDirect3D(hwnd))) if (!SUCCEEDED(InitDirect3D(hwnd)))
return 0; return 0;

BIN
shire.pcm Normal file

Binary file not shown.