home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PASCAL / RODENT_3.ZIP / TCRODENT.C < prev    next >
Text File  |  1989-12-16  |  8KB  |  210 lines

  1. /*********************************************************/
  2. /*                    tcrodent.c                         */
  3. /*                                                       */
  4. /*  Demo program to show how to define a mouse graphics  */
  5. /*  cursor.                                              */
  6. /*                                                       */
  7. /*  Written in Borland TurboC version 2.0                */
  8. /*                                                       */
  9. /*  Hardware req'ts : CGA, EGA, or VGA                   */
  10. /*                  : Microsoft-compatible mouse         */
  11. /*                                                       */
  12. /*********************************************************/
  13.  
  14. #include <dos.h>
  15. #include <stdio.h>
  16. #include <graphics.h>
  17.  
  18. /* Define an hourglass-shaped mouse graphics cursor */
  19. unsigned mcursor[2][16] =
  20.   {
  21.     /* Screen mask; this is ANDed with screen */
  22.     {
  23.       0x0001,     /* 0000000000000001 */
  24.       0x0001,     /* 0000000000000001 */
  25.       0x8003,     /* 1000000000000011 */
  26.       0xc7c7,     /* 1100011111000111 */
  27.       0xe38f,     /* 1110001110001111 */
  28.       0xf11f,     /* 1111000100011111 */
  29.       0xf83f,     /* 1111100000111111 */
  30.       0xfc7f,     /* 1111110001111111 */
  31.       0xf83f,     /* 1111100000111111 */
  32.       0xf11f,     /* 1111000100011111 */
  33.       0xe38f,     /* 1110001110001111 */
  34.       0xc7c7,     /* 1100011111000111 */
  35.       0x8003,     /* 1000000000000011 */
  36.       0x0001,     /* 0000000000000001 */
  37.       0x0001,     /* 0000000000000001 */
  38.       0x0000,     /* 0000000000000000 */
  39.     },
  40.     /* Cursor mask; this is XORed with screen */
  41.     {
  42.       0x0000,     /* 0000000000000000 */
  43.       0x7ffc,     /* 0111111111111100 */
  44.       0x2008,     /* 0010000000001000 */
  45.       0x1010,     /* 0001000000010000 */
  46.       0x0820,     /* 0000100000100000 */
  47.       0x0440,     /* 0000010001000000 */
  48.       0x0280,     /* 0000001010000000 */
  49.       0x0100,     /* 0000000100000000 */
  50.       0x0280,     /* 0000001010000000 */
  51.       0x0440,     /* 0000010001000000 */
  52.       0x0820,     /* 0000100000100000 */
  53.       0x1010,     /* 0001000000010000 */
  54.       0x2008,     /* 0010000000001000 */
  55.       0x7ffc,     /* 0111111111111100 */
  56.       0x0000,     /* 0000000000000000 */
  57.       0x0000      /* 0000000000000000 */
  58.     }
  59.   };
  60.  
  61. union  REGS  regs;   /* 80x86 processor registers */
  62. struct SREGS sregs;  /* segment registers */
  63.  
  64. /* function prototypes */
  65. void msgetstatus(int *, int *, int *); /* get cursor loc & buttons */
  66. void mshidecur(void); /* hide mouse cursor */
  67. int  msinit(void);    /* initialize mouse driver, return # of buttons */
  68. void mssetcur(void);  /* set mouse cursor shape */
  69. void msshowcur(void); /* show (unhide) mouse cursor */
  70. void showbutton(int, int);
  71.  
  72. main()
  73. {
  74.   int buttons;                        /* # of mouse buttons      */
  75.   int mouse_x, mouse_y, mouse_button; /* Mouse loc & button info */
  76.   int old_x, old_y, old_button;
  77.   int rightbutton;
  78.   int graphdriver = CGA;
  79.   int graphmode = CGAHI;
  80.   int gresult;
  81.   char txtbuf[4];
  82.  
  83.   /* Set video to CGA 640x200 BW mode */
  84.   initgraph(&graphdriver, &graphmode, "");
  85.   if (gresult = graphresult())
  86.   {
  87.     puts("This program requires a CGA or other color adapter!");
  88.     printf("Error = %d\n", gresult);
  89.     exit(1);
  90.   }
  91.   /*  msinit() return button count; 0 = no mouse */
  92.   if (!(buttons = msinit()))
  93.   {
  94.     puts("No mouse detected.");
  95.     exit(1);
  96.   }
  97.   if (buttons == 3)  /* determine where right button will be shown */
  98.     rightbutton = 41;
  99.   else
  100.     rightbutton = 34;
  101.   outtextxy(0, 0, "╔════╗                    ┌───┐  ┌───┐");
  102.   outtextxy(0, 8, "║QUIT║  x = xxx  y = yyy  │   │  │   │");
  103.   outtextxy(0,16, "╚════╝                    └───┘  └───┘");
  104.   if (buttons == 3)
  105.   {
  106.     outtextxy(rightbutton*8 - 8,  0, "┌───┐");
  107.     outtextxy(rightbutton*8 - 8,  8, "│   │");
  108.     outtextxy(rightbutton*8 - 8, 16, "└───┘");
  109.   }
  110.   mssetcur();   /* Set mouse cursor to mcursor */
  111.   msshowcur();  /* Show mouse cursor */
  112.   old_x = old_y = old_button = 0;
  113.  
  114.   do  /* main program loop */
  115.   {
  116.     msgetstatus(&mouse_x, &mouse_y, &mouse_button);
  117.     if (mouse_x != old_x)
  118.     {
  119.       sprintf(txtbuf, "%3d", mouse_x);
  120.       old_x = mouse_x;
  121.       setfillstyle(EMPTY_FILL, 3);
  122.       bar(12*8, 8, 14*8+7, 15);
  123.       outtextxy(12*8, 8, txtbuf);
  124.     }
  125.     if (mouse_y != old_y)
  126.     {
  127.       sprintf(txtbuf, "%3d", mouse_y);
  128.       old_y = mouse_y;
  129.       setfillstyle(EMPTY_FILL, 3);
  130.       bar(21*8, 8, 23*8+7, 15);
  131.       outtextxy(21*8, 8, txtbuf);
  132.     }
  133.     showbutton(27, mouse_button & 1);
  134.     showbutton(rightbutton, mouse_button & 2);
  135.     if (buttons == 3)
  136.       showbutton(34, mouse_button & 4);
  137.   } while (mouse_button != 1 || mouse_x > 48 || mouse_y > 24);
  138.   mshidecur();
  139.   closegraph();  /* reset screen */
  140. }  /* end main */
  141.  
  142. /**********************************************************************/
  143. /* get cursor location and button status                              */
  144. void msgetstatus(int *ms_x, int *ms_y, int *ms_b)
  145. {
  146.   regs.x.ax = 3;  /* function 3 = get status */
  147.   int86(0x33, ®s, ®s);
  148.   *ms_x = regs.x.cx; /* horizontal cursor location */
  149.   *ms_y = regs.x.dx; /* vertical cursor location */
  150.   *ms_b = regs.x.bx; /* button info: 1 = left, 2 = right, 4 = center */
  151.        /* note that the button bits are independent of each other;   */
  152.        /* ie., if the left and right buttons are both pressed the    */
  153.        /* returned button value is 3.                                */
  154. } /* end msgetstatus */
  155.  
  156. /**********************************************************************/
  157. /* hide mouse cursor                                                  */
  158. void mshidecur(void)
  159. {
  160.   regs.x.ax = 2;  /* function 2 */
  161.   int86(0x33, ®s, ®s);
  162. } /* end mshidecur */
  163.  
  164. /**********************************************************************/
  165. /* initialize mouse driver & return number of buttons                 */
  166. /* Uses int 33h function 0.  If mouse detected, the interrupt returns */
  167. /* a non-zero value in AX                                             */
  168. int msinit(void)
  169. {
  170.   regs.x.ax = 0;  /* function 0 is init driver */
  171.   if (int86(0x33, ®s, ®s))
  172.     return(regs.x.bx);
  173.   else
  174.     return(0);
  175. } /* end msinit */
  176.  
  177. /**********************************************************************/
  178. /* set up new cursor image via function 9.  Bit mask for new cursor   */
  179. /* is in ES:DX, cursor hot spot is in BX and CX                       */
  180. void mssetcur(void)
  181. {
  182.   segread(&sregs);  /* get segment registers */
  183.   sregs.ds = sregs.es; /* set ES to data segment */
  184.   regs.x.ax = 9;  /* function 9 */
  185.   regs.x.bx = 7;  /* set horiz. hot spot to midpoint of hourglass */
  186.   regs.x.cx = 7;  /* likewise for vertical hot spot */
  187.   regs.x.dx = (unsigned)mcursor; /* ptr to mask */
  188.   int86x(0x33, ®s, ®s, &sregs); /* do it */
  189. } /* end mssetcur */
  190.  
  191. /**********************************************************************/
  192. /* show mouse cursor.  Since function 0 (initialize) leaves the       */
  193. /* cursor hidden we have to explicitly turn it on.                    */
  194. void msshowcur(void)
  195. {
  196.   regs.x.ax = 1;  /* function 1 */
  197.   int86(0x33, ®s, ®s);
  198. } /* end msshowcur */
  199.  
  200. /**********************************************************************/
  201. /* Light or blank a block to show button press                        */
  202. void showbutton(int loc, int condition)
  203. {
  204.   if(condition)  /* if the button is pressed */
  205.     setfillstyle(SOLID_FILL, 3);
  206.   else
  207.     setfillstyle(EMPTY_FILL, 3);
  208.   bar(loc*8+3, 8, (loc+2)*8+3, 15);
  209. }
  210.