-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
162 lines (137 loc) · 3.74 KB
/
main.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
#define COBJMACROS
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include "my_std.h"
#define ENABLE_SANITY_CHECKS 1
#define ENABLE_MEMORY_LEAK_HOOK
#include "memory_leak.h"
#include "program.cpp"
void win32_render(HWND window, HDC window_dc){
auto client_rect = RECT{};
GetClientRect(window, &client_rect);
auto w = client_rect.right - client_rect.left;
auto h = client_rect.bottom - client_rect.top;
// Create a memory device context with a monochrome 1by1 pixels bitmap.
auto dc = CreateCompatibleDC(window_dc);
auto bitmap = CreateCompatibleBitmap(
window_dc, // Use the window HDC to get a bitmap with the correct color format.
w, h
);
SelectObject(dc, bitmap);
render(window, dc);
BitBlt(window_dc,
client_rect.left,
client_rect.top,
w,
h,
dc,
0,
0,
SRCCOPY
);
DeleteObject(bitmap);
DeleteDC(dc);
}
static LRESULT CALLBACK window_proc(HWND window, UINT message, WPARAM w_param, LPARAM l_param) {
LRESULT result = 0;
switch (message) {
case WM_CLOSE: {
DestroyWindow(window);
}break;
case WM_DESTROY: {
PostQuitMessage(0);
} break;
case WM_KEYDOWN: {
switch(w_param){
case VK_RIGHT: {
max_line_width += GetKeyState(VK_SHIFT) & 0x8000 ? 10 : 1;
}break;
case VK_LEFT: {
max_line_width -= GetKeyState(VK_SHIFT) & 0x8000 ? 10 : 1;
}break;
case VK_UP: {
pos_y += GetKeyState(VK_SHIFT) & 0x8000 ? 10 : 1;
}break;
case VK_DOWN: {
pos_y -= GetKeyState(VK_SHIFT) & 0x8000 ? 10 : 1;
}break;
case VK_DIVIDE: {
set_previous_font();
}break;
case VK_MULTIPLY: {
set_next_font();
}break;
case VK_ADD: {
if(GetKeyState(VK_CONTROL) & 0x8000) {
increase_font_size();
}
}break;
case VK_SUBTRACT: {
if(GetKeyState(VK_CONTROL) & 0x8000) {
decrease_font_size();
}
}break;
}
InvalidateRect(window, NULL, FALSE);
UpdateWindow(window);
}break;
case WM_CHAR: {
switch(w_param) {
case VK_BACK: {
backspace_command();
}break;
case VK_RETURN: {
enter_command();
}break;
default: {
str_command((wchar_t) w_param);
}break;
}
} break;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC dc = BeginPaint(window, &ps);
win32_render(window, dc);
EndPaint(window, &ps);
} break;
default: {
result = DefWindowProcW(window, message, w_param, l_param);
} break;
}
return result;
}
int main() {
WNDCLASSEXW window_class = {};
window_class.cbSize = sizeof(window_class);
window_class.lpfnWndProc = &window_proc;
window_class.hInstance = GetModuleHandleW(NULL);
window_class.hIcon = LoadIconW(NULL, (LPWSTR)IDI_APPLICATION);
window_class.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
window_class.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
window_class.lpszClassName = L"test-uniscribe-window";
HWND window = {};
if(RegisterClassExW(&window_class))
{
DWORD ex_style = WS_EX_APPWINDOW;
window = CreateWindowExW(
ex_style, window_class.lpszClassName, L"Test Uniscribe", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
0, 0, window_class.hInstance, 0
);
}
ShowWindow(window, SW_SHOW);
init(window);
MSG Message;
while(true) {
while(PeekMessageW(&Message, 0, 0, 0, PM_REMOVE)) {
TranslateMessage(&Message);
DispatchMessageW(&Message);
}
if(!IsWindow(window))break;
auto dc = GetDC(window);
win32_render(window, dc);
ReleaseDC(window, dc);
}
close(window);
dump_non_free_memory();
}