-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSGL.cpp
444 lines (353 loc) · 8.43 KB
/
SGL.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*********************************************************
COPYRIGHT NOTICE (C) 2014 Erik Örjehag SGL
You may:
* Make profit using this library.
* Distribute this library.
* Alter the source code.
You may not:
* Leave out proper creadit. If this library
(or a altered version) is used for non
personal use a link to the official website
or the GitHub repository is appropritate.
**********************************************************/
#ifndef UNICODE
#define UNICODE
#endif
#include "SGL.h"
SGL *sgl = NULL;
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow)
{
const WCHAR CLASS_NAME[] = L"winSGL";
WNDCLASS wndClass;
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WindowProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = CLASS_NAME;
RegisterClass(&wndClass);
sgl = new SGL();
sgl->window = CreateWindowEx (
0, // Optional window styles
CLASS_NAME, // Window class
L"SGL App", // Window text
WS_OVERLAPPEDWINDOW, // Window style
CW_USEDEFAULT, // Position x
CW_USEDEFAULT, // Position y
CW_USEDEFAULT, // Width
CW_USEDEFAULT, // Height
NULL, // Paranet window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if(sgl->window == NULL) return 0;
ShowWindow(sgl->window, nCmdShow);
SetEvent(sgl->evCanDraw);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Gdiplus::GdiplusShutdown(gdiplusToken);
return msg.wParam;
}
DWORD WINAPI UserThread(LPVOID lpParameter)
{
WaitForSingleObject(sgl->evCanDraw, INFINITE);
main();
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
sgl->hUserThread = CreateThread(0, 0, UserThread, 0, 0, 0);
return 0;
case WM_DESTROY:
sgl->cleanUp();
return 0;
case WM_SIZE:
sgl->resize();
return 0;
case WM_PAINT:
return 0;
case WM_ERASEBKGND:
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
SGL::SGL()
{
gslen = 0;
evCanDraw = CreateEvent(0, TRUE, FALSE, L"evCanDraw");
mxUseGraphics = CreateMutex(0, FALSE, L"mxUseGraphics");
fontSize = 24; strokeStyle = SOLID;
strokeWidth = 1; alpha = 255;
red = 255; green = 255; blue = 255;
updateTools();
}
void SGL::resize()
{
using namespace Gdiplus;
waitTillCanUseGraphics();
RECT client;
GetClientRect(window, &client);
width = client.right - client.left;
height = client.bottom - client.top;
screenDC = BeginPaint(window, &ps);
bufferDC = CreateCompatibleDC(screenDC);
HBITMAP bmp = CreateCompatibleBitmap(screenDC, width, height);
SelectObject(bufferDC, bmp); // Leak?
if(graphics) delete graphics;
graphics = new Graphics(bufferDC);
graphics->SetSmoothingMode(SmoothingMode::SmoothingModeAntiAlias);
graphics->Clear(Color::White);
stopUsingGraphics();
}
void SGL::updateTools()
{
using namespace Gdiplus;
waitTillCanUseGraphics();
if(pen) delete pen;
if(brush) delete brush;
pen = new Pen(Color(alpha, red, green, blue), strokeWidth);
pen->SetDashStyle(strokeStyle);
brush = new SolidBrush(Color(alpha, red, green, blue));
stopUsingGraphics();
}
void SGL::cleanUp()
{
EndPaint(window, &ps);
if(graphics) delete graphics;
if(brush) delete brush;
if(pen) delete pen;
ResetEvent(evCanDraw);
TerminateThread(hUserThread, 0);
PostQuitMessage(0);
}
// API
void SGL::waitTillCanUseGraphics()
{
WaitForSingleObject(mxUseGraphics, INFINITE);
}
void SGL::stopUsingGraphics()
{
ReleaseMutex(mxUseGraphics);
}
void SGL::render()
{
waitTillCanUseGraphics();
BitBlt(screenDC, 0, 0, width, height, bufferDC, 0,0, SRCCOPY);
stopUsingGraphics();
}
void SGL::setStrokeWidth(float w)
{
strokeWidth = w;
updateTools();
}
void SGL::setStrokeStyle(int s) {
strokeStyle = (Gdiplus::DashStyle) s;
updateTools();
}
void SGL::setColor(byte r, byte g, byte b)
{
red = r;
green = g;
blue = b;
updateTools();
}
void SGL::setAlpha(float a)
{
alpha = 255 * a;
updateTools();
}
void SGL::setFontSize(int s)
{
fontSize = s;
}
void SGL::setWindowSize(int w, int h)
{
SetWindowPos(window, 0, 0, 0, w, h, SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
}
void SGL::setWindowTitle(const WCHAR str[])
{
SetWindowText(window, str);
}
int SGL::getWidth()
{
return width;
}
int SGL::getHeight()
{
return height;
}
void SGL::save()
{
waitTillCanUseGraphics();
gs[gslen] = graphics->Save();
gslen++;
stopUsingGraphics();
}
void SGL::restore()
{
waitTillCanUseGraphics();
graphics->Restore(gs[gslen-1]);
gslen--;
stopUsingGraphics();
}
void SGL::translate(float x, float y)
{
waitTillCanUseGraphics();
graphics->TranslateTransform(x, y);
stopUsingGraphics();
}
void SGL::rotate(float angle)
{
waitTillCanUseGraphics();
graphics->RotateTransform(angle);
stopUsingGraphics();
}
void SGL::scale(float x, float y)
{
waitTillCanUseGraphics();
graphics->ScaleTransform(x, y);
stopUsingGraphics();
}
void SGL::clear(byte r, byte g, byte b)
{
waitTillCanUseGraphics();
graphics->Clear(Gdiplus::Color(r, g, b));
stopUsingGraphics();
}
void SGL::fillRect(float x, float y, float w, float h)
{
waitTillCanUseGraphics();
graphics->FillRectangle(brush, x, y, w, h);
stopUsingGraphics();
}
void SGL::fillTriangle(float x, float y, float w, float h)
{
using namespace Gdiplus;
Point points[] = {
Point(x + w / 2, y),
Point(x, y + h),
Point(x + w, y + h),
};
fillPolygon(*points, 3);
}
void SGL::fillCircle(float x, float y, float r)
{
waitTillCanUseGraphics();
graphics->FillEllipse(brush, x, y, r, r);
stopUsingGraphics();
}
void SGL::fillEllipse(float x, float y, float w, float h)
{
waitTillCanUseGraphics();
graphics->FillEllipse(brush, x, y, w, h);
stopUsingGraphics();
}
void SGL::fillPolygon(Gdiplus::Point &points, int count)
{
waitTillCanUseGraphics();
graphics->FillPolygon(brush, &points, count);
stopUsingGraphics();
}
void SGL::strokeRect(float x, float y, float w, float h)
{
waitTillCanUseGraphics();
graphics->DrawRectangle(pen, x, y, w, h);
stopUsingGraphics();
}
void SGL::strokeTriangle(float x, float y, float w, float h)
{
using namespace Gdiplus;
Point points[] = {
Point(x + w / 2, y),
Point(x, y + h),
Point(x + w, y + h),
};
strokePolygon(*points, 3);
}
void SGL::strokeCircle(float x, float y, float r)
{
waitTillCanUseGraphics();
graphics->DrawEllipse(pen, x, y, r, r);
stopUsingGraphics();
}
void SGL::strokeEllipse(float x, float y, float w, float h)
{
waitTillCanUseGraphics();
graphics->DrawEllipse(pen, x, y, w, h);
stopUsingGraphics();
}
void SGL::strokePolygon(Gdiplus::Point &points, int count)
{
waitTillCanUseGraphics();
graphics->DrawPolygon(pen, &points, count);
stopUsingGraphics();
}
void SGL::drawLine(float startx, float starty, float endx, float endy)
{
waitTillCanUseGraphics();
graphics->DrawLine(pen, startx, starty, endx, endy);
stopUsingGraphics();
}
void SGL::moveTo(float x, float y)
{
xPenPos = x;
yPenPos = y;
}
void SGL::lineTo(float x, float y)
{
waitTillCanUseGraphics();
graphics->DrawLine(pen, xPenPos, yPenPos, x, y);
moveTo(x, y);
stopUsingGraphics();
}
void SGL::drawString(const WCHAR str[], float x, float y)
{
using namespace Gdiplus;
waitTillCanUseGraphics();
FontFamily fontFamily(L"Verdana");
Font font(&fontFamily, fontSize, FontStyleRegular, UnitPixel);
PointF pos(x, y);
graphics->DrawString(str, -1, &font, pos, brush);
stopUsingGraphics();
}
void SGL::drawImage(Gdiplus::Image *img, float x, float y)
{
waitTillCanUseGraphics();
graphics->DrawImage(img, x, y, img->GetWidth(), img->GetHeight());
stopUsingGraphics();
}
void SGL::drawPixel(float x, float y)
{
waitTillCanUseGraphics();
graphics->FillRectangle(brush, x, y, 1.f, 1.f);
stopUsingGraphics();
}
Gdiplus::Graphics *SGL::useGDI()
{
return graphics;
}
Gdiplus::Pen *SGL::copyPen()
{
using namespace Gdiplus;
return new Pen(Color(alpha, red, green, blue), strokeWidth);
}
Gdiplus::SolidBrush *SGL::copyBrush()
{
using namespace Gdiplus;
return new SolidBrush(Color(alpha, red, green, blue));
}