Thursday, November 20, 2008

Building Mouse Cursors

Building Mouse Cursors...

In text mode the mouse cursor appears as a block, whereas in graphics mode it appears as an arrow. If we wish we can change the graphics cursor to any other shape the way Windows does. The mouse cursor in graphics mode occupies a 16 by 16 pixel box. By highlighting or dehighlighting some of the pixels in this box we can get the desired shape. For example, the following bit-pattern can be used to generate the cursor which looks like an hour-glass.

1111111111111111 0000000000000000
1000000000000001 0000000000000000
1111111111111111 0000000000000000
1000000000000001 0000000000000000
0100000000000010 1000000000000001
0010000000000100 1100000000000011
0000100000010000 1111000000001111
0000001001000000 1111110000111111
0000001001000000 1111110000111111
0000100000010000 1111000000001111
0010000000000100 1100000000000011
0100000000000010 1000000000000001
1000000000000001 0000000000000000
1111111111111111 0000000000000000
1000000000000001 0000000000000000
1111111111111111 0000000000000000
Mouse pointer bitmap Screen Mask the one's in the mouse pointer bitmap indicate that the pixel would be drawn whereas the zeros indicate that the pixel would stand erased. It is important to note that the mouse pointer bit pattern is 32 bytes long. However, while actually writing a program to change the pointer shape we need a 64 byte bit-map. This provision is made to ensure that when the cursor reaches a position on the screen where something is already written or drawn only that portion should get overwritten which is to be occupied by the mouse cursor. Of the 64 bytes the first 32 bytes contain a bit mask which is first ANDed with the screen image, and then the second 32 bytes bit mask is XORed with the screen image.
The following program changes the mouse cursor in graphics mode to resemble an hour glass.

# include "graphics.h"
# include "dos.h"
union REGS i, o ;
struct SREGS s ;
int cursor[32] =
{
/* Hour-glass screen mask */
0x0000, 0x0000, 0x0000, 0x0000,
0x8001, 0xc003, 0xf00f, 0xfc3f,
0xfc3f, 0xf00f, 0xc003, 0x8001,
0x0000, 0x0000, 0x0000, 0x0000,
/* The mouse pointer bitmap */
0xffff, 0x8001, 0xffff, 0x8001,
0x4002, 0x2004, 0x1008, 0x0240,
0x0240, 0x0810, 0x2004, 0x4002,
0x8001, 0xffff, 0x8001, 0xffff,
} ;
main( )
{
int gd = DETECT, gm ;
initgraph ( &gd, &gm, "c:\\tc\\bgi" ) ;
if ( initmouse( ) == -1 )
{
closegraph( ) ;
printf ( "\n Mouse not installed!" ) ;
exit( ) ;
}
gotoxy ( 10, 1 ) ; printf ( "Press any key to exit..." ) ;
changecursor ( cursor ) ; showmouseptr( ) ;
getch( ) ;
}
initmouse( )
{
i.x.ax = 0 ; int86 ( 0x33, &i, &o ) ;
return ( o.x.ax == 0 ? -1 : 0 ) ;
}
showmouseptr( )
{
i.x.ax = 1 ; int86 ( 0x33, &i, &o ) ;
}
changecursor ( int *shape )
{
i.x.ax = 9 ; /* service number */
i.x.bx = 0 ; /* actual cursor position from left */
i.x.cx = 0 ; /* actual cursor position from top */
i.x.dx = ( unsigned ) shape ; /* offset address of pointer image*/
segread ( &s ) ;
s.es = s.ds ; /* segment address of pointer */
int86x ( 0x33, &i, &i, &s ) ;
}

No comments:

Blog List