简体   繁体   English

如何在汇编中使用鼠标?

[英]How to use mouse in Assembly?

I need to write an assembly code that if you click a particular icon on a screen, a sound is activated.(For example: piano) How to do it?我需要编写一个汇编代码,如果您单击屏幕上的特定图标,则会激活声音。(例如:钢琴)怎么做?

I'm using a Assembly 86 and dosbox 0.74-3 If that helps.我正在使用Assembly 86和 dosbox 0.74-3 如果有帮助的话。

This is the code that creates the piano (5 white rectangles) I need that when the mouse clicks on one of the rectangles, it will make a sound.这是创建钢琴(5 个白色矩形)的代码,我需要当鼠标单击其中一个矩形时,它会发出声音。

I know how to make a sound, I just need the sound to be heard when the icons are clicked.我知道如何发出声音,我只需要在单击图标时听到声音。 The remarks are in Hebrew because it is my native language这些评论是用希伯来语写的,因为它是我的母语

IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
color db 12
Player_CoordinateIn_X dw 5
Player_CoordinateIn_Y dw 10 
Player_CoordinateIn_X1 dw 40
Player_CoordinateIn_X2 dw 75
Player_CoordinateIn_X3 dw 110
Player_CoordinateIn_X4 dw 145
Player_CoordinateIn_X5 dw 180
Player_CoordinateIn_X6 dw 215
Player_CoordinateIn_X7 dw 250

include "iconbox.asm"


; --------------------------
CODESEG
; ---------------------------------------

proc printIcon
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon

proc drawIcon
;פרוצדורה מקבלת מיקום ותמונה
;פרוצדורה מדפיסה תמונה במיקום המבוקש
iconOffset EQU [BP+14] ;מספר האייקון
iconWidth EQU [BP+12] ;רוחב האייקון
iconHeight EQU [BP+10] ;גובה האייקון
iconColor EQU [BP+8] ;צבע האייקון
iconX EQU [BP+6] ;מיקום ציר X
iconY EQU [BP+4] ;מיקום ציר Y

 add [bp],10
 push bp
 mov bp,sp
 ;PushAll
 mov si,0
 mov di,0
 mov bx,iconOffset
 mov dx,iconY
bigLoop: 
 cmp [byte ptr bx],0
 je afterPixel
 MOV AX,iconColor
 MOV AH,0Ch
 MOV CX,iconX
 ADD CX,SI 
 INT 10H
afterPixel:
 inc bx
 inc si
 cmp si,iconWidth
 jne bigLoop
 mov si,0
 inc dx
 inc di
 cmp di,iconHeight
 jne bigLoop
 ;PopAll
 pop bp
 ret 12
endp drawIcon

proc open_grafic
    mov ax,13h
    int 10h
    ret 
endp open_grafic
;------------------------------------
proc printIcon2
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X1] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon2
;--------------------------------------
proc printIcon1
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X2] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon1
;--------------------------------------
proc printIcon3
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X3] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon3
;--------------------------------------
proc printIcon4
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X4] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon4
;--------------------------------------
proc printIcon5
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X5] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon5
;--------------------------------------
proc printIcon6
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X6] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon6
;--------------------------------------
proc printIcon7
 push offset iconbox ;שם האייקון
 push 25d ;Width
 push 150d ;Height
 push 15D ;Color
 push [Player_CoordinateIn_X7] ; x location
 push [Player_CoordinateIn_Y] ;y location
 call drawIcon
 ret
endp printIcon7
;--------------------------------------
start:
    mov ax, @data
    mov ds, ax
; -------------------------
call open_grafic
call printIcon
call printIcon1
call printIcon2
call printIcon3
call printIcon4
call printIcon5
call printIcon6
call printIcon7
    ; --------------------------
    mov ax,0h
int 33h
; Show mouse
mov ax,1h
int 33h
; Loop until mouse click
MouseLP :
mov ax,3h
int 33h
cmp bx, 01h ; check left mouse click
jne MouseLP
; Print dot near mouse location
shr cx,1 ; adjust cx to range 0-319, to fit screen
sub dx, 1 ; move one pixel, so the pixel will not be hidden by mouse
mov bh,0h
mov al,[color]
mov ah,0Ch
int 10h
; Press any key to continue
mov ah,00h
 int 16h
; Text mode
mov ax,3h
int 10h



exit:

end start

Your code already waits for a left button click:您的代码已经在等待左键单击:

MouseLP:
    mov     ax, 0003h   ; MOUSE.GetMousePosition
    int     33h         ; -> BX CX DX
    test    bx, 1       ; Is left button down?
    jz      MouseLP     ; No
    shr     cx, 1       ; Adjust cx to range 0-319, to fit screen

Once the click arrives you start comparing the coordinates that you got in CX (X) and DX (Y) with the coordinates of the rectangle that interests you:单击到达后,您开始将在CX (X) 和DX (Y) 中获得的坐标与您感兴趣的矩形坐标进行比较:

All of your rectangles have the same width (25) and height (150).您所有的矩形都具有相同的宽度 (25) 和高度 (150)。
Next snippet will check if the mouse was clicked while above your first rectangle, whose upperleft corner is at (Player_CoordinateIn_X1, Player_CoordinateIn_Y).下一个片段将检查鼠标是否在您的第一个矩形上方被单击,其左上角位于 (Player_CoordinateIn_X1, Player_CoordinateIn_Y)。

    mov     ax, [Player_CoordinateIn_X1]  ; UpperLeftCornerX
    cmp     cx, ax
    jb      Outside
    add     ax, 25-1                      ; LowerRightCornerX
    cmp     cx, ax
    ja      Outside
    mov     ax, [Player_CoordinateIn_Y]   ; UpperLeftCornerY
    cmp     dx, ax
    jb      Outside
    add     ax, 150-1                     ; LowerRightCornerY
    cmp     dx, ax
    ja      Outside
Inside:
    ... Make the corresponding sound!
Outside:
    ... Go check for another mouse click!

 proc drawIcon add [bp],10 push bp mov bp,sp

Your drawIcon proc starts with this strange instruction add [bp],10 .您的drawIcon proc 以这个奇怪的指令add [bp],10开始。 What's it supposed to do?它应该做什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM