-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows.cpp
320 lines (248 loc) · 8.75 KB
/
windows.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
// C++ standard
#include <iostream>
// Windows libraries
#pragma comment(lib,"user32.lib")
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")
// Termy
#include "windows.hpp"
using std::cout;
using std::endl;
//------------------------------------------------------------------------------
// Exception classes
//------------------------------------------------------------------------------
UnhandledMessageError::UnhandledMessageError(Win::UInt msgType)
: std::runtime_error("unhandled window message")
{
this->msgType = msgType;
}
WindowError::WindowError(const char *msg)
: std::runtime_error(msg)
{
this->window = NULL;
this->errorCode = Win::lastError();
}
WindowError::WindowError(const char *msg, Win::Window window)
: std::runtime_error(msg)
{
this->window = window;
this->errorCode = Win::lastError();
}
WindowError::WindowError(const char *msg, Win::Window window, Win::DWord errorCode)
: std::runtime_error(msg)
{
this->window = window;
this->errorCode = errorCode;
}
//------------------------------------------------------------------------------
// Window process callback
//------------------------------------------------------------------------------
Win::LResult WINAPI
windowProc(Win::Window window, Win::UInt msgType,
Win::WParam wParam, Win::LParam lParam) {
if (msgType == WM_CREATE) {
// lParam is a pointer to CreateStruct which holds the parameters
// passed to CreateWindow.
Win::CreateStruct *pcs = (Win::CreateStruct *)lParam;
// Here we get the termy instance we passed to CreateWindow.
Termy *termy = static_cast<Termy *>(pcs->lpCreateParams);
// And set it as window user data for future access.
SetWindowLongPtrW(
window,
Win::gwlpUserdata,
PtrToUlong(termy));
}
// Retrieve our termy object from the window's user data.
Termy *termy = reinterpret_cast<Termy *>(
static_cast<Win::LPointer>(
GetWindowLongPtrW(window, Win::gwlpUserdata)));
Win::LResult ret;
try {
if (!termy)
throw UnhandledMessageError(msgType);
ret = termy->handleMessage(msgType, wParam, lParam);
} catch (const UnhandledMessageError &ex) {
ret = DefWindowProcW(window, msgType, wParam, lParam);
}
return ret;
}
//------------------------------------------------------------------------------
// Main class
//------------------------------------------------------------------------------
void Termy::init() {
this->instance = {};
this->window = {};
this->factory = NULL;
this->renderTarget = NULL;
this->brush = NULL;
this->writeFactory = NULL;
this->textFormat = NULL;
this->text = L"Hello, world!";
this->textLength = static_cast<Win::UInt>(wcslen(this->text));
}
Termy::Termy(Win::Instance instance) {
this->init();
this->instance = instance;
cout << "Create device independent resources." << endl;
this->createDeviceIndepdendentComponents();
cout << "Create window." << endl;
this->createWindow();
}
void Termy::createWindow() {
Win::WindowClassEx windowClass = {};
windowClass.cbSize = sizeof(Win::WindowClassEx);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = &windowProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = sizeof(LONG_PTR);
windowClass.hInstance = this->instance;
windowClass.lpszClassName = L"TermyMain";
if (!RegisterClassExW(&windowClass)) {
throw WindowError("Failed to register window class.");
}
this->window = CreateWindowExW(
0L,
L"TermyMain",
L"Termy",
WS_OVERLAPPEDWINDOW,
0, 0,
500, 300,
NULL,
NULL,
this->instance,
this);
if (!this->window)
throw WindowError("Unable to create window.");
ShowWindow(this->window, SW_SHOWDEFAULT);
UpdateWindow(this->window);
}
void Termy::createDeviceIndepdendentComponents() {
Win::Result result;
result = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&this->factory);
if (Win::failed(result)) throw WindowError("Failed to create Direct 2D factory.", this->window);
result = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&this->writeFactory));
if (Win::failed(result)) throw WindowError("Failed to create Direct Write factory.", this->window);
result = this->writeFactory->CreateTextFormat(
L"Gabriola",
NULL,
DWRITE_FONT_WEIGHT_REGULAR,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
72.0f,
L"en-us",
&this->textFormat);
if (Win::failed(result)) throw WindowError("Failed to create text format.", this->window);
}
void Termy::createDeviceDependentComponents() {
Win::Rect rect;
Win::Result result;
D2D1_SIZE_U size;
GetClientRect(this->window, &rect);
size = D2D1::SizeU(
rect.right - rect.left,
rect.bottom - rect.top);
result = this->factory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(this->window, size),
&this->renderTarget);
if (Win::failed(result)) throw WindowError("Failed to create render target.", this->window);
result = this->renderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::LightSlateGray),
&this->brush);
if (Win::failed(result)) throw WindowError("Failed to create brush.", this->window);
result = this->textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
if (Win::failed(result)) throw WindowError("Failed to set font alignment.", this->window);
result = this->textFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
if (Win::failed(result)) throw WindowError("Failed to set font paragraph alignment.", this->window);
}
void Termy::messageLoop() {
Win::Msg msg;
Win::Bool status;
while(1) {
status = GetMessageW(&msg, NULL, 0, 0);
if (status == 0) break;
if (status == -1)
throw WindowError("GetMessage failed.", this->window);
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
void Termy::requestPaint() {
InvalidateRect(this->window, NULL, FALSE);
}
void Termy::onResize(int width, int height) {
if (!this->renderTarget) return;
this->renderTarget->Resize(D2D1::SizeU(width, height));
}
void Termy::paint() {
Win::Rect rect;
GetClientRect(this->window, &rect);
D2D1_RECT_F layoutRect = D2D1::RectF(
static_cast<Win::Float>(rect.left),
static_cast<Win::Float>(rect.top),
static_cast<Win::Float>(rect.right - rect.left),
static_cast<Win::Float>(rect.bottom - rect.top));
Win::PaintStruct ps;
BeginPaint(this->window, &ps);
this->renderTarget->BeginDraw();
this->renderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
this->renderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
this->renderTarget->DrawLine(
D2D1::Point2F(0.0f, 0.0f),
D2D1::Point2F(50.0f, 50.0f),
this->brush,
1.0f);
this->renderTarget->DrawText(
this->text,
this->textLength,
this->textFormat,
layoutRect,
this->brush);
this->renderTarget->EndDraw();
ValidateRect(this->window, NULL);
EndPaint(this->window, &ps);
}
Win::LResult Termy::handleMessage(Win::UInt msgType, Win::WParam wParam,
Win::LParam lParam) {
switch (msgType) {
case WM_DISPLAYCHANGE:
{
this->requestPaint();
return 1;
}
case WM_SIZE:
{
unsigned int width;
unsigned int height;
width = static_cast<unsigned int>(Win::lowWord(lParam));
height = static_cast<unsigned int>(Win::highWord(lParam));
this->onResize(width, height);
return 1;
}
case WM_PAINT:
{
if (!this->renderTarget)
this->createDeviceDependentComponents();
this->paint();
return 1;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 1;
}
default:
{
throw UnhandledMessageError(msgType);
}
}
}
void Termy::start() {
cout << "Entering message loop." << endl;
this->messageLoop();
}