home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************/
- /* tcrodent.c */
- /* */
- /* Demo program to show how to define a mouse graphics */
- /* cursor. */
- /* */
- /* Written in Borland TurboC version 2.0 */
- /* */
- /* Hardware req'ts : CGA, EGA, or VGA */
- /* : Microsoft-compatible mouse */
- /* */
- /*********************************************************/
-
- #include <dos.h>
- #include <stdio.h>
- #include <graphics.h>
-
- /* Define an hourglass-shaped mouse graphics cursor */
- unsigned mcursor[2][16] =
- {
- /* Screen mask; this is ANDed with screen */
- {
- 0x0001, /* 0000000000000001 */
- 0x0001, /* 0000000000000001 */
- 0x8003, /* 1000000000000011 */
- 0xc7c7, /* 1100011111000111 */
- 0xe38f, /* 1110001110001111 */
- 0xf11f, /* 1111000100011111 */
- 0xf83f, /* 1111100000111111 */
- 0xfc7f, /* 1111110001111111 */
- 0xf83f, /* 1111100000111111 */
- 0xf11f, /* 1111000100011111 */
- 0xe38f, /* 1110001110001111 */
- 0xc7c7, /* 1100011111000111 */
- 0x8003, /* 1000000000000011 */
- 0x0001, /* 0000000000000001 */
- 0x0001, /* 0000000000000001 */
- 0x0000, /* 0000000000000000 */
- },
- /* Cursor mask; this is XORed with screen */
- {
- 0x0000, /* 0000000000000000 */
- 0x7ffc, /* 0111111111111100 */
- 0x2008, /* 0010000000001000 */
- 0x1010, /* 0001000000010000 */
- 0x0820, /* 0000100000100000 */
- 0x0440, /* 0000010001000000 */
- 0x0280, /* 0000001010000000 */
- 0x0100, /* 0000000100000000 */
- 0x0280, /* 0000001010000000 */
- 0x0440, /* 0000010001000000 */
- 0x0820, /* 0000100000100000 */
- 0x1010, /* 0001000000010000 */
- 0x2008, /* 0010000000001000 */
- 0x7ffc, /* 0111111111111100 */
- 0x0000, /* 0000000000000000 */
- 0x0000 /* 0000000000000000 */
- }
- };
-
- union REGS regs; /* 80x86 processor registers */
- struct SREGS sregs; /* segment registers */
-
- /* function prototypes */
- void msgetstatus(int *, int *, int *); /* get cursor loc & buttons */
- void mshidecur(void); /* hide mouse cursor */
- int msinit(void); /* initialize mouse driver, return # of buttons */
- void mssetcur(void); /* set mouse cursor shape */
- void msshowcur(void); /* show (unhide) mouse cursor */
- void showbutton(int, int);
-
- main()
- {
- int buttons; /* # of mouse buttons */
- int mouse_x, mouse_y, mouse_button; /* Mouse loc & button info */
- int old_x, old_y, old_button;
- int rightbutton;
- int graphdriver = CGA;
- int graphmode = CGAHI;
- int gresult;
- char txtbuf[4];
-
- /* Set video to CGA 640x200 BW mode */
- initgraph(&graphdriver, &graphmode, "");
- if (gresult = graphresult())
- {
- puts("This program requires a CGA or other color adapter!");
- printf("Error = %d\n", gresult);
- exit(1);
- }
- /* msinit() return button count; 0 = no mouse */
- if (!(buttons = msinit()))
- {
- puts("No mouse detected.");
- exit(1);
- }
- if (buttons == 3) /* determine where right button will be shown */
- rightbutton = 41;
- else
- rightbutton = 34;
- outtextxy(0, 0, "╔════╗ ┌───┐ ┌───┐");
- outtextxy(0, 8, "║QUIT║ x = xxx y = yyy │ │ │ │");
- outtextxy(0,16, "╚════╝ └───┘ └───┘");
- if (buttons == 3)
- {
- outtextxy(rightbutton*8 - 8, 0, "┌───┐");
- outtextxy(rightbutton*8 - 8, 8, "│ │");
- outtextxy(rightbutton*8 - 8, 16, "└───┘");
- }
- mssetcur(); /* Set mouse cursor to mcursor */
- msshowcur(); /* Show mouse cursor */
- old_x = old_y = old_button = 0;
-
- do /* main program loop */
- {
- msgetstatus(&mouse_x, &mouse_y, &mouse_button);
- if (mouse_x != old_x)
- {
- sprintf(txtbuf, "%3d", mouse_x);
- old_x = mouse_x;
- setfillstyle(EMPTY_FILL, 3);
- bar(12*8, 8, 14*8+7, 15);
- outtextxy(12*8, 8, txtbuf);
- }
- if (mouse_y != old_y)
- {
- sprintf(txtbuf, "%3d", mouse_y);
- old_y = mouse_y;
- setfillstyle(EMPTY_FILL, 3);
- bar(21*8, 8, 23*8+7, 15);
- outtextxy(21*8, 8, txtbuf);
- }
- showbutton(27, mouse_button & 1);
- showbutton(rightbutton, mouse_button & 2);
- if (buttons == 3)
- showbutton(34, mouse_button & 4);
- } while (mouse_button != 1 || mouse_x > 48 || mouse_y > 24);
- mshidecur();
- closegraph(); /* reset screen */
- } /* end main */
-
- /**********************************************************************/
- /* get cursor location and button status */
- void msgetstatus(int *ms_x, int *ms_y, int *ms_b)
- {
- regs.x.ax = 3; /* function 3 = get status */
- int86(0x33, ®s, ®s);
- *ms_x = regs.x.cx; /* horizontal cursor location */
- *ms_y = regs.x.dx; /* vertical cursor location */
- *ms_b = regs.x.bx; /* button info: 1 = left, 2 = right, 4 = center */
- /* note that the button bits are independent of each other; */
- /* ie., if the left and right buttons are both pressed the */
- /* returned button value is 3. */
- } /* end msgetstatus */
-
- /**********************************************************************/
- /* hide mouse cursor */
- void mshidecur(void)
- {
- regs.x.ax = 2; /* function 2 */
- int86(0x33, ®s, ®s);
- } /* end mshidecur */
-
- /**********************************************************************/
- /* initialize mouse driver & return number of buttons */
- /* Uses int 33h function 0. If mouse detected, the interrupt returns */
- /* a non-zero value in AX */
- int msinit(void)
- {
- regs.x.ax = 0; /* function 0 is init driver */
- if (int86(0x33, ®s, ®s))
- return(regs.x.bx);
- else
- return(0);
- } /* end msinit */
-
- /**********************************************************************/
- /* set up new cursor image via function 9. Bit mask for new cursor */
- /* is in ES:DX, cursor hot spot is in BX and CX */
- void mssetcur(void)
- {
- segread(&sregs); /* get segment registers */
- sregs.ds = sregs.es; /* set ES to data segment */
- regs.x.ax = 9; /* function 9 */
- regs.x.bx = 7; /* set horiz. hot spot to midpoint of hourglass */
- regs.x.cx = 7; /* likewise for vertical hot spot */
- regs.x.dx = (unsigned)mcursor; /* ptr to mask */
- int86x(0x33, ®s, ®s, &sregs); /* do it */
- } /* end mssetcur */
-
- /**********************************************************************/
- /* show mouse cursor. Since function 0 (initialize) leaves the */
- /* cursor hidden we have to explicitly turn it on. */
- void msshowcur(void)
- {
- regs.x.ax = 1; /* function 1 */
- int86(0x33, ®s, ®s);
- } /* end msshowcur */
-
- /**********************************************************************/
- /* Light or blank a block to show button press */
- void showbutton(int loc, int condition)
- {
- if(condition) /* if the button is pressed */
- setfillstyle(SOLID_FILL, 3);
- else
- setfillstyle(EMPTY_FILL, 3);
- bar(loc*8+3, 8, (loc+2)*8+3, 15);
- }