home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TUT04NEW.ZIP / TUT4.CPP < prev    next >
C/C++ Source or Header  |  1994-12-23  |  12KB  |  301 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //                                                                         //
  3. // TUTPROG4.CPP - VGA Trainer Program 4 (in Turbo C++ 3.0)                 //
  4. //                                                                         //
  5. // "The VGA Trainer Program" is written by Denthor of Asphyxia. However it //
  6. // was limited to Pascal only in its first run.  All I have done is taken  //
  7. // his original release, translated it to C++ and touched up a few things. //
  8. // I take absolutely no credit for the concepts presented in this code and //
  9. // am NOT the person to ask for help if you are having trouble.            //
  10. //                                                                         //
  11. // Program Notes : This program implements virtual screens, a great way    //
  12. //                 to update your screen.                                  //
  13. //                                                                         //
  14. //                 For this particular program, I have found the compiler  //
  15. //                 option -mc (Compact memory model) to work better than   //
  16. //                 -ml (Large memory model).  However, you must use -mc or //
  17. //                 greater.                                                //
  18. //                 Also, you might want to go under "Options...Debugger"   //
  19. //                 and increase your programs Heap size to >64k.  I don't  //
  20. //                 know if <64k will lock your system, but I had problems. //
  21. //                                                                         //
  22. // Author        : Grant Smith (Denthor) - denthor@beastie.cs.und.ac.za    //
  23. // Translator    : Christopher G. Mann   - r3cgm@dax.cc.uakron.edu         //
  24. //                                                                         //
  25. // Last Modified : December 23, 1994                                       //
  26. //                                                                         //
  27. /////////////////////////////////////////////////////////////////////////////
  28.  
  29. //               //
  30. // INCLUDE FILES //
  31. //               //
  32.  
  33.   #include <conio.h>
  34.                // clrscr(), getch(), kbhit()
  35.   #include <dos.h>
  36.                // MK_FP, geninterrupt()
  37.   #include <iostream.h>
  38.                // _fmemset(), cout, memset(), _fmemcpy()
  39.   #include <stdlib.h>
  40.                // calloc(), exit(), free()
  41.  
  42. //                     //
  43. // FUNCTION PROTOTYPES //
  44. //                     //
  45.  
  46.   // MODE SETTING FUNCTIONS
  47.   void SetMCGA();
  48.   void SetText();
  49.  
  50.   // VIRTUAL SCREEN FUNCTIONS
  51.   void SetUpVirtual();
  52.   void ShutDown();
  53.   void Flip();
  54.  
  55.   // UTILITY FUNCTIONS
  56.   void Cls(unsigned char Col, unsigned char *Where);
  57.   void Putpixel (int x, int y, unsigned char Col, unsigned int Where);
  58.   void WaitRetrace();
  59.  
  60.   // MID-LEVEL FUNCTIONS
  61.   void BlockMove();
  62.   void PatternDraw();
  63.  
  64. //                              //
  65. // GLOBAL VARIABLE DECLARATIONS //
  66. //                              //
  67.  
  68.   // declare a pointer to the offset of the Virtual Screen
  69.   unsigned char *vaddr = NULL;
  70.  
  71.   // declare a pointer to the offset of the VGA memory
  72.   unsigned char *vga = (unsigned char *) MK_FP(0xA000, 0);
  73.  
  74. ///////////////////////////////////////////////////////////////////////////////
  75. //                                                                           //
  76. //                                MAIN FUNCTION                              //
  77. //                                                                           //
  78. ///////////////////////////////////////////////////////////////////////////////
  79.  
  80. void main() {
  81.  
  82.   clrscr();
  83.   cout
  84.     << "This program will demonstrate the power of virtual screens.\n"
  85.     << "A block will firstly move across the screen, being drawn and\n"
  86.     << "erased totally on the VGA. Then the same block will move\n"
  87.     << "across, but will be drawn on the virtual screen and flipped\n"
  88.     << "to the VGA screen without a retrace (see part 2). The the\n"
  89.     << "block will go again, with flipping and a retrace.\n\n"
  90.     << "I will then draw a pattern, flip it to VGA, draw another\n"
  91.     << "pattern, flip it to VGA, and repeat that until a key is pressed.\n"
  92.     << "This will demonstrate that even when I put down 10000 pixels,\n"
  93.     << "then flip them to the VGA, it is still relatively fast.\n\n";
  94.   cout << "Hit any key to continue ...";
  95.   getch();
  96.   SetMCGA();
  97.   SetUpVirtual();
  98.   Cls(0,vaddr);    // After you have got the memory for the virtual screen,
  99.            // it is usually filled with random garbage. It is always
  100.            // wise to clear the virtual screen directly afterwards
  101.   BlockMove();
  102.  
  103.   do    PatternDraw();
  104.   while (!kbhit());
  105.   getch();         // getch() = clear keyboard buffer from kbhit()
  106.  
  107.   SetText();
  108.   ShutDown();
  109.   cout
  110.     << "All done. This concludes the fourth sample program in the ASPHYXIA\n"
  111.     << "Training series. You may reach DENTHOR under the name of GRANT\n"
  112.     << "SMITH on the MailBox BBS, or leave a message to ASPHYXIA on the\n"
  113.     << "ASPHYXIA BBS. Get the numbers from Roblist, or write to :\n"
  114.     << "             Grant Smith\n"
  115.     << "             P.O. Box 270\n"
  116.     << "             Kloof\n"
  117.     << "             3640\n"
  118.     << "I hope to hear from you soon!\n\n";
  119.   cout << "Hit any key to exit ...";
  120.   getch();
  121.  
  122. }
  123.  
  124.  
  125. /////////////////////////////////////////////////////////////////////////////
  126. //                                                                         //
  127. // SetMCGA() - This function gets you into 320x200x256 mode.               //
  128. //                                                                         //
  129. /////////////////////////////////////////////////////////////////////////////
  130.  
  131. void SetMCGA() {
  132.   _AX = 0x0013;
  133.   geninterrupt (0x10);
  134. }
  135.  
  136.  
  137. /////////////////////////////////////////////////////////////////////////////
  138. //                                                                         //
  139. // SetText() - This function gets you into text mode.                      //
  140. //                                                                         //
  141. /////////////////////////////////////////////////////////////////////////////
  142.  
  143. void SetText() {
  144.   _AX = 0x0003;
  145.   geninterrupt (0x10);
  146. }
  147.  
  148.  
  149. /////////////////////////////////////////////////////////////////////////////
  150. //                                                                         //
  151. // Cls() - This clears the screen to the specified color, on the VGA or on //
  152. //         the Virtual screen.                                             //
  153. //                                                                         //
  154. /////////////////////////////////////////////////////////////////////////////
  155.  
  156. void Cls(unsigned char Col, unsigned char *Where) {
  157.   _fmemset(Where, Col, 64000);
  158. }
  159.  
  160.  
  161. /////////////////////////////////////////////////////////////////////////////
  162. //                                                                         //
  163. // WaitRetrace() - This waits until you are in a Verticle Retrace.         //
  164. //                                                                         //
  165. /////////////////////////////////////////////////////////////////////////////
  166.  
  167. void WaitRetrace() {
  168.  
  169.   _DX = 0x03DA;
  170.  
  171.   l1: asm {
  172.     in  al,dx;
  173.     and al,0x08;
  174.     jnz l1;
  175.       }
  176.  
  177.   l2: asm {
  178.     in  al,dx;
  179.     and al,0x08;
  180.     jz  l2;
  181.       }
  182. }
  183.  
  184.  
  185. /////////////////////////////////////////////////////////////////////////////
  186. //                                                                         //
  187. // SetUpVirtual() - This sets up the memory needed for the virtual screen. //
  188. //                                                                         //
  189. /////////////////////////////////////////////////////////////////////////////
  190.  
  191. void SetUpVirtual() {
  192.  
  193.   vaddr = (unsigned char *) calloc(64000,1);
  194.   if (vaddr == NULL) {
  195.     SetText();
  196.     cout << "Not enough memory available, exiting program...";
  197.     exit(1);
  198.   }
  199. }
  200.  
  201.  
  202. /////////////////////////////////////////////////////////////////////////////
  203. //                                                                         //
  204. // ShutDown() - This frees the memory used by the virtual screen.          //
  205. //                                                                         //
  206. /////////////////////////////////////////////////////////////////////////////
  207.  
  208. void ShutDown() {
  209.   free(vaddr);
  210. }
  211.  
  212.  
  213. /////////////////////////////////////////////////////////////////////////////
  214. //                                                                         //
  215. // Putpixel() - This puts a pixel at X,Y using color Col, on VGA or the    //
  216. //              Virtual Screen;                                            //
  217. //                                                                         //
  218. /////////////////////////////////////////////////////////////////////////////
  219.  
  220. void Putpixel (int x, int y, unsigned char Col, unsigned char *Where) {
  221.   memset(Where+(x+(y*320)),Col,1);
  222. }
  223.  
  224.  
  225. /////////////////////////////////////////////////////////////////////////////
  226. //                                                                         //
  227. // Flip() - This flips the virtual screen to the VGA screen.               //
  228. //                                                                         //
  229. /////////////////////////////////////////////////////////////////////////////
  230.  
  231. void Flip() {
  232.   _fmemcpy(vga,vaddr,64000);
  233. }
  234.  
  235.  
  236. /////////////////////////////////////////////////////////////////////////////
  237. //                                                                         //
  238. // BlockMove() - This tests various ways of moving a block around the      //
  239. //               screen.                                                   //
  240. //                                                                         //
  241. /////////////////////////////////////////////////////////////////////////////
  242.  
  243. void BlockMove() {
  244.  
  245.   int loop1, loop2, loop3;
  246.  
  247.   // This draws a block directly to the VGA with no flipping
  248.   for (loop1=1; loop1<51; loop1++) {
  249.     for (loop2=1; loop2<51; loop2++)
  250.       for (loop3=1; loop3<51; loop3++)
  251.     Putpixel (loop1+loop2,loop3,32, vga);
  252.     Cls(0,vga);
  253.   }
  254.  
  255.   // This draws a block to the virtual screen, then flips it to the VGA
  256.   for (loop1=1; loop1<51; loop1++) {
  257.     for (loop2=1; loop2<51; loop2++)
  258.       for (loop3=1; loop3<51; loop3++)
  259.     Putpixel (loop1+loop2,loop3,32, vaddr);
  260.     Flip();
  261.     Cls(0,vaddr);
  262.   }
  263.  
  264.   // This draws to the virtual screen, waits for retrace, then flips to VGA
  265.   for (loop1=1; loop1<51; loop1++) {
  266.     for (loop2=1; loop2<51; loop2++)
  267.       for (loop3=1; loop3<51; loop3++)
  268.     Putpixel (loop1+loop2,loop3,32, vaddr);
  269.     WaitRetrace();
  270.     Flip();
  271.     Cls(0,vaddr);
  272.   }
  273.  
  274. }
  275.  
  276.  
  277. /////////////////////////////////////////////////////////////////////////////
  278. //                                                                         //
  279. // PatternDraw() - This tests the speed of flipping by drawing two         //
  280. //                 patterns and flipping them.                             //
  281. //                                                                         //
  282. /////////////////////////////////////////////////////////////////////////////
  283.  
  284. void PatternDraw() {
  285.  
  286.   int loop1, loop2;
  287.  
  288.   // This draws pattern #1 to the virtual screen, then flips it to VGA
  289.   for(loop1=1; loop1<101; loop1++)
  290.     for(loop2=1; loop2<101; loop2++)
  291.     Putpixel (loop1,loop2,loop1,vaddr);
  292.   Flip();
  293.  
  294.   // This draws pattern #2 to the virtual screen, then flips it to VGA
  295.   for(loop1=1; loop1<101; loop1++)
  296.     for(loop2=1; loop2<101; loop2++)
  297.     Putpixel (loop1,loop2,loop2,vaddr);
  298.   Flip();
  299.  
  300. }
  301.