-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCG16bitSprite.cpp
320 lines (243 loc) · 5.51 KB
/
CG16bitSprite.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// CG16bitSprite.cpp
//
// Implementation of raw 16-bit sprite object
#include <windows.h>
#include "Alchemy.h"
#include "DirectXUtil.h"
CG16bitSprite::CG16bitSprite (void) :
m_pCode(NULL),
m_pLineIndex(NULL),
m_cxWidth(0),
m_cyHeight(0)
// CG16bitSprite constructor
{
}
CG16bitSprite::~CG16bitSprite (void)
// CG16bitSprite destructor
{
DeleteData();
}
void CG16bitSprite::ColorTransBlt (CG16bitImage &Dest, int xDest, int yDest, int xSrc, int ySrc, int cxWidth, int cyHeight)
// ColorTransBlt
//
// Blt the sprite
{
// Make sure we're in bounds
if (!Dest.AdjustCoords(&xSrc, &ySrc, m_cxWidth, m_cyHeight,
&xDest, &yDest,
&cxWidth, &cyHeight))
return;
// Blt each line
WORD *pDstRow = Dest.GetPixel(Dest.GetRowStart(yDest), xDest);
int iLine;
int iLineEnd = ySrc + cyHeight;
for (iLine = ySrc; iLine < iLineEnd; iLine++)
{
WORD *pCode = m_pLineIndex[iLine];
int cxOffset = xSrc;
int cxBlt = cxWidth;
WORD *pDstPos = pDstRow;
while (cxBlt > 0)
{
switch (*pCode)
{
case codeSkip:
{
pCode++;
int cxCount = (int)(DWORD)(*pCode);
pCode++;
// Deal with offset
if (cxOffset)
{
int cxConsume = Min(cxCount, cxOffset);
cxCount -= cxConsume;
cxOffset -= cxConsume;
}
ASSERT(cxCount == 0 || cxOffset == 0);
// Skip any remaining
if (cxCount > 0)
{
int cxConsume = Min(cxCount, cxBlt);
cxBlt -= cxConsume;
pDstPos += cxConsume;
}
break;
}
case codeRun:
{
pCode++;
int cxCount = (int)(DWORD)(*pCode);
pCode++;
// Deal with offset
if (cxOffset)
{
int cxConsume = Min(cxCount, cxOffset);
cxCount -= cxConsume;
cxOffset -= cxConsume;
pCode += cxConsume;
}
ASSERT(cxCount == 0 || cxOffset == 0);
// Blt any remaining
if (cxCount > 0)
{
int cxConsume = Min(cxCount, cxBlt);
WORD *pCodeEnd = pCode + cxConsume;
while (pCode < pCodeEnd)
*pDstPos++ = *pCode++;
cxBlt -= cxConsume;
ASSERT(cxBlt == 0 || cxConsume == cxCount);
}
break;
}
default:
ASSERT(false);
}
}
// Next row
pDstRow = Dest.NextRow(pDstRow);
}
}
ALERROR CG16bitSprite::CreateFromImage (const CG16bitImage &Source)
// CreateFromImage
//
// Creates the sprite from an image
{
ALERROR error;
int i;
// There are many images types that we don't handle
if (Source.HasAlpha()
|| Source.IsTransparent()
|| Source.GetWidth() <= 0
|| Source.GetHeight() <= 0)
{
ASSERT(false);
return ERR_FAIL;
}
// States
enum States
{
stateStart,
stateInSkip,
stateInRun,
};
// Initialize
int cxWidth = Source.GetWidth();
int cyHeight = Source.GetHeight();
WORD wBackColor = Source.GetBackColor();
// Allocate line index and a temp code block
CMemoryWriteStream Code;
if (error = Code.Create())
return ERR_FAIL;
int *pLineIndex = new int [cyHeight];
// Init state
int iState = stateStart;
int iLine = 0;
// Traverse the image
WORD *pSrcRow = Source.GetRowStart(0);
WORD *pSrcRowEnd = Source.GetRowStart(cyHeight);
while (pSrcRow < pSrcRowEnd)
{
WORD *pSrcPos = pSrcRow;
WORD *pSrcPosEnd = pSrcRow + cxWidth;
WORD *pStart;
pLineIndex[iLine++] = (Code.GetLength() / sizeof(WORD));
while (pSrcPos < pSrcPosEnd)
{
switch (iState)
{
case stateStart:
{
if (*pSrcPos == wBackColor)
{
pStart = pSrcPos;
iState = stateInSkip;
}
else
{
pStart = pSrcPos;
iState = stateInRun;
}
break;
}
case stateInSkip:
{
if (*pSrcPos != wBackColor)
{
WORD wCode = codeSkip;
Code.Write((char *)&wCode, sizeof(WORD));
WORD wCount = (WORD)(DWORD)(pSrcPos - pStart);
Code.Write((char *)&wCount, sizeof(WORD));
pStart = pSrcPos;
iState = stateInRun;
}
break;
}
case stateInRun:
{
if (*pSrcPos == wBackColor)
{
WORD wCode = codeRun;
Code.Write((char *)&wCode, sizeof(WORD));
WORD wCount = (WORD)(DWORD)(pSrcPos - pStart);
Code.Write((char *)&wCount, sizeof(WORD));
Code.Write((char *)pStart, wCount * sizeof(WORD));
pStart = pSrcPos;
iState = stateInSkip;
}
break;
}
}
pSrcPos++;
}
// Done with row, so end code
if (iState == stateInSkip)
{
WORD wCode = codeSkip;
Code.Write((char *)&wCode, sizeof(WORD));
WORD wCount = (WORD)(DWORD)(pSrcPos - pStart);
Code.Write((char *)&wCount, sizeof(WORD));
}
else if (iState == stateInRun)
{
WORD wCode = codeRun;
Code.Write((char *)&wCode, sizeof(WORD));
WORD wCount = (WORD)(DWORD)(pSrcPos - pStart);
Code.Write((char *)&wCount, sizeof(WORD));
Code.Write((char *)pStart, wCount * sizeof(WORD));
}
iState = stateStart;
// Next
pSrcRow = Source.NextRow(pSrcRow);
}
// Convert to compact format
DeleteData();
int iCodeSize = Code.GetLength() / sizeof(WORD);
m_pCode = new WORD [iCodeSize];
utlMemCopy((char *)Code.GetPointer(), (char *)m_pCode, iCodeSize * sizeof(WORD));
m_pLineIndex = new WORD *[cyHeight];
for (i = 0; i < cyHeight; i++)
m_pLineIndex[i] = m_pCode + pLineIndex[i];
m_cxWidth = cxWidth;
m_cyHeight = cyHeight;
// Done
delete [] pLineIndex;
return NOERROR;
}
void CG16bitSprite::DeleteData (void)
// DeleteData
//
// Delete all data
{
if (m_pCode)
{
delete m_pCode;
m_pCode = NULL;
}
if (m_pLineIndex)
{
delete m_pLineIndex;
m_pLineIndex = NULL;
}
m_cxWidth = 0;
m_cyHeight = 0;
}