#include "main.h"

#define STRUKTURES 5

#define EIYLI if (y >= 0 && x >= 0 && y < S_HEIGHT && x < S_WIDTH)

#define SAMPLES 3
#define PICS 11

#define MPITUUS 35000.0

char samples[SAMPLES][20] = {"data/DEMOBIISI.ogg", "data/explosion.ogg", "data/rokket.ogg"};
char kuvat[PICS][20] = {"data/monsteri.png", "data/nuklear.png", "data/rajahdys1.png", "data/rajahdys2.png", "data/rajahdys3.png", "data/rajahdys4.png", "data/nukklear1.png", "data/nukklear2.png", "data/nukklear3.png", "data/teksti1.png", "data/teksti2.png"};

class kommando
{
	private:		
		Mix_Chunk *music[SAMPLES];
		
		SDL_Rect dst;
		SDL_Rect src;
		
		SDL_Surface *effect;
		
		SDL_Surface *pics[PICS];
		
		int beginning;
		
		int x1, x2;
		int y1, y2;
		
		int modifier;
		
	public:
		void run();
		void audio();
		void lataa_kuvat();
		void background();
		void colorize();
		void fire();
		void heaven();
		kommando();
		
		void superman();
		
		int now;
		
		int paska;
		
		int kerta;
};

kommando::kommando()
{
		modifier = 0;
	paska = 0;
	kerta = 0;
	lataa_kuvat();
	effect = SDL_CreateRGBSurface(SDL_SWSURFACE,S_WIDTH, S_HEIGHT,32,0,0,0,0);
}

void kommando::run()
{

        if (modifier > 600)
          running = 0;
	
	now = SDL_GetTicks() - beginning;
	
	if (paska == 0)
	{
	src.x = dst.x;
	src.y = dst.y;
	src.w = 80;
	src.h = pics[1]->h;
	
	SDL_FillRect(screen, &src, 0);
		
		
	background();
	colorize();
	
	superman();
	}
	else if (paska < 5)
		fire();
	else if (paska == 5)
	{
		paska++;
		SDL_FillRect(screen, NULL, 0);
		Mix_Volume(Mix_PlayChannel(-1, music[2], 0),MIX_MAX_VOLUME);
		beginning = SDL_GetTicks();
	}
	else
		heaven();
}

void kommando::heaven()
{
	SDL_FillRect(screen, NULL, 0);
	
	
	dst.x = ((SDL_GetTicks()/2)%1100)-200;
	dst.y = 400;
	SDL_BlitSurface(pics[8], NULL, screen, &dst);
	
	dst.x = ((SDL_GetTicks()/2)%2000)-800;
	dst.y = 20;
	SDL_BlitSurface(pics[8], NULL, screen, &dst);
	
	
	
	dst.x = ((SDL_GetTicks()/6)%1200)-300;
	dst.y = 500;
	SDL_BlitSurface(pics[7], NULL, screen, &dst);
	
	dst.x = ((SDL_GetTicks()/4)%1020)-200;
	dst.y = 300;
	SDL_BlitSurface(pics[7], NULL, screen, &dst);
	
	dst.x = S_WIDTH/2 - pics[6]->w/2 - modifier;
	dst.y = (int)(S_HEIGHT/2 - pics[6]->h/2 + (sin(SDL_GetTicks()/200.)*20));
	SDL_BlitSurface(pics[6], NULL, screen, &dst);
	
	dst.x = ((SDL_GetTicks()/6)%1100)-200;
	dst.y = 100;
	SDL_BlitSurface(pics[7], NULL, screen, &dst);
	

	dst.y = 2;
	dst.x = S_HEIGHT/2 - pics[9]->h/2;
	SDL_BlitSurface(pics[9], NULL, screen, &dst);
	
	dst.y = 2;
	dst.x = S_HEIGHT/2 - pics[9]->h/2;
	
	src.x = 0;
	src.y = 0;
	
	if (now < 9000)
	{
	if (now > 3400)
	{
		src.w = ((now-3400)/6);

		src.h = pics[10]->h/2;
		
		SDL_BlitSurface(pics[10], &src, screen, &dst);
	}
	if (now > 6200)
	{
		src.w = ((now-6200)/6);
		src.h = 666;
		
		SDL_BlitSurface(pics[10], &src, screen, &dst);
	}
	}
	else if (now < 13500)
	{
		if (now > 9000)
		{
			src.w = ((now-9000)/6);

			src.h = pics[10]->h/2;
		
			SDL_BlitSurface(pics[10], &src, screen, &dst);
		}
		if (now > 11500)
		{
			src.w = ((now-11500)/6);
			src.h = 666;
		
			SDL_BlitSurface(pics[10], &src, screen, &dst);
		}
	}
	else
	{
		if (now > 14000)
		{
			src.w = ((now-14000)/6);

			src.h = pics[10]->h/2;
		
			SDL_BlitSurface(pics[10], &src, screen, &dst);
		}
		if (now > 17000)
		{
			src.w = ((now-17000)/6);
			src.h = 666;
		
			SDL_BlitSurface(pics[10], &src, screen, &dst);
		}
	}
	if (now > 20000)
	{
		modifier = (now-20000)/2;
	}
}

void kommando::fire()
{
 	now -= 32500;
	
	if (paska == 1)
	{
	src.x = 0;
	src.y = (int)(S_HEIGHT-(now/2.));
	src.w = S_WIDTH;
	src.h = S_HEIGHT;
	
	dst.x = -30;
	dst.y = (int)(S_HEIGHT-(now/2.));
	
	SDL_BlitSurface(pics[2], &src, screen, &dst);
	
	if (src.y <= 1)
		paska++;
	}
	
	if (paska == 2)
	{
	now -= 1200;
	src.x = 0;
	src.y = (int)(S_HEIGHT-(now/2.));
	src.w = S_WIDTH;
	src.h = S_HEIGHT;
	
	dst.x = -30;
	dst.y = (int)(S_HEIGHT-(now/2.));
	
	SDL_BlitSurface(pics[3], &src, screen, &dst);
	
	if (src.y <= 1)
		paska++;
	}
	if (paska == 3)
	{
	now -= 2400;
	src.x = 0;
	src.y = (int)(S_HEIGHT-(now/2.));
	src.w = S_WIDTH;
	src.h = S_HEIGHT;
	
	dst.x = 0;
	dst.y = (int)(S_HEIGHT-(now/2.));
	
	SDL_BlitSurface(pics[4], &src, screen, &dst);
	
	if (src.y <= 1)
		paska++;
	}
	if (paska == 4)
	{
	now -= 3600;
	src.x = 0;
	src.y = (int)(S_HEIGHT-(now/2.));
	src.w = S_WIDTH;
	src.h = S_HEIGHT;
	
	dst.x = 0;
	dst.y = (int)(S_HEIGHT-(now/2.));
	
	SDL_BlitSurface(pics[5], &src, screen, &dst);
	
	if (src.y <= 1)
		paska++;
	}
}

void kommando::superman()
{	
	if (paska == 0 && now > 32500)
	{
		Mix_PlayChannel(2, music[1], 0);
		paska++;
	}
	
	if (now < 32500)
	{
	y1 = S_HEIGHT/2 - pics[0]->h/2;
	
	if (x1 + pics[0]->w > x2-60 && (x1 + pics[0]->w) - (x2+60) < 300)
		y1 += (int)(sin((x1 + pics[0]->w-x2+20)/120.)*150);
	
	x1 = (int)((S_WIDTH/2 - pics[0]->w/2) * (now/MPITUUS));
	
	dst.y = y1;
	dst.x = x1;
	SDL_BlitSurface(pics[0], NULL, screen, &dst);
	
	if (now < 9000)
	{
		
	x2 = (int)(S_WIDTH - now/10.);
	y2 = S_HEIGHT/2 - pics[0]->h/2;
	
	dst.x = x2;
	dst.y = y2;
	SDL_BlitSurface(pics[1], NULL, screen, &dst);
	}
	else if (now < 16000)
	{
		x2 = (int)(S_WIDTH - (now-9000)/7.);
		y2 = S_HEIGHT/2 - pics[0]->h/2;
	
		dst.x = x2;
		dst.y = y2;
		SDL_BlitSurface(pics[1], NULL, screen, &dst);
	}
	else if (now < 20000)
	{
		x2 = (int)(S_WIDTH - (now-16000)/5.);
		y2 = S_HEIGHT/2 - pics[0]->h/2;
	
		dst.x = x2;
		dst.y = y2;
		SDL_BlitSurface(pics[1], NULL, screen, &dst);
	}
	else if (now < 23000)
	{
		x2 = (int)(S_WIDTH - (now-20000)/2.);
		y2 = S_HEIGHT/2 - pics[0]->h/2;
	
		dst.x = x2;
		dst.y = y2;
		SDL_BlitSurface(pics[1], NULL, screen, &dst);
	}
	else if (now < 25000)
	{
		x2 = (int)(S_WIDTH - (now-23000)*1.1);
		y2 = S_HEIGHT/2 - pics[0]->h/2;
	
		dst.x = x2;
		dst.y = y2;
		SDL_BlitSurface(pics[1], NULL, screen, &dst);
	}
	else if (now < 35000)
	{
		x2 = (int)(S_WIDTH - (now-30000)/5.);
		y2 = S_HEIGHT/2 + pics[0]->h/2;
	
		dst.x = x2;
		dst.y = y2;
		SDL_BlitSurface(pics[1], NULL, screen, &dst);
	}
	}
	else
	{
		SDL_BlitSurface(pics[0], NULL, screen, &dst);
		SDL_BlitSurface(pics[1], NULL, screen, &dst);
	}
}

void kommando::background()
{	
	float sinine = (sin(now/100.))*(cos(now/200.));
	float cosine = (cos(now/500.)+2)*(now/10.);
	
	int simple_s = (int)((sin(now/300.) + 2)*888888);
	
	int color;
	
	Uint8 r, g, b;
	
	kerta++;
	
	Uint32 *p;
	
	for (int y = kerta%2; y < S_HEIGHT; y+=2)
	{
		p = ((Uint32*)screen->pixels) + ((screen->pitch * y) >> 2);
		for (int x = kerta%2; x < S_WIDTH; x+=2)
		{
			color =(int)((x*sinine)*(y+cosine)) * simple_s;
			
			p+=2;
			
			
			if (y > S_HEIGHT/2-S_HEIGHT/4 && y < S_HEIGHT/2 + S_HEIGHT/4)
			{
				SDL_GetRGB(color, screen->format, &r, &g, &b);
				color = SDL_MapRGB(screen->format, r/2, g/2, b/2);
				
				*p = color;
			}
			else
			{
				if (kerta%25 > 20 || kerta%3 == 1)
					*p = color;
			}
		}
	}
}

void kommando::colorize()
{
	Uint8 *p = (Uint8 *)pics[0]->pixels;
	
	int temp = SDL_MapRGB(pics[0]->format,255,255,255);
	
	int temp2 = now%256;
	
	for (int y = 0; y < pics[0]->h; y++)
		for (int x = 0; x < pics[0]->w;x++)
		{
			if ( *(Uint32 *)(p + (y) * pics[0]->pitch + ((x) << 2)) < temp)
				*(Uint32 *)(p + (y) * pics[0]->pitch + ((x) << 2)) = (x^y) * (temp2);
		}
}


void kommando::lataa_kuvat()
{
	for (int i = 0; i < PICS; i++)
	{
		pics[i] = IMG_Load(kuvat[i]);
		
	
	}
	SDL_SetColorKey(pics[0], SDL_SRCCOLORKEY, SDL_MapRGB(pics[0]->format,255,255,255));
	SDL_SetColorKey(pics[1], SDL_SRCCOLORKEY, SDL_MapRGB(pics[0]->format,255,255,255));
	
	SDL_SetColorKey(pics[2], SDL_SRCCOLORKEY, SDL_MapRGB(pics[2]->format,255,255,255));
	SDL_SetColorKey(pics[3], SDL_SRCCOLORKEY, SDL_MapRGB(pics[3]->format,255,255,255));
	SDL_SetColorKey(pics[4], SDL_SRCCOLORKEY, SDL_MapRGB(pics[4]->format,255,255,255));
	SDL_SetColorKey(pics[5], SDL_SRCCOLORKEY, SDL_MapRGB(pics[5]->format,255,255,255));
	
	SDL_SetColorKey(pics[6], SDL_SRCCOLORKEY, SDL_MapRGB(pics[6]->format,255,255,128));
	SDL_SetColorKey(pics[7], SDL_SRCCOLORKEY, SDL_MapRGB(pics[7]->format,255,255,128));
	SDL_SetColorKey(pics[8], SDL_SRCCOLORKEY, SDL_MapRGB(pics[8]->format,255,255,128));
	
	SDL_SetColorKey(pics[9], SDL_SRCCOLORKEY, SDL_MapRGB(pics[9]->format,0,0,0));
	SDL_SetColorKey(pics[10], SDL_SRCCOLORKEY, SDL_MapRGB(pics[10]->format,0,255,255));
	
	for (int i = 0; i < SAMPLES; i++)
		music[i] = Mix_LoadWAV(samples[i]);
}


void kommando::audio()
{	
	beginning = SDL_GetTicks();
 	Mix_Volume(Mix_PlayChannel(-1, music[0], 0),MIX_MAX_VOLUME/5);
}
