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
|
#define UNICODE
#include <windows.h>
#include <assert.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#define SPONGE_IMPLEMENTATION
#include "sponge.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "example.h"
#define DEFAULT_WIDTH 256
#define DEFAULT_HEIGHT 256
#define TARGET_FRAME_TIME_US 16666
static size_t pixels_buffer_count;
static sponge_Texture canvas;
static BITMAPINFO bmi;
int update_canvas(sponge_Texture *canvas, uint32_t new_width, uint32_t new_height) {
size_t new_count = new_width * new_height;
if (new_count > pixels_buffer_count) {
uint32_t *new_pixels_buffer = malloc(new_count * sizeof(uint32_t));
if (!new_pixels_buffer)
return 0;
free(canvas->pixels);
canvas->pixels = new_pixels_buffer;
pixels_buffer_count = new_count;
}
canvas->width = new_width;
canvas->height = new_height;
canvas->stride = new_width; // TODO(kard): think about this if we want image to stay the same without redraw
ZeroMemory(&bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = new_width;
bmi.bmiHeader.biHeight = -(int32_t)new_height; // negative = top-down
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
return 1;
}
// TODO(kard): MSVC complains about freopen (should be freopen_s apparently)
void redirect_io_to_console() {
AllocConsole();
FILE *fp;
fp = freopen("CONOUT$", "w", stdout);
setvbuf(stdout, NULL, _IONBF, 0);
fp = freopen("CONOUT$", "w", stderr);
setvbuf(stderr, NULL, _IONBF, 0);
fp = freopen("CONIN$", "r", stdin);
setvbuf(stdin, NULL, _IONBF, 0);
}
LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY: {
PostQuitMessage(0);
return 0;
}
case WM_SIZE: {
uint32_t width = LOWORD(lParam);
uint32_t height = HIWORD(lParam);
// TODO(kard): handle properly? (what does that even mean)
assert(update_canvas(&canvas, width, height));
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
int WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd
) {
redirect_io_to_console();
const wchar_t CLASS_NAME[] = L"sponge";
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
L"sponge",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, DEFAULT_WIDTH, DEFAULT_HEIGHT,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL)
return 1;
update_canvas(&canvas, DEFAULT_WIDTH, DEFAULT_HEIGHT);
ShowWindow(hwnd, nShowCmd);
MSG msg = {0};
BOOL running = TRUE;
init();
LARGE_INTEGER qpc_freq;
LARGE_INTEGER qpc_start;
LARGE_INTEGER qpc_end;
LARGE_INTEGER qpc_draw;
LARGE_INTEGER qpc_frame;
QueryPerformanceFrequency(&qpc_freq);
QueryPerformanceCounter(&qpc_start);
while (running) {
QueryPerformanceCounter(&qpc_start);
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
running = FALSE;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (!running) break;
draw_frame(canvas);
QueryPerformanceCounter(&qpc_end);
qpc_draw.QuadPart = qpc_end.QuadPart - qpc_start.QuadPart;
qpc_draw.QuadPart *= 1000000; // microseconds
qpc_draw.QuadPart /= qpc_freq.QuadPart;
HDC hdc = GetDC(hwnd);
StretchDIBits(
hdc,
// TODO(kard): check what happens if canvas is bigger than window (it shouldn't differ currently)
0, 0, canvas.width, canvas.height, // dest rectangle
0, 0, canvas.width, canvas.height, // src rectangle
canvas.pixels,
&bmi,
DIB_RGB_COLORS,
SRCCOPY
);
InvalidateRect(hwnd, NULL, FALSE);
UpdateWindow(hwnd);
QueryPerformanceCounter(&qpc_end);
qpc_frame.QuadPart = qpc_end.QuadPart - qpc_start.QuadPart;
qpc_frame.QuadPart *= 1000000; // microseconds
qpc_frame.QuadPart /= qpc_freq.QuadPart;
fprintf(stderr, "draw time: %3lld.%03lldms, frame time: %3lld.%03lldms\n",
qpc_draw.QuadPart / 1000, qpc_draw.QuadPart % 1000,
qpc_frame.QuadPart / 1000, qpc_frame.QuadPart % 1000);
// TODO(kard):
// 1. this can probably be done cleaner
// 2. resetting after every frame is wrong, it should be a value across frames (see 1.)
if (qpc_frame.QuadPart < TARGET_FRAME_TIME_US) {
LARGE_INTEGER remaining;
LARGE_INTEGER actual_end;
actual_end.QuadPart = qpc_start.QuadPart + (TARGET_FRAME_TIME_US * qpc_freq.QuadPart / 1000000);
remaining.QuadPart = (actual_end.QuadPart - qpc_end.QuadPart) * 1000000 / qpc_freq.QuadPart;
if (remaining.QuadPart > 2000) {
Sleep((remaining.QuadPart - 1000) / 1000);
}
while (qpc_end.QuadPart < actual_end.QuadPart) {
QueryPerformanceCounter(&qpc_end);
}
}
Sleep(16);
}
return (int) msg.wParam;
}
|