TickHook: Fix crash when TickHook isn't set.
[gemrb.git] / gemrb / core / ControlAnimation.cpp
blob6e618844ab2306db8a24c16099421ed27322c375
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 "ControlAnimation.h"
23 #include "win32def.h"
25 #include "GameData.h"
26 #include "Interface.h"
27 #include "Palette.h" /* needed only for paperdoll palettes */
28 #include "Video.h" /* needed only for paperdoll palettes */
29 #include "GUI/Button.h"
31 ControlAnimation::ControlAnimation(Control* ctl, const ieResRef ResRef, int Cycle)
33 control = NULL;
34 bam = NULL;
35 cycle = Cycle;
36 frame = 0;
37 anim_phase = 0;
39 bam = ( AnimationFactory* ) gamedata->GetFactoryResource( ResRef,
40 IE_BAM_CLASS_ID, IE_NORMAL );
42 if (! bam)
43 return;
45 control = ctl;
46 control->animation = this;
47 has_palette = false;
50 //freeing the bitmaps only once, but using an intelligent algorithm
51 ControlAnimation::~ControlAnimation(void)
53 //removing from timer first
54 core->timer->RemoveAnimation( this );
56 bam = NULL;
59 bool ControlAnimation::SameResource(const ieResRef ResRef, int Cycle)
61 if (!control ) return false;
62 if (!bam) return false;
63 if (strnicmp(ResRef, bam->ResRef, sizeof(ieResRef) )) return false;
64 int c = cycle;
65 if (control->Flags&IE_GUI_BUTTON_PLAYRANDOM) {
66 c&=~1;
68 if (Cycle!=c) return false;
69 return true;
72 void ControlAnimation::UpdateAnimation(void)
74 unsigned long time;
75 int Cycle = cycle;
77 if (control->Flags & IE_GUI_BUTTON_PLAYRANDOM) {
78 // simple Finite-State Machine
79 if (anim_phase == 0) {
80 frame = 0;
81 anim_phase = 1;
82 time = 500 + 500 * (rand() % 20);
83 cycle&=~1;
84 Cycle=cycle;
85 } else if (anim_phase == 1) {
86 if (rand() % 30 == 0) {
87 cycle|=1;
88 Cycle=cycle;
90 anim_phase = 2;
91 time = 100;
92 } else {
93 frame++;
94 time = 100;
96 } else {
97 frame ++;
98 if (has_palette) {
99 time = 100; //hack for slower movement
100 } else {
101 time = 15;
105 Sprite2D* pic = bam->GetFrame( (unsigned short) frame, (unsigned char) Cycle );
107 if (pic == NULL) {
108 //stopping at end frame
109 if (control->Flags & IE_GUI_BUTTON_PLAYONCE) {
110 core->timer->RemoveAnimation( this );
111 return;
113 anim_phase = 0;
114 frame = 0;
115 pic = bam->GetFrame( 0, (unsigned char) Cycle );
118 if (pic == NULL) {
119 return;
122 if (has_palette) {
123 Palette* palette = pic->GetPalette();
124 palette->SetupPaperdollColours(colors, 0);
125 pic->SetPalette(palette);
126 palette->Release();
129 control->SetAnimPicture( pic );
130 core->timer->AddAnimation( this, time );
133 void ControlAnimation::SetPaletteGradients(ieDword *col)
135 memcpy(colors, col, 8*sizeof(ieDword));
136 has_palette = true;