📄 xmouse.asm
字号:
;-----------------------------------------------------------------------
; MODULE XMOUSE
;
; Mouse functions functions for all MODE X 256 Color resolutions
;
; Compile with Tasm.
; C callable.
;
; ****** XLIB - Mode X graphics library ****************
; ****** ****************
; ****** Written By Themie Gouthas ****************
;
; This module is based on Shane Hyde's module of the same name,
; posted to Rec.Games.Programmer, October 92.
;
;
; egg@dstos3.dsto.gov.au
; teg@bart.dsto.gov.au
;
; mouse cursor shape by Tiaan A Geldenhuys
;
;-----------------------------------------------------------------------
COMMENT $
This is a module implementing very basic mouse functions.
It does not support the full functionality of:
SPLIT SCREENS
SCROLLED WINDOWS
VIRTUAL WINDOWS
--------------------------------------
MS Mouse Driver Functions
Mouse Initialization 0
Show Cursor 1
Hide Cursor 2
Get Mouse Position & Button Status 3
Set Mouse Cursor Position 4
Get Button Press Information 5
Get Button Release Information 6
Set Min/Max Horizontal Position 7
Set Min/Max Vertical Position 8
Define Graphics Cursor Block 9
Define Text Cursor 10
Read Mouse Motion Counters 11
Define Event Handler 12
Light Pen Emulation Mode ON 13
Light Pen Emulation Mode OFF 14
Set Mouse Mickey/Pixel Ratio 15
Conditional Hide Cursor 16
Set Double-Speed Threshold 19
--------------------------------------
$
include xlib.inc
include xdetect.inc
.data
global _MouseInstalled :word
global _MouseHidden :word
global _MouseButtonStatus :word
global _MouseX :word
global _MouseY :word
global _MouseFrozen :byte
global _MouseColor :byte
global _x_define_mouse_cursor :proc
global _x_show_mouse :proc
global _x_hide_mouse :proc
global _x_mouse_remove :proc
global _x_position_mouse :proc
global _x_put_cursor :proc
global _x_update_mouse :proc
global _x_mouse_init :proc
global _x_mouse_window :proc
ALIGN 2
InitMouseDef db 00000001b ; Default mouse mask, note the reverse order
db 00000011b
db 00000111b
db 00001111b
db 00011111b
db 00111111b
db 01111111b
db 11111111b
db 00011111b
db 00011011b
db 00110001b
db 00110000b
db 01100000b
db 01100000b
COMMENT $
Old mouse definition
InitMouseDef db 00000001b ; Default mouse mask, note the reverse order
db 00000011b
db 00000111b
db 00001111b
db 00011111b
db 00111111b
db 01111111b
db 11111111b
db 00011100b
db 00111100b
db 01111100b
db 00000000b
db 00000000b
db 00000000b
$
MouseMask db 168 dup(?)
OldHandlerSeg dw ?
OldHandlerOffs dw ?
OldHandlerMask dw ?
OldX dw ?
OldY dw ?
OldScrnOffs dw ?
BGSaveOffs dw 0
_MouseInstalled dw 0 ; Flag indicating whether mouse is installed
_MouseHidden dw 0 ; Flag indicating whether mouse is hidden
_MouseButtonStatus dw 0 ; Holds current button press information
_MouseX dw 0 ; Coords of cursor hot spot
_MouseY dw 0
_MouseFrozen db 0 ; Mouse motion enable/disable control
_MouseColor db 0 ; Mouse cursor colour
inhandler db 0
.code
;----------------------------------------------------------------------
; Local function that updates the cursor position
;
; Destroys SI,DI,AX,BX
;
;----------------------------------------------------------------------
proc update_cursor near
WaitVsyncStart
mov di,[OldScrnOffs] ; Delete cursor (restore old background)
mov ax,[OldY]
mov bx,[OldX]
call restorebg
mov si,[_VisiblePageOffs] ; Save cursor background
mov ax,[_MouseY]
mov bx,[_MouseX]
mov [OldScrnOffs],si
mov [OldY],ax
mov [OldX],bx
call getbg
push [_VisiblePageOffs] ; Draw the cursor
mov ax,[_ScrnPhysicalHeight]
push ax
mov ax,0
push ax
push [OldY]
push [OldX]
call _x_put_cursor
add sp,10
ret
update_cursor endp
;----------------------------------------------------------------------
; x_mouse_init - Initialise Mode X mouse handler
;
; C Prototype
;
; int x_mouse_init()
;
; This is the first function you must call before using any of the mouse
; functions
;
; WARNING: This function uses and updates "NonVisual_Offset" to allocate
; video ram for the saved mouse background.
;
; This mouse code uses the fastest possible techniques to save and restore
; mouse backgrounds and to draw the mouse cursor.
;
; LIMITATIONS: No clipping is supported horizontally for the mouse cursor
; No validity checking is performed for NonVisual_Offs
;
;
; **WARNING** Hide or freeze mouse while drawing using any of the other
; Modules. VGA register settings are not preserved which will
; result in unpredictable drawing behavior.
; If you know the drawing will occur away from the mouse cursor
; set MouseFrozen to TRUE (1), do your drawing then set it to
; FALSE (0). Alternatively call "x_hide_mouse", perform your
; drawing and then call "x_show_mouse"
;
;
; Written by Themie Gouthas
;----------------------------------------------------------------------
_x_mouse_init proc
push bp
mov bp,sp
cmp [_MouseButtonCount],0 ; Dont initialize if mouse detection
jne @@DontInitialize ; or initialization function previously
; called
xor ax,ax ; FUNC 0: Mouse Initialization
int 33h ;
or ax,ax ; Is there a mouse installed ?
jz @@Done
mov [_MouseButtonCount],bx ; Set the button count
@@DontInitialize:
mov [_MouseInstalled],ax
or ax,ax ; Do we have an installed mouse driver ?
jz @@Done ; Nop!
mov ax,[_NonVisual_Offs]; ; Allocate VRAM for saved background
mov BGSaveOffs,ax
add ax,14*3
mov [_NonVisual_Offs],ax ; Update NonVisualOffset
mov ax,02 ; FUNC 2: Hide Cursor
int 33h ; (hide the mouse driver's default cursor)
mov _MouseInstalled,TRUE ; Indicate user mouse driver present
mov ax,07h ; FUNC 7:Set min/max horizontal position
mov cx,0
mov dx,[_ScrnPhysicalPixelWidth]
shl dx,1 ; Mult X by 2 as cursor steps by 2 pixels
int 33h ; 0 < X < _ScrnPhysicalPixelWidth
mov ax,08h ; FUNC 8:Set min/max vertical position
mov cx,0
mov dx,_ScrnPhysicalHeight
int 33h ; 0 < Y < _ScrnPhysicalHeight
mov ax,0fh ; FUNC 15: Mouse Hor/Vert resolution
mov cx,4 ; Horiz speed >> Value => << Speed
mov dx,8 ; Vert Speed
int 33h
mov ax,3 ; FUNC 3: Get mouse pos & button status
int 33h
mov [_MouseY],dx
shr cx,1
mov [_MouseX],cx
mov ax,12 ; FUNC 12: Define Event Handler
mov bx,seg mouse_handler ; ES:DX -> Event handler
mov es,bx
mov dx,offset mouse_handler
mov cx,1fh ; Set handler for all events
int 33h
mov [_MouseHidden],TRUE ; Mouse initially hidden
push ds ; Set the default cursor shape
mov ax,offset InitMouseDef
push ax
call _x_define_mouse_cursor
add sp,04h
mov ax,[_MouseInstalled] ; Return MouseInstalled flag
@@Done:
pop bp
ret
_x_mouse_init endp
;----------------------------------------------------------------------
; x_mouse_window - Define a mouse window
;
; C Prototype
;
; void x_mouse_window(int x0, int y0, int x1, int y1);
;
;
; Written by Themie Gouthas
;----------------------------------------------------------------------
_x_mouse_window proc
ARG x0:word,y0:word,x1:word,y1:word
push bp
mov bp,sp
mov ax,7 ; FUNC 7: Set X range
mov cx,x0
shl cx,1
mov dx,x1
shl dx,1
int 33h
mov ax,8 ; FUNC 8: Set Y range
mov cx,y0
mov dx,y1
int 33h
pop bp
ret
_x_mouse_window endp
;----------------------------------------------------------------------
; x_define_mouse_cursor - Define a mouse cursor from an input bitmask
;
; C Prototype
;
; void x_define_mouse_cursor(char far *MouseDef, unsigned char MouseColor)
;
; WARNING: This function assumes MouseDef points to 14 bytes.
;
; Note: Bit order is in reverse. ie bit 7 represents pixel 0 ..
; bit 0 represents pixel 7 in each "MouseDef" byte.
;
; Written by Themie Gouthas
;----------------------------------------------------------------------
_x_define_mouse_cursor proc
ARG MouseDef:dword,MouseColor:byte
push bp
mov bp,sp
cmp [_MouseInstalled],FALSE ; Check whether we have installed
je @@Done ; our mouse handler and leave if not
mov al,[MouseColor] ; Set the mouse pointers color
mov [_MouseColor],al
push si
push di
push ds
mov ax,ds ; ES:DI -> Stored plane mask for all
mov es,ax ; pixel alignments of mouse cursor
mov di,offset MouseMask
lds si,MouseDef
xor cl,cl ; CL = current alignment (initially zero)
@@AlignmentLoop:
push si ; save MouseDef ptr for next alignment
mov dh,14 ; Init Row counter to Cursor Height
@@RowLoop:
lodsb ; Load first cursor def byte each bit
; representing pixel in the row
xor ah,ah ; AH is the shift overflow byte
shl ax,cl ; Shift mask for current alignment
mov bl,al ; store first three nibbles of ax into
and bl,0fh ; consecutive bytes in the destination
mov es:[di],bl ; buffer
inc di
shr al,4
stosw
dec dh ; Next row for this alignment if there
jnz @@RowLoop ; are more to do
pop si ; point to the start of the cursor def.
inc cl ; Select next pixel alignment
cmp cl,4 ; If there are more alignments to do
jne @@AlignmentLoop ; then jump
pop ds
pop di
pop si
@@Done:
pop bp
ret
_x_define_mouse_cursor endp
;----------------------------------------------------------------------
; x_show_mouse - Shows a previously hidden mouse cursor
;
; C Prototype
;
; void x_show_mouse()
;
; Written by Themie Gouthas
;----------------------------------------------------------------------
_x_show_mouse proc
push bp
mov bp,sp
cmp [_MouseInstalled],FALSE ; Make sure our handler is installed
je @@Done
cmp [_MouseHidden],FALSE ; If not hidden then exit
je @@Done
push si
push di
@@WaitEndOfHandler: ; Make sure handler not currently active
mov cl,[inhandler]
or cl,cl
jnz @@WaitEndOfHandler
mov si,[_VisiblePageOffs] ; Save mouse background and pos details
mov ax,[_MouseY]
mov bx,[_MouseX]
mov [OldScrnOffs],si
mov [OldY],ax
mov [OldX],bx
call getbg
push [_VisiblePageOffs] ; Draw cursor
push [_ScrnLogicalHeight]
xor ax,ax
push ax
push [OldY]
push [OldX]
call _x_put_cursor
add sp,10
mov [_MouseHidden],FALSE ; Indicate mouse cursor no longer hidden
pop di
pop si
@@Done:
pop bp
ret
_x_show_mouse endp
;----------------------------------------------------------------------
; x_hide_mouse - Hides a previously visible mouse cursor
;
; C Prototype
;
; void x_hide_mouse()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -