/***************************************************************************
*   Copyright (C) 2004 by Messenger of death                              *
*   riper@fromru.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 <me_defines.h>
 #include <me_config.h>
 #include <me_c_storage_driver_interface.h>
 #include <me_c_log_interface.h>
 #include <me_c_text_log.h>
 #include <me_c_storage.h>
 #include <me_debug.h>
 #include <iostream>
 #include <fstream>
 #include <map>
 #include <me_c_video.h>
 #include <me_c_video_driver_interface.h>
 #include <me_c_video_surface.h>
 #include <me_c_storage_driver_cfg.h>
 #include <ctime>
 #include <stdlib.h>
 #include <cmath>
 #ifdef WIN32
 #define random rand
 #endif

namespace SDL
{
#include <SDL.h>
#include <SDL_mixer.h>
#undef main
}
bool process_events( void )
{
	/* Our SDL event placeholder. */
	SDL::SDL_Event event;

	/* Grab all the events off the queue. */
	while ( SDL::SDL_PollEvent( &event ) )
	{

		switch ( event.type )
		{
			case SDL::SDL_MOUSEMOTION:
			if ( event.motion.xrel > 25 || event.motion.yrel > 25 ||
			        event.motion.xrel < -25 || event.motion.yrel < -25 ) return false;
			break;
			case SDL::SDL_KEYDOWN:
			case SDL::SDL_MOUSEBUTTONDOWN:
			return false;
			break;
		}

	}
	return true;
}

ME_n_Video::ME_c_Video_Surface *IMG_LoadPNG_RW( std::string fname, std::string grp, std::string name );
ME_n_Video::ME_c_Video_Surface *IMG_LoadJPG_RW( std::string fname, std::string grp, std::string name );
bool FT_init( Uint16 size );
ME_n_Video::ME_c_Video_Surface* FT_Render( std::string text, Uint32 color, std::string grp, std::string name );
bool FT_openfont( Uint16 size );
class Snow_struct
{
	public:
		Sint16 x;
		Sint16 y;
		Sint8 xv;
		Sint8 yv;
		Sint16 xvt;
		float scale;
		Snow_struct()
		{
			x = ( random() % 800 ) - 400;
			y = -( random() % 300 - 600 );
			scale = 0.05 * ( double ) ( random() % 15 + 1 );
			xvt = ( random() % 100 ) + 100;
			xv = ( random() % 3 ) - 1;
			yv = ( random() % 7 ) + 1;
		}
		void regen()
		{
			x = ( random() % 800 ) - 400;
			y = -( random() % 100 - 400 );
			scale = 0.05 * ( double ) ( random() % 15 + 1 );
			xvt = ( random() % 100 ) + 100;
			xv = ( random() % 3 ) - 1;
			yv = ( random() % 7 ) + 1;
		}
} ;
Snow_struct *sn_st = 0;
ME_n_Video::ME_c_Video_Surface *snow = 0;
ME_n_Video::ME_c_Video_Surface *ded = 0;
ME_n_Video::ME_c_Video_Surface *star = 0;
ME_n_Video::ME_c_Video_Surface *text = 0;
ME_n_Video::ME_c_Video_Surface *count_d = 0;
Uint32 max_snow_num;

Snow_struct text_s;
Uint32 MAGIC = 319441;
std::string strings[ 200 ];
Uint32 str_num, curnum;
bool read_text()
{
	std::ifstream in( "res/bg_top_f.png" );
	if ( !in.seekg( MAGIC ) ) return false;
	Uint32 a;
	for ( a = 0;std::getline( in, strings[ a ] );a++ );
	str_num = a;
	curnum = 0;
	FT_openfont( 30 );
	text = FT_Render( strings[ curnum ].c_str(), 0xffffdddd , "maintext", "Text" );
	curnum = 1;
	text_s.x = ( random() % 400 ) - 400;
	text_s.y = ( 600-random() % 300);
	text_s.xvt = ( random() % 100 ) + 100;
	text_s.xv = ( random() % 3 ) - 1;
	text_s.yv = ( random() % 4 ) + 2;
	in.close();
	return true;
}
void draw_text()
{
	ME_n_Video::ME_c_Video video;
	ME_n_Video::ME_c_Video_Driver_Interface &vd = ( *video );
	if ( text_s.xvt == 0 )
	{
		text_s.xvt = ( random() % 100 + 100 );
		text_s.xv = ( random() % 5 ) - 2;
	}
	else
	{
		text_s.xvt--;
		text_s.x -= text_s.xv;
		if ( text_s.x < -400 || text_s.x + text->_get_width() > 400 ) text_s.xv = -text_s.xv;
	}
	if ( text_s.y < -340 )
	{
		text_s.x = ( random() % 400 ) - 400;
		text_s.y = (600- random() % 300 - 600 );
		text_s.xvt = ( random() % 100 ) + 100;
		text_s.xv = ( random() % 3 ) - 1;
		text_s.yv = ( random() % 4 ) + 2;
		FT_openfont( 30 );
		vd._free_surface( "Text", "maintext" );
		text = FT_Render( strings[ curnum ].c_str(), 0xffffdddd , "maintext", "Text" );
		curnum++;
		if ( curnum == str_num - 1 ) curnum = 0;
	}
	else text_s.y -= text_s.yv;
	//	snow->_set_scale( sn_st[ a ].scale, sn_st[ a ].scale );

	vd._blit( *text, text_s.x / 400., text_s.y / 300. );

}
Uint32 NYT;

bool audio;


void countdown()
{
	ME_n_Video::ME_c_Video video;
	ME_n_Video::ME_c_Video_Driver_Interface &vd = ( *video );
	static Uint32 z = 30;
	char str[ 200 ];
	Uint32 ds;
	if ( z % 30 == 0 )
	{
		ds = NYT - std::time( NULL );
		if ( ds > 0 )
		{
			sprintf( str, "До Нового Года осталось всего %lu секунд!", ds );
		}
		else
		{
			sprintf( str, "Прошло %lu секунд с начала 2005 года!", ds );
		}
		vd._free_surface( "Text", "countdown" );
		count_d = FT_Render( str, 0xffffdddd , "countdown", "Text" );
		z = 0;
	}
	vd._blit( *count_d, ( -count_d->_get_width() / 2 ) / 400., -1.0 );
	z++;
}


bool intro()
{
	ME_n_Video::ME_c_Video video;
	ME_n_Video::ME_c_Video_Driver_Interface &vd = ( *video );
	ME_n_Video::ME_c_Video_Surface * txt;
	Uint16 a, z, z1, z2, z3, z4, z5;
	Uint32 tt1, tt2;
	FT_openfont( 50 );
	ME_n_Video::ME_c_Video_Surface &intro_t1 = *FT_Render( "MESSENGER OF DEATH", 0xffffffff, "intro", "Text" );
	ME_n_Video::ME_c_Video_Surface *tmp;
	FT_openfont( 24 );
	for ( a = 0;a < 512;a += 2 )
	{
		if ( !process_events( ) ) return false;
		tt1 = SDL::SDL_GetTicks();
		while ( tt1 - tt2 < 33 ) tt1 = SDL::SDL_GetTicks();
		tt2 = SDL::SDL_GetTicks();
		vd._blit( intro_t1, ( -intro_t1._get_width() / 2 ) / 400., 0.0 );
		z = ( a < 255 ) ? a : 255;
		tmp = FT_Render( "представяет", 0x00ffffff | ( z << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, ( -tmp->_get_width() / 2 ) / 400., ( -intro_t1._get_height() / 2 - 10 ) / 300. );
		vd._draw();
		vd._free_surface( "Text", "intro_1" );
	}
	Uint32 dy;
	for ( a = 0;a < 512;a += 3 )
	{
		if ( !process_events( ) ) return false;
		tt1 = SDL::SDL_GetTicks();
		while ( tt1 - tt2 < 33 ) tt1 = SDL::SDL_GetTicks();
		tt2 = SDL::SDL_GetTicks();
		z = ( a < 256 ) ? a : 512 - a;
		tmp = FT_Render( "В сотрудничестве с:", 0x00ffffff | ( z << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 );
		dy = tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "OpenGL Graphics System", 0x00ffffff | ( z << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "&", 0x00ffffff | ( z << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "Simple Directmedia Layer", 0x00ffffff | ( z << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		vd._free_surface( "Text", "intro_1" );

		z1 = ( a > 256 ) ? a - 256 : 0;

		tmp = FT_Render( "При участии:", 0x00ffffff | ( z1 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, ( 400 - tmp->_get_width() ) / 400. - 0.1, -0.4 );
		dy = tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "FreeType Library,", 0x00ffffff | ( z1 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, ( 400 - tmp->_get_width() ) / 400. - 0.1, -0.4 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "Portable Network Graphics,", 0x00ffffff | ( z1 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, ( 400 - tmp->_get_width() ) / 400. - 0.1, -0.4 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "JPEG,", 0x00ffffff | ( z1 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, ( 400 - tmp->_get_width() ) / 400. - 0.1, -0.4 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "Mixer for SDL", 0x00ffffff | ( z1 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, ( 400 - tmp->_get_width() ) / 400. - 0.1, -0.4 - ( float ) dy / 300. );
		vd._free_surface( "Text", "intro_1" );

		vd._draw();
	}
	z = z1 = z2 = z3 = z4 = z5 = 0;
	for ( a = 0;a < 256;a += 2 )
	{
		if ( !process_events( ) ) return false;
		tt1 = SDL::SDL_GetTicks();
		while ( tt1 - tt2 < 33 ) tt1 = SDL::SDL_GetTicks();
		tt2 = SDL::SDL_GetTicks();

		z = a ;
		z1 = ( a > 10 ) ? a - 10 : 0;
		z2 = ( a > 20 ) ? a - 20 : 0;
		z3 = ( a > 30 ) ? a - 30 : 0;

		tmp = FT_Render( "В ролях:", 0x00ffffff | ( z << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 );
		dy = tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "Дерево хвойное, зеленое, женского пола,", 0x00ffffff | ( z << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "Сани Деда Мороза,", 0x00ffffff | ( z1 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "Дед Мороз,", 0x00ffffff | ( z2 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "Вода, родниковая, застывшая.", 0x00ffffff | ( z3 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );

		vd._draw();
	}
	z = z1 = z2 = z3 = z4 = z5 = 0;
	for ( a = 0;a < 256;a += 2 )
	{
		if ( !process_events( ) ) return false;
		tt1 = SDL::SDL_GetTicks();
		while ( tt1 - tt2 < 33 ) tt1 = SDL::SDL_GetTicks();
		tt2 = SDL::SDL_GetTicks();

		z = a ;
		z1 = ( a > 10 ) ? a - 10 : 0;
		z2 = ( a > 20 ) ? a - 20 : 0;
		z3 = ( a > 30 ) ? a - 30 : 0;

		tmp = FT_Render( "Автор выражает признательность:", 0x00ffffff | ( z << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 );
		dy = tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "Линюсу Торвальдсу, ", 0x00ffffff | ( z << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "за лучшую в мире операционную систему - за LINUX", 0x00ffffff | ( z1 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "\"Software is like sex, it's better when it's free.\" - Linus Torvalds", 0x00ffffff | ( z1 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "Сообществу Open Source, за лучшие програмные продукты", 0x00ffffff | ( z2 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "Гиллеру, за предоставленную композицию", 0x00ffffff | ( z3 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		dy += tmp->_get_height();
		vd._free_surface( "Text", "intro_1" );
		tmp = FT_Render( "Себе, за вовремя разработанный Magic Engine", 0x00ffffff | ( z3 << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, -0.8, 0.8 - ( float ) dy / 300. );
		vd._free_surface( "Text", "intro_1" );

		vd._draw();
	}
	FT_openfont( 150 );
	SDL::Mix_Music * music = NULL;
	if ( audio )
	{
		music = SDL::Mix_LoadMUS( "res/sound1.mod" );
		SDL::Mix_PlayMusic( music, 0 );
	}
	for ( a = 0;a < 512;a += 2 )
	{
		if ( !process_events( ) ) return false;
		tt1 = SDL::SDL_GetTicks();
		while ( tt1 - tt2 < 33 ) tt1 = SDL::SDL_GetTicks();
		tt2 = SDL::SDL_GetTicks();
		z = ( a < 255 ) ? a : 512 - a;
		tmp = FT_Render( "NYP2005", 0x00ffffff | ( z << 24 ), "intro_1", "Text" );
		vd._blit( *tmp, ( -tmp->_get_width() / 2 ) / 400., ( -tmp->_get_height() / 2 ) / 300. );
		vd._draw();
		vd._free_surface( "Text", "intro_1" );
	}
	for ( a = 0;a < 80;a += 1 )
	{
		if ( !process_events( ) ) return false;
		tt1 = SDL::SDL_GetTicks();
		while ( tt1 - tt2 < 33 ) tt1 = SDL::SDL_GetTicks();
		tt2 = SDL::SDL_GetTicks();
	}
	vd._free_surface( "Text", "intro" );
	return true;
}
void draw_snow()
{
	ME_n_Video::ME_c_Video video;
	ME_n_Video::ME_c_Video_Driver_Interface &vd = ( *video );
	for ( Uint32 a = 0;a < max_snow_num;a++ )
	{
		if ( sn_st[ a ].xvt == 0 )
		{
			sn_st[ a ].xvt = ( random() % 100 + 100 );
			sn_st[ a ].xv = ( random() % 5 ) - 2;
		}
		else
		{
			sn_st[ a ].xvt--;
			sn_st[ a ].x -= sn_st[ a ].xv;
			if ( sn_st[ a ].x < -400 || sn_st[ a ].x > 400 ) sn_st[ a ].xv = -sn_st[ a ].xv;
		}
		if ( sn_st[ a ].y < -310 )
		{
			sn_st[ a ].regen();
		}
		else sn_st[ a ].y -= sn_st[ a ].yv;

		snow->_set_scale( sn_st[ a ].scale, sn_st[ a ].scale );
		vd._blit( *snow, sn_st[ a ].x / 400., sn_st[ a ].y / 300. );
	}
}
int main( int argc, char *argv[] )
{
#ifdef WIN32
	char path[ 1024 ];
	GetWindowsDirectory( path, 1024 );
	SetCurrentDirectory( path );
	SetCurrentDirectory( "nyp2005" );
#endif


	std::tm dat;
	dat.tm_sec = 0;
	dat.tm_min = 0;
	dat.tm_hour = 0;
	dat.tm_mday = 1;
	dat.tm_mon = 0;
	dat.tm_year = 105;
	Uint32 tm;
	NYT = mktime( &dat );

	ME_n_Log::_init_debug( new ME_n_Log::ME_c_Text_Log( "nyp2005.log" ) );
	ME_n_Log::_set_debug_priority( 254 );
	ME_n_Log::_debug( 255 ) << "We runned as \"";
	for ( int a = 0;a < argc;a++ ) ME_n_Log::_debug( 255 ) << argv[ a ] << ","<<std::fflush;
	ME_n_Log::_debug( 255 ) << "\""<<std::endl;
	for ( int a = 1;a < argc;a++ ) if ( strchr( argv[ a ], 't' ) ) return 1;
	ME_n_Storage::ME_c_Storage &storage = ME_n_Storage::_init_config( new ME_n_Storage::ME_c_Storage_Driver_CFG );
	storage._register_parser( "*" );
	storage._register_parser( "Video" );
	storage._register_parser( "Speed" );
	storage._register_parser( "System" );
	storage._register_parser( "NYP2005" );
	storage._register_parser( "Sound" );
	std::ifstream a( "nyp2005.cfg" );
	storage( a );
	ME_n_Log::_set_debug_priority( storage._get_value( "System->DebugLevel", ( Uint32 ) 255 ) );
	ME_n_Video::ME_c_Video video;
	if ( !video._init_video( storage._get_value( "Video->Driver", ( std::string ) "no such driver", ME_n_Storage::ME_STORAGE_STRING ) ) )
	{
		ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << "Can`t initialize video mode!" << std::endl;
		return 1;
	}
	 SDL::SDL_ShowCursor(SDL_DISABLE);
	audio = true;
	Uint32 arate;
	arate = storage._get_value( "Sound->Rate", ( Uint32 ) 22100 );
	if ( SDL::SDL_InitSubSystem( SDL_INIT_AUDIO ) != 0 ) audio = false;
	if ( SDL::Mix_OpenAudio( arate, AUDIO_S16, storage._get_value( "Sound->Channels", ( Uint32 ) 2 ), storage._get_value( "Sound->Buffers", ( Uint32 ) 4096 ) ) < 0 ) audio = false;
	SDL::Mix_Music * music = NULL;
	if ( audio )
	{
		music = SDL::Mix_LoadMUS( "res/sound.mod" );
	}
	if ( !FT_init( 24 ) )
	{
		ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << "Can`t initialize freetype!" << std::endl;
		return 1;
	}
	process_events( ) ;
	if ( intro() )
	{
		read_text();
		ME_n_Video::ME_c_Video_Surface &bg_top_b = *IMG_LoadJPG_RW( "res/bg_top_b.jpg", "main_top_bg" , "background" );
		ME_n_Video::ME_c_Video_Surface &bg_top_f = *IMG_LoadPNG_RW( "res/bg_top_f.png", "main_top_fg" , "background" );
		ME_n_Video::ME_c_Video_Surface &bg_bottom = *IMG_LoadJPG_RW( "res/bg_bottom.jpg", "main_bottom" , "background" );
		bg_bottom._set_alpha_status( false );
		bg_top_b._set_alpha_status( false );

		IMG_LoadPNG_RW( "res/snow1.png", "snow" , "foreground" );
		IMG_LoadPNG_RW( "res/ded.png", "ded" , "foreground" );
		IMG_LoadPNG_RW( "res/star.png", "star" , "foreground" );
		ME_n_Video::ME_c_Video_Driver_Interface &vd = ( *video );
		try
		{
			snow = &vd._get_surface( "foreground", "snow" );
			ded = &vd._get_surface( "foreground", "ded" );
			star = &vd._get_surface( "foreground", "star" );
		}
		catch ( ME_Exception n )
		{
			ME_n_Log::_debug( ME_n_Log::ME_DEBUG_INFO ) << __FILE__ << ":" << __LINE__ << " - " << n.second << std::endl;
			return 1;
		}
		max_snow_num = storage._get_value( "NYP2005->SnowCount", ( Uint32 ) 50 );
		sn_st = new Snow_struct[ max_snow_num ];

		Uint32 t1, t2, d, tt1, tt2, dt;
		bool star_exist = false, ded_exist = false;
		Snow_struct star_s, ded_s;
		Sint32 c;
		c = 300;
		d = 0;
		ME_n_Video::ME_c_Video_Surface *fps_s;
		dt = 1000. / ( double ) storage._get_value( "Speed->MaxFrameRate", ( Uint32 ) 32 );
		if ( audio ) SDL::Mix_PlayMusic( music, -1 );

		t1 = SDL::SDL_GetTicks();
		tt2 = 0;
		while ( 1 )
		{
			if ( !process_events( ) ) break;

			tt1 = SDL::SDL_GetTicks();

			while ( tt1 - tt2 < dt ) tt1 = SDL::SDL_GetTicks();
			tt2 = SDL::SDL_GetTicks();
			vd._blit( bg_top_b, -400, ( Sint16 ) ( 300 - bg_top_b._get_height() ) );
			// Sky effects
			if ( random() % 100 == 1 && !star_exist )
			{
				star_exist = true;

				star_s.x = ( random() % 800 ) - 400;
				star_s.y = 150 + random() % 150;
				star_s.scale = 0.8 + 0.1 * ( double ) ( random() % 2 + 1 );
				star_s.xvt = 300;
				star_s.xv = ( random() % 5 ) - 2;
				star_s.yv = ( random() % 5 );
			}
			if ( star_exist )
			{
				star_s.xvt--;
				star_s.x -= star_s.xv;
				star_s.y -= star_s.yv;
				star->_set_scale( star_s.scale, star_s.scale );
				vd._blit( *star, star_s.x / 400., star_s.y / 300. );
				if ( star_s.xvt < 0 ) star_exist = false;
			}
			if ( random() % 115 == 1 && !ded_exist )
			{
				ded_exist = true;

				ded_s.x = -500;
				ded_s.y = random() % 300 ;
				ded_s.scale = 0.7 + 0.03 * ( random() % 10 + 1 );
				ded_s.xvt = 800;
				ded_s.xv = ( random() % 14 ) + 1;
				ded_s.yv = ( random() % 7 );
				ded->_set_scale( ded_s.scale, ded_s.scale );
				if ( ded_s.yv != 0 ) ded->_set_rotation( 0, 0, ( ded_s.yv / ded_s.xv ) / ( 1. / 9. ) * 1 );
				else ded->_set_rotation( 0, 0, -22 );
			}
			if ( ded_exist )
			{
				ded_s.xvt--;
				ded_s.x += ded_s.xv;
				ded_s.y += ded_s.yv;
				vd._blit( *ded, ded_s.x / 400., ded_s.y / 300. );
				if ( ded_s.xvt < 0 ) ded_exist = false;
				if ( ded_s.y > 300 ) ded_exist = false;
				if ( ded_s.x > 400 ) ded_exist = false;
			}

			vd._blit( bg_top_f, -400, ( Sint16 ) ( 300 - bg_top_f._get_height() ) );
			vd._blit( bg_bottom, -1.0, -1.0 );
			//text
			draw_text();
			//time counter
			countdown();
			//snow
			draw_snow();
			vd._draw();
			if ( c == -300 ) c = 300;
			c--;
			d++;
		}
		t2 = SDL::SDL_GetTicks();
		std::cout << "Framerate :" << 1000.*( double ) d / ( double ) ( t2 - t1 ) << std::endl;
	}
	video._deinit_video();
	SDL::Mix_FreeMusic( music );
	return 0;
}

