-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTexture.cpp
79 lines (53 loc) · 1.94 KB
/
Texture.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
#include "Texture.hpp"
#include <fstream>
#include <math.h>
namespace Game {
bool Texture::load(const char* path) {
std::ifstream f(path);
if (!f.is_open()) {
return false;
}
bounds = {0, 0, 0, 0};
std::string currentLine;
while (std::getline(f, currentLine)) {
data.push_back(currentLine);
if (currentLine.length() > bounds.w) {
bounds.w = currentLine.length();
}
bounds.h++;
}
f.close();
return true;
}
void Texture::render(Display& d, int x, int y, Rect* src) {
if (src == NULL) {
src = &bounds; // use default bounds
}
int startY = std::max(0, y);
int endY = std::min<int>(y + src->h, d.height);
int startX = std::max(0, x);
for (int r = startY; r < endY; r++) {
int rowPos = r-y+src->y;
int rowWidth = std::min<int>(src->w, data[rowPos].size());
int endX = std::min<int>(x + rowWidth, d.width);
for (int c = startX; c < endX; c++) {
char currentChar = data[rowPos][c-x+src->x];
if (currentChar != background) {
CHAR_INFO* ci = &d.display[r*d.width + c];
WORD currentColor = color;
if (transparency != TRANSPARENT_NO) {
if (transparency == TRANSPARENT_BG) {
currentColor &= 0x0F; // clear bg bits
currentColor |= ci->Attributes & 0xF0;
} else {
currentColor &= 0xF0; // clear fg bits
currentColor |= (ci->Attributes >> 4) & 0x0F;
}
}
ci->Char.AsciiChar = currentChar;
ci->Attributes = currentColor;
}
}
}
}
}