-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphics.cpp
138 lines (120 loc) · 3.15 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
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
#include "Graphics.h"
Graphics::Graphics(DWORD stdHandle)
: _console(GetStdHandle(stdHandle)), _background(Color::Black), _foreground(Color::White)
{
updateConsoleAttributes();
}
void Graphics::clearScreen() const
{
DWORD d;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(_console, &csbi);
auto size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputAttribute(_console, 7, 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(const 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, const 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::moveCursor(COORD c)
{
//working progress
RECT aRect, cli;
POINT ptDiff;
GetWindowRect(GetConsoleWindow(), &aRect);
SetCursorPos(aRect.left + c.X * 9, aRect.top + (c.Y * 21));
}
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)
{
x -= left;
y -= top;
return x >= 0 && y >= 0 && x < width && y < height;
}
void Graphics::clearScreen(uint32_t size, uint8_t x, uint8_t y) const
{
DWORD d;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(_console, &csbi);
FillConsoleOutputAttribute(_console, 7, size, {x, y}, &d);
FillConsoleOutputCharacter(_console, L' ', size, {x, y}, &d);
}