-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubLayer.h
79 lines (56 loc) · 2.12 KB
/
SubLayer.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
#pragma once
/*
TODO: Should probably make Layer & SubLayer inherit from a generic class
*/
class SubLayer {
// The power of two to use for the data cell size (0 = 1 byte).
unsigned cellShift;
// Size of a single cell in bytes.
char cellSize;
// The value used for empty/new tiles.
unsigned defaultValue;
// Raw data (may be reinterpreted)
unsigned char * data;
// Tile count, should always be the same as the parent's
unsigned int width;
unsigned int height;
public:
SubLayer() : data(0), cellShift(0), cellSize(1), defaultValue(0) {}
SubLayer(unsigned cellSize, unsigned defaultValue);
SubLayer(const SubLayer & src);
~SubLayer() { delete[] data; }
char getCellSize() const { return cellSize; }
unsigned getDefaultValue() const { return defaultValue; }
// Check if a layer is usable
bool isValid() const { return width > 0 && height > 0 && data != 0; }
// Check if a coordinate is valid
bool isValid(unsigned int x, unsigned int y) const
{
return isValid() && x < width && y < height;
}
// Get a tile within the layer array
__forceinline unsigned char * getCell(unsigned x, unsigned int y) const
{
return data + ((x + width * y) << cellShift);
}
template <class T>
__forceinline void getCellSafe(unsigned x, unsigned y, T * target) const
{
unsigned maxSize = min(sizeof(*target), cellSize);
memcpy(target, getCell(x, y), maxSize);
}
template <class T> __forceinline T getCellAs(unsigned x, unsigned y)
{
return *reinterpret_cast<T *>(getCell(x, y));
}
__forceinline void setCell(unsigned x, unsigned y, unsigned value)
{
memcpy(getCell(x, y), &value, cellSize);
}
unsigned int getWidth() const { return width; }
unsigned int getHeight() const { return height; }
unsigned char * getDataPointer() { return data; }
unsigned int getByteSize() const { return (width * height) << cellShift; }
// Ensure that the sub-layer conforms with its parent's requirements
void resize(unsigned int newWidth, unsigned int newHeight);
};