-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.h
204 lines (141 loc) · 4.5 KB
/
Sprite.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* @file Sprite.h
* @brief Sprites for the NCursesGameEngine
* @author KrizTioaN ([email protected])
* @date 2021-08-01
* @note BSD-3 licensed
*
***********************************************/
#ifndef CBGLGFX_SPRITE_H
#define CBGLGFX_SPRITE_H
#include <fstream>
#include <streambuf>
#include <string>
#include <utility>
class extbuf : public std::streambuf {
public:
extbuf(const char *d, size_t s) : crt(d), end(d + s) {}
int_type underflow() {
return crt == end ? traits_type::eof() : traits_type::to_int_type(*crt);
}
int_type uflow() {
return crt == end ? traits_type::eof() : traits_type::to_int_type(*crt++);
}
std::streamsize showmanyc() { return end - crt; }
private:
const char *crt, *end;
};
extern "C" {
#include <zlib.h>
}
namespace cb {
class Sprite;
typedef struct {
wchar_t character;
short color;
} Pixel;
}; // namespace cb
class cb::Sprite {
public:
Sprite() {
nSpriteWidth = 0;
nSpriteHeight = 0;
pPixels = nullptr;
}
[[maybe_unused]] bool Create(int nWidth, int nHeight) {
nSpriteWidth = nWidth;
nSpriteHeight = nHeight;
if (nSpriteWidth * nSpriteHeight <= 0)
return false;
delete pPixels;
pPixels = new cb::Pixel[nSpriteWidth * nSpriteHeight];
std::fill_n(pPixels, nSpriteWidth * nSpriteHeight, (cb::Pixel){L' ', 0});
return true;
}
[[maybe_unused]] bool Read(const std::wstring &filename) {
std::ifstream ifstr(filename, std::ios::in | std::ios::binary);
if (!ifstr.fail())
return ReadFromStream(ifstr);
return false;
}
[[maybe_unused]] bool Load(const char *d, std::streamsize s) {
extbuf buf(d, s);
std::istream in(&buf);
return ReadFromStream(in);
}
[[maybe_unused]] bool z_Load(const char *z, std::streamsize s_z, uLongf s) {
char *d = new char[s];
uncompress(reinterpret_cast<Bytef *>(d), &s,
reinterpret_cast<const Bytef *>(z), s_z);
extbuf buf(d, s);
std::istream in(&buf);
bool ok = ReadFromStream(in);
delete[] d;
return ok;
}
[[maybe_unused]] bool ReadolcSprite(const std::wstring &filename) {
std::ifstream ifstr;
ifstr.open(filename, std::ios::in | std::ios::binary);
if (ifstr.fail())
return false;
ifstr.read(reinterpret_cast<char *>(&nSpriteWidth), sizeof(int));
ifstr.read(reinterpret_cast<char *>(&nSpriteHeight), sizeof(int));
auto *colors = new short[nSpriteWidth * nSpriteHeight];
auto *glyphs = new short[nSpriteWidth * nSpriteHeight];
ifstr.read(reinterpret_cast<char *>(colors),
nSpriteWidth * nSpriteHeight * sizeof(short));
ifstr.read(reinterpret_cast<char *>(glyphs),
nSpriteWidth * nSpriteHeight * sizeof(short));
ifstr.close();
delete[] pPixels;
pPixels = new cb::Pixel[nSpriteWidth * nSpriteHeight];
for (int i = 0; i < nSpriteWidth * nSpriteHeight; i++) {
if (glyphs[i] == ' ') {
pPixels[i].color = 0;
pPixels[i].character = ' ';
} else {
pPixels[i].color = colors[i];
pPixels[i].character = glyphs[i]; // == ' ' ? ' ' : L'\u2588';
}
}
delete[] colors;
delete[] glyphs;
return true;
}
[[maybe_unused]] bool Write(const std::wstring &filename) {
if (nSpriteWidth * nSpriteHeight <= 0)
return false;
std::ofstream ofstr(filename, std::ios::binary);
if (ofstr.fail())
return false;
ofstr.write(reinterpret_cast<char *>(&nSpriteWidth), sizeof(int));
ofstr.write(reinterpret_cast<char *>(&nSpriteHeight), sizeof(int));
ofstr.write(reinterpret_cast<char *>(pPixels),
nSpriteWidth * nSpriteHeight * sizeof(cb::Pixel));
return true;
}
~Sprite() { delete pPixels; }
[[maybe_unused]] cb::Pixel &operator[](unsigned i) { return pPixels[i]; }
[[maybe_unused]] [[nodiscard]] inline int SpriteWidth() const {
return nSpriteWidth;
}
[[maybe_unused]] [[nodiscard]] inline int SpriteHeight() const {
return nSpriteHeight;
}
private:
int nSpriteWidth;
int nSpriteHeight;
cb::Pixel *pPixels;
bool ReadFromStream(std::istream &in) {
in.read(reinterpret_cast<char *>(&nSpriteWidth), sizeof(int));
in.read(reinterpret_cast<char *>(&nSpriteHeight), sizeof(int));
if (nSpriteWidth * nSpriteHeight <= 0)
return false;
delete pPixels;
pPixels = new cb::Pixel[nSpriteWidth * nSpriteHeight];
in.read(reinterpret_cast<char *>(pPixels),
nSpriteWidth * nSpriteHeight * sizeof(cb::Pixel));
return true;
}
};
#endif // CBGLGFX_SPRITE_H