#include "./sdl/SDL.h"


void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel);

int main( int argc, char* args[] )
{
    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );
    
	SDL_Surface * screen = SDL_SetVideoMode(500, 400, 32, SDL_SWSURFACE );
	SDL_Event event;
	Uint32 yellow = SDL_MapRGB(screen->format, 0xff, 0xff, 0x00); ;

	bool quit = false;

	while(!quit)
	{
		SDL_Delay(15);
		while( SDL_PollEvent( &event ) )
        {
            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
            //If a key was pressed
            if( event.type == SDL_KEYDOWN )
            {  
                //Set the proper message surface
                switch( event.key.keysym.sym )
                {
                    case SDLK_UP:
                    case SDLK_DOWN:
                    case SDLK_LEFT:
                    case SDLK_RIGHT:
						SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
						break;
                }
			}
            //If a key was released
            if( event.type == SDL_KEYUP )
            {  
                //Set the proper message surface
                switch( event.key.keysym.sym )
                {
                    case SDLK_UP:
                    case SDLK_DOWN:
                    case SDLK_LEFT:
                    case SDLK_RIGHT:
						SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );
						break;
                }
			}
			//If a mouse button was pressed
			if( event.type == SDL_MOUSEBUTTONDOWN )
			{
				//If the left mouse button was pressed
				if( event.button.button == SDL_BUTTON_LEFT )
				{
					//Get the mouse offsets
					int x = event.button.x;
					int y = event.button.y;
					putpixel(screen,x,y,yellow);
				}
			}
        }


		SDL_Flip( screen );
	}
	SDL_FreeSurface( screen );
    //Quit SDL
    SDL_Quit();
    
    return 0;    
}

void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel) 
{ 
 int bpp = surface->format->BytesPerPixel; 
 Uint32 *p = (Uint32 *)surface->pixels + y * surface->pitch + x * bpp; 
 *p = pixel; 
} 