
#include <iostream>
#include <math.h>

using namespace std;


#include "SDL/SDL.h"
#include "SDL/SDL_audio.h"

#include "../lib/gfxMR.hpp"
#include "../lib/sdlMR.hpp"

void create_tone( void *userdata, Uint8 *stream, int len ) 
{
  int i = 0,
      j = 0;
  while (i < len)
  {
    if (j < 50)
    {
      j++;
      *(stream+i) = 16;
    }
    else if (j < 100)
    {
      j++;
      *(stream+i) = 8;
    }
    else
    {
      j = 0;
      *(stream + i) = 16;
    }
    i++;
  }
}

int main(int argc, char * argv[])
{
    SDL_Surface *screen;
    SDL_Event event;

    //extern void mixaudio(void *unused, Uint8 *stream, int len);
    SDL_AudioSpec fmt;
    fmt.freq = 22050;
    fmt.format = AUDIO_U8;
    fmt.channels = 1;
    fmt.samples = 1024;        /* A good value for games */
    fmt.callback = create_tone;
    fmt.userdata = NULL;
    SDL_OpenAudio(&fmt, NULL);

    int keypress = 0;
    int mx = 0,
        my = 0;

    SDL_Init(SDL_INIT_EVERYTHING);
    screen = SDL_SetVideoMode(512, 256, 8, SDL_SWSURFACE);

    sdlFPS fps(25);
    sdlKEY key;

    sdlPutPixel plot(screen);
    sdlGetPixel get(screen);
    ploter * pp = &plot;
    bresenhamCircle cercle(plot);
    bresenhamLine ligne(plot);
    floodFill paint(plot,get);


    //SDL_PauseAudio(0);
    while(!keypress)
    {
        fps();
        SDL_Flip(screen);
        SDL_FillRect(screen,0,0);
        while(SDL_PollEvent(&event))
        {
        	switch (event.type)
        	{
        		case SDL_QUIT:
        			keypress = 1;
        			break;
        		case SDL_KEYDOWN:
        			//keypress = 1;
        			key(event.key.keysym.sym, true);
        			break;
        		case SDL_KEYUP:
        			//keypress = 1;
        		  key(event.key.keysym.sym, false);
        			break;
        		case SDL_MOUSEMOTION:
        			mx = event.motion.x;
        			my = event.motion.y;
        			break;
        		case SDL_MOUSEBUTTONDOWN:
        			break;
        	}
        }
        
        cercle(mx, my, 3, 5);
        
        for (int i=0;i<10;i++)
        {
          for (int j=0;j<4;j++)
          {
            if (key.map[i][j])
            {
              SDL_Rect touch;
              touch.x = i*32;
              touch.y = j*32;
              touch.w = 31;
              touch.h = 31;
              SDL_FillRect(screen,&touch,2);
            }
          }
        }
    }

    SDL_PauseAudio(1);
    SDL_CloseAudio();
    SDL_Quit();
    return 0;
}
