///+ 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);
     const int imagenumber = 128; // To prepare the image array, the array number should be "const".
     Psychlops::Image img[128]; //Prepare "Image" class variable to make offscreen movie.
     Psychlops::Rectangle centerrect(10,10);
     int imageHsize = 100;  //Width for offscreeen.
     int imageVsize = 60;  //Height for offscreeen.

     double R,G,B;
    ///- 0 Declaration linkto BasicCode4_1a



     ///+ 1 Initialize
     ////Initialize image array
     centerrect.centering();

     for (int k=0; k<imagenumber; k++){
         img[k].set(imageHsize, imageVsize); //Allocate offscreen in main memory.
         ///+ 1.1 set1  linkto BasicCode4_1a
         for(int i=0; i <imageHsize; i++){
           for(int j=0; j < imageVsize; j++){
          	////set colors for each pixels
            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[k].pix(i,j, Color(R,G,B));
           }
         }
         ///- 1.1 set1  linkto BasicCode4_1a

        img[k].cache(); //Move offscreen from main memory to video RAM. (Optional)
        img[k].centering().shift(100,100);//set the position to copy offscreen
    }
    ///- 1 Initialize

    ///+ 2 drawing
    ////copy offscreen onto main frame buffer
	int frame=0;
    while(!Keyboard::esc.pushed()){
		frame++;
		frame = frame % imagenumber; //A frame count should not exceed the frame number of offscreen movies
        window.clear(Color::black); //Clear screen with black
        img[frame].draw(); // copy offscreen onto the reverse side of window buffer.
        centerrect.draw(Color::red);// draw reference rectangle at the center.
        window.flip();
    }
    ///- 2 drawing

}
///- Main Routine

./4_2a.xap