//This code is copyright Andrew Story (Kaslai/Aslai) All rights reserved.
//This and derivative works can be distributed as seen fit,
//so long as this notice remains unchanged and present

#include "engine/animation.h"
#include "engine/types.h"


std::vector<Animation*> animations;
std::map<const char*, int, map_char> animation_map;

void Animation::draw( int x, int y, int frame )
{
    parent->draw_image_part( x - origin_x, y - origin_y, (frame%frames) * w, 0, w, h, img );
}

Animation::Animation( CImage image, Context* prnt, int framecnt, int wid, int hig, int or_x, int or_y, char* ID )
{
    frames = framecnt;
    img = image;
    w = wid;
    h = hig;
    origin_x = or_x;
    origin_y = or_y;
    parent = prnt;
    id = (char*) malloc( strlen( ID ) + 1 );
    strcpy( id, ID );
}
Animation::~Animation(){
    free( id );
}

Animation* animation_add( CImage img, Context* cnt, int framecnt, int wid, int hig, int or_x, int or_y, char* ID )
{
    Animation* a = new Animation( img, cnt, framecnt, wid, hig, or_x, or_y, ID );
    int pos = animations.size();
    animations.push_back( a );
    animation_map[a->id] = pos;
    a->pos = pos;
    return a;
}

void animation_rem( Animation* ani )
{
    delete animations[ani->pos];
    animations[ani->pos] = 0;
    //delete ani;
}

Animation* animation_get_by_id( char* ID )
{

    return animations[animation_map[ID]];
}