home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TUT08NEW.ZIP / GFX1.CPP < prev    next >
Text File  |  1995-01-14  |  12KB  |  306 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //                                                                         //
  3. // GFX1.CPP - VGA Trainer Program secondary module containing graphics     //
  4. //            functions.  Note: This module does not follow a lot of good  //
  5. //            programming practices.  It was built to be used with the     //
  6. //            VGA tutorial series.  If you are planning on using this      //
  7. //            module with a different source file, some modifications may  //
  8. //            be necessary.                                                //
  9. //                                                                         //
  10. // Author        : Grant Smith (Denthor) - denthor@beastie.cs.und.ac.za    //
  11. // Translator    : Christopher G. Mann   - r3cgm@dax.cc.uakron.edu         //
  12. //                                                                         //
  13. // Last Modified : January 13, 1995                                        //
  14. //                                                                         //
  15. /////////////////////////////////////////////////////////////////////////////
  16.  
  17. //               //
  18. // INCLUDE FILES //
  19. //               //
  20.  
  21.   #include <dos.h>
  22.                            // geninterrupt()
  23.   #include <math.h>
  24.                            // abs()
  25.  
  26. //         //
  27. // DEFINES //
  28. //         //
  29.  
  30.   #if !defined(PI)
  31.     #define PI 3.1415927
  32.   #endif
  33.  
  34.   #if !defined(VGA)
  35.     #define VGA 0xA000
  36.   #endif
  37.  
  38. //          //
  39. // TYPEDEFS //
  40. //          //
  41.  
  42.   typedef unsigned char byte;
  43.   typedef unsigned int  word;
  44.  
  45. //                     //
  46. // FUNCTION PROTOTYPES //
  47. //                     //
  48.  
  49.   // MODE SETTING FUNCTIONS
  50.   void  SetMCGA     ();
  51.   void  SetText     ();
  52.  
  53.   // PALLETTE FUNCTIONS
  54.   void  Pal         (byte Col, byte  R, byte  G, byte  B);
  55.   void  GetPal      (byte Col, byte &R, byte &G, byte &B);
  56.  
  57.   // MATH-LIKE FUNCTIONS
  58.   float rad         (float theta);
  59.   int   sgn         (int a);
  60.  
  61.   // DRAWING FUNCTIONS
  62.   void  Putpixel    (word X, word Y, byte Col, word Where);
  63.   void  Line        (int a, int b, int c, int  d, int col, word Where);
  64.  
  65.   // VIDEO MEMORY FUNCTIONS
  66.   void  Cls         (byte Col, word Where);
  67.   void  Flip        (word source, word dest);
  68.  
  69.  
  70. //--------------------------MODE SETTING FUNCTIONS-------------------------//
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. //                                                                         //
  74. // SetMCGA() - This function gets you into 320x200x256 mode.               //
  75. //                                                                         //
  76. /////////////////////////////////////////////////////////////////////////////
  77.  
  78. void SetMCGA() {
  79.   _AX = 0x0013;
  80.   geninterrupt (0x10);
  81. }
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. //                                                                         //
  85. // SetText() - This function gets you into text mode.                      //
  86. //                                                                         //
  87. /////////////////////////////////////////////////////////////////////////////
  88.  
  89. void SetText() {
  90.   _AX = 0x0003;
  91.   geninterrupt (0x10);
  92. }
  93.  
  94.  
  95. //----------------------------PALLETTE FUNCTIONS---------------------------//
  96.  
  97. /////////////////////////////////////////////////////////////////////////////
  98. //                                                                         //
  99. // Pal() - This sets the Red, Green, and Blue values of a certain color.   //
  100. //                                                                         //
  101. /////////////////////////////////////////////////////////////////////////////
  102.  
  103. void Pal (byte Col, byte  R, byte  G, byte  B) {
  104.   asm {
  105.     mov     dx, 0x3C8    // load DX with 3C8 (write pallette function)
  106.     mov     al, [Col]    // move color to AL
  107.     out     dx, al       // write DX to the VGA (tell VGA that we want to
  108.                          //   work with the color in AL
  109.     inc     dx           // load DX with 3C9 (write RGB colors)
  110.     mov     al, [R]      // move Red   to AL
  111.     out     dx, al       // write DX to VGA (tell VGA that we want to use
  112.                          //   the Red value in AL
  113.     mov     al, [G]      // move Green to AL
  114.     out     dx, al       // write DX to VGA
  115.     mov     al, [B]      // move Blue  to AL
  116.     out     dx, al       // write DX to VGA
  117.   }
  118. }
  119.  
  120. /////////////////////////////////////////////////////////////////////////////
  121. //                                                                         //
  122. // GetPal() - This reads the values of the Red, Green, and Blue values of  //
  123. //            a certain color.  This function uses pass-by-reference.      //
  124. //                                                                         //
  125. /////////////////////////////////////////////////////////////////////////////
  126.  
  127. void GetPal (byte Col, byte &R, byte &G, byte &B) {
  128.  
  129.   byte rr,gg,bb;
  130.  
  131.   asm {
  132.     mov     dx, 0x03C7   // load DX with 3C7 (read pallette function)
  133.     mov     al, [Col]    // move color to AL
  134.     out     dx, al       // write DX to the VGA (tell VGA that we want to
  135.                          //   work with the color in AL
  136.     add     dx, 2        // load DX with 3C9 (read RGB colors)
  137.     in      al, dx       // read Red   to AL
  138.     mov     [rr],al      // copy AL to rr
  139.     in      al, dx       // read Green to AL
  140.     mov     [gg],al      // copy AL to gg
  141.     in      al, dx       // read Blue  to AL
  142.     mov     [bb],al      // copy AL to bb
  143.   }
  144.  
  145.   R = rr;
  146.   G = gg;
  147.   B = bb;
  148.  
  149. }
  150.  
  151.  
  152. //----------------------------MATH-LIKE FUNCTIONS--------------------------//
  153.  
  154. /////////////////////////////////////////////////////////////////////////////
  155. //                                                                         //
  156. // rad() - This calculates the degrees of an angle.                        //
  157. //                                                                         //
  158. /////////////////////////////////////////////////////////////////////////////
  159.  
  160. float rad(float theta) {
  161.   return ((theta * PI)/180);
  162. }
  163.  
  164. /////////////////////////////////////////////////////////////////////////////
  165. //                                                                         //
  166. // sgn() - This checks the sign of an integer and returns a 1, -1, or 0.   //
  167. //                                                                         //
  168. /////////////////////////////////////////////////////////////////////////////
  169.  
  170. int sgn (int a) {
  171.  
  172.   if (a > 0)  return +1;
  173.   if (a < 0)  return -1;
  174.   return 0;
  175. }
  176.  
  177.  
  178. //-----------------------------DRAWING FUNCTIONS---------------------------//
  179.  
  180. /////////////////////////////////////////////////////////////////////////////
  181. //                                                                         //
  182. // Putpixel() - This puts a pixel on the screen by writing directly to     //
  183. //              memory.                                                    //
  184. //                                                                         //
  185. /////////////////////////////////////////////////////////////////////////////
  186.  
  187. void Putpixel (word X, word Y, byte Col, word Where) {
  188.   asm {
  189.     push    ds           // save DS
  190.     push    es           // save ES
  191.     mov     ax, [Where]  // move segment of Where to AX
  192.     mov     es, ax       // set ES to segment of Where
  193.     mov     bx, [X]      // set BX to X
  194.     mov     dx, [Y]      // set DX to Y
  195.     push    bx           // save BX (our X value)
  196.     mov     bx, dx       // now BX and DX are equal to Y
  197.     mov     dh, dl       // copy DL to DH (multiply Y by 256)
  198.     xor     dl, dl       // zero out DL
  199.     shl     bx, 6        // shift BX left 6 places (multiply Y by 64).
  200.     add     dx, bx       // add BX to DX (Y*64 + Y*256 = Y*320)
  201.     pop     bx           // restore BX (X coordinate)
  202.     add     bx, dx       // add BX to DX (Y*320 + X).  this gives you
  203.                          //   the offset in memory you want
  204.     mov     di, bx       // move the offset to DI
  205.     xor     al, al       // zero out AL
  206.     mov     ah, [Col]    // move value of Col into AH
  207.     mov     es:[di], ah  // move Col to the offset in memory (DI)
  208.     pop     es           // restore ES
  209.     pop     ds           // restore DS
  210.   }
  211. }
  212.  
  213. /////////////////////////////////////////////////////////////////////////////
  214. //                                                                         //
  215. // Line() - This draws a line from a,b to c,d of color col on screne Where //
  216. //                                                                         //
  217. /////////////////////////////////////////////////////////////////////////////
  218.  
  219. void Line(int a, int b, int c, int d, int col, word Where) {
  220.  
  221.   int i,u,s,v,d1x,d1y,d2x,d2y,m,n;
  222.  
  223.   u   = c-a;      // x2-x1
  224.   v   = d-b;      // y2-y1
  225.   d1x = sgn(u);   // d1x is the sign of u (x2-x1) (VALUE -1,0,1)
  226.   d1y = sgn(v);   // d1y is the sign of v (y2-y1) (VALUE -1,0,1)
  227.   d2x = sgn(u);   // d2x is the sign of u (x2-x1) (VALUE -1,0,1)
  228.   d2y = 0;
  229.   m   = abs(u);   // m is the distance between x1 and x2
  230.   n   = abs(v);   // n is the distance between y1 and y2
  231.  
  232.   if (m<=n) {     // if the x distance is greater than the y distance
  233.     d2x = 0;
  234.     d2y = sgn(v); // d2y is the sign of v (x2-x1) (VALUE -1,0,1)
  235.     m   = abs(v); // m is the distance between y1 and y2
  236.     n   = abs(u); // n is the distance between x1 and x2
  237.   }
  238.  
  239.   s = m / 2; // s is the m distance (either x or y) divided by 2
  240.  
  241.   for (i=0;i<m+1;i++) { // repeat this loop until it
  242.                  // is = to m (y or x distance)
  243.     Putpixel(a,b,col,Where); // plot a pixel at the original x1, y1
  244.     s += n;                  // add n (dis of x or y) to s (dis of x of y)
  245.     if (s >= m) {            // if s is >= m (distance between y1 and y2)
  246.       s -= m;
  247.       a += d1x;
  248.       b += d1y;
  249.     }
  250.     else {
  251.       a += d2x;
  252.       b += d2y;
  253.     }
  254.   }
  255.  
  256. }
  257.  
  258.  
  259. //--------------------------VIDEO MEMORY FUNCTIONS-------------------------//
  260.  
  261. /////////////////////////////////////////////////////////////////////////////
  262. //                                                                         //
  263. // Cls() - This clears the screen at location Where to color Col           //
  264. //                                                                         //
  265. /////////////////////////////////////////////////////////////////////////////
  266.  
  267. void Cls(byte Col, word Where) {
  268.   asm {
  269.     push    es           // save ES
  270.     mov     cx, 32000    // this is our loop counter.  we want to clear
  271.                          //   64000 bytes of memory, so why do we use 32000?
  272.                          //   1 word = 2 bytes, and we are moving a word at
  273.                          //   a time
  274.     mov     es, [Where]  // move address in Where to ES
  275.     xor     di, di       // zero out DI
  276.     mov     al, [Col]    // move color to AL
  277.     mov     ah, al       // move color to AH (Remember, will be moving
  278.                          //   a WORDS, so we need two copies
  279.     rep     stosw        // copy AX to Where and drecrement CX by 1
  280.                          //   until CX equals 0
  281.     pop     es           // restore ES
  282.   }
  283. }
  284.  
  285. /////////////////////////////////////////////////////////////////////////////
  286. //                                                                         //
  287. // Flip() - This copies the entire screen at "source" to destination.      //
  288. //                                                                         //
  289. /////////////////////////////////////////////////////////////////////////////
  290.  
  291. void Flip(word source, word dest) {
  292.   asm {
  293.     push    ds           // save DS
  294.     mov     ax, [dest]   // copy segment of destination to AX
  295.     mov     es, ax       // set ES to point to destination
  296.     mov     ax, [source] // copy segment of source to AX
  297.     mov     ds, ax       // set DS to point to source
  298.     xor     si, si       // zero out SI
  299.     xor     di, di       // zero out DI
  300.     mov     cx, 32000    // set our counter to 32000
  301.     rep     movsw        // move source to destination by words.  decrement
  302.                          //   CX by 1 each time until CX is 0
  303.     pop     ds           // restore DS
  304.   }
  305. }
  306.