// proj.cpp : définit le point d'entrée pour l'application console.
//
#include "stdafx.h"
#include "sdl.h"
#include "bloc.cpp"
#include "tableau.cpp"
#include "forme.cpp"

#include <time.h>

#include "SDL_text.c"


ETAT e =CONTINU;
SDL_Surface *ecran;

SDL_Rect d;
SDL_Surface *image;
tableau t(211,55,290,464);
forme f(211,55,"0000000000000000",234<4);
int numero_forme;
int numero_couleur;

SDL_Event even;
SDL_TimerID timer;
rect		Zone;
color		Top, Back;


char forme_phrase[5][17]={

"0000011001100000",
"0000000011110000",
"0000000001110001",
"0000000011100000",
"0000011100100010"
};

Uint32 tab_col[6]={(228<<8)+233,(228<<8)+(233<<4),16776960,16762880,13789470,139};
void put_pixel(int x, int y,SDL_Surface *image,Uint32 pixel)
{
int bpp = image->format->BytesPerPixel;
	Uint8 *p = (Uint8 *)image->pixels + y * image->pitch + x * bpp;
	if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
        p[0] = (pixel >> 16) & 0xff;
        p[1] = (pixel >> 8) & 0xff;
        p[2] = pixel & 0xff;
    } else {
        p[0] = pixel & 0xff;
        p[1] = (pixel >> 8) & 0xff;
        p[2] = (pixel >> 16) & 0xff;
    }

}

Uint32 my_callbackfunc(Uint32 ,void * );

Uint32 my_callbackfunc(Uint32 intervalle,void *parametre)
{
	f.Descendre(&d,image,ecran,t,&e);
	if(e==STOP_FORME)
		printf("je suis la \n");
	return intervalle;
}


void INIT_JEU()
{
	SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
	ecran = SDL_SetVideoMode( 526,562,  32, SDL_SWSURFACE );
	timer = SDL_AddTimer(800, my_callbackfunc, &e);
	srand( time(NULL) );
	
	image = SDL_LoadBMP( "board.bmp" );
	int i=0;
	for(i=0;i<10;i++)
	{	
		put_pixel(210-i, 171,image,0);
		put_pixel(210-i, 172,image,0);
	}
	SDL_BlitSurface( image, NULL, ecran, NULL );
	SDL_UpdateRect( ecran, 0, 0, 0, 0 );
	
	d.x=250;
	d.y=0;
	d.w=29;
	d.h=29;
	image = SDL_LoadBMP( "P.bmp" );

}
int main(int argc, char * argv[])
{
	numero_forme =int((double(rand())/RAND_MAX)*4);
	numero_couleur =int((double(rand())/RAND_MAX)*5);

	f.Set_color(tab_col[numero_couleur]);
	f.Set_forme_Pos(211,55);
	f.Set_forme(forme_phrase[numero_forme]);
	

	INIT_JEU();

/*	
	Zone.l = 100;
	Zone.h = 100;
	

	Zone.x = 160;
	Zone.y = 100;
	Top.r = 0;
	Top.g = 0;
	Top.b = 0;
	Back.r = -1;
	Back.g = -1;
	Back.b = -1;
	SDLText(ecran, "Limite", Zone, 2, Top, Back);

		
	if(SDL_MUSTLOCK(ecran))
		SDL_UnlockSurface(ecran);
	SDL_Flip(ecran);

	*/
		


	f.Put_forme(&d,image,ecran);
	t.ajout_bloc(0,1,1900299,&d,image,ecran);
	t.ajout_bloc(9,1,1900299,&d,image,ecran);

	
	
	while(1)
	{
			
		SDL_WaitEvent(&even);
		switch(even.key.keysym.sym )
		{		
			case SDL_USEREVENT:
					break;
			case SDLK_RIGHT: 
				if(even.key.type==SDL_KEYDOWN)
				{
					f.Bouge_droite(&d,image,ecran,t);
				}	
				break;
			case SDLK_LEFT: 
				if(even.key.type==SDL_KEYDOWN)
				{
					f.Bouge_gauche(&d,image,ecran,t);
				}
				break;
				
			case SDLK_UP:
				if(even.key.type==SDL_KEYDOWN)
				{
				f.Rotation_forme(&d,image,ecran,t);
			
			
				}
				break;
			default : break;
		}
		
	}
	
	return 0;
	
}

