-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathScreenByte.h
83 lines (66 loc) · 1.91 KB
/
ScreenByte.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
#pragma once
#include <array>
#include "Colour.h"
class ScreenByte
{
public:
ScreenByte(uint32_t mode) : m_byte(0),m_offset(0),m_mode(mode) {}
ScreenByte() = delete;
~ScreenByte() {}
static constexpr std::array<uint8_t, 2> twoColourModeValues = { 0, 1 };
static constexpr std::array<uint8_t, 4> fourColourModeValues = { 0b00000, 0b00001, 0b10000, 0b10001 };
static constexpr std::array<uint8_t, 16> sixteenColourModeValues = { 0b00000000, 0b00000001, 0b00000100, 0b00000101, 0b00010000, 0b00010001, 0b00010100, 0b00010101,
0b01000000, 0b01000001, 0b01000100, 0b01000101, 0b01010000, 0b01010001, 0b01010100, 0b01010101 };
bool addPixel(uint8_t pixelValue)
{
if (pixelValue > Colour::getNumberOfColoursForMode(m_mode))
{
throw std::runtime_error("Pixel value too high for this mode");
}
switch (m_mode)
{
case 0:
case 4:
m_byte |= twoColourModeValues[pixelValue];
break;
case 1:
case 5:
m_byte |= fourColourModeValues[pixelValue];
break;
case 2:
m_byte |= sixteenColourModeValues[pixelValue];
break;
default:
throw std::runtime_error("Bad mode");
}
if (++m_offset == getPixelsPerByte())
{
m_offset = 0;
return true;
}
else
{
m_byte <<= 1;
return false;
}
}
bool isEmpty() const
{
return (m_offset==0);
}
uint8_t readByte()
{
uint8_t r = m_byte;
m_byte = 0;
m_offset = 0;
return r;
}
uint32_t getPixelsPerByte() const
{
return Colour::getPixelsPerByteForMode(m_mode);
}
private:
uint8_t m_byte;
uint32_t m_offset;
uint32_t m_mode;
};