// Example 'pageflip'
// Tommy Grandefors
// 2D32 Graphic library
// Beta v1.0
// January 1997

#include <graphix.hh>
#include <videodef.hh>

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>

void UserText(void);

void main(void)
{
   UserText(); //Print out information text
   
   Graphix myGfx; //Instantiate the main object
   
   if(myGfx.SetVideoMode(VGA320x200x256) < 0)
   {
      cout<<"Can't initiate video mode"<<endl;
      return;
   }
   
   myGfx.ClearVideoMemory(0); //Fill videomemory (all pages) with color black.
   
   //Now we are going to use some of the graphic primitives contained
   //in this library to draw some simple patterns.
   //Here, I will also show how to perform page flipping with four
   //availabe pages in video memory.
   myGfx.SetDrawPage(0); //Set first draw page active. Every drawing
   //operation will only affect this page(for the moment at least).
   
   myGfx.Line(0,0,320,200,13); //A simple sloped line.
   
   myGfx.SetDrawPage(1); //Set second draw page active (next screen).
   
   int myCosyX = 666;
   int myCosyY = 666;
   
   for(int i=0;i<1000;i++) //Draw 1000 randomly positioned pixels
   {
      while((myCosyX >= 320) || (myCosyY >= 200)) //Within screen limits
      {
	 if(myCosyX >= 320)
	    myCosyX = rand(); //Get a random x position number
	 if(myCosyY >= 200)
	    myCosyY = rand(); //Get y pos.
      }
      myGfx.PutPixel(myCosyX,myCosyY,i); //Draw!
      myCosyX = myCosyY = 12345; //Are we learning yet?
   }
   
   myGfx.SetDrawPage(2); //Set next draw-page please.
   
   //Draw some rectangles
   for(int j=0;j<100;j++)
      myGfx.Box(j,j,j+10,j+10,j); //Cryptic parameters?
   
   myGfx.SetDrawPage(3); //Set last draw-page.
   
   for(int k=0;k<200;k++)
      myGfx.H_Line(200-k,k,k,k); //Fill right angled triangle.
   
   char key = 0;
   int page = 0;
   //Cycle through the four pages in video memory, until 'q' is pressed
   while(key != 'q')
   {
      myGfx.SetVisiblePage(page++);
      page = page & 3; //Same as: if page > 3 then page = 0
      key = getch(); //You know this one.
   }
   
   myGfx.SetVideoMode(TEXT); //Go back to text mode.
} //That's all folks!

void UserText(void)
{
   cout<<"This example will show you how the page flipping works in VGA 320x200 pixels."<<endl;
   cout<<"Some simple patterns will be displayed on the screen and you are able to"<<endl;
   cout<<"cycle through (flip) the 4 pages by pressing a key."<<endl;
   cout<<"To quit the program and return to DOS, press 'q'."<<endl;
   getch();
}
