/*Copyright (C) 2010 Deveaux Duncan

This file is part of Hikou no mizu.

Hikou no mizu is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Hikou no mizu is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Hikou no mizu.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Animation.hpp"

Animation::Animation(const std::string &framesPath, int framesNo, sf::Sprite *renderImage) :
m_ixFrame(0), m_framesNo(framesNo), m_framesPath(framesPath), m_renderImage(renderImage)
{
    initImages();
}

void Animation::goToFrame(int frame)
{
    if (frame < m_framesNo && frame >= 0) m_ixFrame = frame;
    
    m_renderImage->SetImage(*m_framesList[m_ixFrame], true);
}

void Animation::next()
{
    int bufferFrame = m_ixFrame;
    if (bufferFrame < m_framesNo - 1) bufferFrame++;
    else bufferFrame = 0;
    
    goToFrame(bufferFrame);
}

void Animation::previous()
{
    int bufferFrame = m_ixFrame;
    if (bufferFrame > 0) bufferFrame--;
    else bufferFrame = m_framesNo - 1;
    
    goToFrame(bufferFrame);
}

void Animation::initImages()
{
    ImageEngine *imageManager = ImageEngine::getInstance();
    
    for (int i=0; i < m_framesNo; i++)
    {
        //Get image path
        std::ostringstream convertInt;
        convertInt << i + 1;
        std::string frameName = m_framesPath + "f"+convertInt.str()+".png";
        
        //Get image and register it in m_framesList
        m_framesList.push_back( imageManager->getImage(frameName) );
    }
}

//Get and set methods
int Animation::getFramesNo() const
{
    return m_framesNo;
}

int Animation::getCurrentFrameIx() const
{
    return m_ixFrame;
}