home *** CD-ROM | disk | FTP | other *** search
- /*
- PROGRAM: PENCIL
- AUTHOR: MS PSS HW: Greg Lee
- DATE: 11/16/88
- DESCRIPTION: Example of a simple drawing program using calls to the
- mouse driver. This example demonstrates use of the
- following mouse functions:
- 0 - Mouse Reset and Status
- 1 - Show Cursor
- 2 - Hide Cursor
- 7 - Set Minimum and Maximum Horizontal Cursor Position
- 8 - Set Minimum and Maximum Vertical Cursor Position
- 9 - Set Graphics Cursor Block
- 20 - Swap Interrupt Subroutines
- External Routines: MASM routine to update mouse variables during a
- mouse interrupt specified by a condition mask.
-
- Microsoft C 5.1:
- masm /Ml m20sub;
- cl /AM pencil.c m20sub.obj -link mouse
-
- Microsoft QuickC:
- masm /Ml m20sub;
- Program List: pencil.c, m20sub.obj, mouse.lib
-
- NOTE: Program assumes mouse driver is installed
- */
-
- #include <stdio.h>
- #include <graph.h>
- #include <dos.h>
- #include <malloc.h>
-
- #define MouseInterrupt 0x33 /* Mouse interrupt */
- #define LeftButton 1 /* Left mouse button pressed */
- #define RightButton 2 /* Right mouse button pressed */
- #define ButtonChangeMask 30 /* Button condition bits set */
- #define CursorChangeMask 1 /* Cursor condition bits set */
- #define RightButtonReleaseMask 16 /* Right button release bit set */
- #define LeftButtonReleaseMask 4 /* Left button release bit set */
- #define PencilMenu 0 /* Pencil menu selected */
- #define EraserMenu 1 /* Eraser menu selected */
- #define Quit 2 /* Quit selected */
- #define InverseColor 0 /* Inverse color of menu */
- #define NormalColor 1 /* Normal color of menu */
- #define WHITE 7
- #define BLACK 0
- #define PencilCursor 0 /* Use pencil cursor */
- #define EraserCursor 1 /* Use eraser cursor */
- #define MouseCursor 2 /* Use mouse cursor */
-
- /* Mouse Function 20 Swap Interrupt Subroutine */
-
- unsigned int ButtonState; /* Mouse button state */
- unsigned int HorizCursCoord; /* Horizontal cursor coordinates */
- unsigned int VertCursCoord; /* Vertical cursor coordinates */
- unsigned int MouseConditionBits; /* Mouse condition bits set */
- extern void far NewMouseHardwareSub(); /* New mouse hardware routine */
- char far *graphicsbuffer; /* Graphics buffer to restore screen */
- int MouseEvent; /* Mouse Event result */
- int videomode; /* Video mode */
- /* Menu coordinates */
- int menuselector,xmax, ymax, pencilmenuborderxmin, pencilmenuborderxmax;
- int erasermenuborderxmin, erasermenuborderxmax, menuborderyaxis;
- int pencilmenuxmax, erasermenuxmin, erasermenuxmax, quitmenuxmin;
-
- main(argc,argv)
- int *argv[];
- {
- int m1, m2, m3, m4, viderror; /* Mouse parameters */
- struct videoconfig vioconfig; /* Graphics environment structure */
- if (argc == 2)
- {
- if ((char) * (argv[1]) == 'h') /* Hercules selection */
- {
-
- *((int far *)0x00400049L) = 6; /* Set Hercules page 0 */
-
- m1 = 0; /* Reset mouse */
- cmousem(&m1, &m2, &m3, &m4);
-
- if (m1 == 0) /* If mouse driver not installed */
- {
- printf("Microsoft Mouse not found");
- exit(-1);
- }
-
- viderror = _setvideomode(_HERCMONO); /* Set to Hercules mono */
-
- if (viderror == 0) /* Couldn't set video */
- {
- printf("Couldn't set video mode \n");
- exit(-1);
- }
- }
-
- else
-
- { /* Non-Hercules selection */
- videomode = atoi(argv[1]);
- viderror = _setvideomode(videomode); /* Set video mode */
-
- if (viderror == 0) /* Couldn't set video */
- {
- printf("Couldn't set video mode \n");
- exit(-1);
- }
-
- m1 = 0; /* Reset mouse */
- cmousem(&m1, &m2, &m3, &m4);
-
- if (m1 == 0) /* If mouse driver not installed */
- {
- printf("Microsoft Mouse not found");
- exit(-1);
- }
- }
- }
-
- else
- {
- printf(" USAGE: pencil <video mode selection> \n\n");
- printf(" Adapter Video mode selection \n" );
- printf(" ----------- --------------------\n");
- printf(" CGA 320x200 5\n" );
- printf(" CGA 640x200 6\n" );
- printf(" EGA 640x350 16\n");
- printf(" VGA 640x480 18\n");
- printf(" Herc 720x348 h (assuming MSHERC.COM loaded \n\n");
- printf(" For example: pencil 6\n");
- exit();
- }
-
- _getvideoconfig(&vioconfig) ; /* Get video environment variables */
- /* Graphics buffer for menu */
- xmax = vioconfig.numxpixels;
- ymax = vioconfig.numypixels;
- /* Menu coordinates */
- pencilmenuborderxmax = xmax / 3 ;
- erasermenuborderxmin = (xmax / 3) + 1;
- erasermenuborderxmax = (xmax / 3) * 2;
- menuborderyaxis = ymax / 10;
- pencilmenuxmax = (xmax / 3) - 1;
- erasermenuxmin = (xmax / 3) + 1;
- erasermenuxmax = ((xmax / 3) * 2) - 1;
- quitmenuxmin = ((xmax / 3) * 2) + 1;
-
- /* Allocate memory for buffer */
- graphicsbuffer = (char far *)malloc((unsigned int)_imagesize(1,1,xmax-1,menuborderyaxis));
-
- m1 = 7; /* Set horizontal limits */
- m3 = 2; /* Minimum */
- m4 = (xmax == 320) ? 635 : xmax - 5; /* Maximum */
- cmousem(&m1, &m2, &m3, &m4);
-
- m1 = 8; /* Set vertical limits */
- m3 = 2; /* Minimum */
- m4 = ymax - 3; /* Maximum */
- cmousem(&m1, &m2, &m3, &m4);
-
- DrawScreen(); /* Draw first screen */
- DrawCursor(MouseCursor); /* Draw mouse cursor */
-
- m1 = 1; /* Show cursor */
- cmousem(&m1, &m2, &m3, &m4);
- /* Run hardware routine if right button
- released */
- SetMouseCallMask(RightButtonReleaseMask);
- do
- { /* Check if right button released */
- if ((MouseEvent = (MouseConditionBits & RightButtonReleaseMask) )
- == RightButtonReleaseMask)
- {
- CallMenu(); /* Call menu routine */
- }
- } while(1);
- } /* End of main */
-
-
- /*----------------------------------------------------------------------*/
- DrawScreen() /* Draw a double border */
- {
- _setcolor(WHITE);
- _rectangle(_GBORDER,0,0,xmax - 1,ymax - 1);
- _rectangle(_GBORDER,2,2,xmax - 3,ymax - 3);
- _setcolor(BLACK);
- /* Clear drawing area */
- _rectangle(_GFILLINTERIOR,3,3,xmax - 4,ymax - 4);
- }
-
-
- /*----------------------------------------------------------------------*/
- void PopUpMenu(menuselect,attribute) /* Routine to pop up a menu */
- int menuselect,attribute; /* Select a menu and display
- with attribute */
- {
- int textcolor,fillcolor,xtextpos; /* Text color and fill color */
- /* X coordinate text position */
- fillcolor = (attribute == InverseColor) ? WHITE : BLACK ;
- textcolor = WHITE ;
- _setcolor(fillcolor);
- _settextcolor(textcolor);
- /* Select menu to display */
- switch(menuselect)
- {
- case PencilMenu: /* Pencil menu displayed */
- xtextpos = (xmax == 320) ? 5 : 11; /* In 320 x 200 mode ? */
- _rectangle(_GFILLINTERIOR, 1,1,pencilmenuxmax,(menuborderyaxis - 1));
- _settextposition(2,xtextpos);
- _outtext("Pencil");
- break;
-
- case EraserMenu: /* Eraser menu displayed */
- xtextpos = (xmax == 320) ? 18 : 37;
- _rectangle(_GFILLINTERIOR,erasermenuxmin,1,erasermenuxmax,
- (menuborderyaxis - 1));
- _settextposition(2,xtextpos);
- _outtext("Eraser");
- break;
-
- case Quit: /* Quit menu displayed */
- xtextpos = (xmax == 320) ? 32 : 65 ;
- _rectangle(_GFILLINTERIOR, quitmenuxmin,1,xmax - 2,menuborderyaxis - 1);
- _settextposition(2,xtextpos);
- _outtext("QUIT");
- break;
-
- default:
- xtextpos = (xmax == 320) ? 32 : 65 ;
- _rectangle(_GFILLINTERIOR, quitmenuxmin,1,xmax - 2,menuborderyaxis - 1);
- _settextposition(2,xtextpos);
- _outtext("QUIT");
-
- break;
- }
- } /* End of PopUpMenu */
-
-
- /*----------------------------------------------------------------------*/
- SetMouseCallMask(conditionmask) /* Setup hardware routine */
- int conditionmask; /* When to call hardware MASM
- routine NewMouseHardwareSub */
- {
- union REGS inregs, outregs; /* Declare int86x variables */
- struct SREGS segregs;
- void (far *Func_Addr) (); /* Far pointer to a function */
-
- Func_Addr = NewMouseHardwareSub; /* Point to new mouse hardware
- subroutine*/
- inregs.x.ax = 20; /* Swap Interrupt Routine */
- inregs.x.dx = FP_OFF(Func_Addr); /* Get offset */
- inregs.x.bx = FP_SEG(Func_Addr); /* Get segment */
- segregs.es = FP_SEG(Func_Addr); /* Get segment */
- inregs.x.cx = conditionmask; /* Set condition when to run routine */
- int86x(MouseInterrupt,&inregs,&outregs,&segregs);
- } /* End of SetMouseCallMask */
-
-
- /*----------------------------------------------------------------------*/
- CallMenu() /* Menu routine */
- { /* Mouse parameters, current menu
- selection and previous selection */
- int m1, m2, m3, m4, xcoord, menuselector,lastmenuselector;
- m1 = 2; /* Hide cursor */
- cmousem(&m1,&m2,&m3,&m4);
- SaveScreen(); /* Save the area under menu */
- lastmenuselector = -1; /* Initialize last menu selection */
- MouseConditionBits = 0; /* Initialize condition bits */
- DrawMenuBorders(); /* Draw menu border */
- PopUpMenu(PencilMenu,NormalColor); /* Draw pencil menu */
- PopUpMenu(EraserMenu,NormalColor); /* Draw eraser menu */
- PopUpMenu(Quit,NormalColor); /* Draw quit menu */
- /* Run hardware routine if a button
- is pressed or cursor is moved */
- SetMouseCallMask((ButtonChangeMask + CursorChangeMask));
- do
- {
- xcoord = (xmax == 320) ? HorizCursCoord/2 : HorizCursCoord;
- menuselector = ( xcoord / (xmax / 3)); /* Check horizontal position */
- /* Check if left button released */
- if ((MouseEvent = (MouseConditionBits & LeftButtonReleaseMask))
- == LeftButtonReleaseMask)
- {
- RestoreScreen(); /* Restore area under the menu */
- switch(menuselector) /* Run routine selected */
- {
- case PencilMenu:
- pencil(); /* Run pencil routine */
- return;
- break;
-
- case EraserMenu:
- eraser(); /* Run eraser routine */
- return;
- break;
-
- case Quit: /* Quit program */
- _setvideomode(_DEFAULTMODE); /* Reset video mode */
- m1 = 0; /* Reset mouse */
- cmousem(&m1,&m2,&m3,&m4);
- exit(); /* Quit */
- }
- }
-
- /* Move menu selection if horizontal position moved to next */
- if (menuselector != lastmenuselector)
- {
- /* Restore last selection */
- PopUpMenu(lastmenuselector,NormalColor);
- /* Draw new selection */
- PopUpMenu(menuselector,InverseColor);
- /* Reassign last selection */
- lastmenuselector = menuselector;
- }
- }
-
- while((MouseEvent = (MouseConditionBits & RightButtonReleaseMask))
- != RightButtonReleaseMask); /* While right button not released */
- MouseConditionBits = 0; /* Reset condition bits */
- RestoreScreen(); /* Restore area under menu */
- DrawCursor(MouseCursor); /* Draw mouse cursor */
- } /* End of CallMenu */
-
-
- /*----------------------------------------------------------------------*/
- DrawMenuBorders() /* Draw menu borders */
- {
- _setcolor(WHITE);
- _rectangle(_GBORDER, 0,0,pencilmenuborderxmax,menuborderyaxis);
- _rectangle(_GBORDER,erasermenuborderxmin,0,erasermenuborderxmax,
- menuborderyaxis);
- _rectangle(_GBORDER,quitmenuxmin,0,(xmax-1),menuborderyaxis);
- } /* End of DrawMenuBorders */
-
-
- /*----------------------------------------------------------------------*/
- pencil() /* Pencil drawing routine */
- {
- int m1,m2,m3,m4,xcoord; /* Mouse parameters */
-
- _setcliprgn(3,3,xmax - 4,ymax - 4); /* Set drawing area limit */
- _setcolor(WHITE); /* Set drawing color */
- DrawCursor(PencilCursor); /* Draw pencil cursor */
- MouseConditionBits = 0; /* Initialize mouse condition bits */
- do
- {
- if (ButtonState == LeftButton) /* If left button pressed start drawing */
- { /* 320 x 200 ? */
- xcoord = (xmax == 320) ? HorizCursCoord/2 : HorizCursCoord;
- _moveto(xcoord,VertCursCoord);
- m1 = 2; /* Hide cursor */
- cmousem(&m1,&m2,&m3,&m4);
-
- do
- { /* 320 x 200 ? */
- xcoord = (xmax == 320) ? HorizCursCoord/2 : HorizCursCoord;
- _lineto(xcoord,VertCursCoord); /* Draw */
- } while(ButtonState == LeftButton); /* Draw until LB released */
-
- m1 = 1; /* Show cursor */
- cmousem(&m1,&m2,&m3,&m4);
- }
- } while((MouseEvent = (MouseConditionBits & RightButtonReleaseMask))
- != RightButtonReleaseMask); /* Check if right button released */
- _setcliprgn(0,0,xmax - 1,ymax - 1); /* Reset drawing area limits */
- CallMenu(); /* Call menu */
- } /* End of PENCIL.C */
-
-
- /*----------------------------------------------------------------------*/
- DrawCursor(cursormask)
- int cursormask;
- {
- int m1,m2,m3,m4;
- unsigned short cursor[32]; /* Graphics cursor */
- union REGS inregs,outregs; /* int86x data registers */
- struct SREGS segregs; /* int86x segment registers */
-
- /* set screen mask */
- cursor[0]=0xFFFF;
- cursor[1]=0xFFFF;
- cursor[2]=0xFFFF;
- cursor[3]=0xFFFF;
- cursor[4]=0xFFFF;
- cursor[5]=0xFFFF;
- cursor[6]=0xFFFF;
- cursor[7]=0xFFFF;
- cursor[8]=0xFFFF;
- cursor[9]=0xFFFF;
- cursor[10]=0xFFFF;
- cursor[11]=0xFFFF;
- cursor[12]=0xFFFF;
- cursor[13]=0xFFFF;
- cursor[14]=0xFFFF;
- cursor[15]=0xFFFF;
-
- switch(cursormask) /* Select cursor mask */
- {
- case PencilCursor:
- /* Pencil mask */
- cursor[16]=0x0;
- cursor[17]=0x0;
- cursor[18]=0x0;
- cursor[19]=0x0020;
- cursor[20]=0x0050;
- cursor[21]=0x0088;
- cursor[22]=0x0150;
- cursor[23]=0x0220;
- cursor[24]=0x0440;
- cursor[25]=0x0880;
- cursor[26]=0x1100;
- cursor[27]=0x2200;
- cursor[28]=0x4400;
- cursor[29]=0x8800;
- cursor[30]=0xD000;
- cursor[31]=0xE000;
- break;
-
- case EraserCursor:
- /* Draw eraser */
- cursor[16]=0xFFC0;
- cursor[17]=0x8040;
- cursor[18]=0x8040;
- cursor[19]=0x8040;
- cursor[20]=0x8040;
- cursor[21]=0x8040;
- cursor[22]=0x8040;
- cursor[23]=0x8040;
- cursor[24]=0x8040;
- cursor[25]=0x8040;
- cursor[26]=0xFFC0;
- cursor[27]=0x0;
- cursor[28]=0x0;
- cursor[29]=0x0;
- cursor[30]=0x0;
- cursor[31]=0x0;
- break;
-
- case MouseCursor:
- /* Draw mouse cursor */
- cursor[16]=0x0660;
- cursor[17]=0x0E70;
- cursor[18]=0x0E70;
- cursor[19]=0x0E70;
- cursor[20]=0x0E70;
- cursor[21]=0x0;
- cursor[22]=0x0FF0;
- cursor[23]=0x0FF0;
- cursor[24]=0x0FF0;
- cursor[25]=0x0FF0;
- cursor[26]=0x0FF0;
- cursor[27]=0x07E0;
- cursor[28]=0x0;
- cursor[29]=0x0;
- cursor[30]=0x0;
- cursor[31]=0x0;
- break;
-
- default:
- break;
- } /*End switch */
-
- /* Use int86x instead of cmouse (to pass segment register) */
- inregs.x.ax = 9; /* Set graphics cursor */
- inregs.x.bx = 0;
- /* Change hot spot */
- inregs.x.cx = (cursormask == PencilCursor) ? 16 : 0;
- /* Offset of array */
- inregs.x.dx = (unsigned int)&cursor[0];
- segread(&segregs);
- segregs.es = segregs.ds; /* Set ES = DS */
- int86x(MouseInterrupt,&inregs,&outregs,&segregs);
- segread(&segregs);
-
- m1 = 1; /* Show cursor */
- cmousem(&m1,&m2,&m3,&m4);
- } /* End of DrawCursor */
-
-
- /*----------------------------------------------------------------------*/
- SaveScreen() /* Save area under menu */
- {
- _getimage(1,1,(xmax - 1),menuborderyaxis,graphicsbuffer);
- } /* End of SaveScreen */
-
-
- /*----------------------------------------------------------------------*/
- RestoreScreen() /* Restore area under menu */
- {
- _putimage(1,1,graphicsbuffer,_GPSET);
- } /* End of RestoreScreen */
-
-
- /*----------------------------------------------------------------------*/
- eraser() /* Eraser routine */
- {
- int flag,m1,m2,m3,m4,xcoord,ycoord; /* Mouse parameters and
- x/y coordinates);
-
- MouseConditionBits = 0; /* Initialize condition bits */
- _setcliprgn(3,3,xmax - 4,ymax - 4); /* Set drawing area */
- DrawCursor(EraserCursor); /* Draw eraser cursor */
- do
- {
- if (ButtonState == LeftButton) /* Check if left button pressed */
- {
- m1 = 2; /* Hide cursor */
- cmousem(&m1,&m2,&m3,&m4);
- do
- { /* Check if CGA 320 x 200 and get horizontal coord */
-
- xcoord = (xmax == 320) ? HorizCursCoord / 2 : HorizCursCoord;
- ycoord = VertCursCoord; /* Get vertical coordinate */
- _setcolor(WHITE); /* Set color to white */
- _rectangle(_GBORDER,xcoord,ycoord,xcoord + 9,
- ycoord + 10); /* Draw eraser */
- _setcolor(BLACK); /* Set color to black */
- _rectangle(_GFILLINTERIOR,xcoord,ycoord,xcoord + 9,
- ycoord + 10); /* Erase */
- } while(ButtonState == LeftButton); /* While left button pressed */
- m1 = 1; /* Show cursor */
- cmousem(&m1,&m2,&m3,&m4);
- }
- }
- while((MouseEvent = (MouseConditionBits & RightButtonReleaseMask))
- != RightButtonReleaseMask); /* While right button not released */
-
- _setcliprgn(0,0,xmax - 1,ymax - 1); /* Restore drawing region */
- CallMenu(); /* Call menu */
- } /* End of eraser */
-