-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRendererConsole.cpp
More file actions
75 lines (65 loc) · 1.83 KB
/
RendererConsole.cpp
File metadata and controls
75 lines (65 loc) · 1.83 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
#include "RendererConsole.hpp"
#include "rlutil.h"
#include <iostream>
RendererConsole::RendererConsole()
{
// Constructor
}
void RendererConsole::render(const LifeSimulator& simulation)
{
std::uint8_t sizeX = simulation.getSizeX();
std::uint8_t sizeY = simulation.getSizeY();
if (previousState.empty())
{
// First-time rendering
rlutil::cls();
previousState.resize(sizeY, std::vector<bool>(sizeX, false));
rlutil::hidecursor();
for (std::uint8_t y = 0; y < sizeY; ++y)
{
for (std::uint8_t x = 0; x < sizeX; ++x)
{
bool alive = simulation.getCell(x, y);
previousState[y][x] = alive;
rlutil::locate(x + 1, y + 1); // rlutil positions start at (1,1)
if (alive)
{
std::cout << 'O';
}
else
{
std::cout << ' ';
}
}
}
rlutil::showcursor();
std::cout << std::flush;
}
else
{
// Update only cells that have changed state
rlutil::hidecursor();
for (std::uint8_t y = 0; y < sizeY; ++y)
{
for (std::uint8_t x = 0; x < sizeX; ++x)
{
bool alive = simulation.getCell(x, y);
if (alive != previousState[y][x])
{
rlutil::locate(x + 1, y + 1);
if (alive)
{
std::cout << 'O';
}
else
{
std::cout << ' ';
}
previousState[y][x] = alive;
}
}
}
rlutil::showcursor();
std::cout << std::flush;
}
}