
#include "IOTools.h"

#include "ct_windows.h"
#include "ct_graphics.h"
#include "ct_mouse.h"
#include "ct_delay.h"
#include "ct_text.h"

#include <stdlib.h>
#include <time.h>     // for random numbers

void RandomStuff();
void House(int, int);
void Spiral( int num_turns );

int main(){

    WindowPair();


    // Spiral( 5 ); 
    PaintCircle( 200, 200, 20 ); 

    for( int i=0; i< 1000; i++)
        SetColorPixel( 200 + 50*cos( i*3.14 / 180.0 ),
                       200 + 50*sin( i*3.14 / 180.0 ),
                       255, 0, 0 );


    Delay ( 60 );  // 1 sec
    SetForeColor( 0, 0, 255 );
    PaintCircle( 200, 200, 20 );  // blue
    int r, g, b;
    GetColorPixel( 190, 210, r, g, b );
    cout << r << endl;
    cout << g << endl; 
    cout << b << endl;

    // draw some houses
    SetForeColor( 255, 0, 0 );
    House( 100, 100 );
    SetForeColor( 0, 255, 0 );
    House( 300, 100 );
    SetForeColor( 0, 0, 255 );
    House( 100, 300 );
    SetForeColor( 0, 255, 255 );
    House( 300, 300 );
  
    MoveTo( 200, 200 );
    ShowString( "Hello worlds") ;

    cout << "\nTesting GetMouse and GetColorPixel:\n"; 

    int x, y;
    
    // BUG or feature : the first click is needed to make the graphics window active,
    //                  because the console grabs focus by default when the program starts

    for( int j = 0; j <5; j++ ){ 
        while( ! Button() )         // do this while mouse is up
            GetMouse( x, y );
        GetColorPixel( x, y, r, g, b );
        cout << "( " << x << " " << y << " ) : " ; 
        cout << r << " ";
        cout << g << " "; 
        cout << b << endl ;
        while( Button() ) ;        // wait until it's back up  
    }

    Spiral( 5 ); 

    PressReturn( ); 
    
        
    RandomStuff();
    
    
    PressReturn("Whoa!");
   
    return 0;
}

void Spiral( int num_turns ){   // makes approx num_turns on 400x400 screen

    double delta = (num_turns * 6.28) / 10000.0 ;
    for( int i=0; i<10000; i++){
        SetForeColor( i % 256 , 0, i % 256 );
        // cout << i  << " ";
        Delay( 2 );
        PaintCircle( 200 + 200*( i / 10000.0 ) * cos( delta * i),
                     200 + 200*( i / 10000.0 ) * sin( delta * i),
                     50/num_turns );
    }
}

void House( int x, int y ){
     
    MoveTo(x, y);
    Line( 0, -40 );
    Line( 30, -20 );
    Line( 30, 20 );
    Line( 0, 40 ); 
    Line( -60, 0 );

    PaintRect( x + 20, y - 20, x + 30, y - 35 );
}


void RandomStuff(){
	   int i;
   for( i=0; i<10; i++) 
       cout << "hello!\n";

   FrameCircle( 100, 100, 100 );
   
   SetForeColor( 255, 0, 0 );

   FrameCircle( 300, 300, 100 );
   PaintCircle( 300, 300, 50 );


   SetForeColor( 0, 0, 255 );

   PaintCircle( 300, 100, 100 );
   FrameCircle( 300, 100, 50 );

   int x ;

   SetForeColor( 0, 255, 255 );

   while ( cin >> x ){
     if ( x==57 ) break;
     cout << x << endl; // << flush;

     FrameCircle( 100, 100, x );
  
   }   
  
   ClearDrawing();

   srand( (unsigned)time( NULL ) );  // set seed
   for( int j= 0; j<10; j++ ){
       SetForeColor( rand() % 255, rand() % 255, rand() % 255 );
       FrameRect(  rand() % 400, rand() % 400, rand() % 400 , rand() % 400 );
   }

   cin.ignore();
   PressReturn("Whoa!");
   
   ClearDrawing();

   MoveTo( rand() % 400, rand() % 400 );

   for(int k=0; k<20; k++){
      SetForeColor( rand() % 255, rand() % 255, rand() % 255 );
      LineTo( rand() % 400, rand() % 400 );
   }
}

