-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvasDef.hpp
136 lines (112 loc) · 2.99 KB
/
CanvasDef.hpp
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
#pragma once
#include <SDL2/SDL.h>
#include <algorithm>
#include "Bitmap.hpp"
#include "canvas8.hpp"
struct SDL_Texture;
struct color_t {
byte r = 0, g = 0, b = 0, a = 255;
color_t() {}
color_t(byte __r, byte __g, byte __b, byte __a)
: r(__r), g(__g), b(__b), a(__a)
{}
~color_t() {}
uint32_t as_uint32() const
{
return *reinterpret_cast<uint32_t*>(const_cast<color_t*>(this));
}
};
class Palette {
public:
Palette(const std::array<color_t, 256>& colors)
: _colors(colors)
, _initColors(colors)
{
swapRedBlue();
}
~Palette()
{
}
void setColor(byte index, byte r, byte g, byte b, byte a)
{
color_t& c = _colors[index];
c.r = r;
c.g = g;
c.b = b;
c.a = a;
}
const color_t& getColor(int index) const
{
return _colors.at(index);
}
uint32_t getColorUINT32(byte index) const
{
return (_colors[index].as_uint32());
}
private:
void swapRedBlue(); // necessary because of SDL
std::array<color_t, 256> _colors, _initColors;
};
class Canvas;
/** Canvas is the game's internal representation of a virtual 8-bit display.
This is because SDL2 was giving me a bunch of shit about not allowing
palettized textures, even though it's in the SDL2 API. */
class Canvas {
public:
Canvas(Palette& palette, int width, int height)
: _width(width)
, _height(height)
, _palette(palette)
{
if (!((width > 0) && (height > 0)))
throw std::bad_alloc(); // bad_array_new_length();
_pixels = new byte[width * height];
if (!_pixels)
throw std::bad_alloc();
_destPixels32 = new uint32_t[width * height];
if (!_destPixels32)
throw std::bad_alloc();
memset(_pixels, 0, width * height * sizeof(_pixels[0]));
memset(_destPixels32, 0, width * height * sizeof(_destPixels32[0]));
}
~Canvas()
{
delete[] _destPixels32;
delete[] _pixels;
}
void setPixel(int x, int y, byte color)
{
_pixels[(y * width()) + x] = color;
}
void clear(byte color = 1)
{
memset(_pixels, color, width() * height() * sizeof(_pixels[0]));
}
byte getPixel(int x, int y) const
{
return _pixels[(y * width()) + x];
}
void updateSDLTexture(SDL_Texture* sdlTexture) const;
void blitBitmap(const Bitmap& source, int destx, int desty, bool transparent = true)
{
if (transparent)
blitBitmapMasked(source, destx, desty);
else
blitBitmapNonmasked(source, destx, desty);
}
int width() const
{
return _width;
}
int height() const
{
return _height;
}
private:
void blitBitmapMasked(const Bitmap& source, int destx, int desty);
void blitBitmapNonmasked(const Bitmap& source, int destx, int desty);
byte* _pixels;
uint32_t* _destPixels32;
int _width, _height;
Palette _palette;
};