I am trying to add interactive mouse interfacing to my program but I am not able to understand the need for delay()
in it.
Here's the code:
do
{
getmousepos(&button,&x,&y);
if(button==1)
{
delay(500);
xx[i] = x;
yy[i] = y;
i++;
}
}while(!kbhit());
Not sure what delay()
does here. Because if the button is pressed, the co-ordinates will get allocated to their respective arrays anyway. Then what is the need to add a delay?
The program with delay()
gives proper output.
But if I remove it, nothing gets printed on screen.
What's so important about delay()
in this case?
Turbo C++ ... that takes me back. Here is what I am expecting is happening and why that delay()
is needed.
What you have here is a polling loop. Without the delay()
, you are going to flood your xx[] and yy[] buffers when button is 1, and probably overflow them too. Remember, the computer can execute that loop a great many times while you have that mouse button pressed. The delay()
evens that out somewhat.