-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleBuffer.hpp
More file actions
89 lines (72 loc) · 1.84 KB
/
ConsoleBuffer.hpp
File metadata and controls
89 lines (72 loc) · 1.84 KB
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
#pragma once
#include "stdafx.h"
#include <vector>
#include <string>
#undef DrawText
#ifndef CONSOLEBUFFER_HPP
#define CONSOLEBUFFER_HPP
class ConsoleBuffer
{
public:
ConsoleBuffer(short width, short height)
: width(width), height(height)
{
buffer.resize(width * height);
hConsole = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL);
SetConsoleActiveScreenBuffer(hConsole);
}
HANDLE GetHandle() const {
return hConsole;
}
void Clear(CHAR fill = ' ')
{
for (auto& c : buffer) {
c.Char.AsciiChar = fill;
c.Attributes = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE;
}
}
int DrawText(short x, short y, const std::string& text, WORD attr = 7)
{
int numLines = 1;
if (y < 0 || y >= height) return 0;
short xcounter = 0;
for (size_t i = 0; i < text.size(); ++i)
{
short tx = x + (short)xcounter;
xcounter++;
if (tx < 0 || tx >= width) continue;
if (text[i] == '\n')
{
numLines++;
y++;
xcounter = 0;
continue;
}
buffer[y * width + tx].Char.AsciiChar = text[i];
buffer[y * width + tx].Attributes = attr;
}
return numLines;
}
void Render()
{
SMALL_RECT rect = { 0, 0, width - 1, height - 1 };
COORD size = { width, height };
COORD zero = { 0, 0 };
WriteConsoleOutputA(
hConsole,
buffer.data(),
size,
zero,
&rect);
}
private:
HANDLE hConsole;
short width, height;
std::vector<CHAR_INFO> buffer;
};
#endif