-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin32.c
More file actions
279 lines (230 loc) · 7.76 KB
/
win32.c
File metadata and controls
279 lines (230 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <windows.h>
#include <dsound.h>
typedef struct {
IDirectSoundBuffer *SecondaryBuffer;
HANDLE Thread;
DWORD BytesPerSample;
DWORD BufferSize_Samples;
DWORD Index_Samples;
DWORD Ahead_Samples;
} win32_audio;
typedef struct {
HWND Window;
win32_audio Audio;
UINT_PTR FrameTimer;
} win32_setup;
typedef struct {
short Left;
short Right;
} audio_sample;
typedef struct {
const char *Title;
size_t Width;
size_t Height;
} win32_setup_info;
win32_setup_info CallbackSetup();
LRESULT CallbackEvent(win32_setup *Setup, HWND Window, UINT Msg, WPARAM wParam, LPARAM lParam);
void CallbackFrame(win32_setup *Setup, DWORD32 *FrameBuffer, int Width, int Height);
audio_sample CallbackGetSample(win32_setup *Setup);
void CallbackTeardown(win32_setup *Setup);
static win32_setup GLB_Setup;
static void __BlitToWindow(HWND Window)
{
RECT Rect;
GetWindowRect(Window, &Rect);
int Width = Rect.right-Rect.left;
int Height = Rect.bottom-Rect.top;
DWORD32 *Buffer = VirtualAlloc(NULL, Width*Height*4, MEM_COMMIT, PAGE_READWRITE);
CallbackFrame(&GLB_Setup, Buffer, Width, Height);
HDC DC = GetDC(Window);
BITMAPINFOHEADER BmpInfo = { 0 };
BmpInfo.biSize = sizeof BmpInfo;
BmpInfo.biPlanes = 1;
BmpInfo.biBitCount = 32;
BmpInfo.biWidth = Width;
BmpInfo.biHeight = -Height;
SetDIBitsToDevice(DC, 0, 0, Width, Height, 0, 0, 0, Height, Buffer, (BITMAPINFO*)&BmpInfo, DIB_RGB_COLORS);
ReleaseDC(Window, DC);
VirtualFree(Buffer, 0, MEM_RELEASE);
}
static void __AdvanceCursor(win32_audio *Audio)
{
Audio->Index_Samples++;
if (Audio->Index_Samples >= Audio->BufferSize_Samples)
{
Audio->Index_Samples = 0;
}
}
static void __FillBuffer(win32_setup *Setup, DWORD SamplesToWrite)
{
win32_audio *Audio = &Setup->Audio;
DWORD Cursor_Bytes = Audio->Index_Samples * Audio->BytesPerSample;
DWORD Write_Bytes = SamplesToWrite * Audio->BytesPerSample;
void *Region1, *Region2;
DWORD Bytes1, Bytes2;
if (SUCCEEDED(Audio->SecondaryBuffer->lpVtbl->Lock(Audio->SecondaryBuffer, Cursor_Bytes, Write_Bytes, &Region1, &Bytes1, &Region2, &Bytes2, 0)))
{
for (int i = 0; i < Bytes1/Audio->BytesPerSample; i++)
{
audio_sample Sample = CallbackGetSample(Setup);
((audio_sample*)Region1)[i] = Sample;
__AdvanceCursor(Audio);
}
for (int i = 0; i < Bytes2/Audio->BytesPerSample; i++)
{
audio_sample Sample = CallbackGetSample(Setup);
((audio_sample*)Region2)[i] = Sample;
__AdvanceCursor(Audio);
}
Audio->SecondaryBuffer->lpVtbl->Unlock(Audio->SecondaryBuffer, Region1, Bytes1, Region2, Bytes2);
}
}
static DWORD WINAPI __AudioThread(LPVOID Param)
{
win32_setup *Setup = (win32_setup*)Param;
win32_audio *Audio = &Setup->Audio;
while (1)
{
DWORD _PlayCursor, _WriteCursor;
if (SUCCEEDED(Audio->SecondaryBuffer->lpVtbl->GetCurrentPosition(Audio->SecondaryBuffer, &_PlayCursor, &_WriteCursor)))
{
DWORD PlayCursor = _PlayCursor/Audio->BytesPerSample;
DWORD WriteCursor = (Audio->Index_Samples % Audio->BufferSize_Samples);
DWORD TargetCursor = (PlayCursor + Audio->Ahead_Samples) % Audio->BufferSize_Samples;
DWORD SamplesToWrite = 0;
if (TargetCursor < WriteCursor)
{
SamplesToWrite = (Audio->BufferSize_Samples - WriteCursor) + TargetCursor;
}
else
{
SamplesToWrite = TargetCursor - WriteCursor;
}
__FillBuffer(Setup, SamplesToWrite);
}
Sleep(5);
}
return 0;
}
static void __DoFrame(HWND hwnd)
{
__BlitToWindow(hwnd);
}
static LRESULT CALLBACK __WndProc(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
)
{
switch (Msg) {
case WM_TIMER:
if (wParam == GLB_Setup.FrameTimer)
{
__DoFrame(hWnd);
}
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
TerminateThread(GLB_Setup.Audio.Thread, 0);
CallbackTeardown(&GLB_Setup);
PostQuitMessage(0);
break;
}
return CallbackEvent(&GLB_Setup, hWnd, Msg, wParam, lParam);
}
static void __FatalError(const char *Message)
{
MessageBoxA(NULL, Message, "Fatal Error", MB_OK);
ExitProcess(1);
}
static void __InitializeDirectsound(win32_setup *Setup)
{
const int HzRate = 44100;
const int BufferSize = HzRate*2*2;
IDirectSound *DirectSound;
if (FAILED(DirectSoundCreate(NULL, &DirectSound, NULL)))
{
__FatalError("DirectSoundCreate failed");
}
if (FAILED(DirectSound->lpVtbl->SetCooperativeLevel(DirectSound, Setup->Window, DSSCL_PRIORITY)))
{
__FatalError("SetCooperativeLevel failed");
}
DSBUFFERDESC BufferDesc = { 0 };
BufferDesc.dwSize = sizeof BufferDesc;
BufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
IDirectSoundBuffer *PrimaryBuffer;
if (FAILED(DirectSound->lpVtbl->CreateSoundBuffer(DirectSound, &BufferDesc, &PrimaryBuffer, NULL)))
{
__FatalError("CreateSoundBuffer failed");
}
WAVEFORMATEX Format = { 0 };
Format.wFormatTag = WAVE_FORMAT_PCM;
Format.nChannels = 2;
Format.nSamplesPerSec = HzRate;
Format.wBitsPerSample = 16;
Format.nBlockAlign = Format.nChannels*Format.wBitsPerSample/8;
Format.nAvgBytesPerSec = Format.nSamplesPerSec*Format.nBlockAlign;
if (FAILED(PrimaryBuffer->lpVtbl->SetFormat(PrimaryBuffer, &Format)))
{
__FatalError("SetFormat failed");
}
static DSBUFFERDESC PlaybackBufferDesc = { 0 };
PlaybackBufferDesc.dwSize = sizeof PlaybackBufferDesc;
PlaybackBufferDesc.dwBufferBytes = BufferSize;
PlaybackBufferDesc.lpwfxFormat = &Format;
PlaybackBufferDesc.dwFlags = DSBCAPS_GLOBALFOCUS|DSBCAPS_CTRLPOSITIONNOTIFY|DSBCAPS_GETCURRENTPOSITION2;
IDirectSoundBuffer *PlaybackBuffer;
if (FAILED(DirectSound->lpVtbl->CreateSoundBuffer(DirectSound, &PlaybackBufferDesc, &PlaybackBuffer, NULL)))
{
__FatalError("CreateSoundBuffer failed");
}
IDirectSoundNotify *Notify;
if (FAILED(PlaybackBuffer->lpVtbl->QueryInterface(PlaybackBuffer, &IID_IDirectSoundNotify, (void**)&Notify)))
{
__FatalError("QueryInterface failed");
}
Setup->Audio.SecondaryBuffer = PlaybackBuffer;
Setup->Audio.BytesPerSample = 2 * Format.nChannels;
Setup->Audio.BufferSize_Samples = BufferSize / Setup->Audio.BytesPerSample;
Setup->Audio.Ahead_Samples = HzRate/10;
Setup->Audio.Index_Samples = 0;
__FillBuffer(Setup, Setup->Audio.Ahead_Samples*10);
Setup->Audio.Thread = CreateThread(NULL, 0, __AudioThread, Setup, 0, NULL);
Setup->Audio.SecondaryBuffer->lpVtbl->Play(Setup->Audio.SecondaryBuffer, 0, 0, DSBPLAY_LOOPING);
}
int WinMainCRTStartup()
{
win32_setup_info SetupInfo = CallbackSetup();
WNDCLASSA WindowClass = { 0 };
WindowClass.lpszClassName = "SmallWin32";
WindowClass.lpfnWndProc = __WndProc;
// NOTE(ske): No error handling, but not really necessary.
RegisterClassA(&WindowClass);
// NOTE(ske): No error handling, but not really necessary.
HWND Window = CreateWindowA(
WindowClass.lpszClassName,
SetupInfo.Title,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
SetupInfo.Width, SetupInfo.Height,
NULL,
NULL,
NULL,
NULL
);
GLB_Setup.FrameTimer = 1;
SetTimer(Window, GLB_Setup.FrameTimer, 16, NULL);
GLB_Setup.Window = Window;
__InitializeDirectsound(&GLB_Setup);
static MSG Msg = { 0 };
while (GetMessageA(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
ExitProcess(0);
}