-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile.cpp
62 lines (49 loc) · 1.32 KB
/
tile.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
#include "tile.h"
#include "common.h"
namespace libsonassmd {
void Tile::fromBinaryStream(std::istream &stream)
{
stream.clear();
const auto original_exceptions = stream.exceptions();
stream.exceptions(stream.badbit | stream.eofbit | stream.failbit);
for (unsigned int y = 0; y < height; ++y)
{
for (unsigned int x = 0; x < width; x += 2)
{
const unsigned char byte = ReadU8(stream);
pixels[y][x + 0] = byte >> 4;
pixels[y][x + 1] = byte & 0xF;
}
}
stream.clear();
stream.exceptions(original_exceptions);
}
void Tile::toAssemblyStream(std::ostream &stream) const
{
stream << "\tdc.b ";
for (unsigned int y = 0; y < height; ++y)
{
for (unsigned int x = 0; x < width; x += 2)
{
if (x != 0 || y != 0)
stream << ',';
stream << '$';
static const char nibble_to_char[0x10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
stream << nibble_to_char[pixels[y][x + 0]];
stream << nibble_to_char[pixels[y][x + 1]];
}
}
stream << '\n';
}
void Tile::toBinaryStream(std::ostream &stream) const
{
for (unsigned int y = 0; y < height; ++y)
{
for (unsigned int x = 0; x < width; x += 2)
{
const unsigned char byte = (pixels[y][x + 0] << 4) | (pixels[y][x + 1] << 0);
stream << byte;
}
}
}
}