-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleHandler.cpp
149 lines (134 loc) · 3.77 KB
/
ConsoleHandler.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
#include "ConsoleHandler.h"
/* Constructor.
*
* Instantiates member variables.
*/
ConsoleHandler::ConsoleHandler(void) {
out = GetStdHandle(STD_OUTPUT_HANDLE);
}
/* Clears the console screen. */
void ConsoleHandler::clearScreen() {
DWORD written;
COORD coord = {0, 0};
FillConsoleOutputCharacter(out, ' ', 80*25, coord, &written);
SetConsoleCursorPosition(out, coord);
}
/* Getter.
*
* Returns a COORD for the cursor position.
*/
COORD ConsoleHandler::getCursorPos() {
CONSOLE_SCREEN_BUFFER_INFO si;
GetConsoleScreenBufferInfo(out, &si);
return si.dwCursorPosition;
}
/* Getter.
*
* Returns the width of the console.
*/
int ConsoleHandler::getConsoleWidth() {
CONSOLE_SCREEN_BUFFER_INFO si;
GetConsoleScreenBufferInfo(out, &si);
COORD coord = si.dwSize;
return coord.X;
}
/* Getter.
*
* Returns the height of the console.
*/
int ConsoleHandler::getConsoleHeight() {
CONSOLE_SCREEN_BUFFER_INFO si;
GetConsoleScreenBufferInfo(out, &si);
COORD coord = si.dwSize;
return coord.Y;
}
/* Toggles whether the console cursor is visible. */
void ConsoleHandler::toggleConsoleCursor() {
CONSOLE_CURSOR_INFO ci;
GetConsoleCursorInfo(this->out, &ci);
ci.bVisible = (ci.bVisible == TRUE) ? FALSE : TRUE;
SetConsoleCursorInfo(out, &ci);
}
/* Sets the console title.
*
* Parameters:
* string: A C stlye string to be used as the console title.
*/
void ConsoleHandler::setTitle(char* string) {
SetConsoleTitleA(string);
}
/* Sets the console title.
*
* Parameters:
* string: A string to be used as the console title.
*/
void ConsoleHandler::setTitle(std::string string) {
SetConsoleTitleA(string.c_str());
}
/* Sets the position of the console cursor.
*
* Parameters:
* coord: A COORD structure to move the cursor to.
*/
void ConsoleHandler::setCursorPos(COORD coord) {
SetConsoleCursorPosition(out, coord);
}
/* Sets the position of the console cursor.
*
* Parameters:
* x: An integer representing the place on the x axis to move the cursor to.
* y: An integer representing the place on the y axis to move the cursor to.
*/
void ConsoleHandler::setCursorPos(int x, int y) {
COORD coord = {x, y};
SetConsoleCursorPosition(out, coord);
}
/* Prints a string on a given row.
*
* Parameters:
* row: An integer representing the row in the console for the string to be printed on.
* line: The C style string to be printed.
*/
void ConsoleHandler::printLine(int row, char* line) {
printLine(row, 0, line);
}
/* Prints a string on a given row.
*
* Parameters:
* row: An integer representing the row in the console for the string to be printed on.
* line: The string to be printed.
*/
void ConsoleHandler::printLine(int row, std::string line) {
printLine(row, 0, line);
}
/* Prints a string at a given position.
*
* Parameters:
* row: An integer representing the row in the console for the string to be printed on.
* col: An integer representing the column in which ti start printing.
* line: The C style string to be printed.
*/
void ConsoleHandler::printLine(int row, int col, char* line) {
setCursorPos(row, col);
std::cout << line << std::endl;
}
/* Prints a string at a given position.
*
* Parameters:
* row: An integer representing the row in the console for the string to be printed on.
* col: An integer representing the column in which ti start printing.
* line: The string to be printed.
*/
void ConsoleHandler::printLine(int row, int col, std::string line) {
setCursorPos(row, col);
std::cout << line << std::endl;
}
/* Allows for the setting of console text attributes such as foreground and background
* colours.
*
* Parameters:
* attributes: The character attributes.
*/
void ConsoleHandler::setConsoleTextAttribute(WORD attributes) {
SetConsoleTextAttribute(out, attributes);
}