GameScript: Move initialization out of constructor.
[gemrb.git] / gemrb / core / AnimationFactory.cpp
blobe454a12c4be2aff19f13d1dc7c7ed61151b2f264
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 "win32def.h"
22 #include "AnimationFactory.h"
23 #include "Interface.h"
24 #include "Video.h"
26 AnimationFactory::AnimationFactory(const char* ResRef)
27 : FactoryObject( ResRef, IE_BAM_CLASS_ID )
29 FLTable = NULL;
30 FrameData = NULL;
31 datarefcount = 0;
34 AnimationFactory::~AnimationFactory(void)
36 for (unsigned int i = 0; i < frames.size(); i++) {
37 core->GetVideoDriver()->FreeSprite( frames[i] );
39 if (FLTable)
40 free( FLTable);
42 // FIXME: track down where sprites are being leaked
43 if (datarefcount) {
44 fprintf(stderr, "AnimationFactory %s has refcount %d\n", ResRef, datarefcount);
45 //assert(datarefcount == 0);
47 if (FrameData)
48 free( FrameData);
51 void AnimationFactory::AddFrame(Sprite2D* frame)
53 frames.push_back( frame );
56 void AnimationFactory::AddCycle(CycleEntry cycle)
58 cycles.push_back( cycle );
61 void AnimationFactory::LoadFLT(unsigned short* buffer, int count)
63 if (FLTable) {
64 free( FLTable );
66 //FLTable = new unsigned short[count];
67 FLTable = (unsigned short *) malloc(count * sizeof( unsigned short ) );
68 memcpy( FLTable, buffer, count * sizeof( unsigned short ) );
71 void AnimationFactory::SetFrameData(unsigned char* FrameData)
73 this->FrameData = FrameData;
77 Animation* AnimationFactory::GetCycle(unsigned char cycle)
79 if (cycle >= cycles.size()) {
80 return NULL;
82 int ff = cycles[cycle].FirstFrame;
83 int lf = ff + cycles[cycle].FramesCount;
84 Animation* anim = new Animation( cycles[cycle].FramesCount );
85 int c = 0;
86 for (int i = ff; i < lf; i++) {
87 frames[FLTable[i]]->acquire();
88 anim->AddFrame( frames[FLTable[i]], c++ );
90 return anim;
93 /* returns the required frame of the named cycle, cycle defaults to 0 */
94 Sprite2D* AnimationFactory::GetFrame(unsigned short index, unsigned char cycle)
96 if (cycle >= cycles.size()) {
97 return NULL;
99 int ff = cycles[cycle]. FirstFrame, fc = cycles[cycle].FramesCount;
100 if(index >= fc) {
101 return NULL;
103 Sprite2D* spr = frames[FLTable[ff+index]];
104 spr->acquire();
105 return spr;
108 Sprite2D* AnimationFactory::GetFrameWithoutCycle(unsigned short index)
110 if(index >= frames.size()) {
111 return NULL;
113 Sprite2D* spr = frames[index];
114 spr->acquire();
115 return spr;
118 Sprite2D* AnimationFactory::GetPaperdollImage(ieDword *Colors,
119 Sprite2D *&Picture2, unsigned int type)
121 if (frames.size()<2) {
122 return NULL;
125 Video* video = core->GetVideoDriver();
126 Picture2 = video->DuplicateSprite(frames[1]);
127 if (!Picture2) {
128 return NULL;
130 if (Colors) {
131 Palette* palette = Picture2->GetPalette();
132 palette->SetupPaperdollColours(Colors, type);
133 Picture2->SetPalette(palette);
134 palette->Release();
137 Picture2->XPos = (short)frames[1]->XPos;
138 Picture2->YPos = (short)frames[1]->YPos - 80;
141 Sprite2D* spr = core->GetVideoDriver()->DuplicateSprite(frames[0]);
142 if (Colors) {
143 Palette* palette = spr->GetPalette();
144 palette->SetupPaperdollColours(Colors, type);
145 spr->SetPalette(palette);
146 palette->Release();
149 spr->XPos = (short)frames[0]->XPos;
150 spr->YPos = (short)frames[0]->YPos;
152 //don't free pixels, createsprite stores it in spr
154 return spr;
157 void AnimationFactory::IncDataRefCount()
159 ++datarefcount;
162 void AnimationFactory::DecDataRefCount()
164 assert(datarefcount > 0);
165 --datarefcount;