-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleBuffer.cpp
160 lines (123 loc) · 3.22 KB
/
ConsoleBuffer.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
151
152
153
154
155
156
157
158
159
160
#include "Color.h"
#include "Config.h"
#include "ConsoleBuffer.h"
#include "Pixel.h"
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <Windows.h>
ConsoleBuffer::ConsoleBuffer()
{
// get the default output handle
console = GetStdHandle(STD_OUTPUT_HANDLE);
// set up the buffer
for (int i = 0; i < SCREEN_HEIGHT; i++)
{
for (int j = 0; j < SCREEN_WIDTH; j++)
{
cache[j][i] = buffer[j][i] = new Pixel();
}
}
// paint it black
reset();
}
ConsoleBuffer::~ConsoleBuffer()
{
for (int i = 0; i < SCREEN_WIDTH; i++)
{
for (int j = 0; j < SCREEN_HEIGHT; j++)
{
buffer[i][j] = NULL;
cache[i][j] = NULL;
}
}
}
void ConsoleBuffer::write(int x, int y, Pixel* pixel, bool forceRefresh)
{
// respect screen boundaries
if (x <0 || y < 0 || x >= SCREEN_WIDTH || y >= SCREEN_HEIGHT) return;
// if both foreground and background colors as magenta, the pixel is transparent
if (pixel->bgColor == Color::MAGENTA && pixel->fgColor == Color::MAGENTA) return;
// schedules pixel repaint
buffer[x][y] = pixel;
}
void ConsoleBuffer::write(int x, int y, Sprite* sprite, bool flipX, bool flipY)
{
int startX = flipX ? sprite->width - 1 : 0;
int startY = flipY ? sprite->height - 1 : 0;
for (int j = 0; j < (signed)sprite->width; j++)
{
for (int i = 0; i < (signed)sprite->height; i++)
{
write(x + j, y + i, &sprite->map[std::abs(startY - i)][std::abs(startX - j)]);
}
}
}
void ConsoleBuffer::write(int x, int y, std::string text, int fgColor, int bgColor)
{
if (y >= SCREEN_HEIGHT) return;
for (unsigned int i = 0; i < text.length(); i++)
{
if (x + i >= SCREEN_WIDTH) return;
write(x + i, y, new Pixel(text[i], fgColor, bgColor));
}
}
void ConsoleBuffer::writeCentered(std::string text, int fgColor, int bgColor)
{
int x = (SCREEN_WIDTH - text.length()) / 2;
int y = (int) SCREEN_HEIGHT / 2;
write(x, y, text, fgColor, bgColor);
}
void ConsoleBuffer::writeCentered(Sprite* sprite)
{
int x = (SCREEN_WIDTH - sprite->width) / 2;
int y = (SCREEN_HEIGHT - sprite->height) / 2;
write(x, y, sprite);
}
void ConsoleBuffer::forceRefresh(int fromX, int fromY, int toX, int toY)
{
if (toX < 0) toX = fromX;
if (toY < 0) toY = fromY;
for (int i = fromY; i <= toY; i++) {
for (int j = fromX; j <= toX; j++) {
buffer[j][i]->forceRefresh = true;
}
}
}
void ConsoleBuffer::flush()
{
for (int x = 0; x < SCREEN_WIDTH; x++)
{
for (int y = 0; y < SCREEN_HEIGHT; y++)
{
if (buffer[x][y]->forceRefresh || !cache[x][y]->equalsTo(buffer[x][y]))
{
cache[x][y] = buffer[x][y];
cache[x][y]->forceRefresh = false;
printAtCoord(x, y);
}
}
}
// puts the cursor outside the screen, to stop annoying
printAtCoord(0, SCREEN_HEIGHT - 1);
}
void ConsoleBuffer::gotoCoord(int x, int y)
{
SetConsoleCursorPosition(console, { x, y });
}
void ConsoleBuffer::printAtCoord(int x, int y)
{
Pixel* pixel = buffer[x][y];
int color = pixel->fgColor | pixel->bgColor << 4;
SetConsoleTextAttribute(console, color);
gotoCoord(x, y);
printf("%c", pixel->value);
}
void ConsoleBuffer::reset()
{
// ♫ I see a red door and I want to paint it black ♪
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console, 0);
std::cout << std::setw(2000) << ' ';
}