home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / MPP400 / CGC_BGI.CPP next >
C/C++ Source or Header  |  1992-10-07  |  5KB  |  162 lines

  1. /* -------------------------------------------------------------------- */
  2. /* Mouse++ Version 4.0            cgc_bgi.cpp          Revised 10/05/92 */
  3. /*                                                                      */
  4. /* General mouse class for Turbo C++/Borland C++.                       */
  5. /* Copyright 1991, 1992 by Carl W. Moreland                             */
  6. /* -------------------------------------------------------------------- */
  7. /* This module demonstrates how to use the BGI to draw cursor images to */
  8. /* the screen. It is incomplete in two areas. First, it does not pro-   */
  9. /* perly mask the cursor image at the edges of the screen such that the */
  10. /* image is partially shown. Second, it assumes that the current view-  */
  11. /* port is the entire screen, which might not be true.                  */
  12. /* -------------------------------------------------------------------- */
  13.  
  14. #include <stdlib.h>
  15. #include <graphics.h>
  16. #include "mouse.h"
  17.  
  18. const int screenWidth  = 640;        // VGA/EGA = 640
  19. const int screenHeight = 350;           // VGA = 480, EGA = 350
  20. static int new_x, new_y;
  21.  
  22. int            ColorGraphicsCursor::x = -1;
  23. int            ColorGraphicsCursor::y = -1;
  24. void*          ColorGraphicsCursor::tmpImage = NULL;
  25. int            ColorGraphicsCursor::tmpImageSize = 0;
  26. unsigned char* ColorGraphicsCursor::videoAddress;
  27. int            ColorGraphicsCursor::videoLeftMask;
  28. int            ColorGraphicsCursor::videoRightMask;
  29. int            ColorGraphicsCursor::screenLeftMask;
  30. int            ColorGraphicsCursor::screenRightMask;
  31. unsigned char  ColorGraphicsCursor::xStart;
  32. unsigned char  ColorGraphicsCursor::yStart;
  33. unsigned char  ColorGraphicsCursor::xEnd;
  34. unsigned char  ColorGraphicsCursor::yEnd;
  35. unsigned char  ColorGraphicsCursor::iWidth;
  36. unsigned char  ColorGraphicsCursor::iHeight;
  37.  
  38.  
  39. ColorGraphicsCursor::ColorGraphicsCursor(unsigned char hotx,
  40.                                          unsigned char hoty,
  41.                                          unsigned char image[],
  42.                                          unsigned char width,
  43.                                          unsigned char height)
  44. {
  45.   register int i, j, k;
  46.   unsigned imageSize, nBytes;
  47.   unsigned char *plane0, *plane1, *plane2, *plane3, *plane4;
  48.   unsigned char *cursorImage, *cursorMask;
  49.  
  50.   this->hotx = hotx;
  51.   this->hoty = hoty;
  52.   cWidth     = width;
  53.   cHeight    = height;
  54.   cBytes     = width/8 + (width%8 ? 1 : 0);
  55.  
  56.   nBytes = cHeight * cBytes;
  57.  
  58.   imageSize = 4*cBytes*cHeight + 4;
  59.  
  60.   if(imageSize > tmpImageSize)        // resize the tmp image if
  61.   {                    //  necessary
  62.     if(tmpImage)
  63.       free(tmpImage);
  64.     tmpImage = malloc(imageSize);
  65.     tmpImageSize = imageSize;
  66.   }
  67.   cursorMask  = (unsigned char *)malloc(imageSize);
  68.   cursorImage = (unsigned char *)malloc(imageSize);
  69.   cImage = (void *)cursorImage;
  70.   cMask  = (void *)cursorMask;
  71.  
  72.   plane0 = image;
  73.   plane1 = plane0 + nBytes;
  74.   plane2 = plane1 + nBytes;
  75.   plane3 = plane2 + nBytes;
  76.   plane4 = plane3 + nBytes;
  77.  
  78.   cursorMask[0]  = cWidth-1;
  79.   cursorMask[1]  = 0;
  80.   cursorMask[2]  = cHeight-1;
  81.   cursorMask[3]  = 0;
  82.   cursorMask += 4;
  83.  
  84.   cursorImage[0] = cWidth-1;
  85.   cursorImage[1] = 0;
  86.   cursorImage[2] = cHeight-1;
  87.   cursorImage[3] = 0;
  88.   cursorImage += 4;
  89.  
  90.   for(i = 0; i < cHeight; i++)        // convert the mask to BGI format
  91.   {
  92.     for(j = 0; j < 4; j++)
  93.     {
  94.       for(k = 0; k < cBytes; k++)
  95.       {
  96.         *cursorMask = *plane0;
  97.         cursorMask++; plane0++;
  98.       }
  99.       plane0 -= cBytes;
  100.     }
  101.     plane0 += cBytes;
  102.   }
  103.  
  104.   for(i = 0; i < cHeight; i++)        // convert the cursor to BGI format
  105.   {
  106.     for(j = 0; j < cBytes; j++)
  107.     {
  108.       *cursorImage = *plane4;
  109.       cursorImage++; plane4++;
  110.     }
  111.     for(j = 0; j < cBytes; j++)
  112.     {
  113.       *cursorImage = *plane1;
  114.       cursorImage++; plane1++;
  115.     }
  116.     for(j = 0; j < cBytes; j++)
  117.     {
  118.       *cursorImage = *plane2;
  119.       cursorImage++; plane2++;
  120.     }
  121.     for(j = 0; j < cBytes; j++)
  122.     {
  123.       *cursorImage = *plane3;
  124.       cursorImage++; plane3++;
  125.     }
  126.   }
  127. }
  128.  
  129. void ColorGraphicsCursor::VideoRead(void)
  130. {
  131.   new_x = x - hotx;
  132.   xEnd  = (cBytes<<3) - 1;
  133.   new_y = y - hoty;
  134.   yEnd  = cHeight - 1;
  135.  
  136.   if(new_x < 0)
  137.     new_x = 0;
  138.  
  139.   if(new_y < 0)
  140.     new_y = 0;
  141.  
  142.   if(new_x+xEnd > screenWidth-1)
  143.     new_x = screenWidth - (cBytes<<3);
  144.  
  145.   if(new_y+yEnd > screenHeight-1)
  146.     new_y = screenHeight - cHeight;
  147.  
  148.   getimage(new_x, new_y, new_x+xEnd, new_y+yEnd, tmpImage);
  149. }
  150.  
  151. void ColorGraphicsCursor::VideoWrite(void)
  152. {
  153.   putimage(new_x, new_y, tmpImage, COPY_PUT);
  154. }
  155.  
  156. void ColorGraphicsCursor::CursorWrite(void)
  157. {
  158.   putimage(new_x, new_y, cMask,  AND_PUT);
  159.   putimage(new_x, new_y, cImage, XOR_PUT);
  160. }
  161.  
  162.