Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Anim.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "Anim.h"
00010 
00011 #include "Log.h"
00012 #include "Path.h"
00013 #include "ResImagePack.h"
00014 #include "EffectNone.h"
00015 #include "LogicException.h"
00016 
00017 //-----------------------------------------------------------------
00018 /**
00019  * Create new animation sprite.
00020  */
00021     Anim::Anim()
00022 : m_viewShift(0, 0)
00023 {
00024     m_animPack[SIDE_LEFT] = new ResImagePack();
00025     m_animPack[SIDE_RIGHT] = new ResImagePack();
00026 
00027     m_animName = "";
00028     m_animPhase = 0;
00029     m_run = false;
00030     m_specialAnimName = "";
00031     m_specialAnimPhase = 0;
00032     m_effect = new EffectNone();
00033 }
00034 //-----------------------------------------------------------------
00035 Anim::~Anim()
00036 {
00037     m_animPack[SIDE_LEFT]->removeAll();
00038     m_animPack[SIDE_RIGHT]->removeAll();
00039     delete m_animPack[SIDE_LEFT];
00040     delete m_animPack[SIDE_RIGHT];
00041     delete m_effect;
00042 }
00043 //-----------------------------------------------------------------
00044 /**
00045  * Draw anim phase at screen position.
00046  * Increase phase when anim is running.
00047  */
00048     void
00049 Anim::drawAt(SDL_Surface *screen, int x, int y, eSide side)
00050 {
00051     if (!m_effect->isInvisible()) {
00052         SDL_Surface *surface =
00053             m_animPack[side]->getRes(m_animName, m_animPhase);
00054         m_effect->blit(screen, surface, x, y);
00055         if (m_run) {
00056             m_animPhase++;
00057             if (m_animPhase >= m_animPack[side]->countRes(m_animName)) {
00058                 m_animPhase = 0;
00059             }
00060         }
00061 
00062         if (!m_specialAnimName.empty()) {
00063             surface =
00064                 m_animPack[side]->getRes(m_specialAnimName, m_specialAnimPhase);
00065             m_effect->blit(screen, surface, x, y);
00066             m_specialAnimName = "";
00067         }
00068     }
00069 
00070     m_effect->updateEffect();
00071 }
00072 //-----------------------------------------------------------------
00073 /**
00074  * Add picture to anim,
00075  * default side is left side.
00076  */
00077     void
00078 Anim::addAnim(const std::string &name, const Path &picture, eSide side)
00079 {
00080     m_usedPath = picture.getPosixName();
00081     m_animPack[side]->addImage(name, picture);
00082 }
00083 //-----------------------------------------------------------------
00084 /**
00085  * Run this animation.
00086  * Nothing is changed when animation is already running.
00087  */
00088     void
00089 Anim::runAnim(const std::string &name, int start_phase)
00090 {
00091     if (m_animName != name) {
00092         setAnim(name, start_phase);
00093     }
00094     m_run = true;
00095 }
00096 //-----------------------------------------------------------------
00097 /**
00098  * Set static visage.
00099  */
00100     void
00101 Anim::setAnim(const std::string &name, int phase)
00102 {
00103     m_run = false;
00104     m_animName = name;
00105     m_animPhase = phase;
00106 
00107     int count = m_animPack[SIDE_LEFT]->countRes(name);
00108     if (m_animPhase >= count) {
00109         if (count == 0) {
00110             m_animPhase = 0;
00111         }
00112         else {
00113             m_animPhase %= count;
00114         }
00115         LOG_WARNING(ExInfo("anim phase over-flow")
00116                 .addInfo("anim", name)
00117                 .addInfo("phase", phase)
00118                 .addInfo("count", count)
00119                 .addInfo("usedPath", m_usedPath));
00120     }
00121 }
00122 //-----------------------------------------------------------------
00123 /**
00124  * Use special efect for one phase.
00125  * Efect will be blited in second layer.
00126  *
00127  * @param name anim name, empty is no anim
00128  * @param phase anim phase
00129  */
00130     void
00131 Anim::useSpecialAnim(const std::string &name, int phase)
00132 {
00133     m_specialAnimName = name;
00134     m_specialAnimPhase = phase;
00135 
00136     int count = m_animPack[SIDE_LEFT]->countRes(name);
00137     if (m_specialAnimPhase >= count) {
00138         if (count == 0) {
00139             m_specialAnimName = "";
00140             m_specialAnimPhase = 0;
00141         }
00142         else {
00143             m_specialAnimPhase %= count;
00144         }
00145         LOG_WARNING(ExInfo("special anim phase over-flow")
00146                 .addInfo("anim", name)
00147                 .addInfo("phase", phase)
00148                 .addInfo("count", count));
00149     }
00150 }
00151 //-----------------------------------------------------------------
00152 /**
00153  * Change effect.
00154  * @throws LogicException when new_effect is NULL.
00155  */
00156 void
00157 Anim::changeEffect(ViewEffect *new_effect)
00158 {
00159     if (NULL == new_effect) {
00160         throw LogicException(ExInfo("new_effect is NULL")
00161                 .addInfo("animName", m_animName)
00162                 .addInfo("specialAnimName", m_specialAnimName));
00163     }
00164 
00165     delete m_effect;
00166     m_effect = new_effect;
00167 }
00168 //-----------------------------------------------------------------
00169     int
00170 Anim::countAnimPhases(const std::string &anim, eSide side) const
00171 {
00172     return m_animPack[side]->countRes(anim);
00173 }
00174 

Generated on Wed Jun 1 09:54:30 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2