forked from naclander/openmugen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SffManager.cpp
394 lines (286 loc) · 10.2 KB
/
SffManager.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "global.h"
CSffManager::CSffManager()
{
m_pVideoSystem=0;
m_pAlloc=0;
xScaleValue=1.0;
yScaleValue=1.0;
}
CSffManager::~CSffManager()
{
}
//Set the pointers to the class
void CSffManager::SetPointers(CVideoSystem* p,CAllocater* a,CGameTimer *t,CAirManager *air)
{
m_pVideoSystem=p;
m_pAlloc=a;
m_pTimer=t;
m_pAirManger=air;
}
//reste the class variables
void CSffManager::ResetManager()
{
bPalletLoaded=false;
lpSffFile=0;
nTotalImages=0;
nCurrImage=0;
nImageListSize=100;
lpSpriteList=(SFFSPRITE*)m_pAlloc->Alloc(sizeof(SFFSPRITE)*nImageListSize);
//NORMAL BLIT
nFlags=BLT_NORMAL;
}
//load and deccode the SFF sprite and store it in a array
bool CSffManager::LoadSffFile(const char *strSffFile)
{
SFFHEADER header;
PrintMessage("CSffManager::Loading %s",strSffFile);
lpSffFile=fopen(strSffFile,"rb");
if(lpSffFile==0)
{
PrintMessage("CSffManager::Can't find %s",strSffFile);
return false;
}
//read the header of the file
fread(&header,sizeof(header),1,lpSffFile);
//check file signatur
if(strcmp((char*)header.signature,"ElecbyteSpr")!=0)
{
PrintMessage("CSffManager::%s is not an SFF file",strSffFile);
return false;
}
PrintMessage("CSffManager::Sff file include %i images",header.NumberOfImage);
//move the file to the first subheader
fseek(lpSffFile,header.SubHeaderFileOffset,SEEK_SET);
DecodeSffFile();
fclose(lpSffFile);
//save memory
lpSpriteList=(SFFSPRITE*)m_pAlloc->Realloc(lpSpriteList,sizeof(SFFSPRITE)*nTotalImages);
return true;
}
//Load an Colorpallet from file
bool CSffManager::LoadActToSff(const char *strActFile)
{
bPalletLoaded=true;
FILE *Act=fopen(strActFile,"rb");
if(Act==NULL)
PrintMessage("CSffSpriteManager %s File not found",strActFile);
for(int j=255;j>=0;j--)
{
int r,g,b;
r=fgetc(Act);
g=fgetc(Act);
b=fgetc(Act);
ColorPallet[j]=m_pVideoSystem->MapRGB(r,
g,
b) ;
}
fclose(Act);
return true;
}
//blt function
void CSffManager::BlitSprite(s16 nGroupNumber,s16 nImageNumber,s16 x, s16 y)
{
//search the Sprite int the list
int i=FindSprite(nGroupNumber,nImageNumber);
if(i==-1)
return;
//normal blit with with masked bits
if(nFlags==BLT_NORMAL)
m_pVideoSystem->NormalBlt(&lpSpriteList[i],x,y,false);
if(nFlags== BLT_NORMALMASKED)
m_pVideoSystem->NormalBlt(&lpSpriteList[i],x,y,true);
if(nFlags== BLT_FLIPH)
m_pVideoSystem->NormalFlipH(&lpSpriteList[i],x,y,false);
if(nFlags== BLT_FLIPHMASKED)
m_pVideoSystem->NormalFlipH(&lpSpriteList[i],x,y,true);
}
//prepares the animation
void CSffManager::PrepareAnim(s32 nAnim)
{
Anim=m_pAirManger->GetAction(nAnim);
Anim->nDuringTime=m_pTimer->GetGameTime()+Anim->AnimationElement[0].nDuringTime;
Anim->nCurrentImage=0;
Anim->bLooped=false;
Anim->nCurrentImageTime=0;
Anim->nCurrTime=0;
}
//Blit the given animation
void CSffManager::BlitAnim(s16 x, s16 y)
{
BlitSprite(Anim->AnimationElement[Anim->nCurrentImage].nGroupNumber,
Anim->AnimationElement[Anim->nCurrentImage].nImageNumber,
x,y);
//is the current image in time? && do not check if during time of the current image is -1
if(Anim->nDuringTime <= m_pTimer->GetGameTime() && Anim->AnimationElement[Anim->nCurrentImage].nDuringTime!=-1)
{
Anim->nCurrentImage++;
if(Anim->nCurrentImage >= Anim->nNumberOfElements)
{
Anim->nCurrentImage=0;
Anim->nCurrTime=0;
//if we have a loop start go to it
if(Anim->loopStart!=-1)
Anim->nCurrentImage=Anim->loopStart;
}
//calculate the new during time
Anim->nDuringTime=m_pTimer->GetGameTime()+Anim->AnimationElement[Anim->nCurrentImage].nDuringTime;
}
Anim->nCurrTime++;
}
//decodes one PCX file
u8* CSffManager::DecodePcx(u8* PcxByte,PCXHEADER header)
{
u8* byImageBuffer=0;
u8 BPP,byData;
u16 size;
s16 x,y,widht;
u32 Pos=0;
u32 nCountByte,nTotalyByte,
nHowManyBytes,nHowManyBlank;
nTotalyByte=header.bytesPerLine*header.NPlanes;
nHowManyBytes=0;
nHowManyBlank=0;
nCountByte=0;
BPP=header.NPlanes*8;
//allocate memory
byImageBuffer=(u8*)m_pAlloc->Alloc(sizeof(u8)*(header.widht*header.NPlanes*header.height+1));
widht=header.widht;
if(widht<header.bytesPerLine * header.NPlanes)
widht=header.bytesPerLine*header.NPlanes;
//we do not support 24bit pcx images
if(BPP>8)
{
PrintMessage("24Bit pcx file is not supproted");
return byImageBuffer;
}
size=0;
y=0;
x=0;
for(y=0;y<header.height;y++)
{
x=0;
while(x < widht)
{
byData=PcxByte[Pos++];
if( (byData & 0xC0) == 0xC0 )
{
size = byData & 0x3F;
byData=PcxByte[Pos++];
}
else
{
size=1;
}
while(size-- > 0)
{
if(x <= header.widht)
{
byImageBuffer[x + (y * header.widht)]=byData;
}
//this it to Skip blank data on PCX image wich are on the right side
//TODO:OK? Skip two bytes
if(x == widht && widht != header.widht)
{
nHowManyBlank=widht-header.widht;
for(u32 i=0;i<nHowManyBlank;i++)
Pos+=2;
}
x++;
}
}
}
return byImageBuffer;
}
void CSffManager::DecodeSffFile()
{
SFFSUBHEADER subheader;
u8 *tempByte;
//read the first subheader
fread(&subheader,sizeof(subheader),1,lpSffFile);
while(!feof(lpSffFile))
{
//when we need more a bigger array to store the information that get an bigger array
if(nTotalImages > nImageListSize-1)
{
nImageListSize+=100;
lpSpriteList=(SFFSPRITE*)m_pAlloc->Realloc(lpSpriteList,sizeof(SFFSPRITE)*nImageListSize);
}
//copy the information contained in the subheader
lpSpriteList[nTotalImages].GroupNumber=subheader.GroubNumber;
lpSpriteList[nTotalImages].ImageNumber=subheader.ImageNumber;
lpSpriteList[nTotalImages].x=subheader.x;
lpSpriteList[nTotalImages].y=subheader.y;
//is it a linked sprite??
if(subheader.LenghtOfSubheader!=0)
{
//read the pcx header
fread(&lpSpriteList[nTotalImages].PcxHeader,sizeof(PCXHEADER),1,lpSffFile);
//Correct the Image dimension
lpSpriteList[nTotalImages].PcxHeader.widht=lpSpriteList[nTotalImages].PcxHeader.widht - lpSpriteList[nTotalImages].PcxHeader.x + 1;
lpSpriteList[nTotalImages].PcxHeader.height=lpSpriteList[nTotalImages].PcxHeader.height - lpSpriteList[nTotalImages].PcxHeader.y +1;
//now read the pcx data
//TODO:check subheader.LenghtOfSubheader
tempByte=(u8*)m_pAlloc->Alloc(sizeof(u8)*(subheader.LenghtOfSubheader-127));
fread(tempByte,sizeof(u8)*(subheader.LenghtOfSubheader-127),1,lpSffFile);
lpSpriteList[nTotalImages].byPcxFile=DecodePcx(tempByte,
lpSpriteList[nTotalImages].PcxHeader);
//free memormy
m_pAlloc->Free(tempByte);
//move the file pointer to Color pallet of the PCX file
fseek(lpSffFile,-768L,SEEK_CUR);
//eat empty 8bit
fgetc(lpSffFile);
if(fgetc(lpSffFile)==12 && !subheader.PalletSame &&
!bPalletLoaded && lpSpriteList[nTotalImages].PcxHeader.NPlanes<=1)
{
for(int j=0;j<256;j++)
{
int r,g,b;
r=fgetc(lpSffFile);
g=fgetc(lpSffFile);
b=fgetc(lpSffFile);
ColorPallet[j]=m_pVideoSystem->MapRGB(r,
g,
b );
}
}
//copy the color pallet to the Sffsprite Struct
memcpy(&lpSpriteList[nTotalImages].ColorPallet,
&ColorPallet,
sizeof(u16)*256);
}
else
{
//subheader.IndexOfPrevious-1 if the first ellement is 1 and not 0
// we have a linked image here
memcpy(&lpSpriteList[nTotalImages],
&lpSpriteList[subheader.IndexOfPrevious-1],
sizeof(SFFSPRITE));
lpSpriteList[nTotalImages].GroupNumber=subheader.GroubNumber;
lpSpriteList[nTotalImages].ImageNumber=subheader.ImageNumber;
}
nTotalImages++;
fseek(lpSffFile,subheader.NextSubheaderFileOffset,SEEK_SET);
//read the next subheader
fread(&subheader,sizeof(subheader),1,lpSffFile);
}
PrintMessage("CSffManager:: %i image are decoded",nTotalImages);
}
int CSffManager::FindSprite(s16 nGroupNumber,s16 nImageNumber)
{
for(int i=0;i<nImageListSize;i++)
{
if(lpSpriteList[i].GroupNumber == nGroupNumber &&
lpSpriteList[i].ImageNumber == nImageNumber)
{
return i;
}
}
//no image found
return -1;
}
void CSffManager::FreeManager()
{
m_pAlloc->Free(lpSpriteList);
lpSpriteList=0;
}