//
// C++ Implementation: fpscounter
//
// Description:
//
//
// Author: Messenger of death aka Denis A. Birukov <messengerofdeath@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "fpscounter.h"
#include "text.h"
#include "video_int.h"
#include "video.h"
#include "storage.h"
#include "config.h"
#include "sceneobjectfabric.h"
#include "textrenderer.h"
#include "signal_manager.h"
#include <time.h>
#include <stdio.h>
        
ObjectFabric<FPSCounter> FPSCounterFabric;

FPSCounter::FPSCounter()
        : TextObject(),
        _pre_text_ ( "" ),
        _frames_count_limit_ ( 150 ),
        _cur_frame_ (0),
        _last_time_ ( 0 ),
        INIT_SLOT( FPSCounter, this, _on_new_frame )
{
}


FPSCounter::~FPSCounter()
{
}


bool FPSCounter::load ( std::string storage_name, std::string storage_prefix, std::string scene_name, std::string object_name )
{
    if ( !TextObject::load ( storage_name, storage_prefix, scene_name, object_name ) ) return false;

    storage_prefix += "FPSConter->";
    Config_Interface *intf = config_get_interface ( storage_name );
    if ( !intf )
    {
        _set_error ( "Can't get config interface:" + config_error() );
        return false;
    }
    _pre_text_ = intf->_get_value ( storage_prefix + "pre_text", "FPS:" );
    _frames_count_limit_ = atoi ( intf->_get_value ( storage_prefix + "frames_count", "150" ).c_str() );
    
    register_slot( _scene_name_ + "->" + _object_name_ + "->on_new_frame", &_on_new_frame);
    connect(_scene_name_ + "->new_cycle", _scene_name_ + "->" + _object_name_ + "->on_new_frame");
    return true;
}

void FPSCounter::draw()
{
    if (!_last_time_)
    {
        _last_time_ = time(0);
    }
    unsigned int ctime = time(0);
    Video_Interface *vid=video_get_interface ( _video_name_ );
    if ( !vid )
    {
        _set_error("Can't get video interface:"+video_error());
        return;
    }
    if (ctime>_last_time_+1)
    {
        int ctime = time(0);
        char textbuf[20];
        snprintf(textbuf, 20, " %.1f", (float)_cur_frame_/(float)(ctime - _last_time_));
        _cur_frame_ = 0;
        _last_time_ = ctime;
        _text_ = this->_pre_text_;
        _text_ += textbuf;
        
        Text_Renderer *renderer;
        renderer=textrenderer_get(_renderer_type_);
        if ( !renderer )
        {
            _set_error("Can't get renderer:"+textrenderer_error());
            return;
        }
        if ( !renderer->render_text_line( _video_name_, _font_, _text_, _clr_, _font_size_, _object_name_, _scene_name_, true ) )
        {
            _set_error ( "Can't render text:"+renderer->_get_error() );
            return;
        }
        if(!vid->_create_sprite(_object_name_, _scene_name_, _object_name_, _scene_name_, true))
        {
            _set_error("Can't create sprite: "+vid->_get_error());
            return;
        }

        Video_Sprite *spr=vid->_get_sprite ( _object_name_, _scene_name_);
        if(!spr)
        {
            _set_error("Can't get sprite: "+vid->_get_error());
            return;
        }
        spr->_set_base_point ( _xb_, _yb_ );
        spr->_set_coord(_x_, _y_);
    }
    vid->_add_sprite ( _object_name_, _scene_name_ );
}
DEFINE_SLOT( FPSCounter, _on_new_frame )
{
    _cur_frame_++;
    return false;
}
