factored out the EFFv2 saving into EFFImporter
[gemrb.git] / gemrb / core / AnimationFactory.cpp
blobe6637394c460952c32170dde74d79531b4ace834
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "AnimationFactory.h"
23 #include "win32def.h"
25 #include "Interface.h"
26 #include "Video.h"
28 AnimationFactory::AnimationFactory(const char* ResRef)
29 : FactoryObject( ResRef, IE_BAM_CLASS_ID )
31 FLTable = NULL;
32 FrameData = NULL;
33 datarefcount = 0;
36 AnimationFactory::~AnimationFactory(void)
38 for (unsigned int i = 0; i < frames.size(); i++) {
39 core->GetVideoDriver()->FreeSprite( frames[i] );
41 if (FLTable)
42 free( FLTable);
44 // FIXME: track down where sprites are being leaked
45 if (datarefcount) {
46 fprintf(stderr, "AnimationFactory %s has refcount %d\n", ResRef, datarefcount);
47 //assert(datarefcount == 0);
49 if (FrameData)
50 free( FrameData);
53 void AnimationFactory::AddFrame(Sprite2D* frame)
55 frames.push_back( frame );
58 void AnimationFactory::AddCycle(CycleEntry cycle)
60 cycles.push_back( cycle );
63 void AnimationFactory::LoadFLT(unsigned short* buffer, int count)
65 if (FLTable) {
66 free( FLTable );
68 //FLTable = new unsigned short[count];
69 FLTable = (unsigned short *) malloc(count * sizeof( unsigned short ) );
70 memcpy( FLTable, buffer, count * sizeof( unsigned short ) );
73 void AnimationFactory::SetFrameData(unsigned char* FrameData)
75 this->FrameData = FrameData;
79 Animation* AnimationFactory::GetCycle(unsigned char cycle)
81 if (cycle >= cycles.size()) {
82 return NULL;
84 int ff = cycles[cycle].FirstFrame;
85 int lf = ff + cycles[cycle].FramesCount;
86 Animation* anim = new Animation( cycles[cycle].FramesCount );
87 int c = 0;
88 for (int i = ff; i < lf; i++) {
89 frames[FLTable[i]]->acquire();
90 anim->AddFrame( frames[FLTable[i]], c++ );
92 return anim;
95 /* returns the required frame of the named cycle, cycle defaults to 0 */
96 Sprite2D* AnimationFactory::GetFrame(unsigned short index, unsigned char cycle) const
98 if (cycle >= cycles.size()) {
99 return NULL;
101 int ff = cycles[cycle]. FirstFrame, fc = cycles[cycle].FramesCount;
102 if(index >= fc) {
103 return NULL;
105 Sprite2D* spr = frames[FLTable[ff+index]];
106 spr->acquire();
107 return spr;
110 Sprite2D* AnimationFactory::GetFrameWithoutCycle(unsigned short index) const
112 if(index >= frames.size()) {
113 return NULL;
115 Sprite2D* spr = frames[index];
116 spr->acquire();
117 return spr;
120 Sprite2D* AnimationFactory::GetPaperdollImage(ieDword *Colors,
121 Sprite2D *&Picture2, unsigned int type) const
123 if (frames.size()<2) {
124 return NULL;
127 Video* video = core->GetVideoDriver();
128 Picture2 = video->DuplicateSprite(frames[1]);
129 if (!Picture2) {
130 return NULL;
132 if (Colors) {
133 Palette* palette = Picture2->GetPalette();
134 palette->SetupPaperdollColours(Colors, type);
135 Picture2->SetPalette(palette);
136 palette->Release();
139 Picture2->XPos = (short)frames[1]->XPos;
140 Picture2->YPos = (short)frames[1]->YPos - 80;
143 Sprite2D* spr = core->GetVideoDriver()->DuplicateSprite(frames[0]);
144 if (Colors) {
145 Palette* palette = spr->GetPalette();
146 palette->SetupPaperdollColours(Colors, type);
147 spr->SetPalette(palette);
148 palette->Release();
151 spr->XPos = (short)frames[0]->XPos;
152 spr->YPos = (short)frames[0]->YPos;
154 //don't free pixels, createsprite stores it in spr
156 return spr;
159 void AnimationFactory::IncDataRefCount()
161 ++datarefcount;
164 void AnimationFactory::DecDataRefCount()
166 assert(datarefcount > 0);
167 --datarefcount;