-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_base_1.cpp
423 lines (366 loc) · 12.2 KB
/
window_base_1.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "dwmapi.lib")
#include <windows.h>
#include <stdint.h>
#include <dsound.h>
#include <dwmapi.h>
#define internal static
#define local_persist static
#define global_variable static
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef int32 bool32;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
struct win32_offscreen_buffer
{
// NOTE(casey): Pixels are alwasy 32-bits wide, Memory Order BB GG RR XX
BITMAPINFO Info;
void *Memory;
int Width;
int Height;
int Pitch;
};
struct win32_window_dimension
{
int Width;
int Height;
};
// TODO(casey): This is a global for now.
global_variable bool GlobalRunning;
global_variable win32_offscreen_buffer GlobalBackbuffer;
#define DIRECT_SOUND_CREATE(name) HRESULT WINAPI name(LPCGUID pcGuidDevice, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter)
typedef DIRECT_SOUND_CREATE(direct_sound_create);
internal void
Win32InitDSound(HWND Window, int32 SamplesPerSecond, int32 BufferSize)
{
// NOTE(casey): Load the library
HMODULE DSoundLibrary = LoadLibraryA("dsound.dll");
if(DSoundLibrary)
{
// NOTE(casey): Get a DirectSound object! - cooperative
direct_sound_create *DirectSoundCreate = (direct_sound_create *)
GetProcAddress(DSoundLibrary, "DirectSoundCreate");
// TODO(casey): Double-check that this works on XP - DirectSound8 or 7??
LPDIRECTSOUND DirectSound;
if(DirectSoundCreate && SUCCEEDED(DirectSoundCreate(0, &DirectSound, 0)))
{
WAVEFORMATEX WaveFormat = {};
WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
WaveFormat.nChannels = 2;
WaveFormat.nSamplesPerSec = SamplesPerSecond;
WaveFormat.wBitsPerSample = 16;
WaveFormat.nBlockAlign = (WaveFormat.nChannels*WaveFormat.wBitsPerSample) / 8;
WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec*WaveFormat.nBlockAlign;
WaveFormat.cbSize = 0;
if(SUCCEEDED(DirectSound->SetCooperativeLevel(Window, DSSCL_PRIORITY)))
{
DSBUFFERDESC BufferDescription = {};
BufferDescription.dwSize = sizeof(BufferDescription);
BufferDescription.dwFlags = DSBCAPS_PRIMARYBUFFER;
// NOTE(casey): "Create" a primary buffer
// TODO(casey): DSBCAPS_GLOBALFOCUS?
LPDIRECTSOUNDBUFFER PrimaryBuffer;
if(SUCCEEDED(DirectSound->CreateSoundBuffer(&BufferDescription, &PrimaryBuffer, 0)))
{
HRESULT Error = PrimaryBuffer->SetFormat(&WaveFormat);
if(SUCCEEDED(Error))
{
// NOTE(casey): We have finally set the format!
OutputDebugStringA("Primary buffer format was set.\n");
}
else
{
// TODO(casey): Diagnostic
}
}
else
{
// TODO(casey): Diagnostic
}
}
else
{
// TODO(casey): Diagnostic
}
// TODO(casey): DSBCAPS_GETCURRENTPOSITION2
DSBUFFERDESC BufferDescription = {};
BufferDescription.dwSize = sizeof(BufferDescription);
BufferDescription.dwFlags = 0;
BufferDescription.dwBufferBytes = BufferSize;
BufferDescription.lpwfxFormat = &WaveFormat;
LPDIRECTSOUNDBUFFER SecondaryBuffer;
HRESULT Error = DirectSound->CreateSoundBuffer(&BufferDescription, &SecondaryBuffer, 0);
if(SUCCEEDED(Error))
{
OutputDebugStringA("Secondary buffer created successfully.\n");
}
}
else
{
// TODO(casey): Diagnostic
}
}
else
{
// TODO(casey): Diagnostic
}
}
internal win32_window_dimension
Win32GetWindowDimension(HWND Window)
{
win32_window_dimension Result;
RECT ClientRect;
GetClientRect(Window, &ClientRect);
Result.Width = ClientRect.right - ClientRect.left;
Result.Height = ClientRect.bottom - ClientRect.top;
return(Result);
}
internal void
RenderWeirdGradient(win32_offscreen_buffer *Buffer, int BlueOffset, int GreenOffset)
{
// TODO(casey): Let's see what the optimizer does
uint8 *Row = (uint8 *)Buffer->Memory;
for(int Y = 0;
Y < Buffer->Height;
++Y)
{
uint32 *Pixel = (uint32 *)Row;
for(int X = 0;
X < Buffer->Width;
++X)
{
uint8 Blue = (X + BlueOffset);
uint8 Green = (Y + GreenOffset);
*Pixel++ = ((Green << 8) | Blue);
}
Row += Buffer->Pitch;
}
}
internal void
Win32ResizeDIBSection(win32_offscreen_buffer *Buffer, int Width, int Height)
{
// TODO(casey): Bulletproof this.
// Maybe don't free first, free after, then free first if that fails.
if(Buffer->Memory)
{
VirtualFree(Buffer->Memory, 0, MEM_RELEASE);
}
Buffer->Width = Width;
Buffer->Height = Height;
int BytesPerPixel = 4;
// NOTE(casey): When the biHeight field is negative, this is the clue to
// Windows to treat this bitmap as top-down, not bottom-up, meaning that
// the first three bytes of the image are the color for the top left pixel
// in the bitmap, not the bottom left!
Buffer->Info.bmiHeader.biSize = sizeof(Buffer->Info.bmiHeader);
Buffer->Info.bmiHeader.biWidth = Buffer->Width;
Buffer->Info.bmiHeader.biHeight = -Buffer->Height;
Buffer->Info.bmiHeader.biPlanes = 1;
Buffer->Info.bmiHeader.biBitCount = 32;
Buffer->Info.bmiHeader.biCompression = BI_RGB;
// NOTE(casey): Thank you to Chris Hecker of Spy Party fame
// for clarifying the deal with StretchDIBits and BitBlt!
// No more DC for us.
int BitmapMemorySize = (Buffer->Width*Buffer->Height)*BytesPerPixel;
Buffer->Memory = VirtualAlloc(0, BitmapMemorySize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
Buffer->Pitch = Width*BytesPerPixel;
// TODO(casey): Probably clear this to black
}
internal void
Win32DisplayBufferInWindow(win32_offscreen_buffer *Buffer,
HDC DeviceContext, int WindowWidth, int WindowHeight)
{
// TODO(casey): Aspect ratio correction
// TODO(casey): Play with stretch modes
StretchDIBits(DeviceContext,
/*
X, Y, Width, Height,
X, Y, Width, Height,
*/
0, 0, WindowWidth, WindowHeight,
0, 0, Buffer->Width, Buffer->Height,
Buffer->Memory,
&Buffer->Info,
DIB_RGB_COLORS, SRCCOPY);
}
internal LRESULT CALLBACK
Win32MainWindowCallback(HWND Window,
UINT Message,
WPARAM WParam,
LPARAM LParam)
{
LRESULT Result = 0;
switch(Message)
{
case WM_CLOSE:
{
// TODO(casey): Handle this with a message to the user?
GlobalRunning = false;
} break;
case WM_ACTIVATEAPP:
{
OutputDebugStringA("WM_ACTIVATEAPP\n");
} break;
case WM_DESTROY:
{
// TODO(casey): Handle this as an error - recreate window?
GlobalRunning = false;
} break;
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_KEYDOWN:
case WM_KEYUP:
{
uint32 VKCode = WParam;
bool WasDown = ((LParam & (1 << 30)) != 0);
bool IsDown = ((LParam & (1 << 31)) == 0);
if(WasDown != IsDown)
{
if(VKCode == 'W')
{
}
else if(VKCode == 'A')
{
}
else if(VKCode == 'S')
{
}
else if(VKCode == 'D')
{
}
else if(VKCode == 'Q')
{
}
else if(VKCode == 'E')
{
}
else if(VKCode == VK_UP)
{
}
else if(VKCode == VK_LEFT)
{
}
else if(VKCode == VK_DOWN)
{
}
else if(VKCode == VK_RIGHT)
{
}
else if(VKCode == VK_ESCAPE)
{
OutputDebugStringA("ESCAPE: ");
if(IsDown)
{
OutputDebugStringA("IsDown ");
}
if(WasDown)
{
OutputDebugStringA("WasDown");
}
OutputDebugStringA("\n");
}
else if(VKCode == VK_SPACE)
{
}
}
bool32 AltKeyWasDown = (LParam & (1 << 29));
if((VKCode == VK_F4) && AltKeyWasDown)
{
GlobalRunning = false;
}
} break;
case WM_PAINT:
{
PAINTSTRUCT Paint;
HDC DeviceContext = BeginPaint(Window, &Paint);
win32_window_dimension Dimension = Win32GetWindowDimension(Window);
Win32DisplayBufferInWindow(&GlobalBackbuffer, DeviceContext,
Dimension.Width, Dimension.Height);
EndPaint(Window, &Paint);
} break;
case WM_GETMINMAXINFO:
{
MINMAXINFO* minMaxInfo = (MINMAXINFO*)LParam;
}
default:
{
// OutputDebugStringA("default\n");
Result = DefWindowProc(Window, Message, WParam, LParam);
} break;
}
return(Result);
}
int CALLBACK
WinMain(HINSTANCE Instance,
HINSTANCE PrevInstance,
LPSTR CommandLine,
int ShowCode)
{
WNDCLASSA WindowClass = {};
Win32ResizeDIBSection(&GlobalBackbuffer, 1280, 720);
WindowClass.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
WindowClass.lpfnWndProc = Win32MainWindowCallback;
WindowClass.hInstance = Instance;
// WindowClass.hIcon;
WindowClass.lpszClassName = "DaemonWindowClass";
if(RegisterClassA(&WindowClass))
{
HWND Window =
CreateWindowExA(
0,
WindowClass.lpszClassName,
"Daemon Nexus",
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
Instance,
0);
if(Window)
{
const MARGINS WindowMargins = {-1};
DwmExtendFrameIntoClientArea(Window, &WindowMargins);
HDC DeviceContext = GetDC(Window);
int XOffset = 0;
int YOffset = 0;
Win32InitDSound(Window, 48000, 48000*sizeof(int16)*2);
GlobalRunning = true;
while(GlobalRunning)
{
MSG Message;
while(PeekMessage(&Message, 0, 0, 0, PM_REMOVE))
{
if(Message.message == WM_QUIT)
{
GlobalRunning = false;
}
TranslateMessage(&Message);
DispatchMessageA(&Message);
}
// TODO(casey): Should we poll this more frequently
RenderWeirdGradient(&GlobalBackbuffer, XOffset, YOffset);
win32_window_dimension Dimension = Win32GetWindowDimension(Window);
Win32DisplayBufferInWindow(&GlobalBackbuffer, DeviceContext,
Dimension.Width, Dimension.Height);
}
}
else
{
// TODO(casey): Logging
}
}
else
{
// TODO(casey): Logging
}
return(0);
}