-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathConsoleRenderer.cpp
130 lines (105 loc) · 3.07 KB
/
ConsoleRenderer.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
#include <cassert>
#include "ConsoleRenderer.h"
#include "ConsoleFrame.h"
ConsoleRenderer::ConsoleRenderer(int width, int height, float frameRate)
: mWidth(width)
, mHeight(height)
, mFrameRate(frameRate)
, mFrameTime(1.0f / frameRate)
{
mActualWidth = mWidth * CELL_WIDTH;
mConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD bufferSize = {(SHORT)mActualWidth, (SHORT)mHeight};
SetConsoleScreenBufferSize(mConsoleHandle, bufferSize);
CONSOLE_CURSOR_INFO cursorInfo = {1, FALSE};
SetConsoleCursorInfo(mConsoleHandle, &cursorInfo);
mBuffer = new CHAR_INFO[mActualWidth * mHeight];
mMainFrame = new ConsoleFrame(0, 0, width, height);
}
ConsoleRenderer::~ConsoleRenderer()
{
Clear();
delete mMainFrame;
delete[] mBuffer;
}
void ConsoleRenderer::Clear()
{
for(auto frame : mFrames)
{
delete frame;
}
mFrames.clear();
assert(mMainFrame != nullptr);
mMainFrame->Clear();
}
ConsoleFrame* ConsoleRenderer::AddFrame(int positionX, int positionY, int width, int height)
{
ConsoleFrame* output = new ConsoleFrame(positionX, positionY, width, height);
mFrames.push_back(output);
return output;
}
ConsoleFrame * ConsoleRenderer::GetMainFrame()
{
return mMainFrame;
}
float ConsoleRenderer::GetFrameRate() const
{
return mFrameRate;
}
float ConsoleRenderer::GetFrameTime() const
{
return mFrameTime;
}
void ConsoleRenderer::RemoveFrame(ConsoleFrame* frame)
{
if(frame == nullptr)
return;
auto it = std::find(mFrames.begin(), mFrames.end(), frame);
if (it != mFrames.end())
{
delete *it;
mFrames.erase(it);
}
}
void ConsoleRenderer::SetBuffer(int row, int column, CHAR_INFO charInfo)
{
assert(0 <= row && row < mHeight);
assert(0 <= column && column < mActualWidth);
mBuffer[row * mActualWidth + column] = charInfo;
}
void ConsoleRenderer::RenderFrame(ConsoleFrame* frame)
{
for (int row = 0; row < frame->GetHeight(); ++row)
{
for (int column = 0; column < frame->GetWidth(); ++column)
{
int globalRow = frame->GetY() + row;
int globalColumn = (frame->GetX() + column) * CELL_WIDTH;
const Cell& cell = frame->GetCell(column, row);
if (LEFT_SPACE)
{
SetBuffer(globalRow, globalColumn, {L' ', cell.GetAttributes()});
++globalColumn;
}
SetBuffer(globalRow, globalColumn, cell.ToCharInfo());
++globalColumn;
if (RIGHT_SPACE)
{
SetBuffer(globalRow, globalColumn, {L' ', cell.GetAttributes()});
}
}
}
}
void ConsoleRenderer::Render()
{
assert(mMainFrame != nullptr);
RenderFrame(mMainFrame);
for (const auto& frame : mFrames)
{
RenderFrame(frame);
}
SMALL_RECT writeRegion = {0, 0, (SHORT)(mActualWidth - 1), (SHORT)(mHeight - 1)};
COORD bufferSize = {(SHORT)mActualWidth, (SHORT)mHeight};
COORD bufferCoord = {0, 0};
WriteConsoleOutputW(mConsoleHandle, mBuffer, bufferSize, bufferCoord, &writeRegion);
}