-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBufferScreen.cpp
150 lines (116 loc) · 4.68 KB
/
BufferScreen.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
#include "BufferScreen.hpp"
#include <iostream>
#include <windows.h>
#include <conio.h>
BufferScreen::BufferScreen() : nScreenWidth(120), nScreenHeight(30){
initialize();
setTitle(L"File Explorer");
}
BufferScreen::BufferScreen(const std::wstring& title) : nScreenWidth(120), nScreenHeight(30){
initialize();
setTitle(title);
}
BufferScreen::BufferScreen(int width, int height) : nScreenWidth(width), nScreenHeight(height){
initialize();
}
BufferScreen::BufferScreen(int width, int height, const std::wstring& title) : nScreenWidth(width), nScreenHeight(height){
initialize();
setTitle(title);
}
BufferScreen::~BufferScreen(){
delete[] screenBuffer;
CloseHandle(consoleHandle);
}
void BufferScreen::initialize(){
screenBuffer = new wchar_t[nScreenWidth * nScreenHeight];
consoleHandle = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(consoleHandle);
SetConsoleScreenBufferSize(consoleHandle, { static_cast<short>(nScreenWidth), static_cast<short>(nScreenHeight)});
windowSize = {0, 0, static_cast<short>(nScreenWidth - 1), static_cast<short>(nScreenHeight - 1)};
SetConsoleWindowInfo(consoleHandle, TRUE, &windowSize);
GetConsoleScreenBufferInfo(consoleHandle, &defaultConsoleInfo);
saved_attr = defaultConsoleInfo.wAttributes;
SetConsoleMode(consoleHandle, ENABLE_VIRTUAL_TERMINAL_PROCESSING);
clearScreen();
}
HANDLE BufferScreen::getHandle(){
return consoleHandle;
}
void BufferScreen::write(int row, int col, wchar_t ch){
screenBuffer[row * nScreenWidth + col] = ch;
}
void BufferScreen::write(int row, int col, const std::wstring& s){
size_t length = s.size();
size_t maxWrite = static_cast<size_t>(nScreenWidth * nScreenHeight - row * nScreenWidth - col);
size_t writeCount = std::min(length, maxWrite);
if (writeCount > 0) {
for (size_t i = 0; i < writeCount; ++i) {
screenBuffer[row * nScreenWidth + col + i] = s[i];
}
}
}
void BufferScreen::colored_write(int row, int col, wchar_t ch, WORD attr){
SetConsoleTextAttribute(consoleHandle, attr);
screenBuffer[row * nScreenWidth + col] = ch;
SetConsoleTextAttribute(consoleHandle, saved_attr);
}
void BufferScreen::colored_write(int row, int col, const std::wstring& s, WORD attr){
size_t length = s.size();
size_t maxWrite = static_cast<size_t>(nScreenWidth * nScreenHeight - row * nScreenWidth - col);
size_t writeCount = std::min(length, maxWrite);
SetConsoleTextAttribute(consoleHandle, attr);
if (writeCount > 0) {
for (size_t i = 0; i < writeCount; ++i) {
screenBuffer[row * nScreenWidth + col + i] = s[i];
}
}
SetConsoleTextAttribute(consoleHandle, saved_attr);
}
void BufferScreen::setTitle(const std::wstring& title){
SetConsoleTitleW(title.c_str());
}
void BufferScreen::clearScreen(){
for(int i = 0; i < nScreenWidth * nScreenHeight; i++) screenBuffer[i] = L' ';
}
void BufferScreen::drawBorder(){
// Corners
write(0, 0, L'╔');
write(0, nScreenWidth - 1, L'╗');
write(nScreenHeight -1, 0, L'╚');
write(nScreenHeight - 1, nScreenWidth - 1, L'╝');
//Left and Right Encasing
for(int row = 1; row < nScreenHeight - 1; row++)
{
write(row, 0, L'║');
write(row, nScreenWidth - 1, L'║');
}
//Upper and Lower Encasing
for(int col = 1; col < nScreenWidth - 1; col++)
{
write(0, col, L'═');
write(nScreenHeight - 1, col, L'═');
}
}
void BufferScreen::fillRect(int x1, int y1, int x2, int y2, wchar_t ch){
// Ensure the coordinates are within the buffer bounds
x1 = std::max(0, std::min(x1, nScreenHeight - 1));
y1 = std::max(0, std::min(y1, nScreenWidth - 1));
x2 = std::max(0, std::min(x2, nScreenHeight - 1));
y2 = std::max(0, std::min(y2, nScreenWidth - 1));
// Swap coordinates if needed
if (x1 > x2) std::swap(x1, x2);
if (y1 > y2) std::swap(y1, y2);
// Fill the rectangular area with the specified character
for(int row = x1; row <= x2; row++)
for(int col = y1; col <= y2; col++) write(row, col, ch);
}
void BufferScreen::hideCursor(){
CONSOLE_CURSOR_INFO cursorInfo;
cursorInfo.dwSize = 1;
cursorInfo.bVisible = false;
SetConsoleCursorInfo(consoleHandle, &cursorInfo);
}
void BufferScreen::display(){
DWORD bytesWritten = 0;
WriteConsoleOutputCharacterW(consoleHandle, screenBuffer, nScreenWidth * nScreenHeight, { 0, 0 }, &bytesWritten);
}