forked from pratapppv/graphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.h
398 lines (348 loc) · 7.39 KB
/
graphics.h
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
/*
This library is provided as is and the Author doesnot share any accountability nor guarentee any.
#ppvftw
contact me at [email protected]
*/
#pragma once
#ifndef GRAPHICS_H
#define GRAPHICS_H
#define FOREGROUND_REDLIGHT 0x000C
#include <iostream>
#include <math.h>
#include <windows.h>
#include <Windows.h>
#include <windef.h>
#include <string>
using namespace std;
/*
#define BLACK "0"
#define BLUE "1"
#define GREEN "2"
#define CYAN "3"
#define RED "4"
#define PURPLE "5"
#define YELLOW "6"
#define WHITE "7"
#define GREY "8"
#define LIGHTBLUE "9"
#define LIGHTGREEN "A"
#define LIGHTCYAN "B"
#define LIGHTRED "C"
#define LIGHTPURPLE "D"
#define LIGHTYELLOW "E"
string bg = to_string(RGB(0, 0, 0));
string fc = to_string(RGB(0, 0, 0));
*/
struct rgb
{
int r;
int g;
int b;
};
static class canvas
{
protected:
COORD BGN, POS;
short mov;
short movx;
HWND hWnd;
RECT pRECT;
COORD POS1;
COORD BGN1;
HDC hDC;
HANDLE StdOut;
COORD setCordScale(COORD, RECT);
public:
canvas()
{
mov = 30;
movx = 0;
hWnd = GetForegroundWindow();
pRECT = { 0 };
POS1 = { 0 };
BGN1 = setCordScale(POS1, pRECT);
GetWindowRect(hWnd, &pRECT);
hDC = GetWindowDC(hWnd);
StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
}
HPEN getPen(int iPEN_STYLE, int iPEN_SIZE, int iCOLORREF)
{
return CreatePen(iPEN_STYLE, iPEN_SIZE, iCOLORREF);
}
};
COORD canvas::setCordScale(COORD POS, RECT pRECT)
{
if (POS.X == 0)
POS.X = 1;
if (POS.Y == 0)
POS.Y = 1;
int nROWS = 25;
int nCOLS = 80;
double CX = (pRECT.right - pRECT.left) / nCOLS;
double CY = (pRECT.bottom - pRECT.top) / nROWS;
POS.X *= CX;
POS.Y *= CY;
int xBORDER = GetSystemMetrics(SM_CXBORDER);
int yBORDER = GetSystemMetrics(SM_CYMENU);
int xDRAG = GetSystemMetrics(SM_CXDRAG);
int yDRAG = GetSystemMetrics(SM_CYDRAG);
POS.X += xBORDER + xDRAG;
POS.Y += yBORDER + yDRAG;
return POS;
}
class rect :public canvas
{
HPEN PEN;
int thick;
BOOL PlotRect(HDC, HPEN, int r, int g, int b);
public:
void draw(int x1, int y1, int x2, int y2, int r, int g, int b);
rect(int thik,int r,int g,int b)
{
thick = thik;
PEN = getPen(PS_SOLID, thik, RGB(r,g,b));
}
void Delete();
};
/*
x1 and y1 are the corrdinates of the top left hand vertex
x2 and y2 are the corrdinates of the bottom right hand vertex
r,g,b are the values to fill the rectangle
*/
void rect::draw(int x1, int y1, int x2, int y2, int r, int g, int b)
{
BGN.X = (short)x1 + movx;
BGN.Y = (short)y1 + mov;
POS.X = (short)x2 + movx;
POS.Y = (short)y2 + mov;
PlotRect(hDC, PEN,r,g,b);
}
BOOL rect::PlotRect(HDC hDC, HPEN hPen, int r,int g,int b)
{
SelectObject(hDC, hPen);
RECT a;
a.bottom = POS.Y - thick+1;
a.right = POS.X - thick+1;
a.top = BGN.Y + thick-2;
a.left = BGN.X + thick-2;
Rectangle(hDC, BGN.X, BGN.Y, POS.X, POS.Y);
HBRUSH brush = CreateSolidBrush(RGB(r, g, b));
FillRect(hDC, &a, brush);
DeleteObject(brush);
return true;
}
void rect::Delete()
{
PEN = getPen(PS_SOLID, thick, RGB(0, 0, 0));
SelectObject(hDC, PEN);
RECT a;
a.bottom = POS.Y;
a.right = POS.X;
a.top = BGN.Y;
a.left = BGN.X;
Rectangle(hDC, BGN.X, BGN.Y, POS.X, POS.Y);
HBRUSH brush;
brush = CreateSolidBrush(RGB(0, 0, 0));
FillRect(hDC, &a, brush);
DeleteObject(brush);
DeleteObject(PEN);
}
class line :public canvas
{
HPEN PEN;
int thick;
BOOL PlotLine();
public:
void draw(int x1, int y1, int x2, int y2);
void Delete();
line(int thic, int r, int g, int b)
{
thick = thic;
PEN = getPen(PS_SOLID, thic, RGB(r, g, b));
}
};
/*
x1 and y1 are the coordinates of the start of the line
x2 and y2 are the coordinates of the end of the line
*/
void line::Delete()
{
PEN = getPen(PS_SOLID, thick, RGB(0, 0, 0));
SelectObject(hDC, PEN);
MoveToEx(hDC, BGN.X, BGN.Y, NULL);
LineTo(hDC, POS.X, POS.Y);
DeleteObject(PEN);
}
void line::draw(int x1, int y1, int x2, int y2)
{
BGN.X = (short)x1 + movx;
BGN.Y = (short)y1 + mov;
POS.X = (short)x2 + movx;
POS.Y = (short)y2 + mov;
PlotLine();
}
BOOL line::PlotLine()
{
SelectObject(hDC, PEN);
MoveToEx(hDC, BGN.X, BGN.Y, NULL);
LineTo(hDC, POS.X, POS.Y);
return true;
}
class circle:public canvas
{
HPEN PEN;
int thick;
BOOL PlotCirc(int r,int g,int b);
public:
void Delete();
void draw(int x, int y, int rad,int r,int g,int b);
circle(int thic, int r, int g, int b)
{
thick = thic;
PEN = getPen(PS_SOLID, thic, RGB(r, g, b));
}
};
void circle::Delete()
{
PEN = getPen(PS_SOLID, thick, RGB(0, 0, 0));
SelectObject(hDC, PEN);
HBRUSH hbr = CreateSolidBrush(RGB(0, 0, 0));
HBRUSH hOld = (HBRUSH)SelectObject(hDC, hbr);
Ellipse(hDC, BGN.X, BGN.Y, POS.X, POS.Y);
DeleteObject(hbr);
DeleteObject(hOld);
DeleteObject(PEN);
}
void circle::draw(int x, int y, int rad, int r, int g, int b)
{
BGN.X = (short)x + movx - rad / 2;
BGN.Y = (short)y + mov - rad / 2;
POS.X = (short)x + movx + rad / 2;
POS.Y = (short)y + mov + rad / 2;
PlotCirc(r,g,b);
}
BOOL circle::PlotCirc(int r,int g,int b)
{
SelectObject(hDC, PEN);
HBRUSH hbr = CreateSolidBrush(RGB(r, g, b));
HBRUSH hOld = (HBRUSH)SelectObject(hDC, hbr);
Ellipse(hDC, BGN.X, BGN.Y, POS.X, POS.Y);
DeleteObject(hbr);
DeleteObject(hOld);
DeleteObject(PEN);
return true;
}
struct mouse
{
long x;
long y;
};
class Mouse:protected canvas
{
protected:
int keyPressed(int key)
{
return (GetAsyncKeyState(key));
}
public:
mouse FSmouse()
{
POINT a;
GetCursorPos(&a);
mouse p;
p.x = a.x;
p.y = a.y;
return p;
}
mouse Smouse()
{
POINT a;
GetCursorPos(&a);
ScreenToClient(hWnd, &a);
mouse p;
p.x = a.x;
p.y = a.y;
return p;
}
char getMouseClick()
{
if (keyPressed(VK_LBUTTON) == -32768)
{
return 'L';
}
else if (keyPressed(VK_RBUTTON))
{
return 'R';
}
}
};
int getmaxx(void) { return 640; }
int getmaxy(void) { return 300; }
class text :protected canvas
{
PAINTSTRUCT ps;
HFONT hFont;
HDC hdc;
RECT rect;
public:
void cfont(string,int,...);
void txt(int,int,string);
text(int r,int g,int b)
{
hdc = GetDC(hWnd);
SetBkColor(hdc, RGB(0, 0, 0));
SetTextColor(hdc, RGB(r, g, b));
}
};
#define BOLD 1
#define ITALIC 2
#define UNDERLINE 3
void text::cfont(string f,int size,...)
{
va_list ap;
va_start(ap,f);
int bold = 400;
bool italic = FALSE, underline = FALSE;
for (int i = 0; i < 5; i++)
{
int k = va_arg(ap, int);
switch (k)
{
case 1:
bold = 700;
break;
case 2:
italic = true;
break;
case 3:
underline = true;
break;
}
}
wstring stemp = std::wstring(f.begin(), f.end());
hFont = CreateFont(size, 0, 0, 0, bold, italic, underline, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, stemp.c_str());
SelectObject(hdc, hFont);
}
void text::txt(int x,int y,string t)
{
wstring stemp = std::wstring(t.begin(), t.end());
TextOut(hdc, x, y, stemp.c_str(),t.length());
EndPaint(hWnd, &ps);
ReleaseDC(hWnd, hdc);
}
void clear_text()
{
system("cls");
}
rgb getPixel(int x, int y)
{
rgb clr;
HDC dc = GetDC(NULL);
COLORREF color = GetPixel(dc, x, y);
ReleaseDC(NULL, dc);
clr.r = GetRValue(color);
clr.g = GetGValue(color);
clr.b = GetBValue(color);
return clr;
}
#endif