-
Notifications
You must be signed in to change notification settings - Fork 0
/
screensaver_gl.c
357 lines (305 loc) · 8.69 KB
/
screensaver_gl.c
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
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <scrnsave.h>
#include <strsafe.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <wchar.h>
#include <stdint.h>
static UINT_PTR uTimer;
#define FPS_AVG_MAX 10
enum
{
GL_FRAGMENT_SHADER = 0x8B30,
GL_VERTEX_SHADER,
GL_ARRAY_BUFFER = 0x8892,
GL_POINT_SPITE = 0x8861,
GL_PROGRAM_POINT_SPRITE = 0x8642,
GL_STATIC_DRAW = 0x88E4,
};
static HDC hdc;
static HGLRC context;
typedef int (WINAPI *wglSwapIntervalEXT_t)(int interval);
wglSwapIntervalEXT_t wglSwapIntervalEXT;
struct
{
unsigned (WINAPI *CreateProgram)();
unsigned (WINAPI *CreateShader)(GLenum shadertype);
void (WINAPI *ShaderSource)(unsigned shader, int count, const char * const * string, const int * length);
void (WINAPI *CompileShader)(unsigned shader);
void (WINAPI *AttachShader)(unsigned prog, unsigned shader);
void (WINAPI *LinkProgram)(unsigned prog);
void (WINAPI *UseProgram)(unsigned prog);
void (WINAPI *Uniform1f)(int location, float v0);
void (WINAPI *Uniform2f)(int location, float v0, float v1);
int (WINAPI *GetUniformLocation)(unsigned program, const char * name);
void (WINAPI *VertexAttribPointer)(unsigned index, int size, GLenum type, GLboolean normalized, int stride, const void* pointer);
void (WINAPI *EnableVertexAttribArray)(unsigned index);
void (WINAPI *DisableVertexAttribArray)(unsigned index);
void (WINAPI *BindBuffer)(GLenum target, unsigned buffer);
void (WINAPI *BufferData)(GLenum target, intptr_t size, const void* data, GLenum usage);
void (WINAPI *GenBuffers)(int n, unsigned* buffers);
int (WINAPI *GetAttribLocation)(unsigned program, const char * name);
} glf;
void scrtoy_GLLoadFunctions();
typedef struct LLfile_s LLfile;
LLfile * scrtoy_LoadFile(const char * filename);
char * scrtoy_GetContents(LLfile * file);
void scrtoy_CloseFile(LLfile * file);
static struct
{
unsigned int prog;
int sizex, sizey;
int resolutionLoc;
int timeLoc;
GLuint bufId;
} glData;
unsigned int scrtoy_GLCompileShader(const char * shadercode, GLenum shadertype);
void scrtoy_GLLoadShader();
void scrtoy_GLRender(uint64_t time);
static inline float averagef(float* floats, size_t num)
{
float avg = 0.f;
for (size_t i = 0; i < num; i++)
{
avg += floats[i];
}
return avg / (float)num;
}
static HWND mainWindow;
LRESULT WINAPI ScreenSaverProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
static clock_t prevtime;
static HFONT fpsFont;
static float fps10[FPS_AVG_MAX] = { 0.f };
static int fps10index = 0;
static uint64_t time = 0;
switch (msg)
{
case WM_TIMER:
InvalidateRect(hwnd, NULL, FALSE);
break;
case WM_PAINT:
{
clock_t currenttime = clock();
time += (uint64_t)(currenttime - prevtime);
/* OpenGL drawing */
fps10[fps10index] = 1.f / ((float)(currenttime - prevtime) / 1000.f);
fps10index = (fps10index + 1) % FPS_AVG_MAX;
prevtime = currenttime;
int intfps = (int)(averagef(fps10, FPS_AVG_MAX) + .5f);
char fps_[9] = { "FPS: " };
_itoa(intfps, fps_ + 5, 10);
for (int i = 6; i < 9; i++) if (fps_[i] == '\0') fps_[i] = ' ';
scrtoy_GLRender(time);
SwapBuffers(hdc);
break;
}
case WM_SIZE:
// Update resolution of pixel shader
glData.sizex = LOWORD(lp);
glData.sizey = HIWORD(lp);
glf.Uniform2f(glData.resolutionLoc, (float)glData.sizex, (float)glData.sizey);
break;
case WM_CLOSE:
DeleteObject(fpsFont);
ReleaseDC(hwnd, hdc);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
{
mainWindow = hwnd;
hdc = GetDC(hwnd);
static const PIXELFORMATDESCRIPTOR pfd = {
0, 0, PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER
};
SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd);
context = wglCreateContext(hdc);
wglMakeCurrent(hdc, context);
if ((wglSwapIntervalEXT = (wglSwapIntervalEXT_t)wglGetProcAddress("wglSwapIntervalEXT")))
wglSwapIntervalEXT(1);
scrtoy_GLLoadFunctions();
fpsFont = CreateFontW(
48,
0,
0,
0,
FW_SEMIBOLD,
FALSE,
FALSE,
FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
NONANTIALIASED_QUALITY,
FF_DONTCARE,
L"Terminal"
);
scrtoy_GLLoadShader();
// Create timer for refreshing the screen
uTimer = SetTimer(hwnd, 1, 1, NULL);
break;
}
default:
return DefScreenSaverProc(hwnd, msg, wp, lp);
}
return 0;
}
BOOL WINAPI RegisterDialogClasses(HANDLE __attribute__((unused)) hInst)
{
return TRUE;
}
BOOL WINAPI ScreenSaverConfigureDialog(
HWND __attribute__((unused)) hDlg,
UINT __attribute__((unused)) message,
WPARAM __attribute__((unused)) wParam,
LPARAM __attribute__((unused)) lParam
)
{
return FALSE;
}
void scrtoy_GLLoadFunctions()
{
#define scrtoy_LoadFunc(func) (glf.func = (__typeof(glf.func))wglGetProcAddress("gl" #func))
scrtoy_LoadFunc(CreateProgram);
scrtoy_LoadFunc(CreateShader);
scrtoy_LoadFunc(ShaderSource);
scrtoy_LoadFunc(CompileShader);
scrtoy_LoadFunc(AttachShader);
scrtoy_LoadFunc(LinkProgram);
scrtoy_LoadFunc(UseProgram);
scrtoy_LoadFunc(Uniform1f);
scrtoy_LoadFunc(Uniform2f);
scrtoy_LoadFunc(GetUniformLocation);
scrtoy_LoadFunc(VertexAttribPointer);
scrtoy_LoadFunc(EnableVertexAttribArray);
scrtoy_LoadFunc(DisableVertexAttribArray);
scrtoy_LoadFunc(BindBuffer);
scrtoy_LoadFunc(BufferData);
scrtoy_LoadFunc(GenBuffers);
scrtoy_LoadFunc(GetAttribLocation);
#undef scrtoy_LoadFunc
}
struct LLfile_s
{
HANDLE file;
char * contents;
};
LLfile * scrtoy_LoadFile(const char * filename)
{
HANDLE hFile = CreateFileA(
filename,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hFile == INVALID_HANDLE_VALUE)
return NULL;
LLfile * file = malloc(sizeof(LLfile));
if (file == NULL)
return NULL;
DWORD filesz = GetFileSize(hFile, NULL);
file->contents = malloc(filesz);
if (file->contents == NULL)
{
CloseHandle(hFile);
free(file);
return NULL;
}
DWORD dwRead;
ReadFile(hFile, file->contents, filesz, &dwRead, NULL);
if (dwRead != filesz)
{
CloseHandle(hFile);
free(file->contents);
free(file);
return NULL;
}
file->file = hFile;
return file;
}
char * scrtoy_GetContents(LLfile * file)
{
return file->contents;
}
void scrtoy_CloseFile(LLfile * file)
{
if (file == NULL)
return;
CloseHandle(file->file);
free(file->contents);
free(file);
}
unsigned int scrtoy_GLCompileShader(const char * shadercode, GLenum shadertype)
{
unsigned int sh = glf.CreateShader(shadertype);
glf.ShaderSource(sh, 1, &shadercode, NULL);
glf.CompileShader(sh);
return sh;
}
void scrtoy_GLLoadShader()
{
static const char * vertexshadercode =
"#version 330\n"
"layout(location = 0) in vec3 vertexPosition_modelspace;"
"out vec2 fragCoord;"
"void main()"
"{"
"gl_Position.xyz = vertexPosition_modelspace;"
"gl_Position.w = 1.0;"
"fragCoord = gl_Position.xy;"
"}\n";
// Compile shader
glData.prog = glf.CreateProgram();
// Fragment shader
LLfile * shadercode = scrtoy_LoadFile("shader.glsl");
const unsigned int vertexshader = scrtoy_GLCompileShader(vertexshadercode, GL_VERTEX_SHADER);
const unsigned int fragshader = scrtoy_GLCompileShader(scrtoy_GetContents(shadercode), GL_FRAGMENT_SHADER);
scrtoy_CloseFile(shadercode);
glf.AttachShader(glData.prog, vertexshader);
glf.AttachShader(glData.prog, fragshader);
glf.LinkProgram(glData.prog);
// Get variable locations
glData.resolutionLoc = glf.GetUniformLocation(glData.prog, "iResolution");
glData.timeLoc = glf.GetUniformLocation(glData.prog, "iTime");
glData.sizex = GetSystemMetrics(SM_CXSCREEN);
glData.sizey = GetSystemMetrics(SM_CXSCREEN);
glf.GenBuffers(1, &glData.bufId);
glf.BindBuffer(GL_ARRAY_BUFFER, glData.bufId);
/*float points[] = {
-1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f
};*/
float points[] = {
-1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f
};
glf.BufferData(GL_ARRAY_BUFFER, 12 * sizeof(float), points, GL_STATIC_DRAW);
glf.EnableVertexAttribArray(0);
}
void scrtoy_GLRender(uint64_t time)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glf.UseProgram(glData.prog);
glf.Uniform1f(glData.timeLoc, (float)time / 1000.f);
glf.Uniform2f(glData.resolutionLoc, (float)glData.sizex, (float)glData.sizey);
glf.BindBuffer(GL_ARRAY_BUFFER, glData.bufId);
glf.VertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glDrawArrays(GL_QUADS, 0, 4);
}