-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScreen.cpp
More file actions
154 lines (120 loc) · 3.14 KB
/
Screen.cpp
File metadata and controls
154 lines (120 loc) · 3.14 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
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
#include <chrono>
#include <iostream>
#include <thread>
#include "Screen.h"
// for Windows keyboard inputs - program doesn't detect key inputs on other platforms
#ifdef _WIN32
#include <Windows.h>
#else
short GetKeyState(char k) {
return 0;
}
#endif // _WIN32
Screen::Screen(Pixel size, int fps) {
this->size = size;
this->fps = fps;
// store initial toggle state of keys
for (int c = 0; c < 256; c++) {
toggleState[c] = (GetKeyState(c) & 0x0001) != 0;
}
}
Screen::~Screen() = default;
Pixel Screen::getSize() {
return size;
}
void Screen::display() {
std::string output = "";
// pad with newlines
for (int i = 0; i < 20; i++) {
output += '\n';
}
// top border
output += "╔";
for (int col = 0; col < size.x; col++) {
output += "═";
}
output += "╗\n";
// rows of screen
for (int row = 0; row < size.y; row++) {
output += "║";
for (int col = 0; col < size.x; col++) {
output += charAtPixel(Pixel(col, row));
}
output += "║\n";
}
// bottom border
output += "╚";
for (int col = 0; col < size.x; col++) {
output += "═";
}
output += "╝\n";
std::cout << output;
}
char Screen::charAtPixel(Pixel pixel) {
// check for objects in the list at this position.
// objects that are later in the list are "on top"
char character = ' ';
for (Object *object : objectsAtPixel(pixel)) {
for (auto pair : object->shape()) {
if (object->position + pair.first == pixel) {
character = pair.second;
}
}
}
return character;
}
void Screen::mainLoop() {
mainLoop(-1);
}
void Screen::tick() {
for (int c = 0; c < 256; c++) {
keys[c] = false;
// if toggle state has changed, then key was pressed since the last frame
bool newToggleState = (GetKeyState(c) & 0x0001) != 0;
if (newToggleState != toggleState[c]) {
toggleState[c] = newToggleState;
keys[c] = true;
}
}
for (int i = 0; i < objects.size(); i++) {
objects[i]->update();
}
display();
std::this_thread::sleep_for(std::chrono::milliseconds(1000 / fps));
}
void Screen::mainLoop(int numFrames) {
while (numFrames-- != 0) {
tick();
}
}
std::vector<Object *> Screen::getObjects() {
return objects;
}
Object *Screen::addObject(Object *object) {
objects.push_back(object);
return object;
}
void Screen::removeObject(Object *object) {
// fancy
for (auto i = objects.begin(); i < objects.end(); i++) {
if (*i == object) {
objects.erase(i);
return;
}
}
}
bool Screen::key(char key) {
return keys[key];
}
std::vector<Object *> Screen::objectsAtPixel(Pixel pixel) {
std::vector<Object *> objectsAtPixel;
for (Object *object : objects) {
for (auto pair : object->shape()) {
if (object->position + pair.first == pixel) {
objectsAtPixel.push_back(object);
break;
}
}
}
return objectsAtPixel;
}