-
Notifications
You must be signed in to change notification settings - Fork 2
/
pal_win32.h
417 lines (364 loc) · 12.9 KB
/
pal_win32.h
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
#ifndef PAL_WIN32_H
#define PAL_WIN32_H
#include "common.h"
void *malloc(size_t size)
{
return GlobalAlloc(GMEM_ZEROINIT, size);
}
HWAVEOUT hWaveOut;
WAVEHDR header = { 0, 0, 0, 0, 0, 0, 0, 0 };
HDC hdc;
HGLRC glrc;
int flip_buffers()
{
SwapBuffers(hdc);
MSG msg = { 0 };
while ( PeekMessageA( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if ( msg.message == WM_QUIT ) {
return FALSE;
}
TranslateMessage( &msg );
DispatchMessageA( &msg );
}
return TRUE;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
ExitProcess(0);
break;
case VK_SPACE:
// pause/unpaused render timer
paused = !paused;
if(paused)
waveOutPause(hWaveOut);
else
waveOutRestart(hWaveOut);
break;
}
break;
case WM_RBUTTONDOWN:
ExitProcess(0);
break;
case WM_MOUSEMOVE:
mx = GET_X_LPARAM(lParam);
my = GET_Y_LPARAM(lParam);
break;
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_COMMAND:
UINT id = LOWORD(wParam);
HWND hSender = (HWND)lParam;
switch(id)
{
case 5:
{
int index = SendMessage(hSender, CB_GETCURSEL, 0, 0);
if(index == 0)
{
w = 1920;
h = 1080;
}
else if(index == 1)
{
w = 960;
h = 540;
}
else if(index == 2)
{
w = 1024;
h = 768;
}
}
break;
case 6:
muted = !muted;
if(muted)
SendMessage(hSender, BM_SETCHECK, BST_CHECKED, 0);
else
SendMessage(hSender, BM_SETCHECK, BST_UNCHECKED, 0);
break;
case 7:
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
case 8: // Full screen Antialiasing
{
int index = SendMessage(hSender, CB_GETCURSEL, 0, 0);
fsaa = (index + 1)*(index + 1);
}
break;
case 9: // Texture buffer size
{
int index = SendMessage(hSender, CB_GETCURSEL, 0, 0);
texs = 128;
for(int i=0; i<index; ++i)
texs *= 2;
block_size = texs*texs;
}
break;
case 10:
{
override_index = SendMessage(hSender, CB_GETCURSEL, 0, 0);
scene_override = override_index > 0;
}
break;
}
break;
case WM_CLOSE:
ExitProcess(0);
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI demo(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
#ifdef DEBUG
AllocConsole();
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
#endif
// Display settings selector
WNDCLASS wca = { 0 };
wca.lpfnWndProc = DialogProc;
wca.hInstance = hInstance;
wca.lpszClassName = L"Settings";
RegisterClass(&wca);
HWND lwnd = CreateWindowEx(
0, // Optional window styles.
L"Settings", // Window class
demoname, // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
200, 200, 300, 300,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
// Add "Resolution: " text
HWND hResolutionText = CreateWindow(WC_STATIC, "Resolution: ", WS_VISIBLE | WS_CHILD | SS_LEFT, 10,15,100,100, lwnd, NULL, hInstance, NULL);
// Add resolution Combo box
HWND hResolutionComboBox = CreateWindow(WC_COMBOBOX, TEXT(""),
CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
100, 10, 175, 80, lwnd, (HMENU)5, hInstance,
NULL);
// Add items to resolution combo box and select full HD
const char *fullhd = "1920*1080",
*halfhd = "960*540",
*normal = "1024*768";
SendMessage(hResolutionComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (fullhd));
SendMessage(hResolutionComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (halfhd));
SendMessage(hResolutionComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (normal));
SendMessage(hResolutionComboBox, CB_SETCURSEL, 0, 0);
// Add mute checkbox
HWND hMuteCheckbox = CreateWindow(WC_BUTTON, TEXT("Mute"),
WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
10, 40, 100, 20,
lwnd, (HMENU) 6, hInstance, NULL);
// Add "Antialiasing: " text
HWND hAntialiasingText = CreateWindow(WC_STATIC, "FSAA: ", WS_VISIBLE | WS_CHILD | SS_LEFT, 10,65,100,100, lwnd, NULL, hInstance, NULL);
// Add Fullscreen Antialiasing combo box
HWND hFSAAComboBox= CreateWindow(WC_COMBOBOX, TEXT(""),
CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
100, 60, 175, 280, lwnd, (HMENU)8, hInstance,
NULL);
// Populate with entries
const char *fsaa1= "None",
*fsaa4 = "4*FSAA",
*fsaa9 = "9*FSAA",
*fsaa16 = "16*FSAA",
*fsaa25 = "25*FSAA",
*fsaa36 = "36*FSAA",
*fsaa49 = "49*FSAA",
*fsaa64 = "64*FSAA",
*fsaa81 = "81*FSAA",
*fsaa100 = "100*FSAA";
SendMessage(hFSAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (fsaa1));
SendMessage(hFSAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (fsaa4));
SendMessage(hFSAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (fsaa9));
SendMessage(hFSAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (fsaa16));
SendMessage(hFSAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (fsaa25));
SendMessage(hFSAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (fsaa36));
SendMessage(hFSAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (fsaa49));
SendMessage(hFSAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (fsaa64));
SendMessage(hFSAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (fsaa81));
SendMessage(hFSAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (fsaa100));
SendMessage(hFSAAComboBox, CB_SETCURSEL, 9, 0);
// Add "SFX Buffer: " text
HWND hTXAAText = CreateWindow(WC_STATIC, "SFX Buffer: ", WS_VISIBLE | WS_CHILD | SS_LEFT, 10,95,100,100, lwnd, NULL, hInstance, NULL);
// Add SFX buffer size combo box
HWND hTXAAComboBox= CreateWindow(WC_COMBOBOX, TEXT(""),
CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
100, 90, 175, 280, lwnd, (HMENU)9, hInstance,
NULL);
// Populate with entries
const char *buf128= "128^2 px",
*buf256 = "256^2 px",
*buf512 = "512^2 px",
*buf1024 = "1024^2 px";
SendMessage(hTXAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (buf128));
SendMessage(hTXAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (buf256));
SendMessage(hTXAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (buf512));
SendMessage(hTXAAComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (buf1024));
//SendMessage(hTXAAComboBox, CB_SETCURSEL, 3, 0);
SendMessage(hTXAAComboBox, CB_SETCURSEL, 2, 0);
// Add "Antialiasing: " text
HWND hSceneText = CreateWindow(WC_STATIC, "Scene: ", WS_VISIBLE | WS_CHILD | SS_LEFT, 10,125,100,100, lwnd, NULL, hInstance, NULL);
// Add scene selector
HWND hSceneComboBox = CreateWindow(WC_COMBOBOX, TEXT(""),
CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
100, 120, 175, 280, lwnd, (HMENU)10, hInstance,
NULL);
// Populate with entries
const char *all_scenes = "All scenes",
*graffiti_scene = "Graffiti",
*block_scene = "Transparent Blocks",
*voronoi_scene = "Voronoi Canyon",
*blood_cell_scene = "Blood Cells";
SendMessage(hSceneComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (all_scenes));
SendMessage(hSceneComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (graffiti_scene));
SendMessage(hSceneComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (block_scene));
SendMessage(hSceneComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (voronoi_scene));
SendMessage(hSceneComboBox,(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) (blood_cell_scene));
SendMessage(hSceneComboBox, CB_SETCURSEL, 0, 0);
// Add start button
HWND hwndButton = CreateWindow(WC_BUTTON,"Party!",WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,185,165,90,90,lwnd,(HMENU)7,hInstance,NULL);
// Show the selector
ShowWindow(lwnd, TRUE);
UpdateWindow(lwnd);
MSG msg = { 0 };
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
#ifdef DEBUG
printf("Rendering Demo with:\nSound ");
// if(muted)printf("muted");
// else printf("playing");
printf("\nResolution: %d * %d\n", w, h);
printf("FSAA: %d*\n", fsaa);
#endif
// Display demo window
CHAR WindowClass[] = "Team210 Demo Window";
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = &WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = WindowClass;
wc.hIconSm = NULL;
RegisterClassEx(&wc);
// Get full screen information
HMONITOR hmon = MonitorFromWindow(0, MONITOR_DEFAULTTONEAREST);
MONITORINFO mi = { sizeof(mi) };
GetMonitorInfo(hmon, &mi);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
WindowClass, // Window class
":: NR4^QM/Team210 :: GO - MAKE A DEMO ::", // Window text
WS_POPUP | WS_VISIBLE, // Window style
mi.rcMonitor.left,
mi.rcMonitor.top,
mi.rcMonitor.right - mi.rcMonitor.left,
mi.rcMonitor.bottom - mi.rcMonitor.top, // Size and position
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
0 // Additional application data
);
// Show it
ShowWindow(hwnd, TRUE);
UpdateWindow(hwnd);
// Create OpenGL context
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags
PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette.
32, // Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24, // Number of bits for the depthbuffer
8, // Number of bits for the stencilbuffer
0, // Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE,
0,
0, 0, 0
};
hdc = GetDC(hwnd);
int pf = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, pf, &pfd);
glrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, glrc);
// OpenGL extensions
glGetProgramiv = (PFNGLGETPROGRAMIVPROC) wglGetProcAddress("glGetProgramiv");
glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) wglGetProcAddress("glGetProgramInfoLog");
glGetShaderiv = (PFNGLGETSHADERIVPROC) wglGetProcAddress("glGetShaderiv");
glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) wglGetProcAddress("glGetShaderInfoLog");
glCreateShader = (PFNGLCREATESHADERPROC) wglGetProcAddress("glCreateShader");
glCreateProgram = (PFNGLCREATEPROGRAMPROC) wglGetProcAddress("glCreateProgram");
glShaderSource = (PFNGLSHADERSOURCEPROC) wglGetProcAddress("glShaderSource");
glCompileShader = (PFNGLCOMPILESHADERPROC) wglGetProcAddress("glCompileShader");
glAttachShader = (PFNGLATTACHSHADERPROC) wglGetProcAddress("glAttachShader");
glLinkProgram = (PFNGLLINKPROGRAMPROC) wglGetProcAddress("glLinkProgram");
glUseProgram = (PFNGLUSEPROGRAMPROC) wglGetProcAddress("glUseProgram");
glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) wglGetProcAddress("glGetUniformLocation");
glUniform2f = (PFNGLUNIFORM2FPROC) wglGetProcAddress("glUniform2f");
glUniform1f = (PFNGLUNIFORM1FPROC) wglGetProcAddress("glUniform1f");
glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC) wglGetProcAddress("glGenFramebuffers");
glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC) wglGetProcAddress("glBindFramebuffer");
glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) wglGetProcAddress("glFramebufferTexture2D");
glNamedRenderbufferStorageEXT = (PFNGLNAMEDRENDERBUFFERSTORAGEEXTPROC) wglGetProcAddress("glNamedRenderbufferStorage");
glActiveTexture = (PFNGLACTIVETEXTUREPROC) wglGetProcAddress("glActiveTexture");
glUniform1i = (PFNGLUNIFORM1IPROC) wglGetProcAddress("glUniform1i");
load_demo();
// Main loop
t_start = 0.;
while(flip_buffers())
{
static MMTIME MMTime = { TIME_SAMPLES, 0};
waveOutGetPosition(hWaveOut, &MMTime, sizeof(MMTIME));
t_now = ((double)MMTime.u.sample)/( 44100.0);
draw();
}
return 0;
}
void initialize_sound()
{
hWaveOut = 0;
int n_bits_per_sample = 16;
WAVEFORMATEX wfx = { WAVE_FORMAT_PCM, channels, sample_rate, sample_rate*channels*n_bits_per_sample / 8, channels*n_bits_per_sample / 8, n_bits_per_sample, 0 };
waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, 0, 0, CALLBACK_NULL);
header.lpData = smusic1;
header.dwBufferLength = 4 * music1_size;
waveOutPrepareHeader(hWaveOut, &header, sizeof(WAVEHDR));
waveOutWrite(hWaveOut, &header, sizeof(WAVEHDR));
}
#endif