///+ Prefix linkto basiccode1
//// Lines for set up Psychlops environment
#include <psychlops.h>
using namespace Psychlops;
///- Prefix linkto basiccode1


///+ Main Routine
//// Psychlops runs at the first line of this function psychlops_main().
void psychlops_main() {
    ///+ 0 Declaration linkto basiccode4_1a
    ////Prepare global parameters
     Canvas window(Canvas::window);
     Psychlops::Image img; //Prepare "Image" class variable to make offscreen movie.
     Psychlops::Rectangle centerrect(10,10);
     int imageHsize = 100;  //Width for offscreeen.
     int imageVsize = 100;  //Height for offscreeen.
	double R,G,B;
    ///- 0 Declaration linkto basiccode4_1a
     double env;
     Psychlops::Rectangle figurerect(imageHsize, imageVsize);


     ///+ 1 Initialize
     ////Initialize
     img.set(imageHsize, imageVsize); //Allocate offscreen in main memory.
     double _i, _j, sd;//variables to axis conversion and Gaussian SD.
     for(int i=0; i  < imageHsize; i++){
         for(int j=0; j  < imageVsize; j++){
         ///+ 1.1 set1
          ////set colors for each pixels
          _i = i-imageHsize*0.5; //get position in image center coordinates
          _j = j-imageVsize*0.5; //get position in image center coordinates
          sd = imageVsize/6.0/*ID:sigma 3.0 8.0 1.0*/; //calculate SD for gaussian
          env=exp(-(_i*_i+_j*_j)/(2.0*(sd*sd)) ); //calculate 2D gaussian
          
          R=random(1.0) * 0.5/*ID:RGain 0.0 1.0 0.5*/; //Set R values. 
          G=random(1.0) * 0.5/*ID:GGain 0.0 1.0 0.5*/; //Set G values.
          B=random(1.0) * 0.5/*ID:BGain 0.0 1.0 0.5*/; //Set B values. 
          
          img.pix(i,j, Color(env*R, env*G, env*B));
         ///- 1.1 set1
        
         }
    }

    img.cache(); //Move offscreen from main memory to video RAM. (Optional)
    img.centering(); //centering the position to copy offscreen
    img.shift(100,100); //centering the position to copy offscreen
    centerrect.centering();
    figurerect.centering().shift(100,100);
    ///- 1 Initialize

    ///+ 2 drawing
    ////drawing offscreen

    while(!Keyboard::esc.pushed()){
       ///+ 2.1 linkto basiccode4_1a
        //// copy offscreen image 
        window.clear(Color::black); //Clear screen with black       
        img.draw(); // copy offscreen onto the reverse side of window buffer.
        ///- 2.1 linkto basiccode4_1a
        centerrect.draw(Color::red);// draw a reference rectangle at the center.        
        if(Keyboard::spc.pressed())figurerect.draw(Color(1.0,0.25,0.25, 0.2/*ID:figurerectalpha 0.0 1.0 0.2*/)); //Draw a rectangle with alpha color.
        window.flip();
    }
    ///- 2 drawing

}
///- Main Routine

./4_1b.xap