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.
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.
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:
All of your rectangles have the same width (25) and height (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).
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
. What's it supposed to do?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.