#include <math.h>
#include <SDL/SDL.h>


Uint32 yellow;
Uint32 red;
Uint32 green;
Uint32 blue;
Uint32 white;
Uint32 brown;
Uint32 grey;

SDL_Event event;
struct noidZ_mice {
	float x;
	float y;
	int b;
};
	struct noidZ_mice souris_verte;
	struct noidZ_mice souris_brune;
struct sincos {
	float sinus;
	float cosinus;
};
	struct sincos kossin;

//=================================================================================================
//=================================================================================================
//=================================================================================================
//=================================================================================================

void noidZ_Init(SDL_Surface *surface)
{
	yellow = SDL_MapRGB(surface->format, 0xff, 0xff, 0x00);
	red = SDL_MapRGB(surface->format, 0xff, 0x00, 0x00);
	blue = SDL_MapRGB(surface->format, 0x00, 0x00, 0xff);
	green = SDL_MapRGB(surface->format, 0x00, 0xff, 0x00);
	white = SDL_MapRGB(surface->format, 0xff, 0xff, 0xff);
	brown = SDL_MapRGB(surface->format, 0xcc, 0x99, 0x00);
	grey = SDL_MapRGB(surface->format, 0xcc, 0xcc, 0xcc);
}

int noidZ_Loop(void) 
{
	int finit = 0;
	while ( SDL_PollEvent(&event) ) {
		switch ( event.type ) {
			case SDL_QUIT:
				finit = 1;
				break;
			case SDL_MOUSEMOTION:
				souris_verte.x = (float)(event.motion.x);
				souris_verte.y = (float)(event.motion.y);
				break;
			case SDL_MOUSEBUTTONDOWN:
				souris_verte.b = 1;
				break;
			case SDL_MOUSEBUTTONUP:
				souris_verte.b = 0;
				break;
		}
	}
	return finit;
}

//=================================================================================================
//=================================================================================================
//=================================================================================================
//=================================================================================================

struct noidZ_mice noidZ_Got_Mouse(SDL_Surface *surface)
{
	souris_brune.x = (float)(((float)event.motion.x / (surface->w/2))-1);
	souris_brune.y = (float)(-(((float)event.motion.y / (surface->h/2))-1));
	if (event.button.state == SDL_PRESSED)
	{	souris_brune.b = 1;	}
	if (event.button.state == SDL_RELEASED)
	{	souris_brune.b = 0;	}
	return souris_brune;
}

float noidZ_Got_Time(void)
{
	Uint32 milliseconde;
	milliseconde = SDL_GetTicks();
	float seconde;
	seconde = ((float)milliseconde) / 1000;
	return seconde;
}

//=================================================================================================
//=================================================================================================
//=================================================================================================
//=================================================================================================
void noidZ_Refresh(SDL_Surface *surface)
{
		SDL_UpdateRect(surface, 0, 0, 0, 0);
		SDL_FillRect(surface,NULL,0x00ffffff);
}

Uint32 noidZ_Got_Pixel(SDL_Surface *surface, float w, float h)
{
if ((w <= 1)&&(w >= -1)&&(h <= 1)&&(h >= -1))
    {
	int x = (int)(((1+w)) * (surface->w/2));
	int y = (int)((1-(h+1)) * (surface->h/2));
    int bpp = surface->format->BytesPerPixel;
    Uint8 *offzet = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
    switch(bpp) {
	    case 1:
        	return *offzet;
	    case 2:
	        return *(Uint16 *)offzet;
	    case 3:
	        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
	            return offzet[0] << 16 | offzet[1] << 8 | offzet[2];
	        else
	            return offzet[0] | offzet[1] << 8 | offzet[2] << 16;
	    case 4:
	        return *(Uint32 *)offzet;
	    default:
	        return 0;
	    }
	}
return 0;
}

void noidZ_Giv_Pixel(SDL_Surface *surface, float w, float h,Uint32 color)
{
if ((w <= 1)&&(w >= -1)&&(h <= 1)&&(h >= -1))
    {
	int x = (int)(((1+w)/2) * surface->w);
	int y = (int)(((1-h)/2) * surface->h);
    int bpp = surface->format->BytesPerPixel;
    Uint8 *offzet = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
    switch(bpp) {
	    case 1:
	        *offzet = color;
	        break;
	    case 2:
        	*(Uint16 *)offzet = color;
	        break;
	    case 3:
	        if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
	            offzet[0] = (color >> 16) & 0xff;
	            offzet[1] = (color >> 8) & 0xff;
	            offzet[2] = color & 0xff;
	        } else {
	            offzet[0] = color & 0xff;
	            offzet[1] = (color >> 8) & 0xff;
	            offzet[2] = (color >> 16) & 0xff;
	        }
	        break;
	    case 4:
	        *(Uint32 *)offzet = color;
	        break;
	    }
	}
}

void brescircle(SDL_Surface *surface,float radius,float xcenter,float ycenter,Uint32 color) 
{
  float xstart = 0;
  float ystart = radius;
  float sum = 0.03-2*radius;
  while(xstart <= ystart)
  {
                    SDL_LockSurface(surface);
                    noidZ_Giv_Pixel(surface,xstart+xcenter,ystart+ycenter,color);
                    noidZ_Giv_Pixel(surface,-xstart+xcenter,ystart+ycenter,color);
                    noidZ_Giv_Pixel(surface,xstart+xcenter,-ystart+ycenter,color);
                    noidZ_Giv_Pixel(surface,-xstart+xcenter,-ystart+ycenter,color);
                    noidZ_Giv_Pixel(surface,ystart+xcenter,xstart+ycenter,color);
                    noidZ_Giv_Pixel(surface,-ystart+xcenter,xstart+ycenter,color);
                    noidZ_Giv_Pixel(surface,ystart+xcenter,-xstart+ycenter,color);
                    noidZ_Giv_Pixel(surface,-ystart+xcenter,-xstart+ycenter,color);
                    SDL_UnlockSurface(surface);
      //set_eight(xstart,ystart,xcenter,ycenter);
      if(sum <= 0)
      {
        sum = sum + 4*xstart + 0.06;
        }
      else
      {
          sum = sum + 4*(xstart-ystart) + 0.10;
          ystart = ystart - 0.003;
      }
      xstart = xstart + 0.003;
  }
}

void bresline(SDL_Surface *surface,float lx1,float lx2, float ly1, float ly2,Uint32 color)
{
     float deltax,deltay,lx,ly,xinc1,xinc2,yinc1,yinc2,den,num,numadd,numpixels,curpixel;
     deltax = fabs(lx2 - lx1);        // The difference between the x's
     deltay = fabs(ly2 - ly1);        // The difference between the y's
     lx = lx1;                       // Start x off at the first pixel
     ly = ly1;                       // Start y off at the first pixel

     if (lx2 >= lx1)                 // The x-values are increasing
     {
           xinc1 = 0.003;
           xinc2 = 0.003;
     } else {
           xinc1 = -0.003;
           xinc2 = -0.003;
     }

     if (ly2 >= ly1)                 // The y-values are increasing
     {
           yinc1 = 0.003;
           yinc2 = 0.003;
     } else {
           yinc1 = -0.003;
           yinc2 = -0.003;
     }

     if (deltax >= deltay)         // There is at least one x-value for every y-value
     {
           xinc1 = 0;                  // Don't change the x when numerator >= denominator
           yinc2 = 0;                  // Don't change the y for every iteration
           den = deltax;
           num = deltax / 2;
           numadd = deltay;
           numpixels = deltax;         // There are more x-values than y-values
     } else {
           xinc2 = 0;                  // Don't change the x for every iteration
           yinc1 = 0;                  // Don't change the y when numerator >= denominator
           den = deltay;
           num = deltay / 2;
           numadd = deltax;
           numpixels = deltay;         // There are more y-values than x-values
     }
           SDL_LockSurface(surface);
     for (curpixel = 0; curpixel <= numpixels; curpixel = curpixel + 0.003)
     {
           noidZ_Giv_Pixel(surface,lx,ly,color);
           num += numadd;              // Increase the numerator by the top of the fraction
           if (num >= den)             // Check if numerator >= denominator
           {
                  num -= den;               // Calculate the new numerator value
                  lx += xinc1;               // Change the x as appropriate
                  ly += yinc1;               // Change the y as appropriate
           }
           lx += xinc2;                 // Change the x as appropriate
           ly += yinc2;                 // Change the y as appropriate
     }
           SDL_UnlockSurface(surface);
}
//=================================================================================================
//=================================================================================================
//=================================================================================================
//=================================================================================================
