// Example 'usetiles'
// Tommy Grandefors
// 2D32 Graphic library
// Beta v1.0
// February 1997

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

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

#define TRANSPARENCY_OFF	0

void UserText(void);

void main(void)
{
   UserText();

   Graphix myGfx; //Create the main object.
   Palette thePalette; //We must have a palette for the picture.

   if(myGfx.SetVideoMode(VGA320x200x256) < 0)
   {
      cout << "Can't initiate video mode" << endl;
      return;
   }
   
   Bitmap *myTiles;
   myTiles = new Bitmap[40]; //Allocate memory for 40 tiled bitmaps.

   //Load a picture containing 40 tiles with the width and height of 40 pixels each and we don't want them to be transparent.
   if( myGfx.LoadPCXTile("tiles.pcx", myTiles, thePalette, 40, 40, 40, TRANSPARENCY_OFF, 0) == -1)
   {
      myGfx.SetVideoMode(TEXT);
      cout << "Can't load the file 'tiles.pcx'" << endl;
      return;
   }
   
   myGfx.SetPalette(thePalette); //Set palette associated with the tiles.

   int x = 0;
   int y = 0;
   int i;

   //Draw all tiles one by one on the screen.
   for(i = 0; i < 40; i++)
   {
      myGfx.DrawBitmap(x, y, myTiles[i]);
      x += 40;
      if(x >= 320)
      {
	 x = 0;
	 y += 40;
      }
   }

   myGfx.Print(0,100, "All 40 tiles are drawn in correct order.");
   myGfx.Print(0,110, "Press a key to reverse the order of all");
   myGfx.Print(0,120, "tiles!");

   getch(); //Wait until a key has been pressed.

   x = y = 0;
   //Draw all tiles one by one on the screen again but in reversed order.   
   for(i = 39;i >= 0;i--)
   {
      myGfx.DrawBitmap(x, y, myTiles[i]);
      x += 40;
      if(x >= 320)
      {
	 x = 0;
	 y += 40;
      }
   }

   myGfx.Print(0,100, "Now it looks much better. Press a key");
   myGfx.Print(0,110, "to quit!");

   getch();
   
   delete [] myTiles; //Free the memory in heap.

   myGfx.SetVideoMode(TEXT); //Go back to text mode.
}

void UserText(void)
{
   cout << "This example will show you how to handle fixed sized tiles." << endl;
   cout << "We will load 40 tiles from the file 'tiles.pcx' and store them in a bitmap\nvector and then draw each tile on screen." << endl;
   cout << "Press a key to continue!" << endl;

   getch();
}
