-
Notifications
You must be signed in to change notification settings - Fork 0
/
CBuffer.h
155 lines (134 loc) · 2.98 KB
/
CBuffer.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
#ifndef CBUFFER_H
#define CBUFFER_H
#include <iostream>
#include "CRect.h"
#include <string.h>
template <class T>
class CBuffer
{
public:
CBuffer(int width, int heigth) : m_width(width), m_heigth(heigth)
{
m_stride = m_width;
m_isSelfCreated = true;
if (width > 0 && heigth > 0)
m_itsBuffer = new T[width * heigth];
else
m_itsBuffer = NULL;
}
~CBuffer()
{
if (m_isSelfCreated && m_itsBuffer != NULL)
delete[] m_itsBuffer;
}
//copy const. does a shallow copy
CBuffer(const CBuffer<T> &rhs)
{
m_isSelfCreated = false;
m_heigth = rhs.m_heigth;
m_width = rhs.m_width;
m_stride = rhs.m_stride;
m_itsBuffer = rhs.m_itsBuffer;
}
CBuffer(const CBuffer<T> &rhs, CRect<int> borders)
{
m_isSelfCreated = false;
m_heigth = borders.getHeigth();
m_width = borders.getWidth();
m_stride = rhs.m_stride;
m_itsBuffer = rhs.m_itsBuffer +
m_stride * borders.getTop() + borders.getLeft();
}
const CBuffer<T> operator=(const CBuffer<T> rhs)
{
if (m_isSelfCreated && m_itsBuffer != NULL)
delete[] m_itsBuffer;
m_itsBuffer = rhs.m_itsBuffer;
m_stride = rhs.m_stride;
m_width = rhs.m_width;
m_heigth = rhs.m_heigth;
m_isSelfCreated = false;
return *this;
}
T *getBufferPointer()
{
return m_itsBuffer;
}
void Fill(const T fillThis)
{
T *it = m_itsBuffer;
int strideDiff = m_stride - m_width;
for (int y = 0; y < m_heigth; ++y)
{
for (int x = 0; x < m_width; ++x)
{
*it = fillThis;
++it;
}
it += strideDiff;
}
}
//fill entire buffer with a single byte
void FillWithByte(const char filler){
if(m_stride == m_width){
memset(m_itsBuffer, filler, m_width*m_heigth*sizeof(T));
return;
}
char* lineStart = (char*)m_itsBuffer;
const int length = m_width * sizeof(T);
const int skipLength = m_stride * sizeof(T);
for(int i = 0; i < m_heigth; ++i){
memset(lineStart, filler, length);
lineStart += skipLength;
}
}
T &getElmRef(int x, int y)
{
T &retVal = m_itsBuffer[y * m_stride + x];
return retVal;
}
int getWidth()
{
return m_width;
}
int getHeigth()
{
return m_heigth;
}
int getStride()
{
return m_stride;
}
CPoint<int> getSize()
{
return CPoint<int>(m_width, m_heigth);
}
CRect<int> getAreaRect()
{
return CRect<int>(0, 0, m_heigth - 1, m_width - 1);
}
void CopyFrom(CBuffer<T> &source)
{
int copyWidth = std::min(m_width, source.m_width);
int copyHeigth = std::min(m_heigth, source.m_heigth);
T *pdest = m_itsBuffer;
T *psource = source.m_itsBuffer;
int destShifter = m_stride;
int srcShifter = source.m_stride;
int copySize = sizeof(T)*copyWidth;
for (int i = 0; i < copyHeigth; ++i)
{
memcpy(pdest, psource, copySize);
pdest += destShifter;
psource += srcShifter;
}
}
private:
CBuffer(); //disallow defc
T *m_itsBuffer;
int m_width;
int m_heigth;
int m_stride;
bool m_isSelfCreated;
};
#endif