/***************************************************************************
 *   Copyright (C) 2007 by Denis A. Birukov   *
 *   Denis A. Birukov@gmail.com   *
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "snow.h"
#include "imageloader.h"
#include <video_int.h>
#include <video.h>
#include <stdlib.h>
#include <iostream>

snow::snow ( int snow_count )
		: effects(), _snow_count_ ( snow_count )
{
}


snow::~snow()
{
}

bool snow::prepare ( std::string video_name, Video_Interface* vid )
{
	if ( !imageloader_loadimage ( "png", video_name, "res/images/snow.png", "fg", "snow" ) )
	{
		std::cerr<<"Cant load image \"res/images/snow.png\":"<<imageloader_error() <<std::endl;
		return false;
	}
	if ( !vid->_create_particles ( "snow", "snow", "snow", "fg", _snow_count_, false ) )
	{
		std::cerr<<"Cant create particles:"<<vid->_get_error() <<std::endl;
		return false;
	}
	Video_Particles *par=vid->_get_particles ( "snow", "snow" );
	par->_set_particle_size ( 4 );
	for ( int a=0;a<_snow_count_;a++ )
	{
		double x_s, y_s;
		x_s= ( double ) ( rand() ) / ( double ) ( RAND_MAX/32. );
		y_s= ( double ) ( rand() ) / ( double ) ( RAND_MAX/4. );
		double y_sp;
		y_sp= ( double ) ( rand() ) / ( double ) RAND_MAX;
		y_sp/=100.;
		y_sp+=0.01;
		snow_data snows ( x_s-16.0, y_s-1.0, 0.0, -y_sp );
		_snows_.push_back ( snows );
	}
	timer.reset();
	return true;
}

bool snow::run ( int frame_no, Video_Interface *vid, float param )
{
	float dt = timer.get_elapsed_time();
	std::vector<snow_data>::iterator it;
	Video_Particles *par=vid->_get_particles ( "snow", "snow" );
	int a=0;
	for ( it=_snows_.begin(); it!=_snows_.end(); ++it ,++a )
	{
		it->x+=it->x_s* dt*30.0;
		if(param!=0.0)
		{
			it->x-=param* (dt/15.0);
		}
		it->y+=it->y_s* dt*30.0;
		if ( it->y<=-1.1 )
		{
			it->y_s= ( double ) ( rand() ) / ( double ) RAND_MAX;
			it->y_s/=100.;
			it->y_s=-it->y_s;
			it->y_s-=0.01;
			it->y=1.0+ ( double ) ( rand() ) / ( double ) ( RAND_MAX/2. );
		}
		par->_set_particle ( a, it->x, it->y, 220, 220, 255, 1.0 );

	}
	vid->_add_particles ( "snow", "snow" );
	return true;
}

