Debian Codegraph @

src/level/Anim.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "Anim.h"
10
11 #include "Log.h"
12 #include "Path.h"
13 #include "ResImagePack.h"
14 #include "LogicException.h"
15 #include "StringTool.h"
16
17 #include "EffectNone.h"
18 #include "EffectMirror.h"
19 #include "EffectInvisible.h"
20 #include "EffectReverse.h"
21 #include "EffectZx.h"
22
23
24 //-----------------------------------------------------------------
25 /**
26 * Create new animation sprite.
27 */
28 Anim::Anim()
29 : m_viewShift(0, 0)
30 {
31 m_animPack[SIDE_LEFT] = new ResImagePack();
32 m_animPack[SIDE_RIGHT] = new ResImagePack();
33
34 m_animName = "";
35 m_animPhase = 0;
36 m_run = false;
37 m_specialAnimName = "";
38 m_specialAnimPhase = 0;
39 m_effect = new EffectNone();
40 }
41 //-----------------------------------------------------------------
42 Anim::~Anim()
43 {
44 m_animPack[SIDE_LEFT]->removeAll();
45 m_animPack[SIDE_RIGHT]->removeAll();
46 delete m_animPack[SIDE_LEFT];
47 delete m_animPack[SIDE_RIGHT];
48 delete m_effect;
49 }
50 //-----------------------------------------------------------------
51 /**
52 * Draw anim phase at screen position.
53 * Increase phase when anim is running.
54 */
55 void
56 Anim::drawAt(SDL_Surface *screen, int x, int y, eSide side)
57 {
58 if (!m_effect->isInvisible()) {
59 SDL_Surface *surface =
60 m_animPack[side]->getRes(m_animName, m_animPhase);
61 m_effect->blit(screen, surface, x, y);
62 if (m_run) {
63 m_animPhase++;
64 if (m_animPhase >= m_animPack[side]->countRes(m_animName)) {
65 m_animPhase = 0;
66 }
67 }
68
69 if (!m_specialAnimName.empty()) {
70 surface =
71 m_animPack[side]->getRes(m_specialAnimName, m_specialAnimPhase);
72 m_effect->blit(screen, surface, x, y);
73 }
74 }
75
76 m_effect->updateEffect();
77 }
78 //-----------------------------------------------------------------
79 /**
80 * Add picture to anim,
81 * default side is left side.
82 */
83 void
84 Anim::addAnim(const std::string &name, const Path &picture, eSide side)
85 {
86 m_usedPath = picture.getPosixName();
87 m_animPack[side]->addImage(name, picture);
88 }
89 //-----------------------------------------------------------------
90 /**
91 * Add prepared picture to anim,
92 * default side is left side.
93 */
94 void
95 Anim::addAnim(const std::string &name, SDL_Surface *new_image, eSide side)
96 {
97 m_animPack[side]->addRes(name, new_image);
98 }
99 //-----------------------------------------------------------------
100 /**
101 * Run this animation.
102 * Nothing is changed when animation is already running.
103 */
104 void
105 Anim::runAnim(const std::string &name, int start_phase)
106 {
107 if (m_animName != name) {
108 setAnim(name, start_phase);
109 }
110 m_run = true;
111 }
112 //-----------------------------------------------------------------
113 /**
114 * Set static visage.
115 */
116 void
117 Anim::setAnim(const std::string &name, int phase)
118 {
119 m_run = false;
120 m_animName = name;
121 m_animPhase = phase;
122
123 int count = m_animPack[SIDE_LEFT]->countRes(name);
124 if (m_animPhase >= count) {
125 if (count == 0) {
126 m_animPhase = 0;
127 }
128 else {
129 m_animPhase %= count;
130 }
131 LOG_WARNING(ExInfo("anim phase over-flow")
132 .addInfo("anim", name)
133 .addInfo("phase", phase)
134 .addInfo("count", count)
135 .addInfo("usedPath", m_usedPath));
136 }
137 }
138 //-----------------------------------------------------------------
139 /**
140 * Use special efect for one phase.
141 * Efect will be blited in second layer.
142 *
143 * @param name anim name, empty is no anim
144 * @param phase anim phase
145 */
146 void
147 Anim::useSpecialAnim(const std::string &name, int phase)
148 {
149 m_specialAnimName = name;
150 m_specialAnimPhase = phase;
151 if (m_specialAnimName.empty()) {
152 return;
153 }
154
155 int count = m_animPack[SIDE_LEFT]->countRes(name);
156 if (m_specialAnimPhase >= count) {
157 if (count == 0) {
158 m_specialAnimName = "";
159 m_specialAnimPhase = 0;
160 }
161 else {
162 m_specialAnimPhase %= count;
163 }
164 LOG_WARNING(ExInfo("special anim phase over-flow")
165 .addInfo("anim", name)
166 .addInfo("phase", phase)
167 .addInfo("count", count));
168 }
169 }
170 //-----------------------------------------------------------------
171 /**
172 * Change effect.
173 * @throws LogicException when new_effect is NULL.
174 */
175 void
176 Anim::changeEffect(ViewEffect *new_effect)
177 {
178 if (NULL == new_effect) {
179 throw LogicException(ExInfo("new_effect is NULL")
180 .addInfo("animName", m_animName)
181 .addInfo("specialAnimName", m_specialAnimName));
182 }
183
184 delete m_effect;
185 m_effect = new_effect;
186 }
187 //-----------------------------------------------------------------
188 int
189 Anim::countAnimPhases(const std::string &anim, eSide side) const
190 {
191 return m_animPack[side]->countRes(anim);
192 }
193 //-----------------------------------------------------------------
194 void
195 Anim::setEffect(const std::string &effectName) {
196 if (EffectNone::NAME == effectName) {
197 changeEffect(new EffectNone());
198 }
199 else if (EffectMirror::NAME == effectName) {
200 changeEffect(new EffectMirror());
201 }
202 else if (EffectInvisible::NAME == effectName) {
203 changeEffect(new EffectInvisible());
204 }
205 else if (EffectReverse::NAME == effectName) {
206 changeEffect(new EffectReverse());
207 }
208 else if (EffectZx::NAME == effectName) {
209 changeEffect(new EffectZx());
210 }
211 else {
212 ExInfo error = ExInfo("unknown view effect")
213 .addInfo("effect", effectName);
214 LOG_WARNING(error);
215 }
216 }
217
218 //-----------------------------------------------------------------
219 static std::string
220 encode(const std::string &input)
221 {
222 std::string output = input;
223 StringTool::replace(output, "&", "&");
224 StringTool::replace(output, ",", ",");
225 return output;
226 }
227 //-----------------------------------------------------------------
228 static std::string
229 encode(int value)
230 {
231 return StringTool::toString(value);
232 }
233 //-----------------------------------------------------------------
234 static std::string
235 decode(const std::string &input)
236 {
237 std::string output = input;
238 StringTool::replace(output, ",", ",");
239 StringTool::replace(output, "&", "&");
240 return output;
241 }
242 //-----------------------------------------------------------------
243 static int
244 decodeInt(const std::string &input)
245 {
246 bool ok;
247 int result = StringTool::readInt(input.c_str(), &ok);
248 if (!ok) {
249 LOG_WARNING(ExInfo("invalid int")
250 .addInfo("input", input));
251 }
252 return result;
253 }
254 //-----------------------------------------------------------------
255 std::string
256 Anim::getState() const
257 {
258 std::string output;
259 output += encode(m_effect->getName());
260 output += "," + encode(m_viewShift.getX());
261 output += "," + encode(m_viewShift.getY());
262 output += "," + encode(m_animName);
263 output += "," + encode(m_animPhase);
264 output += "," + encode(m_run);
265 output += "," + encode(m_specialAnimName);
266 output += "," + encode(m_specialAnimPhase);
267 return output;
268 }
269 //-----------------------------------------------------------------
270 void
271 Anim::restoreState(const std::string &state)
272 {
273 StringTool::t_args values = StringTool::split(state, ',');
274 if (values.size() != 8) {
275 LOG_WARNING(ExInfo("invalid anim state")
276 .addInfo("state", state));
277 return;
278 }
279
280 int i = 0;
281 std::string effectName = decode(values[i++]);
282 int x = decodeInt(values[i++]);
283 int y = decodeInt(values[i++]);
284 m_animName = decode(values[i++]);
285 m_animPhase = decodeInt(values[i++]);
286 m_run = decodeInt(values[i++]);
287 m_specialAnimName = decode(values[i++]);
288 m_specialAnimPhase = decodeInt(values[i++]);
289
290 setEffect(effectName);
291 m_viewShift = V2(x, y);
292 }