-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDisplay.cpp
141 lines (108 loc) · 3.6 KB
/
Display.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
#include "Display.hpp"
#include <memory.h>
#include "Color.hpp"
namespace Game {
Display::Display(HANDLE window, HANDLE input)
: window(window), input(input)
{
SetConsoleMode(input, ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
// get the console size in characters
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(window, &csbi)) {
width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
writeArea = {0, 0, width, height};
// create the display
display = (CHAR_INFO*)malloc(sizeof(CHAR_INFO) * width * height);
} else {
// TODO:: error
}
}
void Display::render() {
WriteConsoleOutput(window, display, {width, height}, {0, 0}, &writeArea);
}
void Display::clear(char c) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
display[y*width + x].Char.AsciiChar = c;
display[y*width + x].Attributes = color;
}
}
}
void Display::setChar(char c, unsigned int x, unsigned int y) {
if (x < width && y < height) {
display[y*width + x].Char.AsciiChar = c;
display[y*width + x].Attributes = getDrawColor(x, y);
}
}
void Display::writeString(const char* s, int x, int y) {
if (x >= width || y >= height || y < 0) {
return;
}
int pos = 0;
// pass over any characters that are outside of the window
while (x + pos < 0) {
if (s[pos] != '\0') {
pos++;
} else {
return;
}
}
while (s[pos] != '\0') {
setChar(s[pos], x + pos, y);
pos++;
}
}
void Display::drawLine(char c, int x1, int y1, int x2, int y2) {
int dx = abs(x2-x1), sx = x1<x2 ? 1 : -1;
int dy = abs(y2-y1), sy = y1<y2 ? 1 : -1;
int err = (dx>dy ? dx : -dy)/2, e2;
for(;;){
setChar(c, x1, y1);
if (x1 == x2 && y1 == y2) {
break;
}
e2 = err;
if (e2 >-dx) { err -= dy; x1 += sx; }
if (e2 < dy) { err += dx; y1 += sy; }
}
}
WORD Display::getDrawColor(unsigned int x, unsigned int y) {
CHAR_INFO* ci = &display[y*width + x];
WORD drawColor = color;
if (transparency != TRANSPARENT_NO) {
if (transparency == TRANSPARENT_BG) {
drawColor &= 0x0F; // clear bg bits
drawColor |= ci->Attributes & 0xF0;
} else {
drawColor &= 0xF0; // clear fg bits
drawColor |= (ci->Attributes >> 4) & 0x0F;
}
}
return drawColor;
}
bool Display::pollEvent(INPUT_RECORD* event) {
if (currentInput < inputCount) {
*event = inputBuff[currentInput++];
return true;
}
unsigned long eventCount;
GetNumberOfConsoleInputEvents(input, &eventCount);
if (eventCount == 0) {
return false;
}
currentInput = 0;
// read the next 128 events
if (!ReadConsoleInput(
input, // input buffer handle
inputBuff, // buffer to read into
128, // size of read buffer
&inputCount)) // number of records read
{
inputCount = 0;
return false;
}
*event = inputBuff[currentInput++];
return true;
}
}