forked from m0nkeykong/MethodsInSE_graphicSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphics.cpp
105 lines (90 loc) · 2.67 KB
/
Graphics.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
#include "Graphics.h"
Graphics::Graphics(DWORD stdHandle)
: _console(GetStdHandle(stdHandle)), _background(Color::Black), _foreground(Color::White)
{
updateConsoleAttributes();
}
void Graphics::clearScreen()
{
DWORD d;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(_console, &csbi);
auto size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputAttribute(_console, csbi.wAttributes, size, { 0, 0 }, &d);
FillConsoleOutputCharacter(_console, L' ', size, { 0, 0 }, &d);
}
void Graphics::moveTo(int x, int y)
{
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(_console, c);
}
void Graphics::setBackground(Color color)
{
_background = color;
updateConsoleAttributes();
}
void Graphics::setForeground(Color color)
{
_foreground = color;
updateConsoleAttributes();
}
void Graphics::write(string s)
{
WriteConsoleA(_console, s.c_str(), s.size(), nullptr, nullptr);
}
void Graphics::write(wstring s)
{
WriteConsoleW(_console, s.c_str(), s.size(), nullptr, nullptr);
}
void Graphics::write(int x, int y, string s)
{
moveTo(x, y);
write(s);
}
void Graphics::write(int x, int y, wstring s)
{
moveTo(x, y);
write(s);
}
void Graphics::setCursorVisibility(bool isVisible)
{
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(_console, &cci);
cci.bVisible = isVisible;
SetConsoleCursorInfo(_console, &cci);
}
void Graphics::updateConsoleAttributes()
{
DWORD attributes = 0;
switch (_foreground)
{
case Color::Black: break;
case Color::Blue: attributes |= FOREGROUND_BLUE; break;
case Color::Green: attributes |= FOREGROUND_GREEN; break;
case Color::Red: attributes |= FOREGROUND_RED; break;
case Color::Cyan: attributes |= FOREGROUND_BLUE | FOREGROUND_GREEN; break;
case Color::Purple: attributes |= FOREGROUND_BLUE | FOREGROUND_RED; break;
case Color::Orange: attributes |= FOREGROUND_GREEN | FOREGROUND_RED; break;
case Color::White: attributes |= FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED; break;
}
switch (_background)
{
case Color::Black: break;
case Color::Blue: attributes |= BACKGROUND_BLUE; break;
case Color::Green: attributes |= BACKGROUND_GREEN; break;
case Color::Red: attributes |= BACKGROUND_RED; break;
case Color::Cyan: attributes |= BACKGROUND_BLUE | BACKGROUND_GREEN; break;
case Color::Purple: attributes |= BACKGROUND_BLUE | BACKGROUND_RED; break;
case Color::Orange: attributes |= BACKGROUND_GREEN | BACKGROUND_RED; break;
case Color::White: attributes |= BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED; break;
}
SetConsoleTextAttribute(_console, attributes);
}
bool isInside(int x, int y, int left, int top, int width, int height) //check mouse click location
{
x -= left;
y -= top;
return x >= 0 && y >= 0 && x < width && y < height;
}